Tenim.php 2.8 KB

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