Browse Source

无关紧要的提交,腾讯im方法

lizhen_gitee 8 months ago
parent
commit
2873c24391

+ 12 - 2
application/api/controller/Demo.php

@@ -3,7 +3,8 @@
 namespace app\api\controller;
 
 use app\common\controller\Api;
-
+use app\common\library\Tenim;
+use think\Db;
 /**
  * 示例接口
  */
@@ -40,7 +41,16 @@ class Demo extends Api
      */
     public function test()
     {
-        $this->success('返回成功', $this->request->param());
+        //腾讯im注册用户
+        $tenim = new Tenim();
+
+        $user_list = Db::name('user')->order('id asc')->select();
+        foreach($user_list as $key => $user){
+            $rs = $tenim->register(''.$user['id'],$user['nickname'],'');
+
+        }
+
+
     }
 
     /**

+ 2 - 2
application/api/controller/User.php

@@ -527,7 +527,7 @@ class User extends Api
             }
             //如果已经有账号则直接登录
             $ret = $this->auth->direct($user['id']);
-            $this->success('success',$this->userInfo('return'));
+            $this->success('success',$this->auth->getUserinfo());
         }
 
         //新的openid用户
@@ -553,7 +553,7 @@ class User extends Api
             $this->error($this->auth->getError());
         }
 
-        $this->success('success',$this->userInfo('return'));
+        $this->success('success',$this->auth->getUserinfo());
 
     }
 

+ 3 - 19
application/common/library/Auth.php

@@ -218,19 +218,9 @@ class Auth
      */
     public function register($username, $password, $email = '', $mobile = '', $extend = [])
     {
-        // 检测用户名、昵称、邮箱、手机号是否存在
-        /*if (User::getByUsername($username)) {
-            $this->setError('Username already exist');
-            return false;
-        }*/
-        /*if (User::getByNickname($username)) {
-            $this->setError('Nickname already exist');
-            return false;
-        }*/
-        /*if ($email && User::getByEmail($email)) {
-            $this->setError('Email already exist');
-            return false;
-        }*/
+
+
+
         if ($mobile && User::getByMobile($mobile)) {
             $this->setError('Mobile already exist');
             return false;
@@ -242,9 +232,6 @@ class Auth
         $introcode = User::column("introcode");
 
         $data = [
-            //'username' => $username,
-            //'password' => $password,
-            //'email'    => $email,
             'mobile'   => $mobile,
             /*'level'    => 1,
             'score'    => 0,*/
@@ -252,9 +239,7 @@ class Auth
             'introcode' => $this->getUinqueNo(6, $introcode),
         ];
         $params = array_merge($data, [
-            //'nickname'  => preg_match("/^1[3-9]{1}\d{9}$/",$username) ? substr_replace($username,'****',3,4) : $username,
             'nickname'  => $this->get_rand_nick_name(),
-            //'salt'      => Random::alnum(),
             'jointime'  => $time,
             'joinip'    => $ip,
             'logintime' => $time,
@@ -262,7 +247,6 @@ class Auth
             'prevtime'  => $time,
             'status'    => 1
         ]);
-        //$params['password'] = $this->getEncryptPassword($password, $params['salt']);
         $params = array_merge($params, $extend);
         //查询文字语音视频价格
         $charge_config_data = [

+ 120 - 2
application/common/library/Tenim.php

@@ -25,7 +25,7 @@ class Tenim
      */
     //https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
     public function sendMessageToUser($from_user,$to_user,$message) {
-        return true;
+
         $random = rand(10000000,99999999);
         $usersig = $this->usersig("administrator");
         // 获取配置信息
@@ -51,10 +51,128 @@ class Tenim
                 "Text"=> $message
             ],
         ];
-        $tencentObj->toSend($data);
+        $res = $tencentObj->toSend($data);
+
+        if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
+            $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
+            return $error;
+        }
+
+        return true;
 
     }
 
+    /**
+     * 发送自定义消息给某人
+     */
+    public function sendCustomMessageToUser($from_user,$to_user,$message) {
+
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/openim/sendmsg";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        $data = [];
+        $data["SyncOtherMachine"] = 1;//1=消息同步至发送方,2=消息不同步至发送方
+        $data["From_Account"] = (string)$from_user;
+        $data["To_Account"] = (string)$to_user;
+        $data["MsgRandom"] = rand(1000000,9999999);
+        $data["MsgTimeStamp"] = time();
+
+        $data["MsgBody"][] = [
+            "MsgType" => "TIMCustomElem",
+            "MsgContent" => [
+                "Data" => json_encode($message),
+                "Desc" => $message['name'],
+                "Ext"  => $message['name'],
+                "Sound"=> '',
+            ],
+        ];
+        $data['CloudCustomData'] = $message['id'];
+        $res = $tencentObj->toSend($data);
+
+        if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
+            $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
+            return $error;
+        }
+
+        return true;
+
+    }
+
+    //注册用户到im
+    public function register($userid,$nickname,$avatar) {
+
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        //dump($usersig);
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        $data = [
+            'UserID' => $userid,
+            'Nick'  => $nickname,
+            'FaceUrl' => $avatar,
+        ];
+
+        $res = $tencentObj->toSend($data);
+
+        if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
+            $error = !empty($res['ErrorInfo']) ? 'im error:'.$res['ErrorInfo'] : 'im error';
+            return $error;
+        }
+
+        return true;
+    }
+
+    //某个用户是否im在线
+    public function is_online($userid){
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        //dump($usersig);
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/openim/query_online_status";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        $data = [
+            'To_Account' => [$userid],
+        ];
+
+        $res = $tencentObj->toSend($data);
+
+        if (!empty($res['ActionStatus']) && $res['ActionStatus'] == 'OK') {
+            if(    isset($res['QueryResult'][0]['To_Account'])
+                && isset($res['QueryResult'][0]['Status'])
+                && $res['QueryResult'][0]['To_Account'] == $userid
+                && $res['QueryResult'][0]['Status'] == 'Online'){
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+
 
     /**
      * 获取usersig签名-具体操作

+ 1 - 1
application/config.php

@@ -309,7 +309,7 @@ return [
         'key' => '295e9bdb79396600a7df9ae2ef8fb023f0c3cf43ea6d182c59195bae5ac7ba9b',
         'identifier' => 'administrator',
 
-        //trtc回调秘钥   校友缘 正式
+        //trtc回调秘钥
         'callback_secret' => 'shezhihuidiaomiyao123456',
 
         'SecretId' => 'AKIDQmxumjoGuZ4FMPKEJGE5qT2TrkXFzfAZ', //人脸核身用,二要素用