Forráskód Böngészése

后台创建用户同步IM

zhangxiaobin 1 éve
szülő
commit
953a6afd2d

+ 10 - 0
application/admin/controller/user/User.php

@@ -6,6 +6,7 @@ use app\admin\model\Message;
 use app\admin\model\UserPower;
 use app\common\controller\Backend;
 use app\common\library\Auth;
+use app\common\service\TenimService;
 use fast\Random;
 use think\Db;
 use think\Exception;
@@ -118,6 +119,15 @@ class User extends Backend
                 if (!$userPowerRes) {
                     throw new Exception('创建用户权限失败');
                 }
+                //创建IM用户
+                $tenimService = new TenimService();
+                $imParams['user_id'] = $userPowerRes;
+                $imParams['nickname'] = $params['nickname'];
+                $imParams['avatar'] = cdnurl($params['avatar']);
+                $tenimRes = $tenimService->accountImport($imParams);
+                if (!$tenimRes['status']) {
+                    throw new Exception($tenimRes['msg']);
+                }
             } catch (ValidateException|PDOException|Exception $e) {
                 Db::rollback();
                 $this->error($e->getMessage());

+ 21 - 1
application/api/controller/Tenim.php

@@ -16,7 +16,7 @@ use think\Db;
 class Tenim extends Api
 {
     protected $noNeedLogin = ['trtc_callback',"callback",'test','createIMGroup','updateRoomInfo','setImManage','sendMessageToUser','outMemberFromRoom',
-        'autoUserLine','getRoomUser','getGroupList','getGroupInfo'];
+        'autoUserLine','getRoomUser','getGroupList','getGroupInfo','getUserList'];
     protected $noNeedRight = ['*'];
 
 
@@ -1257,4 +1257,24 @@ exit;
         }
         $this->success('操作成功',$imResult['data']);
     }
+
+    /**
+     * 获取用户列表
+     */
+    public function getUserList() {
+        $userId = $this->request->param('user_ids','');
+        if (empty($userId)) {
+            $this->error('参数错误');
+        }
+        $userIdArr = explode(',',$userId);
+        $tenimService = new TenimService();
+        $params = [
+            'user_ids' => $userIdArr,
+        ];
+        $imResult = $tenimService->accountCheck($params);
+        if (!$imResult['status']) {
+            $this->error($imResult['msg']);
+        }
+        $this->success('操作成功',$imResult['data']);
+    }
 }

+ 70 - 0
application/common/service/TenimService.php

@@ -238,4 +238,74 @@ class TenimService
         }
         return $result;
     }
+
+    /**
+     * 创建用户账号
+     */
+    public function accountImport($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '获取成功',
+            'data' => [],
+        ];
+        try {
+            $userId = isset($params['user_id']) ? $params['user_id'] : 0;
+            $nickname = isset($params['nickname']) ? $params['nickname'] : '';
+            $avatar = isset($params['avatar']) ? $params['avatar'] : '';
+
+            $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import".$this->url;
+            $tencentObj = new tencentim($url);
+            if(!empty($userId)) {
+                $data["UserID"] = (string)$userId;
+                $data["Nick"] = $nickname;
+                $data["FaceUrl"] = $avatar;
+                $imRes = $tencentObj->toSend($data);
+                if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
+                    $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
+                    throw new Exception(json_encode($errorInfo));
+                }
+            }
+        } catch (\Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
+
+    /**
+     * 查询用户账号
+     */
+    public function accountCheck($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '获取成功',
+            'data' => [],
+        ];
+        try {
+            $userIds = isset($params['user_ids']) ? $params['user_ids'] : [];
+
+            if(!empty($userIds)) {
+                $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_check".$this->url;
+                $tencentObj = new tencentim($url);
+                foreach ($userIds as $key => $value) {
+                    $userIdArr[] = [
+                        'UserID' => $value,
+                    ];
+                }
+                $data["CheckItem"] = $userIdArr;
+                $imRes = $tencentObj->toSend($data);
+                if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
+                    $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
+                    throw new Exception(json_encode($errorInfo));
+                }
+                $result['data'] = isset($imRes['ResultItem']) ? $imRes['ResultItem'] : [];
+            }
+        } catch (\Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
 }