TencentImController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Api\Notify;
  4. use App\Master\Enum\RedisKeyEnum;
  5. use App\Master\Framework\Library\Tencent\TencentIm;
  6. use App\Model\Arts\LiveRoomLogModel;
  7. use App\Model\Arts\LiveRoomModel;
  8. use App\Service\QueueService;
  9. use App\Utils\AppResult;
  10. use App\Utils\LogUtil;
  11. use App\Utils\RedisUtil;
  12. use Hyperf\Di\Annotation\Inject;
  13. use Hyperf\HttpServer\Contract\RequestInterface;
  14. use Hyperf\HttpServer\Contract\ResponseInterface;
  15. class TencentImController
  16. {
  17. // 日志模块名称
  18. const LOG_MODULE = 'Notify/TencentImController';
  19. #[Inject]
  20. protected QueueService $service;
  21. /**
  22. * im 回调通知
  23. * @return \Psr\Http\Message\MessageInterface|\Psr\Http\Message\ResponseInterface
  24. */
  25. public function callback(RequestInterface $request,ResponseInterface $response)
  26. {
  27. $params = $request->all();
  28. $CallbackCommand = $params["CallbackCommand"] ?? '';
  29. switch ($CallbackCommand) {
  30. case 'Group.CallbackAfterGroupDestroyed':// 群组解散之后回调
  31. LogUtil::info('群组解散之后回调',self::LOG_MODULE,__FUNCTION__);
  32. $room_no = $params["GroupId"];
  33. $model = new LiveRoomModel();
  34. $room = $model->getDetail(params: ['room_no'=>$room_no]);
  35. if (!$room){
  36. return AppResult::im_success('直播间不存在或已下播');
  37. }
  38. $model = new LiveRoomModel();
  39. if (!$model->closeRoom($room['id'])){
  40. return AppResult::im_error($model->getMessage() ?? '操作失败');
  41. }
  42. break;
  43. case 'Group.CallbackAfterMemberExit': // 成员离开
  44. LogUtil::info('群成员离开之后回调',self::LOG_MODULE,__FUNCTION__);
  45. $room_no = $params["GroupId"];
  46. $members = $params["ExitMemberList"];
  47. $model = new LiveRoomModel();
  48. $model->setIsStatusSearchValue(0);
  49. $room = $model->getDetail(params: ['room_no'=>$room_no]);
  50. if (!$room){
  51. LogUtil::info('直播间不存在或已下播',self::LOG_MODULE,__FUNCTION__);
  52. }
  53. if($members) {
  54. foreach($members as $k => $v) {
  55. $gz_user_id = im_un_prefix($v["Member_Account"]);
  56. // 从原房间内移除此用户
  57. RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST,$room_no)->zRem($gz_user_id);
  58. // 扣除在线用户在房间情况
  59. RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_IN)->hDel((string)$gz_user_id);
  60. if ($room){
  61. $this->service->liveRoomDataPush(['session' => $room['session'], 'ccu' => -1]);
  62. if ($room['user_id'] == $gz_user_id){
  63. // 如果是主播,则关闭直播间
  64. $model = new LiveRoomModel();
  65. $model->closeRoom($room['id']);
  66. // 腾讯直播解散群组
  67. $im = new TencentIm();
  68. $im->destroy_group($room_no);
  69. }
  70. }
  71. }
  72. }
  73. break;
  74. case 'Group.CallbackOnMemberStateChange':
  75. LogUtil::info('直播群成员在线状态回调',self::LOG_MODULE,__FUNCTION__);
  76. $type = $params['EventType'];
  77. $room_no = $params["GroupId"];
  78. $members = $params["MemberList"];
  79. $model = new LiveRoomModel();
  80. $room = $model->where('room_no',$room_no)->whereIn('status',[1,2])->find();
  81. if (!$room){
  82. LogUtil::info('直播间不存在或已下播',self::LOG_MODULE,__FUNCTION__);
  83. }
  84. if ($type == 'Offline'){
  85. LogUtil::info('直播群成员离线',self::LOG_MODULE,__FUNCTION__);
  86. if($members) {
  87. foreach($members as $k => $v) {
  88. $gz_user_id = im_un_prefix($v["Member_Account"]);
  89. // 从原房间内移除此用户
  90. RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST,$room_no)->zrem($gz_user_id);
  91. // 扣除在线用户在房间情况
  92. RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_IN)->hDel($gz_user_id);
  93. if ($room){
  94. $this->service->liveRoomDataPush(['session' => $room['session'], 'ccu' => -1]);
  95. if ($room['user_id'] == $gz_user_id){
  96. // 如果是主播,则关闭直播间
  97. $model = new LiveRoomModel();
  98. $model->closeRoom($room['id']);
  99. // 腾讯直播解散群组
  100. $im = new TencentIm();
  101. $im->destroy_group($room_no);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. break;
  108. }
  109. // 处理结果通知
  110. return AppResult::im_success('处理成功');
  111. }
  112. }