Browse Source

黑名单同步IM

zhangxiaobin 1 year ago
parent
commit
6a8720f5d5

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

@@ -1214,7 +1214,7 @@ exit;
      */
     public function getGroupAttr() {
         $partyId = $this->request->param('party_id','');
-        if (!empty($partyIds)) {
+        if (empty($partyIds)) {
             $this->error('参数错误');
         }
         $random = rand(10000000,99999999);
@@ -1236,4 +1236,25 @@ exit;
         }
         $this->success('操作成功',$result);
     }
+
+    /**
+     * 获取黑名单列表
+     */
+    public function getBlackList() {
+        $userId = $this->request->param('user_id',0);
+        if (empty($userId)) {
+            $this->error('参数错误');
+        }
+        $tenimService = new TenimService();
+        $params = [
+            'user_id' => $userId,
+            'page' => 0,
+            'limit' => 30,
+        ];
+        $imResult = $tenimService->getBlackList($params);
+        if (!$imResult['status']) {
+            $this->error($imResult['msg']);
+        }
+        $this->success('操作成功',$imResult['data']);
+    }
 }

+ 73 - 33
application/api/controller/Usercenter.php

@@ -6,6 +6,7 @@ use addons\epay\library\Service;
 use app\api\controller\Common;
 use app\api\validate\user\Report;
 use app\common\model\UserPower;
+use app\common\service\TenimService;
 use outh\outh;
 use think\Db;
 use app\common\controller\RedisLeaderboard;
@@ -769,21 +770,37 @@ class UserCenter extends Common
      * 加入黑名单
      */
     public function addBlacklist() {
-        $black_user_id = $this->request->request('black_user_id'); // 黑名单用户ID
-        if (!$black_user_id) {
-            $this->error(__('Invalid parameters'));
-        }
-        $userblacklistModel = new \app\common\model\UserBlacklist();
-        $data = [];
-        $data["user_id"] = $this->auth->id;
-        $data["black_user_id"] = $black_user_id;
-        if($userblacklistModel->where($data)->find()) $this->error(__('已在黑名单!'));
-        $data["createtime"] = time();
-        $res = $userblacklistModel->insertGetId($data);
-        if($res) {
+        Db::startTrans();
+        try {
+            $black_user_id = $this->request->request('black_user_id'); // 黑名单用户ID
+            if (!$black_user_id) {
+                throw new Exception(__('Invalid parameters'));
+            }
+            $userblacklistModel = new \app\common\model\UserBlacklist();
+            $data = [];
+            $data["user_id"] = $this->auth->id;
+            $data["black_user_id"] = $black_user_id;
+            if($userblacklistModel->where($data)->find()) $this->error(__('已在黑名单!'));
+            $data["createtime"] = time();
+            $res = $userblacklistModel->insertGetId($data);
+            if(!$res) {
+                throw new Exception("网络错误,请稍后重试");
+            }
+            //im加入黑名单
+            $tenimService = new TenimService();
+            $params = [
+                'user_id' => $this->auth->id,
+                'black_user_ids' => [(string)$black_user_id],
+            ];
+            $imResult = $tenimService->addBlack($params);
+            if (!$imResult['status']) {
+                throw new Exception($imResult['msg']);
+            }
+            Db::commit();
             $this->success("加入成功!");
-        } else {
-            $this->error("网络错误,请稍后重试");
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
         }
     }
 
@@ -815,27 +832,50 @@ class UserCenter extends Common
      * 移除用户黑名单
      */
     public function removeUserBlack() {
-        $id = $this->request->request('id'); // 黑名单ID
-        $userId = $this->request->request('user_id'); // 用户ID
-        if (!$id && !$userId) {
-            $this->error(__('Invalid parameters'));
-        }
+        Db::startTrans();
+        try {
+            $id = $this->request->request('id'); // 黑名单ID
+            $userId = $this->request->request('user_id'); // 用户ID
+            if (!$id && !$userId) {
+                throw new Exception(__('Invalid parameters'));
+            }
 
-        $userblacklistModel = new \app\common\model\UserBlacklist();
-        $where = [];
-        $id && $where["id"] = $id;
-        if ($userId) {
-            $where['user_id'] = $this->auth->id;
-            $where['black_user_id'] = $userId;
-        }
-        $res = false;
-        if (!empty($where)) {
-            $res = $userblacklistModel->where($where)->delete();
-        }
-        if($res) {
+            $userblacklistModel = new \app\common\model\UserBlacklist();
+            $where = [];
+            $id && $where["id"] = $id;
+            if ($userId) {
+                $where['user_id'] = $this->auth->id;
+                $where['black_user_id'] = $userId;
+            }
+            $userBlack = $userblacklistModel->where($where)->find();
+            if (empty($userBlack)) {
+                throw new Exception('未找到黑名单信息');
+            }
+            $blackUserId = isset($userBlack['black_user_id']) ? $userBlack['black_user_id'] : 0;
+            $res = false;
+            if (!empty($userBlack)) {
+                $res = $userblacklistModel->where($where)->delete();
+            }
+            if(!$res) {
+                $this->error("网络错误,请稍后重试!");
+            }
+            //im加入黑名单
+            if (!empty($blackUserId)) {
+                $tenimService = new TenimService();
+                $params = [
+                    'user_id' => $this->auth->id,
+                    'black_user_ids' => [(string)$blackUserId],
+                ];
+                $imResult = $tenimService->delBlack($params);
+                if (!$imResult['status']) {
+                    throw new Exception($imResult['msg']);
+                }
+            }
+            Db::commit();
             $this->success("移除成功!",$res);
-        } else {
-            $this->error("网络错误,请稍后重试!");
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
         }
     }
 

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

@@ -141,4 +141,101 @@ class TenimService
         }
         return $result;
     }
+
+    /**
+     * 添加黑名单
+     */
+    public function addBlack($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '操作成功',
+            'data' => [],
+        ];
+        try {
+            $userId = isset($params['user_id']) ? $params['user_id'] : 0;
+            $blackUserIds = isset($params['black_user_ids']) ? $params['black_user_ids'] : [];
+            $url = "https://console.tim.qq.com/v4/sns/black_list_add".$this->url;
+            $tencentObj = new tencentim($url);
+            if(!empty($userId)) {
+                $data["From_Account"] = (string)$userId;
+                $data["To_Account"] = $blackUserIds;
+                $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 delBlack($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '操作成功',
+            'data' => [],
+        ];
+        try {
+            $userId = isset($params['user_id']) ? $params['user_id'] : 0;
+            $blackUserIds = isset($params['black_user_ids']) ? $params['black_user_ids'] : [];
+            $url = "https://console.tim.qq.com/v4/sns/black_list_delete".$this->url;
+            $tencentObj = new tencentim($url);
+            if(!empty($userId)) {
+                $data["From_Account"] = (string)$userId;
+                $data["To_Account"] = $blackUserIds;
+                $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 getBlackList($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '获取成功',
+            'data' => [],
+        ];
+        try {
+            $userId = isset($params['user_id']) ? $params['user_id'] : 0;
+            $page = isset($params['page']) ? $params['page'] : 0;
+            $limit = isset($params['limit']) ? $params['limit'] : 30;
+            $url = "https://console.tim.qq.com/v4/sns/black_list_get".$this->url;
+            $tencentObj = new tencentim($url);
+            if(!empty($userId)) {
+                $data["From_Account"] = $userId;
+                $data["StartIndex"] = $page;
+                $data["MaxLimited"] = $limit;
+                $data["LastSequence"] = 0;
+                $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['BlackListItem']) ? $imRes['BlackListItem'] : [];
+            }
+        } catch (\Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
 }