Tenim.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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','aaa'];
  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 = input();
  35. //配置
  36. $config = config('tencent_im');
  37. //App 后台在收到回调请求之后,务必校验请求 URL 中的参数 SDKAppID 是否是自己的 SDKAppID。
  38. $SdkAppid = $input['SdkAppid'];
  39. if($SdkAppid != $config['sdkappid']){
  40. //应答包示例
  41. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  42. }
  43. //回掉正文
  44. $CallbackCommand = $input['CallbackCommand'];
  45. switch ($CallbackCommand) {
  46. case 'State.StateChange': // 用户在线状态变更
  47. $info = $input['Info'];
  48. if(strpos($info['To_Account'],'doctor') === false){
  49. //应答包示例
  50. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  51. }
  52. $role = 'doctor';
  53. $doctor_id = substr($info['To_Account'],6);
  54. if($info['Action'] == 'Login') {
  55. //登录
  56. Db::name('doctor')->where('id',$doctor_id)->update(['is_online'=>1,'onlinetime'=>time()]);
  57. //应答包示例
  58. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  59. } elseif($info['Action'] == 'Logout' || $info['Action'] == 'Disconnect') { // 用户退出 不需要管什么原因
  60. // 更新用户在线状态为离线
  61. Db::name('doctor')->where('id',$doctor_id)->update(['is_online'=>0]);
  62. //应答包示例
  63. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. //应答包示例
  70. $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
  71. }
  72. public function aaa(){
  73. $a = '{"CallbackCommand":"State.StateChange","ClientIP":"182.37.138.94","OptPlatform":"Android","RequestId":"crp5hq2r8lc58sqn92jg-144115245635848666-Disconnect-LinkClose","SdkAppid":"1600053964","contenttype":"json","Info":{"To_Account":"doctor24","Action":"Disconnect","Reason":"LinkClose"},"EventTime":"1727158505072"}';
  74. dump(json_decode($a,true));
  75. }
  76. }