123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- declare(strict_types=1);
- namespace App\Controller\Api\Notify;
- use App\Master\Enum\RedisKeyEnum;
- use App\Master\Framework\Library\Tencent\TencentIm;
- use App\Model\Arts\LiveRoomLogModel;
- use App\Model\Arts\LiveRoomModel;
- use App\Service\QueueService;
- use App\Utils\AppResult;
- use App\Utils\LogUtil;
- use App\Utils\RedisUtil;
- use Hyperf\Di\Annotation\Inject;
- use Hyperf\HttpServer\Contract\RequestInterface;
- use Hyperf\HttpServer\Contract\ResponseInterface;
- class TencentImController
- {
- // 日志模块名称
- const LOG_MODULE = 'Notify/TencentImController';
- #[Inject]
- protected QueueService $service;
- /**
- * im 回调通知
- * @return \Psr\Http\Message\MessageInterface|\Psr\Http\Message\ResponseInterface
- */
- public function callback(RequestInterface $request,ResponseInterface $response)
- {
- $params = $request->all();
- $CallbackCommand = $params["CallbackCommand"] ?? '';
- switch ($CallbackCommand) {
- case 'Group.CallbackAfterGroupDestroyed':// 群组解散之后回调
- LogUtil::info('群组解散之后回调',self::LOG_MODULE,__FUNCTION__);
- $room_no = $params["GroupId"];
- $model = new LiveRoomModel();
- $room = $model->getDetail(params: ['room_no'=>$room_no]);
- if (!$room){
- return AppResult::im_success('直播间不存在或已下播');
- }
- $model = new LiveRoomModel();
- if (!$model->closeRoom($room['id'])){
- return AppResult::im_error($model->getMessage() ?? '操作失败');
- }
- break;
- case 'Group.CallbackAfterMemberExit': // 成员离开
- LogUtil::info('群成员离开之后回调',self::LOG_MODULE,__FUNCTION__);
- $room_no = $params["GroupId"];
- $members = $params["ExitMemberList"];
- $model = new LiveRoomModel();
- $model->setIsStatusSearchValue(0);
- $room = $model->getDetail(params: ['room_no'=>$room_no]);
- if (!$room){
- LogUtil::info('直播间不存在或已下播',self::LOG_MODULE,__FUNCTION__);
- }
- if($members) {
- foreach($members as $k => $v) {
- $gz_user_id = im_un_prefix($v["Member_Account"]);
- // 从原房间内移除此用户
- RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST,$room_no)->zRem($gz_user_id);
- // 扣除在线用户在房间情况
- RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_IN)->hDel((string)$gz_user_id);
- if ($room){
- $this->service->liveRoomDataPush(['session' => $room['session'], 'ccu' => -1]);
- if ($room['user_id'] == $gz_user_id){
- // 如果是主播,则关闭直播间
- $model = new LiveRoomModel();
- $model->closeRoom($room['id']);
- // 腾讯直播解散群组
- $im = new TencentIm();
- $im->destroy_group($room_no);
- }
- }
- }
- }
- break;
- case 'Group.CallbackOnMemberStateChange':
- LogUtil::info('直播群成员在线状态回调',self::LOG_MODULE,__FUNCTION__);
- $type = $params['EventType'];
- $room_no = $params["GroupId"];
- $members = $params["MemberList"];
- $model = new LiveRoomModel();
- $room = $model->where('room_no',$room_no)->whereIn('status',[1,2])->find();
- if (!$room){
- LogUtil::info('直播间不存在或已下播',self::LOG_MODULE,__FUNCTION__);
- }
- if ($type == 'Offline'){
- LogUtil::info('直播群成员离线',self::LOG_MODULE,__FUNCTION__);
- if($members) {
- foreach($members as $k => $v) {
- $gz_user_id = im_un_prefix($v["Member_Account"]);
- // 从原房间内移除此用户
- RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST,$room_no)->zrem($gz_user_id);
- // 扣除在线用户在房间情况
- RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_IN)->hDel($gz_user_id);
- if ($room){
- $this->service->liveRoomDataPush(['session' => $room['session'], 'ccu' => -1]);
- if ($room['user_id'] == $gz_user_id){
- // 如果是主播,则关闭直播间
- $model = new LiveRoomModel();
- $model->closeRoom($room['id']);
- // 腾讯直播解散群组
- $im = new TencentIm();
- $im->destroy_group($room_no);
- }
- }
- }
- }
- }
- break;
- }
- // 处理结果通知
- return AppResult::im_success('处理成功');
- }
- }
|