Tenim.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\TenimService;
  5. use think\Request;
  6. use getusersig\getusersig;
  7. use tencentim\tencentim;
  8. use Redis;
  9. use think\Db;
  10. /**
  11. * 腾讯im接口
  12. */
  13. class Tenim extends Api
  14. {
  15. protected $noNeedLogin = ['trtc_callback','callback',];
  16. protected $noNeedRight = ['*'];
  17. /**
  18. * 获取usersig签名
  19. */
  20. public function getUsersig() {
  21. $user_id = $this->auth->id;
  22. $this->success('获取成功!',$this->usersig($user_id));
  23. }
  24. /**
  25. * 获取usersig签名-具体操作
  26. */
  27. private function usersig($user_id) {
  28. // 获取配置信息
  29. $config = config('tencent_im');
  30. $usersigObj = new getusersig($config['sdkappid'],$config['key']);
  31. $usersig = $usersigObj->genUserSig($user_id);
  32. return $usersig;
  33. }
  34. /**
  35. * 回调
  36. */
  37. public function callback() {
  38. // 主题信息
  39. $input = file_get_contents('php://input');
  40. $input = json_decode($input,true);
  41. //配置
  42. $config = config('tencent_im');
  43. //App 后台在收到回调请求之后,务必校验请求 URL 中的参数 SDKAppID 是否是自己的 SDKAppID。
  44. $SdkAppid = $input['SdkAppid'];
  45. if($SdkAppid != $config['sdkappid']){
  46. //应答包示例
  47. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  48. }
  49. //回掉正文
  50. $CallbackCommand = $input['CallbackCommand'];
  51. switch ($CallbackCommand) {
  52. case 'State.StateChange': // 用户在线状态变更
  53. $info = $input['Info'];
  54. if(strpos($info['To_Account'],'doctor') === false){
  55. //应答包示例
  56. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  57. }
  58. $role = 'doctor';
  59. $doctor_id = substr($info['To_Account'],6);
  60. if($info['Action'] == 'Login') {
  61. //登录
  62. Db::name('doctor')->where('id',$doctor_id)->update(['is_online'=>1,'onlinetime'=>time()]);
  63. //应答包示例
  64. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  65. } elseif($info['Action'] == 'Logout' || $info['Action'] == 'Disconnect') { // 用户退出 不需要管什么原因
  66. // 更新用户在线状态为离线
  67. Db::name('doctor')->where('id',$doctor_id)->update(['is_online'=>0]);
  68. //应答包示例
  69. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. //应答包示例
  76. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  77. }
  78. }