LiveRoomAdminModel.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Master\Framework\Library\Tencent\TencentIm;
  5. use App\Model\Model;
  6. class LiveRoomAdminModel extends Model
  7. {
  8. /**
  9. * The table associated with the model.
  10. *
  11. * @var ?string
  12. */
  13. protected ?string $table = 'live_room_admin';
  14. protected ?string $dateFormat = 'U';
  15. public bool $timestamps = false;
  16. protected int $is_status_search = 0;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
  17. protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
  18. /**
  19. * 默认查询字段
  20. *
  21. * @var array|string[]
  22. */
  23. public array $select = [
  24. '*'
  25. ];
  26. /**
  27. * 添加场控
  28. * @param int $user_id 主播ID
  29. * @param string $room_no 房间号
  30. * @param int $admin_id 场控ID
  31. * @return bool
  32. */
  33. public function add(int $user_id, string $room_no, int $admin_id)
  34. {
  35. $model = new LiveRoomModel();
  36. if (!$room = $model->getDetail(params: ['room_no' => $room_no])) {
  37. return $this->error('直播间已关闭');
  38. }
  39. if ($room['user_id'] != $user_id) {
  40. return $this->error('未拥有此权限');
  41. }
  42. if (!$admin = $this->getDetail(params: ['admin_id' => $admin_id, 'room_no' => $room_no])) {
  43. // 没有直播间 创建一个
  44. $insert = [
  45. 'room_id' => $room['id'],
  46. 'room_no' => $room_no,
  47. 'user_id' => $user_id,
  48. 'admin_id' => $admin_id,
  49. 'status' => 1,
  50. 'create_time' => time(),
  51. ];
  52. if (!$this->insertGetId($insert)) {
  53. return $this->error('设置失败');
  54. }
  55. } else {
  56. // 更新状态
  57. if (!$this->where('id', $admin['id'])->update(['status' => 1,'create_time' => time()])) {
  58. return $this->error('设置失败');
  59. }
  60. }
  61. // $im = new TencentIm();
  62. // if (!$im->modify_admin($room_no,im_prefix($user_id))){
  63. // return $this->error($im->getMessage() ?? '设置失败');
  64. // }
  65. return $this->success('成功');
  66. }
  67. /**
  68. * 删除场控
  69. * @param int $user_id 主播ID
  70. * @param string $room_no 房间号
  71. * @param int $admin_id 场控ID
  72. * @return bool
  73. */
  74. public function del(int $user_id, string $room_no, int $admin_id)
  75. {
  76. $model = new LiveRoomModel();
  77. if (!$room = $model->getDetail(params: ['room_no' => $room_no])) {
  78. return $this->error('直播间已关闭');
  79. }
  80. if ($room['user_id'] != $user_id) {
  81. return $this->error('未拥有此权限');
  82. }
  83. if ($admin = $this->getDetail(params: ['admin_id' => $admin_id, 'room_no' => $room_no])) {
  84. // 更新状态
  85. if (!$this->where('id', $admin['id'])->update(['status' => 0, 'create_time' => time()])) {
  86. return $this->error('删除失败');
  87. }
  88. }
  89. // $im = new TencentIm();
  90. // if (!$im->modify_admin($room_no,im_prefix($user_id),2)){
  91. // return $this->error($im->getMessage() ?? '设置失败');
  92. // }
  93. return $this->success('成功');
  94. }
  95. /**
  96. * 获取场控信息
  97. * @param $admin_id
  98. * @param $room_no
  99. * @return array
  100. */
  101. public function getAdmin($admin_id, $room_no)
  102. {
  103. $this->setIsStatusSearchValue(1);
  104. return $this->getDetail(params: [
  105. 'admin_id' => $admin_id,
  106. 'room_no' => $room_no,
  107. ]);
  108. }
  109. public function searchRoomNoAttribute($query, $value, array $params): mixed
  110. {
  111. if (empty($value)) {
  112. return $query;
  113. }
  114. return $query->where('room_no', $value);
  115. }
  116. public function searchAdminIdAttribute($query, $value, array $params): mixed
  117. {
  118. if (empty($value)) {
  119. return $query;
  120. }
  121. return $query->where('admin_id', $value);
  122. }
  123. public function user()
  124. {
  125. return $this->hasOne(UserModel::class, 'id', 'admin_id');
  126. }
  127. }