123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 腾讯im接口
- */
- class Tenim extends Api
- {
- protected $noNeedLogin = ['trtc_callback','callback','aaa'];
- protected $noNeedRight = ['*'];
- /**
- * 获取usersig签名
- */
- public function getUsersig() {
- $user_id = $this->auth->id;
- $this->success('获取成功!',$this->usersig($user_id));
- }
- /**
- * 获取usersig签名-具体操作
- */
- private function usersig($user_id) {
- // 获取配置信息
- $config = config('tencent_im');
- $usersigObj = new getusersig($config['sdkappid'],$config['key']);
- $usersig = $usersigObj->genUserSig($user_id);
- return $usersig;
- }
- /**
- * 回调
- */
- public function callback() {
- // 主题信息
- $input = input();
- //配置
- $config = config('tencent_im');
- //App 后台在收到回调请求之后,务必校验请求 URL 中的参数 SDKAppID 是否是自己的 SDKAppID。
- $SdkAppid = $input['SdkAppid'];
- if($SdkAppid != $config['sdkappid']){
- //应答包示例
- $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
- }
- //回掉正文
- $CallbackCommand = $input['CallbackCommand'];
- switch ($CallbackCommand) {
- case 'State.StateChange': // 用户在线状态变更
- $info = $input['Info'];
- if(strpos($info['To_Account'],'doctor') === false){
- //应答包示例
- $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
- }
- $role = 'doctor';
- $doctor_id = substr($info['To_Account'],6);
- if($info['Action'] == 'Login') {
- //登录
- Db::name('doctor')->where('id',$doctor_id)->update(['is_online'=>1,'onlinetime'=>time()]);
- //应答包示例
- $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
- } elseif($info['Action'] == 'Logout' || $info['Action'] == 'Disconnect') { // 用户退出 不需要管什么原因
- // 更新用户在线状态为离线
- Db::name('doctor')->where('id',$doctor_id)->update(['is_online'=>0]);
- //应答包示例
- $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
- }
- break;
- default:
- break;
- }
- //应答包示例
- $res = ['ActionStatus'=>'OK','ErrorCode'=>0,'ErrorInfo'=>''];echo json_encode($res);exit;
- }
- public function aaa(){
- $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"}';
- dump(json_decode($a,true));
- }
- }
|