LiveRoomSendJob.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Job;
  4. use App\Master\Enum\RedisKeyEnum;
  5. use App\Master\Framework\Extend\BaseJob;
  6. use App\Master\Framework\Library\Tencent\TencentIm;
  7. use App\Model\Arts\LiveRoomLogLikeModel;
  8. use App\Model\Arts\LiveRoomLogModel;
  9. use App\Model\Arts\LiveRoomModel;
  10. use App\Utils\LogUtil;
  11. use App\Utils\RedisUtil;
  12. class LiveRoomSendJob extends BaseJob
  13. {
  14. //日志板块
  15. protected string $LOG_MODULE = 'LiveRoomSendJob';
  16. /**
  17. * 任务执行失败后的重试次数,即最大执行次数为 $maxAttempts+1 次
  18. */
  19. protected int $maxAttempts = 2;
  20. public function __construct($params)
  21. {
  22. parent::__construct($params);
  23. }
  24. /**
  25. * 执行
  26. * @param $params
  27. * @return bool
  28. */
  29. protected function do($params): bool
  30. {
  31. $im = new TencentIm();
  32. $model = new LiveRoomModel();
  33. $room = $model->getDetail(params: ['room_no' => $params['room_no']],with: ['log']);
  34. if (!$room) {
  35. return $this->error('直播间异常');
  36. }
  37. // 更新发言权
  38. $counter[] = [
  39. 'key' => 'talk_status',
  40. 'value' => (string)$room['talk_status']
  41. ];
  42. // 更新直播间右上角头像
  43. $model = new LiveRoomLogLikeModel();
  44. $list = $model->getTopList($room['room_no'], $room['user_id'], 4);
  45. $avatar = array_column($list, 'avatar');
  46. $counter[] = [
  47. 'key' => 'top_avatar',
  48. 'value' => !empty($avatar) ? implode(',', $avatar) : ''
  49. ];
  50. // 更新直播间点赞量
  51. $counter[] = [
  52. 'key' => 'like',
  53. 'value' => (string)($room['log']['like'] ?? 0)
  54. ];
  55. // 更新直播间ccu数
  56. $im->get_online_member_num($room['room_no']);
  57. $online_member = $im->getData()['OnlineMemberNum'] ?? 0;
  58. $online_member = $online_member > 0 ? $online_member - 1 : 0;
  59. LiveRoomLogModel::query()->where('room_no', $room['room_no'])->where('session', $room['session'])->update(['ccu' => $online_member]);
  60. $counter[] = [
  61. 'key' => 'online_member_num',
  62. 'value' => (string)$online_member
  63. ];
  64. $now = md5(json_encode($counter));
  65. $old = RedisUtil::getInstance(RedisKeyEnum::ROOM_SEND_DATA, $params['room_no'])->get();
  66. if ($now != $old){
  67. // 统一更新群属性
  68. $im->modify_group_attr($room['room_no'], $counter);
  69. }
  70. RedisUtil::getInstance(RedisKeyEnum::ROOM_SEND_DATA, $params['room_no'])->setex($now,10);
  71. return $this->success('执行成功', $im->getData());
  72. }
  73. }