Files
mier-php/application/api/model/RoomHost.php

317 lines
13 KiB
PHP
Raw Permalink Normal View History

2025-08-11 10:22:05 +08:00
<?php
namespace app\api\model;
use think\Db;
use think\Model;
class RoomHost extends Model
{
//设置主持
public function set_room_host($uid, $rid, $user_id){
$user_info = db::name('user')->where('uid', $uid)->find();
if(!$user_info){
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
}
$room_info = db::name('room')->where('rid', $rid)->find();
if(!$room_info){
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
}
$host_info = db::name('user')->where('uid', $user_id)->find();
if(!$host_info){
return ['code' => 201, 'msg' => '指定用户信息不存在', 'data' => null];
}
if($uid != $room_info['room_owner_uid']){
return ['code' => 201, 'msg' => '无权限设置', 'data' => null];
}
if($user_id == $room_info['room_owner_uid']){
return ['code' => 201, 'msg' => '无法设置房主为主持', 'data' => null];
}
//该用户是否已在别的厅为主持
$map = [];
$map[] = ['uid', '=', $user_id];
$map[] = ['is_delete', '=', 1];
$room_host_info = db::name('room_host')->where($map)->find();
if($room_host_info){
if($room_host_info['rid'] == $rid){
return ['code' => 201, 'msg' => '该用户已经是主持', 'data' => null];
}else{
return ['code' => 201, 'msg' => '该用户已经是别的房间主持', 'data' => null];
}
}
$map = [];
$map[] = ['uid', '=', $user_id];
$map[] = ['rid', '=', $rid];
$map[] = ['is_delete', '=', 2];
$info = db::name('room_host')->where($map)->find();
if($info){
$reslut = db::name('room_host')->where('id', $info['id'])->update(['is_delete' => 1, 'ratio' => 0, 'add_time' => time()]);
}else{
$insert = [];
$insert['uid'] = $user_id;
$insert['rid'] = $rid;
$insert['add_time'] = time();
$reslut = db::name('room_host')->insert($insert);
}
if($reslut){
return ['code' => 200, 'msg' => '设置成功', 'data' => null];
}else{
return ['code' => 201, 'msg' => '设置失败', 'data' => null];
}
}
//取消主持
public function unset_room_host($uid, $rid, $user_id){
$user_info = db::name('user')->where('uid', $uid)->find();
if(!$user_info){
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
}
$room_info = db::name('room')->where('rid', $rid)->find();
if(!$room_info){
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
}
$host_info = db::name('user')->where('uid', $user_id)->find();
if(!$host_info){
return ['code' => 201, 'msg' => '指定用户信息不存在', 'data' => null];
}
if($uid != $room_info['room_owner_uid']){
return ['code' => 201, 'msg' => '无权限取消', 'data' => null];
}
//该用户是否已在别的厅为主持
$map = [];
$map[] = ['uid', '=', $user_id];
$map[] = ['rid', '=', $rid];
$map[] = ['is_delete', '=', 1];
$room_host_info = db::name('room_host')->where($map)->find();
if(!$room_host_info){
return ['code' => 201, 'msg' => '该用户当前不是本房主持', 'data' => null];
}
$update = [];
$update['is_delete'] = 2;
$update['update_time'] = time();
$reslut = db::name('room_host')->where('id', $room_host_info['id'])->update($update);
if($reslut){
return ['code' => 200, 'msg' => '成功', 'data' => null];
}else{
return ['code' => 201, 'msg' => '失败', 'data' => null];
}
}
//房间主持列表
public function get_room_host_list($uid, $rid){
$room_info = db::name('room')->find($rid);
if(!$room_info){
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
}
//当前房间主持列表
$map = [];
$map[] = ['a.rid', '=', $rid];
$map[] = ['a.is_delete', '=', 1];
$room_host_list = db::name('room_host')->alias('a')->join('yy_user b', 'a.uid = b.uid')->field('a.uid,b.nick_name,b.base64_nick_name,b.head_pic')->where($map)->order('a.add_time asc')->select();
foreach ($room_host_list as $k => &$v) {
$v['nick_name'] = mb_convert_encoding(base64_decode($v['base64_nick_name']), 'UTF-8', 'UTF-8');
$v['head_pic'] = localpath_to_netpath($v['head_pic']);
}
$map = [];
$map[] = ['rid', '=', $rid];
$map[] = ['is_delete', '=', 1];
$room_host_uid = db::name('room_host')->where($map)->column('uid');
$room_visitor = db::name('room_visitor')->where('rid', $rid)->column('uid');
//游客信息
$where = [];
$where[] = ['uid', 'in', $room_visitor];
$where[] = ['uid', 'not in', $room_host_uid];
$room_visitor_list = db::name('user')->where($where)->field('uid, nick_name, base64_nick_name, head_pic')->select();
foreach ($room_visitor_list as $a => &$b){
$b['nick_name'] = mb_convert_encoding(base64_decode($b['base64_nick_name']), 'UTF-8', 'UTF-8');
$b['head_pic'] = localpath_to_netpath($b['head_pic']);
}
$data = [];
$data['room_host_list'] = $room_host_list;
$data['room_visitor_list'] = $room_visitor_list;
return ['code' => 200, 'msg' => '获取成功', 'data' => $data];
}
//房间主持列表
public function get_room_host_info($uid, $rid){
$room_info = db::name('room')->find($rid);
if(!$room_info){
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
}
$map = [];
$map[] = ['a.rid', '=', $rid];
$map[] = ['a.is_delete', '=', 1];
$room_host_list = db::name('room_host')->alias('a')->join('yy_user b', 'a.uid = b.uid')->field('a.id, b.uid, b.nick_name, b.base64_nick_name, b.head_pic, a.ratio')->where($map)->order('a.id desc')->select();
foreach ($room_host_list as $k => &$v){
$v['nick_name'] = mb_convert_encoding(base64_decode($v['base64_nick_name']), 'UTF-8', 'UTF-8');
$v['head_pic'] = localpath_to_netpath($v['head_pic']);
$v['room_owner_ratio'] = round((1 - $v['ratio']),2) * 100;
$v['ratio'] = $v['ratio'] * 100;
//今日收益
$v['today_profit'] = db::name('user_room_profit_day')->where('rid', $rid)->where('uid', $v['uid'])->whereTime('add_time', 'today')->sum('amount');
//今日主持时长
$v['today_host_time'] = db::name('room_host_online_time_log')->where('uid', $v['uid'])->where('rid', $rid)->whereTime('add_time', 'today')->sum('online_time');
}
return ['code' => 200, 'msg' => '获取成功', 'data' => $room_host_list];
}
//设置主持比例
public function update_room_host_ratio($uid, $rid, $user_id, $ratio){
$room_info = db::name('room')->find($rid);
if(!$room_info){
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
}
if($uid != $room_info['room_owner_uid']){
return ['code' => 201, 'msg' => '无权限设置', 'data' => null];
}
if($ratio < 0){
return ['code' => 201, 'msg' => '设置主持比例不能小于0', 'data' => null];
}
if(ceil($ratio) != $ratio){
return ['code' => 201, 'msg' => '设置比例必须为整数', 'data' => null];
}
if($ratio > 100){
return ['code' => 201, 'msg' => '设置主持比例最大不能超过100%', 'data' => null];
}
//该主持是否存在
$room_host_info = db::name('room_host')->where('rid', $rid)->where('uid', $user_id)->where('is_delete', 1)->find();
if(!$room_host_info){
return ['code' => 201, 'msg' => '该主持不存在', 'data' => null];
}
//修改主持比例
$update = [];
$update['ratio'] = $ratio * 0.01;
$update['update_time'] = time();
$reslut = db::name('room_host')->where('id', $room_host_info['id'])->update($update);
if(!$reslut){
return ['code' => 201, 'msg' => '设置失败', 'data' => null];
}else{
return ['code' => 200, 'msg' => '设置成功', 'data' => null];
}
}
//用户所在房间权限列表
public function get_user_room_power_list($uid){
$user_info = db::name('user')->find($uid);
if(!$user_info){
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
}
$list = [];
//是否是房主
$room_owner_list = db::name('room')->where('room_owner_uid', $uid)->find();
if($room_owner_list){
$data = [];
$data['rid'] = $room_owner_list['rid'];
$data['room_number'] = $room_owner_list['room_number'];
$data['room_name'] = mb_convert_encoding(base64_decode($room_owner_list['base64_room_name']), 'UTF-8', 'UTF-8');
$data['room_cover'] = localpath_to_netpath($room_owner_list['room_cover']);
$data['user_type'] = 1;
$list[] = $data;
}
//是否有主持
$room_host_list = db::name('room_host')->alias('a')->join('yy_room b', 'a.rid = b.rid')->field('a.*, b.base64_room_name, b.room_cover, b.room_number')->where('a.uid', $uid)->where('a.is_delete', 1)->select();
foreach ($room_host_list as $k => $v){
$data = [];
$data['rid'] = $v['rid'];
$data['room_number'] = $v['room_number'];
$data['room_name'] = mb_convert_encoding(base64_decode($v['base64_room_name']), 'UTF-8', 'UTF-8');
$data['room_cover'] = localpath_to_netpath($v['room_cover']);
$data['user_type'] = 3;
$list[] = $data;
}
//是否有管理
$room_admin_list = db::name('room_admin')->alias('a')->join('yy_room b', 'a.rid = b.rid')->field('a.*, b.room_number, b.base64_room_name, b.room_cover')->where('a.uid', $uid)->where('a.is_delete', 1)->select();
foreach ($room_admin_list as $k => $v){
$data = [];
$data['rid'] = $v['rid'];
$data['room_number'] = $v['room_number'];
$data['room_name'] = mb_convert_encoding(base64_decode($v['base64_room_name']), 'UTF-8', 'UTF-8');
$data['room_cover'] = localpath_to_netpath($v['room_cover']);
$data['user_type'] = 2;
$list[] = $data;
}
return ['code' => 200, 'msg' => '获取成功', 'data' => $list];
}
//辞退房间职位
public function user_retire_room_power($uid, $rid, $user_type){
$user_info = db::name('user')->find($uid);
if(!$user_info){
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
}
$room_info = db::name('room')->find($rid);
if(!$room_info){
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
}
if(!in_array($user_type, [2,3])){
return ['code' => 201, 'msg' => '辞退职位信息不存在', 'data' => null];
}
if($user_type == 2){
//是否是该房间管理
$user_room_admin_info = db::name('room_admin')->where('rid', $rid)->where('uid', $uid)->where('is_delete', 1)->find();
if(!$user_room_admin_info){
return ['code' => 201, 'msg' => '你当前不是该房间管理', 'data' => null];
}
$reslut = db::name('room_admin')->where('id', $user_room_admin_info['id'])->update(['is_delete' => 2, 'update_time' => time()]);
}else if($user_type == 3){
//是否是该房间主持
$user_room_host_info = db::name('room_host')->where('rid', $rid)->where('uid', $uid)->where('is_delete', 1)->find();
if(!$user_room_host_info){
return ['code' => 201, 'msg' => '你当前不是该房间主持', 'data' => null];
}
$reslut = db::name('room_host')->where('id', $user_room_host_info['id'])->update(['is_delete' => 2, 'update_time' => time()]);
}else{
return ['code' => 201, 'msg' => '辞退职位信息不存在', 'data' => null];
}
if($reslut){
return ['code' => 200, 'msg' => '辞退成功', 'data' => null];
}else{
return ['code' => 201, 'msg' => '辞退失败', 'data' => null];
}
}
}