TencentIm.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace app\utils\Service;
  3. use app\utils\CurlUtil;
  4. use getusersig\getusersig;
  5. class TencentIm
  6. {
  7. private $config;
  8. private string $host = "https://console.tim.qq.com";
  9. protected string $message = 'error';
  10. protected $data = [];
  11. public function __construct()
  12. {
  13. // 获取配置信息
  14. $this->config = config("tencent_im");
  15. }
  16. /**
  17. * 发送消息给某人
  18. * @param $from_user
  19. * @param $to_user
  20. * @param $message
  21. * @param $type //0普通消息 1自定义消息
  22. * @return bool
  23. */
  24. public function sendMessageToUser($from_user, $to_user, $message, $type = 0)
  25. {
  26. $data = [
  27. 'SyncOtherMachine' => 1,
  28. 'From_Account' => (string)$from_user,
  29. 'To_Account' => (string)$to_user,
  30. 'MsgRandom' => rand(1000000, 9999999),
  31. 'MsgTimeStamp' => time(),
  32. ];
  33. if ($type == 1) { //自定义消息
  34. $body_message = '{"businessID":"custom_tips_message","receiverShow":true,"senderId":"' . $from_user . '","text":"' . $message . '","tipType":257,"version":0}';
  35. $data["MsgBody"][] = [
  36. "MsgType" => "TIMCustomElem",
  37. "MsgContent" => [
  38. "Data" => $body_message,
  39. "Desc" => $body_message,
  40. "Ext" => 'custom_tips_message',
  41. "Sound" => ''
  42. ],
  43. ];
  44. } else {
  45. $data["MsgBody"][] = [
  46. "MsgType" => "TIMTextElem",
  47. "MsgContent" => [
  48. "Text" => $message
  49. ],
  50. ];
  51. }
  52. $res = $this->postJson('/v4/openim/sendmsg', $data);
  53. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  54. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  55. }
  56. return $this->success('创建成功', $res);
  57. }
  58. /**
  59. * 创建群组
  60. * @param $user_id
  61. * @param $room_no
  62. * @param $room_name
  63. * @param $type
  64. * @param $Notification // 群公告
  65. * @return bool
  66. */
  67. public function create_group($user_id, $room_no, $room_name, $type = 'AVChatRoom', $Notification = '')
  68. {
  69. $data = [
  70. 'Owner_Account' => (string)$user_id, // 群主 ID
  71. 'Type' => $type, // 群组形态,包括 Public(陌生人社交群),Private(即 Work,好友工作群),ChatRoom(即 Meeting,会议群),AVChatRoom(直播群),Community(社群)
  72. 'GroupId' => (string)$room_no, // 自定义群组 ID
  73. 'Name' => $room_name,
  74. 'Introduction' => json_encode([
  75. ''
  76. ]),// 群简介
  77. 'Notification' => $Notification,// 群公告
  78. ];
  79. $res = $this->postJson('/v4/group_open_http_svc/create_group', $data);
  80. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  81. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  82. }
  83. return $this->success('创建成功', $res);
  84. }
  85. /**
  86. * 解散群组
  87. * @param $room_no
  88. * @return bool
  89. */
  90. public function destroy_group($room_no)
  91. {
  92. $data = [
  93. 'GroupId' => (string)$room_no
  94. ];
  95. $res = $this->postJson('/v4/group_open_http_svc/destroy_group', $data);
  96. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  97. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  98. }
  99. return $this->success('获取成功', $res);
  100. }
  101. /**
  102. * 在群组中发送系统通知
  103. * @param $room_no
  104. * @param $type // 雷松:10=点赞通知 20=pk通知
  105. * @param $content
  106. * @return bool
  107. */
  108. public function send_group_system_notification($room_no, $type, $content)
  109. {
  110. $data = [
  111. 'GroupId' => (string)$room_no,
  112. 'Content' => json_encode([
  113. 'type' => $type,
  114. 'content' => $content,
  115. ]),
  116. ];
  117. $res = $this->postJson('/v4/group_open_http_svc/send_group_system_notification', $data);
  118. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  119. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  120. }
  121. return $this->success('获取成功', $res);
  122. }
  123. /**
  124. * 封禁用户
  125. * @param $room_no
  126. * @param $account
  127. * @param $time
  128. * @param $remark
  129. * @return bool
  130. */
  131. public function ban_group_member($room_no, $account, $time, $remark = '违规操作')
  132. {
  133. $data = [
  134. 'GroupId' => (string)$room_no,
  135. 'Members_Account' => [$account],
  136. 'Duration' => $time, // 封禁时长,单位:秒
  137. 'Description' => $remark// 封禁信息
  138. ];
  139. $res = $this->postJson('/v4/group_open_http_svc/ban_group_member', $data);
  140. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  141. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  142. }
  143. return $this->success('获取成功', $res);
  144. }
  145. /**
  146. * 解封用户
  147. * @param $room_no
  148. * @param $account
  149. * @return bool
  150. */
  151. public function unban_group_member($room_no, $account)
  152. {
  153. $data = [
  154. 'GroupId' => (string)$room_no,
  155. 'Members_Account' => [$account]
  156. ];
  157. $res = $this->postJson('/v4/group_open_http_svc/unban_group_member', $data);
  158. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  159. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  160. }
  161. return $this->success('获取成功', $res);
  162. }
  163. /**
  164. * 移除房间
  165. * @param $room_no
  166. * @param $account
  167. * @return bool
  168. */
  169. public function delete_group_member($room_no, $account)
  170. {
  171. $data = [
  172. 'GroupId' => (string)$room_no,
  173. 'MemberToDel_Account' => [$account]
  174. ];
  175. $res = $this->postJson('/v4/group_open_http_svc/delete_group_member', $data);
  176. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  177. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  178. }
  179. return $this->success('获取成功', $res);
  180. }
  181. /**
  182. * 禁言 or 解除
  183. * @param $room_no
  184. * @param $account
  185. * @param $time // time = 0 解除
  186. * @return bool
  187. */
  188. public function forbid_send_msg($room_no, $account, $time = 0)
  189. {
  190. $data = [
  191. 'GroupId' => (string)$room_no,
  192. 'Members_Account' => [$account],
  193. 'MuteTime' => $time
  194. ];
  195. $res = $this->postJson('/v4/group_open_http_svc/forbid_send_msg', $data);
  196. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  197. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  198. }
  199. return $this->success('获取成功', $res);
  200. }
  201. /**
  202. * 获取全部群组
  203. * @return bool
  204. */
  205. public function get_appid_group_list()
  206. {
  207. $data = [
  208. 'Limit' => 1000,// 群主 ID
  209. 'Next' => 0
  210. ];
  211. $res = $this->postJson('/v4/group_open_http_svc/get_appid_group_list', $data);
  212. if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
  213. return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
  214. }
  215. return $this->success('获取成功', $res);
  216. }
  217. private function postJson(string $uri, array $params = [])
  218. {
  219. $random = rand(10000000, 99999999);
  220. $userSig = $this->usersig($this->config['identifier']);
  221. return CurlUtil::postJson($this->host . $uri . "?sdkappid={$this->config['sdkappid']}&identifier={$this->config['identifier']}&usersig={$userSig}&random={$random}&contenttype=json", $params);
  222. }
  223. /**
  224. * 获取usersig签名-具体操作
  225. */
  226. private function userSig($user_id)
  227. {
  228. // 获取配置信息
  229. $userSigObj = new getusersig($this->config["sdkappid"], $this->config["key"]);
  230. return $userSigObj->genUserSig($user_id);
  231. }
  232. /**
  233. * 返回成功结果
  234. * @param string $message
  235. * @param mixed $data
  236. * @return bool
  237. */
  238. protected function success(string $message = 'success', $data = []): bool
  239. {
  240. $this->message = $message;
  241. $this->data = $data;
  242. return true;
  243. }
  244. /**
  245. * 返回失败结果
  246. * @param string $message
  247. * @param mixed $data
  248. * @return bool
  249. */
  250. protected function error(string $message = 'error', $data = []): bool
  251. {
  252. $this->message = $message;
  253. $this->data = $data;
  254. return false;
  255. }
  256. /**
  257. * 获取成功数据
  258. * @return mixed
  259. */
  260. public function getData()
  261. {
  262. return $this->data;
  263. }
  264. /**
  265. * 获取消息
  266. * @return string
  267. */
  268. public function getMessage(): string
  269. {
  270. return $this->message;
  271. }
  272. }