123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- namespace app\common\service;
- use getusersig\getusersig;
- use tencentim\tencentim;
- use think\Db;
- use think\Exception;
- class TenimService
- {
- private $url = '';
- /**
- * 初始化方法
- */
- public function __construct()
- {
- $random = rand(10000000,99999999);
- $usersig = $this->usersig("administrator");
- // 获取配置信息
- $config = config("tencent_im");
- $url = "?sdkappid=".$config["sdkappid"];
- $url .= "&identifier=administrator";
- $url .= "&usersig=".$usersig;
- $url .= "&random=".$random;
- $url .= "&contenttype=json";
- $this->url = $url;
- }
- /**
- * 获取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 getRoomUser($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '获取成功',
- 'data' => [],
- ];
- try {
- $partyArr = isset($params['party_ids']) ? $params['party_ids'] : [];
- $url = "https://console.tim.qq.com/v4/group_open_avchatroom_http_svc/get_members".$this->url;
- $tencentObj = new tencentim($url);
- //获取用户 ["57","62","63","105","106","107","108","109","116","119"]
- $memberList = [];
- if(!empty($partyArr)) {
- foreach ($partyArr as $key => $value) {
- $data["GroupId"] = (string)$value;
- $tencentRes = $tencentObj->toSend($data);
- if (isset($tencentRes['ActionStatus']) && $tencentRes['ActionStatus'] == 'OK') {
- if (isset($tencentRes['MemberList']) && !empty($tencentRes['MemberList'])) {
- foreach ($tencentRes['MemberList'] as $ukey => $uval) {
- $memberList[$value]['member_list'][] = [
- 'avatar' => $uval['Avatar'],
- 'user_id'=> $uval['Member_Account'],
- 'nickname' => $uval['NickName'],
- ];
- }
- $memberList[$value]['online_num'] = count($memberList[$value]['member_list']);
- }
- }
- }
- }
- $result['data'] = $memberList;
- } catch (\Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- /**
- * 获取房间自定义属性
- */
- public function getGroupAttr($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '获取成功',
- 'data' => [],
- ];
- try {
- $partyId = isset($params['party_id']) ? $params['party_id'] : [];
- $url = "https://console.tim.qq.com/v4/group_open_attr_http_svc/get_group_attr".$this->url;
- $tencentObj = new tencentim($url);
- $imResult = [];
- if(!empty($partyId)) {
- $data["GroupId"] = $partyId;
- $imRes = $tencentObj->toSend($data);
- if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
- $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
- throw new Exception(json_encode($errorInfo));
- }
- $imResult = isset($imRes['GroupAttrAry']) ? $imRes['GroupAttrAry'] : [];
- }
- $result['data'] = $imResult;
- } catch (\Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- /**
- * 更新房间属性
- */
- public function getGroupAttrEdit($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '获取成功',
- 'data' => [],
- ];
- try {
- $partyId = isset($params['party_id']) ? $params['party_id'] : [];
- $partyAttr = isset($params['party_attr']) ? $params['party_attr'] : [];
- $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_attr".$this->url;
- $tencentObj = new tencentim($url);
- if(!empty($partyId)) {
- $data["GroupId"] = (string)$partyId;
- $data["GroupAttr"] = $partyAttr;
- $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 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;
- }
- /**
- * 创建用户账号
- */
- 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;
- }
- }
|