|
@@ -0,0 +1,300 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\utils\Service;
|
|
|
+
|
|
|
+use app\utils\CurlUtil;
|
|
|
+use getusersig\getusersig;
|
|
|
+
|
|
|
+class TencentIm
|
|
|
+{
|
|
|
+ private $config;
|
|
|
+ private string $host = "https://console.tim.qq.com";
|
|
|
+ protected string $message = 'error';
|
|
|
+ protected $data = [];
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ // 获取配置信息
|
|
|
+ $this->config = config("tencent_im");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送消息给某人
|
|
|
+ * @param $from_user
|
|
|
+ * @param $to_user
|
|
|
+ * @param $message
|
|
|
+ * @param $type //0普通消息 1自定义消息
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function sendMessageToUser($from_user, $to_user, $message, $type = 0)
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'SyncOtherMachine' => 1,
|
|
|
+ 'From_Account' => (string)$from_user,
|
|
|
+ 'To_Account' => (string)$to_user,
|
|
|
+ 'MsgRandom' => rand(1000000, 9999999),
|
|
|
+ 'MsgTimeStamp' => time(),
|
|
|
+ ];
|
|
|
+ if ($type == 1) { //自定义消息
|
|
|
+ $body_message = '{"businessID":"custom_tips_message","receiverShow":true,"senderId":"' . $from_user . '","text":"' . $message . '","tipType":257,"version":0}';
|
|
|
+ $data["MsgBody"][] = [
|
|
|
+ "MsgType" => "TIMCustomElem",
|
|
|
+ "MsgContent" => [
|
|
|
+ "Data" => $body_message,
|
|
|
+ "Desc" => $body_message,
|
|
|
+ "Ext" => 'custom_tips_message',
|
|
|
+ "Sound" => ''
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $data["MsgBody"][] = [
|
|
|
+ "MsgType" => "TIMTextElem",
|
|
|
+ "MsgContent" => [
|
|
|
+ "Text" => $message
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $res = $this->postJson('/v4/openim/sendmsg', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('创建成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建群组
|
|
|
+ * @param $user_id
|
|
|
+ * @param $room_no
|
|
|
+ * @param $room_name
|
|
|
+ * @param $type
|
|
|
+ * @param $Notification // 群公告
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function create_group($user_id, $room_no, $room_name, $type = 'AVChatRoom', $Notification = '')
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'Owner_Account' => (string)$user_id, // 群主 ID
|
|
|
+ 'Type' => $type, // 群组形态,包括 Public(陌生人社交群),Private(即 Work,好友工作群),ChatRoom(即 Meeting,会议群),AVChatRoom(直播群),Community(社群)
|
|
|
+ 'GroupId' => (string)$room_no, // 自定义群组 ID
|
|
|
+ 'Name' => $room_name,
|
|
|
+ 'Introduction' => json_encode([
|
|
|
+ ''
|
|
|
+ ]),// 群简介
|
|
|
+ 'Notification' => $Notification,// 群公告
|
|
|
+ ];
|
|
|
+ $res = $this->postJson('/v4/group_open_http_svc/create_group', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('创建成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解散群组
|
|
|
+ * @param $room_no
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function destroy_group($room_no)
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'GroupId' => (string)$room_no
|
|
|
+ ];
|
|
|
+ $res = $this->postJson('/v4/group_open_http_svc/destroy_group', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在群组中发送系统通知
|
|
|
+ * @param $room_no
|
|
|
+ * @param $type // 雷松:10=点赞通知 20=pk通知
|
|
|
+ * @param $content
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function send_group_system_notification($room_no, $type, $content)
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'GroupId' => (string)$room_no,
|
|
|
+ 'Content' => json_encode([
|
|
|
+ 'type' => $type,
|
|
|
+ 'content' => $content,
|
|
|
+ ]),
|
|
|
+ ];
|
|
|
+ $res = $this->postJson('/v4/group_open_http_svc/send_group_system_notification', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 封禁用户
|
|
|
+ * @param $room_no
|
|
|
+ * @param $account
|
|
|
+ * @param $time
|
|
|
+ * @param $remark
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function ban_group_member($room_no, $account, $time, $remark = '违规操作')
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'GroupId' => (string)$room_no,
|
|
|
+ 'Members_Account' => [$account],
|
|
|
+ 'Duration' => $time, // 封禁时长,单位:秒
|
|
|
+ 'Description' => $remark// 封禁信息
|
|
|
+ ];
|
|
|
+ $res = $this->postJson('/v4/group_open_http_svc/ban_group_member', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解封用户
|
|
|
+ * @param $room_no
|
|
|
+ * @param $account
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function unban_group_member($room_no, $account)
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'GroupId' => (string)$room_no,
|
|
|
+ 'Members_Account' => [$account]
|
|
|
+ ];
|
|
|
+ $res = $this->postJson('/v4/group_open_http_svc/unban_group_member', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移除房间
|
|
|
+ * @param $room_no
|
|
|
+ * @param $account
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function delete_group_member($room_no, $account)
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'GroupId' => (string)$room_no,
|
|
|
+ 'MemberToDel_Account' => [$account]
|
|
|
+ ];
|
|
|
+ $res = $this->postJson('/v4/group_open_http_svc/delete_group_member', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 禁言 or 解除
|
|
|
+ * @param $room_no
|
|
|
+ * @param $account
|
|
|
+ * @param $time // time = 0 解除
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function forbid_send_msg($room_no, $account, $time = 0)
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'GroupId' => (string)$room_no,
|
|
|
+ 'Members_Account' => [$account],
|
|
|
+ 'MuteTime' => $time
|
|
|
+ ];
|
|
|
+ $res = $this->postJson('/v4/group_open_http_svc/forbid_send_msg', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取全部群组
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public function get_appid_group_list()
|
|
|
+ {
|
|
|
+ $data = [
|
|
|
+ 'Limit' => 1000,// 群主 ID
|
|
|
+ 'Next' => 0
|
|
|
+ ];
|
|
|
+ $res = $this->postJson('/v4/group_open_http_svc/get_appid_group_list', $data);
|
|
|
+ if (empty($res['ActionStatus']) || $res['ActionStatus'] != 'OK') {
|
|
|
+ return $this->error(!empty($res['ErrorInfo']) ? $res['ErrorInfo'] : 'im error', $res ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->success('获取成功', $res);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function postJson(string $uri, array $params = [])
|
|
|
+ {
|
|
|
+ $random = rand(10000000, 99999999);
|
|
|
+ $userSig = $this->usersig($this->config['identifier']);
|
|
|
+ return CurlUtil::postJson($this->host . $uri . "?sdkappid={$this->config['sdkappid']}&identifier={$this->config['identifier']}&usersig={$userSig}&random={$random}&contenttype=json", $params);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取usersig签名-具体操作
|
|
|
+ */
|
|
|
+ private function userSig($user_id)
|
|
|
+ {
|
|
|
+ // 获取配置信息
|
|
|
+ $userSigObj = new getusersig($this->config["sdkappid"], $this->config["key"]);
|
|
|
+ return $userSigObj->genUserSig($user_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回成功结果
|
|
|
+ * @param string $message
|
|
|
+ * @param mixed $data
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ protected function success(string $message = 'success', $data = []): bool
|
|
|
+ {
|
|
|
+ $this->message = $message;
|
|
|
+ $this->data = $data;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回失败结果
|
|
|
+ * @param string $message
|
|
|
+ * @param mixed $data
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ protected function error(string $message = 'error', $data = []): bool
|
|
|
+ {
|
|
|
+ $this->message = $message;
|
|
|
+ $this->data = $data;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取成功数据
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getData()
|
|
|
+ {
|
|
|
+ return $this->data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取消息
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public function getMessage(): string
|
|
|
+ {
|
|
|
+ return $this->message;
|
|
|
+ }
|
|
|
+}
|