123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- <?php
- declare(strict_types=1);
- namespace App\Master\Framework\Library\Tencent;
- use App\Master\Framework\Library\Library;
- use Hyperf\Config\Annotation\Value;
- use function Hyperf\Config\config;
- class TencentIm extends Library
- {
- public string $base_uri = "https://console.tim.qq.com";
- #[Value("tencent.im")]
- private array $config;
- /**
- * 创建群组
- * @param string $user_chat_id
- * @param string $group_no
- * @param string $room_name
- * @param string $type
- * @param string $notification
- * @return bool
- */
- public function create_group(string $user_chat_id,string $group_no,string $room_name,string $type = 'AVChatRoom', string $notification = '')
- {
- $data = [
- 'Owner_Account' => $user_chat_id, // 群主 ID
- 'Type' => $type, // 群组形态,包括 Public(陌生人社交群),Private(即 Work,好友工作群),ChatRoom(即 Meeting,会议群),AVChatRoom(直播群),Community(社群)
- 'GroupId' => $group_no, // 自定义群组 ID
- 'Name' => $room_name,
- 'Introduction' => json_encode([
- ''
- ]),// 群简介
- 'Notification' => $notification,// 群公告
- ];
- $response = $this->post('/v4/group_open_http_svc/create_group', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('创建成功', $body);
- }
- /**
- * 创建群组
- * @param string $user_chat_id
- * @param string $group_no
- * @param string $room_name
- * @param string $type
- * @param string $notification
- * @return bool
- */
- public function destroy_group(string $group_no)
- {
- $data = [
- 'GroupId' => $group_no, // 自定义群组 ID
- ];
- $response = $this->post('/v4/group_open_http_svc/destroy_group', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('关闭成功', $body);
- }
- /**
- * 移除房间
- * @param string $group_no
- * @param string $user_chat_id
- * @return bool
- */
- public function delete_group_member(string $group_no,string $user_chat_id)
- {
- $data = [
- 'GroupId' => $group_no, // 自定义群组 ID
- 'MemberToDel_Account' => [$user_chat_id]
- ];
- $response = $this->post('/v4/group_open_http_svc/delete_group_member', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('操作成功', $body);
- }
- /**
- * 设置/取消直播群管理员
- * @param string $group_no
- * @param string $user_chat_id
- * @return bool
- */
- public function modify_admin(string $group_no,string $user_chat_id,int $type = 1)
- {
- $data = [
- 'GroupId' => $group_no, // 自定义群组 ID
- 'CommandType' => $type,
- 'Admin_Account' => [$user_chat_id],
- ];
- $response = $this->post('/v4/group_open_avchatroom_http_svc/modify_admin', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('操作成功', $body);
- }
- /**
- * 禁言 or 解除
- * @param string $group_no
- * @param string $user_chat_id
- * @param $time // time = 0 解除
- * @return bool
- */
- public function forbid_send_msg(string $group_no,string $user_chat_id,int $time = 0)
- {
- $data = [
- 'GroupId' => $group_no, // 自定义群组 ID
- 'Members_Account' => [$user_chat_id],
- 'MuteTime' => $time
- ];
- $response = $this->post('/v4/group_open_http_svc/forbid_send_msg', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('操作成功', $body);
- }
- /**
- * 获取被禁言群成员列表
- * @param string $group_no
- * @return bool
- */
- public function get_group_muted_account(string $group_no)
- {
- $data = [
- 'GroupId' => $group_no, // 自定义群组 ID
- ];
- $response = $this->post('/v4/group_open_http_svc/get_group_muted_account', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('操作成功', $body);
- }
- /**
- * 获取被禁言群成员列表
- * @param string $group_no
- * @return bool
- */
- public function get_group_ban_member(string $group_no,int $limit = 100,int $Offset = 0)
- {
- $data = [
- 'GroupId' => $group_no, // 自定义群组 ID
- 'Limit' => $limit, // 单次拉取限制,最大为100
- 'Offset' => $Offset, // 分页标识,首次传0,如果封禁的成员数大于100,下一次拉取需要设置为后台回复的 NextOffset
- ];
- $response = $this->post('/v4/group_open_http_svc/get_group_ban_member', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('操作成功', $body);
- }
- /**
- * 封禁用户
- * @param string $group_no
- * @param string $user_chat_id
- * @param $time // time = 0 解除
- * @param $remark
- * @return bool
- */
- public function ban_group_member(string $group_no,string $user_chat_id,int $time = 0, $remark = '违规操作')
- {
- $data = [
- 'GroupId' => $group_no,
- 'Members_Account' => [$user_chat_id],
- 'Duration' => $time, // 封禁时长,单位:秒
- 'Description' => $remark// 封禁信息
- ];
- $response = $this->post('/v4/group_open_http_svc/ban_group_member', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('操作成功', $body);
- }
- /**
- * 解除封禁用户
- * @param string $group_no
- * @param string $user_chat_id
- * @return bool
- */
- public function unban_group_member(string $group_no,string $user_chat_id)
- {
- $data = [
- 'GroupId' => $group_no,
- 'Members_Account' => [$user_chat_id]
- ];
- $response = $this->post('/v4/group_open_http_svc/unban_group_member', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('操作成功', $body);
- }
- /**
- * 获取直播群在线人数
- * @param int $room_no
- * @return bool
- */
- public function get_online_member_num(string $room_no)
- {
- $data = [
- 'GroupId' => (string)$room_no
- ];
- $response = $this->post('/v4/group_open_http_svc/get_online_member_num', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('获取成功', $body);
- }
- /**
- * 设置计数器
- * @param string $room_no
- * @param array $counter
- * @return bool
- */
- public function update_group_counter(string $room_no, array $counter)
- {
- $data = [
- 'GroupId' => $room_no,// 群组 ID
- 'GroupCounter' => $counter,
- 'Mode' => 'Set'
- ];
- $response = $this->post('/v4/group_open_http_svc/update_group_counter', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('获取成功', $body);
- }
- /**
- * 设置群属性
- * @param string $room_no
- * @param array $attr
- * @return bool
- */
- public function modify_group_attr(string $room_no, array $attr)
- {
- $data = [
- 'GroupId' => $room_no,// 群组 ID
- 'GroupAttr' => $attr
- ];
- $response = $this->post('/v4/group_open_http_svc/modify_group_attr', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('获取成功', $body);
- }
- /**
- * 获取全部群组
- * @return bool
- */
- public function get_appid_group_list()
- {
- $data = [
- 'Limit' => 1000,// 群主 ID
- 'Next' => 0
- ];
- $response = $this->post('/v4/group_open_http_svc/get_appid_group_list', $data);
- if ($response->getStatusCode() != 200) {
- return $this->error($response->getReasonPhrase());
- }
- $json = $response->getBody()->getContents();
- $body = json_decode($json, true);
- if (empty($body['ActionStatus']) || $body['ActionStatus'] != 'OK') {
- return $this->error(!empty($body['ErrorInfo']) ? $body['ErrorInfo'] : 'im error', $body ?? []);
- }
- return $this->success('获取成功', $body);
- }
- /**
- * 获取直播推流信息
- * @param string $stream_name 直播间号
- * @param string $time 过期时间
- * @return string
- */
- public function getLivePushUrl(string $stream_name, string $time = '')
- {
- $key = $this->config['push_key'];
- $time = !empty($time) ? $time : date('Y-m-d H:i:s', strtotime('+1 day'));
- $domain = $this->config['push_domain'];
- if ($key && $time) {
- $txTime = strtoupper(base_convert((string)strtotime($time), 10, 16));
- //txSecret = MD5( KEY + streamName + txTime )
- $txSecret = md5($key . $stream_name . $txTime);
- $ext_str = "?" . http_build_query(["txSecret" => $txSecret, "txTime" => $txTime]);
- }
- return "rtmp://{$domain}/live/{$stream_name}" . ($ext_str ?? "");
- }
- /**
- * 获取直播播放地址
- * @param string $stream_name 您用来区别不同推流地址的唯一流名称
- * @return string
- */
- public function getLivePlayUrl(string $stream_name)
- {
- return "rtmp://{$this->config['play_domain']}/live/".$stream_name;
- }
- /**
- * 获取usersig签名-具体操作
- */
- public function userSig($user_id)
- {
- // 获取配置信息
- $userSigObj = new GetUserSig($this->config["appid"], $this->config["key"]);
- return $userSigObj->genUserSig($user_id);
- }
- private function post(string $uri, array $params = [])
- {
- $random = rand(10000000, 99999999);
- $userSig = $this->usersig($this->config['identifier']);
- return $this->postJson("{$uri}?sdkappid={$this->config['appid']}&identifier={$this->config['identifier']}&usersig={$userSig}&random={$random}&contenttype=json", $params);
- }
- public function setConfig(array $config): TencentIm
- {
- $this->config = $config;
- return $this;
- }
- }
|