TenimService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\common\service;
  3. use getusersig\getusersig;
  4. use tencentim\tencentim;
  5. use think\Db;
  6. use think\Exception;
  7. class TenimService
  8. {
  9. private $url = '';
  10. /**
  11. * 初始化方法
  12. */
  13. public function __construct()
  14. {
  15. $random = rand(10000000,99999999);
  16. $usersig = $this->usersig("administrator");
  17. // 获取配置信息
  18. $config = config("tencent_im");
  19. $url = "?sdkappid=".$config["sdkappid"];
  20. $url .= "&identifier=administrator";
  21. $url .= "&usersig=".$usersig;
  22. $url .= "&random=".$random;
  23. $url .= "&contenttype=json";
  24. $this->url = $url;
  25. }
  26. /**
  27. * 获取usersig签名-具体操作
  28. */
  29. private function usersig($user_id) {
  30. // 获取配置信息
  31. $config = config("tencent_im");
  32. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  33. $usersig = $usersigObj->genUserSig($user_id);
  34. return $usersig;
  35. }
  36. /**
  37. * 获取房间用户
  38. */
  39. public function getRoomUser($params=[])
  40. {
  41. $result = [
  42. 'status' => 1,
  43. 'msg' => '获取成功',
  44. 'data' => [],
  45. ];
  46. try {
  47. $partyArr = isset($params['party_ids']) ? $params['party_ids'] : [];
  48. $url = "https://console.tim.qq.com/v4/group_open_avchatroom_http_svc/get_members".$this->url;
  49. $tencentObj = new tencentim($url);
  50. //获取用户 ["57","62","63","105","106","107","108","109","116","119"]
  51. $memberList = [];
  52. if(!empty($partyArr)) {
  53. foreach ($partyArr as $key => $value) {
  54. $data["GroupId"] = (string)$value;
  55. $tencentRes = $tencentObj->toSend($data);
  56. if (isset($tencentRes['ActionStatus']) && $tencentRes['ActionStatus'] == 'OK') {
  57. if (isset($tencentRes['MemberList']) && !empty($tencentRes['MemberList'])) {
  58. foreach ($tencentRes['MemberList'] as $ukey => $uval) {
  59. $memberList[$value]['member_list'][] = [
  60. 'avatar' => $uval['Avatar'],
  61. 'user_id'=> $uval['Member_Account'],
  62. 'nickname' => $uval['NickName'],
  63. ];
  64. }
  65. $memberList[$value]['online_num'] = count($memberList[$value]['member_list']);
  66. }
  67. }
  68. }
  69. }
  70. $result['data'] = $memberList;
  71. } catch (\Exception $e) {
  72. $result['status'] = 0;
  73. $result['msg'] = $e->getMessage();
  74. }
  75. return $result;
  76. }
  77. /**
  78. * 获取房间自定义属性
  79. */
  80. public function getGroupAttr($params=[])
  81. {
  82. $result = [
  83. 'status' => 1,
  84. 'msg' => '获取成功',
  85. 'data' => [],
  86. ];
  87. try {
  88. $partyId = isset($params['party_id']) ? $params['party_id'] : [];
  89. $url = "https://console.tim.qq.com/v4/group_open_attr_http_svc/get_group_attr".$this->url;
  90. $tencentObj = new tencentim($url);
  91. $imResult = [];
  92. if(!empty($partyId)) {
  93. $data["GroupId"] = $partyId;
  94. $imRes = $tencentObj->toSend($data);
  95. if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
  96. $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
  97. throw new Exception(json_encode($errorInfo));
  98. }
  99. $imResult = isset($imRes['GroupAttrAry']) ? $imRes['GroupAttrAry'] : [];
  100. }
  101. $result['data'] = $imResult;
  102. } catch (\Exception $e) {
  103. $result['status'] = 0;
  104. $result['msg'] = $e->getMessage();
  105. }
  106. return $result;
  107. }
  108. /**
  109. * 更新房间属性
  110. */
  111. public function getGroupAttrEdit($params=[])
  112. {
  113. $result = [
  114. 'status' => 1,
  115. 'msg' => '获取成功',
  116. 'data' => [],
  117. ];
  118. try {
  119. $partyId = isset($params['party_id']) ? $params['party_id'] : [];
  120. $partyAttr = isset($params['party_attr']) ? $params['party_attr'] : [];
  121. $url = "https://console.tim.qq.com/v4/group_open_http_svc/modify_group_attr".$this->url;
  122. $tencentObj = new tencentim($url);
  123. if(!empty($partyId)) {
  124. $data["GroupId"] = (string)$partyId;
  125. $data["GroupAttr"] = $partyAttr;
  126. $imRes = $tencentObj->toSend($data);
  127. if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
  128. $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
  129. throw new Exception(json_encode($errorInfo));
  130. }
  131. }
  132. } catch (\Exception $e) {
  133. $result['status'] = 0;
  134. $result['msg'] = $e->getMessage();
  135. }
  136. return $result;
  137. }
  138. /**
  139. * 添加黑名单
  140. */
  141. public function addBlack($params=[])
  142. {
  143. $result = [
  144. 'status' => 1,
  145. 'msg' => '操作成功',
  146. 'data' => [],
  147. ];
  148. try {
  149. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  150. $blackUserIds = isset($params['black_user_ids']) ? $params['black_user_ids'] : [];
  151. $url = "https://console.tim.qq.com/v4/sns/black_list_add".$this->url;
  152. $tencentObj = new tencentim($url);
  153. if(!empty($userId)) {
  154. $data["From_Account"] = (string)$userId;
  155. $data["To_Account"] = $blackUserIds;
  156. $imRes = $tencentObj->toSend($data);
  157. if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
  158. $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
  159. throw new Exception(json_encode($errorInfo));
  160. }
  161. }
  162. } catch (\Exception $e) {
  163. $result['status'] = 0;
  164. $result['msg'] = $e->getMessage();
  165. }
  166. return $result;
  167. }
  168. /**
  169. * 删除黑名单
  170. */
  171. public function delBlack($params=[])
  172. {
  173. $result = [
  174. 'status' => 1,
  175. 'msg' => '操作成功',
  176. 'data' => [],
  177. ];
  178. try {
  179. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  180. $blackUserIds = isset($params['black_user_ids']) ? $params['black_user_ids'] : [];
  181. $url = "https://console.tim.qq.com/v4/sns/black_list_delete".$this->url;
  182. $tencentObj = new tencentim($url);
  183. if(!empty($userId)) {
  184. $data["From_Account"] = (string)$userId;
  185. $data["To_Account"] = $blackUserIds;
  186. $imRes = $tencentObj->toSend($data);
  187. if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
  188. $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
  189. throw new Exception(json_encode($errorInfo));
  190. }
  191. }
  192. } catch (\Exception $e) {
  193. $result['status'] = 0;
  194. $result['msg'] = $e->getMessage();
  195. }
  196. return $result;
  197. }
  198. /**
  199. * 删除黑名单
  200. */
  201. public function getBlackList($params=[])
  202. {
  203. $result = [
  204. 'status' => 1,
  205. 'msg' => '获取成功',
  206. 'data' => [],
  207. ];
  208. try {
  209. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  210. $page = isset($params['page']) ? $params['page'] : 0;
  211. $limit = isset($params['limit']) ? $params['limit'] : 30;
  212. $url = "https://console.tim.qq.com/v4/sns/black_list_get".$this->url;
  213. $tencentObj = new tencentim($url);
  214. if(!empty($userId)) {
  215. $data["From_Account"] = $userId;
  216. $data["StartIndex"] = $page;
  217. $data["MaxLimited"] = $limit;
  218. $data["LastSequence"] = 0;
  219. $imRes = $tencentObj->toSend($data);
  220. if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
  221. $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
  222. throw new Exception(json_encode($errorInfo));
  223. }
  224. $result['data'] = isset($imRes['BlackListItem']) ? $imRes['BlackListItem'] : [];
  225. }
  226. } catch (\Exception $e) {
  227. $result['status'] = 0;
  228. $result['msg'] = $e->getMessage();
  229. }
  230. return $result;
  231. }
  232. /**
  233. * 创建用户账号
  234. */
  235. public function accountImport($params=[])
  236. {
  237. $result = [
  238. 'status' => 1,
  239. 'msg' => '获取成功',
  240. 'data' => [],
  241. ];
  242. try {
  243. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  244. $nickname = isset($params['nickname']) ? $params['nickname'] : '';
  245. $avatar = isset($params['avatar']) ? $params['avatar'] : '';
  246. $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import".$this->url;
  247. $tencentObj = new tencentim($url);
  248. if(!empty($userId)) {
  249. $data["UserID"] = (string)$userId;
  250. $data["Nick"] = $nickname;
  251. $data["FaceUrl"] = $avatar;
  252. $imRes = $tencentObj->toSend($data);
  253. if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
  254. $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
  255. throw new Exception(json_encode($errorInfo));
  256. }
  257. }
  258. } catch (\Exception $e) {
  259. $result['status'] = 0;
  260. $result['msg'] = $e->getMessage();
  261. }
  262. return $result;
  263. }
  264. /**
  265. * 查询用户账号
  266. */
  267. public function accountCheck($params=[])
  268. {
  269. $result = [
  270. 'status' => 1,
  271. 'msg' => '获取成功',
  272. 'data' => [],
  273. ];
  274. try {
  275. $userIds = isset($params['user_ids']) ? $params['user_ids'] : [];
  276. if(!empty($userIds)) {
  277. $url = "https://console.tim.qq.com/v4/im_open_login_svc/account_check".$this->url;
  278. $tencentObj = new tencentim($url);
  279. foreach ($userIds as $key => $value) {
  280. $userIdArr[] = [
  281. 'UserID' => $value,
  282. ];
  283. }
  284. $data["CheckItem"] = $userIdArr;
  285. $imRes = $tencentObj->toSend($data);
  286. if (isset($imRes['ActionStatus']) && $imRes['ActionStatus'] != 'OK') {
  287. $errorInfo = isset($imRes['ErrorInfo']) ? $imRes['ErrorInfo'] : [];
  288. throw new Exception(json_encode($errorInfo));
  289. }
  290. $result['data'] = isset($imRes['ResultItem']) ? $imRes['ResultItem'] : [];
  291. }
  292. } catch (\Exception $e) {
  293. $result['status'] = 0;
  294. $result['msg'] = $e->getMessage();
  295. }
  296. return $result;
  297. }
  298. }