LiveRoomModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Master\Enum\RedisKeyEnum;
  5. use App\Master\Framework\Library\Tencent\TencentIm;
  6. use App\Model\Model;
  7. use App\Utils\RedisUtil;
  8. use Hyperf\DbConnection\Db;
  9. class LiveRoomModel extends Model
  10. {
  11. /**
  12. * The table associated with the model.
  13. *
  14. * @var ?string
  15. */
  16. protected ?string $table = 'live_room';
  17. protected ?string $dateFormat = 'U';
  18. public bool $timestamps = false;
  19. protected int $is_status_search = 1;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
  20. protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
  21. /**
  22. * 默认查询字段
  23. *
  24. * @var array|string[]
  25. */
  26. public array $select = [
  27. '*'
  28. ];
  29. public function searchRoomNoAttribute($query, $value, array $params): mixed
  30. {
  31. if (empty($value)) {
  32. return $query;
  33. }
  34. return $query->where('room_no', $value);
  35. }
  36. public function searchUserIdAttribute($query, $value, array $params): mixed
  37. {
  38. if (empty($value)) {
  39. return $query;
  40. }
  41. return $query->where('user_id', $value);
  42. }
  43. /**
  44. * 创建直播间
  45. * @param array $user auth_user
  46. * @param string $room_name 直播间名称
  47. * @return bool
  48. */
  49. public function createRoom(array $user,string $room_name = '', string $image = ''): bool
  50. {
  51. $this->setIsStatusSearchValue(0);
  52. if (!$room = $this->getDetail(params: ['user_id' => $user['id']])) {
  53. // 没有直播间 创建一个
  54. $room_no = (rand(10, 99) . (sprintf("%06d", $user['id'])));
  55. $room_session = $room_no . "_" . date('YmdHis');
  56. $insert = [
  57. 'user_id' => $user['id'],
  58. 'name' => $room_name,
  59. 'logo' => $user['avatar'],
  60. 'image' => !empty($image) ? $image : $user['avatar'],
  61. 'room_no' => $room_no,
  62. 'session' => $room_session,
  63. 'status' => 1,
  64. 'create_time' => time(),
  65. ];
  66. if (!$room_id = $this->query()->insertGetId($insert)) {
  67. return $this->error('创建失败');
  68. }
  69. } else {
  70. // 有直播间 更新直播状态
  71. if ($room['status'] == 2) {
  72. return $this->error('直播被封禁!');
  73. }
  74. // 已经开播了
  75. if ($room['status'] == 1) {
  76. $update = [
  77. 'name' => $room_name,
  78. 'logo' => $user['avatar'],
  79. 'image' => !empty($image) ? $image : $user['avatar'],
  80. 'update_time' => time()
  81. ];
  82. if (!$this->query()->where('user_id', $user['id'])->update($update)) {
  83. return $this->error('开播失败');
  84. }
  85. return $this->success('开播成功', [
  86. 'room_no' => $room['room_no'],
  87. 'room_name' => $room['name'],
  88. 'talk_status' => $room['talk_status'],
  89. 'status' => 1,
  90. ]);
  91. }
  92. $room_no = $room['room_no'];
  93. $room_session = $room_no . "_" . date('YmdHis');
  94. $room_id = $room['id'];
  95. $room_name = !empty($room_name) ? $room_name : $room['name'];
  96. $update = [
  97. 'name' => $room_name,
  98. 'logo' => $user['avatar'],
  99. 'image' => !empty($image) ? $image : $user['avatar'],
  100. 'session' => $room_session,
  101. 'status' => 1,
  102. 'update_time' => time()
  103. ];
  104. if (!$this->query()->where('user_id', $user['id'])->update($update)) {
  105. return $this->error('开播失败');
  106. }
  107. }
  108. $log = [
  109. 'user_id' => $user['id'],
  110. 'room_id' => $room_id,
  111. 'room_no' => $room_no,
  112. 'session' => $room_session,
  113. 'open_time' => time(),
  114. ];
  115. if (!(new LiveRoomLogModel())->query()->insertGetId($log)) {
  116. return $this->error('开播失败');
  117. }
  118. return $this->success('开播成功', [
  119. 'room_no' => $room_no,
  120. 'room_name' => $room_name,
  121. 'talk_status' => $room['talk_status'] ?? 1,
  122. ]);
  123. }
  124. /**
  125. * 关闭房间
  126. * @param $room_id
  127. * @return bool
  128. */
  129. public function closeRoom($room_id)
  130. {
  131. if (!$room = $this->getDetail(params: ['id' => $room_id])) {
  132. return $this->success('关闭成功');
  133. }
  134. if (!$this->where('id', $room_id)->update(['status' => 0])) {
  135. return $this->error('关闭失败');
  136. }
  137. $time = time();
  138. $logUp = [
  139. 'duration' => Db::raw("{$time} - open_time"),
  140. 'close_time' => $time,
  141. ];
  142. if (!(new LiveRoomLogModel())->where('session',$room['session'])->update($logUp)) {
  143. return $this->error('开播失败');
  144. }
  145. // 解散直播间移除观众
  146. RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST,$room['room_no'])->del();
  147. return $this->success('关播成功');
  148. }
  149. /**
  150. * 禁言
  151. * @param int $user_id 主播ID
  152. * @param string $room_no 房间号
  153. * @param int $admin_id 观众ID
  154. * @return bool
  155. */
  156. public function shut_up(int $user_id, string $room_no, int $audience_id, int $time = 86400)
  157. {
  158. if (!$room = $this->getDetail(params: ['room_no' => $room_no])) {
  159. return $this->error('直播间已关闭');
  160. }
  161. if ($room['user_id'] != $user_id) {
  162. $model = new LiveRoomAdminModel();
  163. if (!$model->getAdmin($user_id, $room_no)) {
  164. return $this->error('未拥有此权限');
  165. }
  166. }
  167. RedisUtil::getInstance(RedisKeyEnum::ROOM_SHUT_UP,$room_no,im_prefix($audience_id))->setex(1,$time+1);
  168. // 腾讯直播创建房间 && 创建群组
  169. $im = new TencentIm();
  170. if (!$im->forbid_send_msg($room_no, im_prefix($audience_id), $time)) {
  171. return $this->error($im->getMessage() ?? '操作失败');
  172. }
  173. return $this->success('成功');
  174. }
  175. /**
  176. * 封禁 黑名单
  177. * @param int $user_id 主播ID
  178. * @param string $room_no 房间号
  179. * @param int $admin_id 观众ID
  180. * @return bool
  181. */
  182. public function black_add(int $user_id, string $room_no, int $audience_id, int $time = 86400)
  183. {
  184. if (!$room = $this->getDetail(params: ['room_no' => $room_no])) {
  185. return $this->error('直播间已关闭');
  186. }
  187. if ($room['user_id'] != $user_id) {
  188. $model = new LiveRoomAdminModel();
  189. if (!$model->getAdmin($user_id, $room_no)) {
  190. return $this->error('未拥有此权限');
  191. }
  192. }
  193. Db::beginTransaction();
  194. if (!$info = LiveRoomBlackModel::query()->where(['user_id'=>$audience_id,'room_id'=>$room['id'],'room_no'=>$room_no])->first()){
  195. $log = [
  196. 'user_id' => $audience_id,
  197. 'room_id' => $room['id'],
  198. 'room_no' => $room_no,
  199. 'status' => 1,
  200. 'create_time' => time(),
  201. 'end_time' => time() + $time,
  202. ];
  203. if (!LiveRoomBlackModel::query()->insertGetId($log)) {
  204. Db::rollBack();
  205. return $this->error('拉黑失败');
  206. }
  207. }else{
  208. if ($info->status == 1 && $info->end_time > time()){
  209. Db::rollBack();
  210. return $this->error('已拉黑过了');
  211. }
  212. if (!LiveRoomBlackModel::query()->where('id',$info->id)->update(['status'=>1,'end_time'=>time() + $time,'create_time'=>time()])){
  213. Db::rollBack();
  214. return $this->error('拉黑失败了');
  215. }
  216. }
  217. RedisUtil::getInstance(RedisKeyEnum::ROOM_BLACK,$room_no,im_prefix($audience_id))->setex(1,$time);
  218. // 腾讯直播创建房间 && 创建群组
  219. $im = new TencentIm();
  220. // if (!$im->ban_group_member($room_no, im_prefix($audience_id), $time)) {
  221. // return $this->error($im->getMessage() ?? '操作失败');
  222. // }
  223. if (!$im->delete_group_member($room_no, im_prefix($audience_id))) {
  224. Db::rollBack();
  225. return $this->error($im->getMessage() ?? '操作失败');
  226. }
  227. Db::commit();
  228. return $this->success('成功');
  229. }
  230. /**
  231. * 移除 封禁 黑名单
  232. * @param int $user_id 主播ID
  233. * @param string $room_no 房间号
  234. * @param int $admin_id 观众ID
  235. * @return bool
  236. */
  237. public function black_remove(int $user_id, string $room_no, int $audience_id)
  238. {
  239. if (!$room = $this->getDetail(params: ['room_no' => $room_no])) {
  240. return $this->error('直播间已关闭');
  241. }
  242. if ($room['user_id'] != $user_id) {
  243. $model = new LiveRoomAdminModel();
  244. if (!$model->getAdmin($user_id, $room_no)) {
  245. return $this->error('未拥有此权限');
  246. }
  247. }
  248. if (!$info = LiveRoomBlackModel::query()->where(['user_id'=>$user_id,'room_id'=>$room['id'],'room_no'=>$room_no])->first()){
  249. return $this->error('您未拉黑过');
  250. }else{
  251. if ($info->status != 1){
  252. return $this->error('已取消拉黑');
  253. }
  254. if (!LiveRoomBlackModel::query()->where('id',$info->id)->update(['status'=>0])){
  255. return $this->error('操作失败');
  256. }
  257. }
  258. RedisUtil::getInstance(RedisKeyEnum::ROOM_BLACK,$room_no,im_prefix($audience_id))->del();
  259. // 腾讯直播创建房间 && 创建群组
  260. // $im = new TencentIm();
  261. // if (!$im->unban_group_member($room_no, im_prefix($audience_id))) {
  262. // return $this->error($im->getMessage() ?? '操作失败');
  263. // }
  264. return $this->success('成功');
  265. }
  266. /**
  267. * 采集器
  268. * @param $value
  269. * @param $data
  270. * @return mixed
  271. */
  272. public function dataLogoAttribute($value,$data)
  273. {
  274. return cdn_url($value);
  275. }
  276. public function dataImageAttribute($value,$data)
  277. {
  278. return cdn_url($value);
  279. }
  280. // 开播日志
  281. public function log()
  282. {
  283. return $this->hasOne(LiveRoomLogModel::class, 'session', 'session');
  284. }
  285. // 主播信息
  286. public function user()
  287. {
  288. return $this->hasOne(UserModel::class, 'id', 'user_id');
  289. }
  290. }