lizhen_gitee 5 mēneši atpakaļ
vecāks
revīzija
fa9afd07ce
1 mainītis faili ar 102 papildinājumiem un 0 dzēšanām
  1. 102 0
      application/api/controller/Tenim.php

+ 102 - 0
application/api/controller/Tenim.php

@@ -0,0 +1,102 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use app\common\service\TenimService;
+use think\Request;
+use getusersig\getusersig;
+use tencentim\tencentim;
+use Redis;
+use think\Db;
+
+/**
+ * 腾讯im接口
+ */
+class Tenim extends Api
+{
+    protected $noNeedLogin = ['trtc_callback','callback',];
+    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 = file_get_contents('php://input');
+        $input = json_decode($input,true);
+
+        //配置
+        $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;
+    }
+
+
+
+}