123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?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");
- }
-
- 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);
- }
-
- public function create_group($user_id, $room_no, $room_name, $type = 'AVChatRoom', $Notification = '')
- {
- $data = [
- 'Owner_Account' => (string)$user_id,
- 'Type' => $type,
- 'GroupId' => (string)$room_no,
- '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);
- }
-
- 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);
- }
-
- 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);
- }
-
- 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);
- }
-
- 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);
- }
-
- 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);
- }
-
- 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);
- }
-
- public function get_appid_group_list()
- {
- $data = [
- 'Limit' => 1000,
- '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);
- }
-
- private function userSig($user_id)
- {
-
- $userSigObj = new getusersig($this->config["sdkappid"], $this->config["key"]);
- return $userSigObj->genUserSig($user_id);
- }
-
- protected function success(string $message = 'success', $data = []): bool
- {
- $this->message = $message;
- $this->data = $data;
- return true;
- }
-
- protected function error(string $message = 'error', $data = []): bool
- {
- $this->message = $message;
- $this->data = $data;
- return false;
- }
-
- public function getData()
- {
- return $this->data;
- }
-
- public function getMessage(): string
- {
- return $this->message;
- }
- }
|