123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Master\Enum\RedisKeyEnum;
- use App\Master\Framework\Library\Tencent\TencentIm;
- use App\Model\Model;
- use App\Utils\RedisUtil;
- use Hyperf\DbConnection\Db;
- class LiveRoomModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'live_room';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 1;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- public function searchRoomNoAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('room_no', $value);
- }
- public function searchUserIdAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('user_id', $value);
- }
- /**
- * 创建直播间
- * @param array $user auth_user
- * @param string $room_name 直播间名称
- * @return bool
- */
- public function createRoom(array $user,string $room_name = '', string $image = ''): bool
- {
- $this->setIsStatusSearchValue(0);
- if (!$room = $this->getDetail(params: ['user_id' => $user['id']])) {
- // 没有直播间 创建一个
- $room_no = (rand(10, 99) . (sprintf("%06d", $user['id'])));
- $room_session = $room_no . "_" . date('YmdHis');
- $insert = [
- 'user_id' => $user['id'],
- 'name' => $room_name,
- 'logo' => $user['avatar'],
- 'image' => !empty($image) ? $image : $user['avatar'],
- 'room_no' => $room_no,
- 'session' => $room_session,
- 'status' => 1,
- 'create_time' => time(),
- ];
- if (!$room_id = $this->query()->insertGetId($insert)) {
- return $this->error('创建失败');
- }
- } else {
- // 有直播间 更新直播状态
- if ($room['status'] == 2) {
- return $this->error('直播被封禁!');
- }
- // 已经开播了
- if ($room['status'] == 1) {
- $update = [
- 'name' => $room_name,
- 'logo' => $user['avatar'],
- 'image' => !empty($image) ? $image : $user['avatar'],
- 'update_time' => time()
- ];
- if (!$this->query()->where('user_id', $user['id'])->update($update)) {
- return $this->error('开播失败');
- }
- return $this->success('开播成功', [
- 'room_no' => $room['room_no'],
- 'room_name' => $room['name'],
- 'talk_status' => $room['talk_status'],
- 'status' => 1,
- ]);
- }
- $room_no = $room['room_no'];
- $room_session = $room_no . "_" . date('YmdHis');
- $room_id = $room['id'];
- $room_name = !empty($room_name) ? $room_name : $room['name'];
- $update = [
- 'name' => $room_name,
- 'logo' => $user['avatar'],
- 'image' => !empty($image) ? $image : $user['avatar'],
- 'session' => $room_session,
- 'status' => 1,
- 'update_time' => time()
- ];
- if (!$this->query()->where('user_id', $user['id'])->update($update)) {
- return $this->error('开播失败');
- }
- }
- $log = [
- 'user_id' => $user['id'],
- 'room_id' => $room_id,
- 'room_no' => $room_no,
- 'session' => $room_session,
- 'open_time' => time(),
- ];
- if (!(new LiveRoomLogModel())->query()->insertGetId($log)) {
- return $this->error('开播失败');
- }
- return $this->success('开播成功', [
- 'room_no' => $room_no,
- 'room_name' => $room_name,
- 'talk_status' => $room['talk_status'] ?? 1,
- ]);
- }
- /**
- * 关闭房间
- * @param $room_id
- * @return bool
- */
- public function closeRoom($room_id)
- {
- if (!$room = $this->getDetail(params: ['id' => $room_id])) {
- return $this->success('关闭成功');
- }
- if (!$this->where('id', $room_id)->update(['status' => 0])) {
- return $this->error('关闭失败');
- }
- $time = time();
- $logUp = [
- 'duration' => Db::raw("{$time} - open_time"),
- 'close_time' => $time,
- ];
- if (!(new LiveRoomLogModel())->where('session',$room['session'])->update($logUp)) {
- return $this->error('开播失败');
- }
- // 解散直播间移除观众
- RedisUtil::getInstance(RedisKeyEnum::ROOM_USER_LIST,$room['room_no'])->del();
- return $this->success('关播成功');
- }
- /**
- * 禁言
- * @param int $user_id 主播ID
- * @param string $room_no 房间号
- * @param int $admin_id 观众ID
- * @return bool
- */
- public function shut_up(int $user_id, string $room_no, int $audience_id, int $time = 86400)
- {
- if (!$room = $this->getDetail(params: ['room_no' => $room_no])) {
- return $this->error('直播间已关闭');
- }
- if ($room['user_id'] != $user_id) {
- $model = new LiveRoomAdminModel();
- if (!$model->getAdmin($user_id, $room_no)) {
- return $this->error('未拥有此权限');
- }
- }
- RedisUtil::getInstance(RedisKeyEnum::ROOM_SHUT_UP,$room_no,im_prefix($audience_id))->setex(1,$time+1);
- // 腾讯直播创建房间 && 创建群组
- $im = new TencentIm();
- if (!$im->forbid_send_msg($room_no, im_prefix($audience_id), $time)) {
- return $this->error($im->getMessage() ?? '操作失败');
- }
- return $this->success('成功');
- }
- /**
- * 封禁 黑名单
- * @param int $user_id 主播ID
- * @param string $room_no 房间号
- * @param int $admin_id 观众ID
- * @return bool
- */
- public function black_add(int $user_id, string $room_no, int $audience_id, int $time = 86400)
- {
- if (!$room = $this->getDetail(params: ['room_no' => $room_no])) {
- return $this->error('直播间已关闭');
- }
- if ($room['user_id'] != $user_id) {
- $model = new LiveRoomAdminModel();
- if (!$model->getAdmin($user_id, $room_no)) {
- return $this->error('未拥有此权限');
- }
- }
- Db::beginTransaction();
- if (!$info = LiveRoomBlackModel::query()->where(['user_id'=>$audience_id,'room_id'=>$room['id'],'room_no'=>$room_no])->first()){
- $log = [
- 'user_id' => $audience_id,
- 'room_id' => $room['id'],
- 'room_no' => $room_no,
- 'status' => 1,
- 'create_time' => time(),
- 'end_time' => time() + $time,
- ];
- if (!LiveRoomBlackModel::query()->insertGetId($log)) {
- Db::rollBack();
- return $this->error('拉黑失败');
- }
- }else{
- if ($info->status == 1 && $info->end_time > time()){
- Db::rollBack();
- return $this->error('已拉黑过了');
- }
- if (!LiveRoomBlackModel::query()->where('id',$info->id)->update(['status'=>1,'end_time'=>time() + $time,'create_time'=>time()])){
- Db::rollBack();
- return $this->error('拉黑失败了');
- }
- }
- RedisUtil::getInstance(RedisKeyEnum::ROOM_BLACK,$room_no,im_prefix($audience_id))->setex(1,$time);
- // 腾讯直播创建房间 && 创建群组
- $im = new TencentIm();
- // if (!$im->ban_group_member($room_no, im_prefix($audience_id), $time)) {
- // return $this->error($im->getMessage() ?? '操作失败');
- // }
- if (!$im->delete_group_member($room_no, im_prefix($audience_id))) {
- Db::rollBack();
- return $this->error($im->getMessage() ?? '操作失败');
- }
- Db::commit();
- return $this->success('成功');
- }
- /**
- * 移除 封禁 黑名单
- * @param int $user_id 主播ID
- * @param string $room_no 房间号
- * @param int $admin_id 观众ID
- * @return bool
- */
- public function black_remove(int $user_id, string $room_no, int $audience_id)
- {
- if (!$room = $this->getDetail(params: ['room_no' => $room_no])) {
- return $this->error('直播间已关闭');
- }
- if ($room['user_id'] != $user_id) {
- $model = new LiveRoomAdminModel();
- if (!$model->getAdmin($user_id, $room_no)) {
- return $this->error('未拥有此权限');
- }
- }
- if (!$info = LiveRoomBlackModel::query()->where(['user_id'=>$user_id,'room_id'=>$room['id'],'room_no'=>$room_no])->first()){
- return $this->error('您未拉黑过');
- }else{
- if ($info->status != 1){
- return $this->error('已取消拉黑');
- }
- if (!LiveRoomBlackModel::query()->where('id',$info->id)->update(['status'=>0])){
- return $this->error('操作失败');
- }
- }
- RedisUtil::getInstance(RedisKeyEnum::ROOM_BLACK,$room_no,im_prefix($audience_id))->del();
- // 腾讯直播创建房间 && 创建群组
- // $im = new TencentIm();
- // if (!$im->unban_group_member($room_no, im_prefix($audience_id))) {
- // return $this->error($im->getMessage() ?? '操作失败');
- // }
- return $this->success('成功');
- }
- /**
- * 采集器
- * @param $value
- * @param $data
- * @return mixed
- */
- public function dataLogoAttribute($value,$data)
- {
- return cdn_url($value);
- }
- public function dataImageAttribute($value,$data)
- {
- return cdn_url($value);
- }
- // 开播日志
- public function log()
- {
- return $this->hasOne(LiveRoomLogModel::class, 'session', 'session');
- }
- // 主播信息
- public function user()
- {
- return $this->hasOne(UserModel::class, 'id', 'user_id');
- }
- }
|