roomTypeArr = [1=>"party",2=>"live"]; //redis $redis = new Redis(); $redisconfig = config("redis"); $redis->connect($redisconfig["host"], $redisconfig["port"]); if ($redisconfig['redis_pwd']) { $redis->auth($redisconfig['redis_pwd']); } if($redisconfig['redis_selectdb'] > 0){ $redis->select($redisconfig['redis_selectdb']); } $this->redis = $redis; parent::__construct($request); } /** * 创建/进入派对 */ public function createParty() { if(!$this->apiLimit()){ $this->error('访问频繁,请稍后重试'); } $party_logo = input('party_logo'); $party_name = input('party_name'); $seatnum = input('seatnum'); $is_public = input('is_public',1); //是否公开:1=是,0=否 $room_type = input('room_type',2); // 房间类型:1=工会房,2=K歌房 $partyModel = new \app\common\model\Party(); $user_id = $this->auth->id; // 开直播时,判断当前用户是否为主播 $anchorInfo = []; if($room_type == 2) { /*$anchorInfo = \app\common\model\UserAnchor::where(["user_id"=>$user_id])->find(); $anchorInfo || $this->error("您还未申请主播!"); $anchorInfo->status == 0 && $this->error("您的主播申请信息还未受理,请耐心等待!"); $anchorInfo->status == 2 && $this->error("您的主播申请未通过,请重新提交!");*/ } // 1。实名认证 $userAuthInfo = \app\common\model\UserAuth::where(["user_id"=>$user_id])->find(); if($userAuthInfo) { if($userAuthInfo->status == 0) { $this->error("您的实名认证还在这审核中...,请耐心等待!"); } elseif($userAuthInfo->status == 2) { $this->error("您的实名认证审核未通过,请重新审核!"); } } else { $this->error("请先申请实名认证!"); } if($room_type == 1) { // 2。开厅申请 $authInfo = model('Guild')->where(["user_id"=>$user_id])->order('id desc')->find(); if($authInfo) { if($authInfo->status == 0) { $this->error("您的工会申请还在这审核中...,请耐心等待!"); } elseif($authInfo->status == -1) { $this->error("您的工会申请已解散,请重新申请!"); } } else { $this->error("请先申请工会!"); } } // 判断派对是否存在 $where = []; $where["user_id"] = $user_id; $where["room_type"] = $room_type; $sqlPartyInfo = $partyModel->where($where)->find(); if(!$sqlPartyInfo) { // sql中不存在派对信息 $partylimit = config("site.roomLimit"); $partycount = $partyModel->where(['is_close'=>0])->count("id"); if($partycount >= $partylimit && $partylimit != -1) { $this->error("房间数量已达上线,请联系客服!"); } /*$party_name = $this->auth->u_id."的房间"; // 派对名称 $party_logo = "/assets/api/party_logo.png"; // 派对logo*/ $party_type = 8; // 派对类型(情感互动,心动点单 等) $party_notice = "请编辑房间公告"; // 派对公告 $party_notice_detail = "请编辑房间公告内容!"; // 派对公告详情 $party_ids = $partyModel->column("party_id"); // 创建派对ID (临时ID四位,派对数不超过8999) $party_id = $this->auth->getUinqueId(4,$party_ids); if($party_id > 9999) { $this->error("房间超限,请联系客服"); } if ($room_type == 1) {//家族房间获取家族信息 $guildWhere['user_id'] = $user_id; $guildWhere['status'] = 1; $guild = model('Guild')->where($guildWhere)->order('id desc')->find(); if (!empty($guild)) { $party_name = $guild['name'].'的房间'; $party_logo = $guild['image']; $party_notice = $guild['desc']; $party_notice_detail = $guild['notice']; } $party_type = 7; } //[环信]创建聊天室 $easemob = new Easemob(); $easemob_room_id = $easemob->room_createRoom($party_name,$party_name,$this->auth->id); if(empty($easemob_room_id)){ $this->error('创建房间失败'); } //[环信]初始化房间 $matedata = [ //麦位数量 'seatnum'=> $seatnum, //已点歌曲列表 'waitsing_list' => json_encode([]), //房名字、logo 'party_name' => $party_name, 'party_logo' => $party_logo, 'is_public' => $is_public, 'room_type' => $room_type, ]; $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$matedata); //[环信]初始化N个麦位 $seatdata = []; for($i=1;$i<=$seatnum;$i++){ $seat = [ 'charm' => '', //红心,魅力值 'isMaster' => false, // 是否是房主 'headUrl' => '', // 头像 'userNo' => '', // 座位上用户no 'rtcUid' => '', // 座位上用户id,与rtc的userId一致 'name' => '', // 座位上用户昵称 'seatIndex' => $i, // 座位编号 'chorusSongCode' => '', // 是否合唱 'isAudioMuted' => 1, // 是否静音 'isVideoMuted' => 0, // 是否开启视频 ]; $seatdata['seat'.$i] = json_encode($seat); } $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$seatdata); //写入派对 Db::startTrans(); $data = []; $data["user_id"] = $this->auth->id; $data["room_type"] = $room_type; $data["party_id"] = $party_id; $data["party_hot"] = 0; $data["party_name"] = $party_name; $data["party_logo"] = $party_logo; $data["party_type"] = $party_type; $data["party_notice"] = $party_notice; $data["party_notice_detail"] = $party_notice_detail; $data["is_online"] = 1; $data["status"] = 1;//直接正常状态 $data["is_recommend"] = 0; $data["createtime"] = time(); $data["easemob_room_id"] = $easemob_room_id; $id = $partyModel->insertGetId($data); if(!$id) { Db::rollback(); $this->error("房间创建失败,请稍后重试!"); } $data["id"] = $id; if($room_type == 2) { \app\common\model\User::update(["is_live"=>1],["id"=>$user_id]); } $partyInfo = $partyModel->where(["id"=>$id])->find(); $partyInfo["is_new"] = 1; // 冠名 $userInfo = []; if($partyInfo["naming"]) { $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$partyInfo["naming"]])->find(); } // 头像 if($partyInfo["user_id"] > 0 && $room_type == 2) { $partyInfo["avatar"] = \app\common\model\User::where(["id"=>$partyInfo["user_id"]])->value("avatar"); } // 派对类型 $partyTypeName = "普通房"; if($partyInfo["party_type"]) { $partyTypeName = \app\common\model\PartyType::where(["id"=>$partyInfo["party_type"]])->value("name"); } $partyInfo["naming"] = $userInfo; $partyInfo["type_name"] = $partyTypeName; // 加入缓存排序,rediskey:派对热度 $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]); // 加入缓存,rediskey:派对详情 $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($partyInfo)); if($room_type != 2) { // 创建公会 /*\app\common\model\Guild::createGuild($partyInfo["id"], $partyInfo["party_name"], $user_id); \app\common\model\User::update(["is_guild" => 3,'guild_id'], ["id" => $user_id]);*/ //更新家族 party_id $guildWhere['user_id'] = $user_id; $guildWhere['status'] = 1; $guild = model('Guild')->where($guildWhere)->order('id desc')->find(); if (!empty($guild)) { $rs_guild = model('Guild')->where($guildWhere)->update(['party_id'=>$partyInfo['id']]); if($rs_guild === false){ Db::rollback(); $this->error('创建房间失败'); } } } Db::commit(); } else { if($sqlPartyInfo["is_close"] == 1) $this->error("该房间已被关闭!"); // 如果是房主并且房间处于离线状态下激活 if ($sqlPartyInfo['user_id'] != $user_id) { $this->error("当前直播已结束!"); } $partyInfo = $sqlPartyInfo; $partyInfo["is_new"] = 0; } $this->success("获取成功!",$partyInfo); } /** * 删除派对(群解散) */ public function closeParty() { $party_id = input('party_id'); // 派对ID $room_type = input('room_type',1); // 房间类型:1=派对,2=直播 if (!$party_id || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters')); $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find(); if(!$partyInfo) $this->error(__('派对不存在!')); $res = $partyInfo->delete(); if($res !== false) { // redis 删除 //rediskey:派对热度 $this->redis->zRem($this->roomTypeArr[$room_type]."Rank",$party_id); //rediskey:派对详情 $this->redis->del($this->roomTypeArr[$room_type]."_".$party_id); $this->success("删除成功!"); } else { $this->error("删除失败!"); } } /** * 获取派对列表排序 */ public function getPartyList() { $order = input('orderby','new'); $this->reset_all_party(); // $orderby = 'party.party_hot desc, party.id desc'; $where = []; if($order == 'recommend'){ $where['party.is_recommend'] = 1; } if($order == 'follow'){ $follow_party_ids = Db::name('party_follow')->where('user_id',$this->auth->id)->column('party_id'); $where['party.id'] = ['IN',$follow_party_ids]; } $where['party.status'] = 1; //正常 $where['party.is_online'] = 1; //在线 $where['party.is_close'] = 0; //未关闭 $field = 'party.id,party.room_type,party.party_id,party.party_name,party.party_hot,party.party_logo,party.party_pass,party.party_type,party.easemob_room_id,type.name as type_name'; $list = Db::name('party')->alias('party') ->join('party_type type','party.party_type = type.id','LEFT') ->field($field)->where($where)->order($orderby)->select(); $list = list_domain_image($list,['party_logo']); $resdata = []; if (!empty($list)) { $roomService = new RoomService(); foreach($list as $k => $v) { $mod = isset($v["party_type"]) ? intval($v["party_type"])%5 : 1; //房间用户列表 $userDataResult = $roomService->getPartyUserList(['party_id'=>$v['id']]); $userDataRes = isset($userDataResult['data']) ? $userDataResult['data'] : []; $userData = isset($userDataRes['member_list']) ? $userDataRes['member_list'] : []; $userDatas = []; if (!empty($userData)) { $userDatas = $userData; $userDatas = list_domain_image($userDatas,['avatar']); } $partyUserList = [ 'user_list' => $userDatas, 'online_num' => count($userData), ]; $resdata[] = [ "id" => $v["id"], "room_type" => $v['room_type'], "party_id" => $v["party_id"], "r_id" => $v["party_id"], "party_name" => $v["party_name"], "party_pass" => $v["party_pass"] ? $v["party_pass"]:"", "party_hot" => $v['party_hot'], "party_logo" => $v["party_logo"], "party_type" => $v['type_name'], "party_type_color" => $mod == 0?5:$mod, "party_user" => $partyUserList, ]; } } $this->success("获取成功!",$resdata); } private function reset_all_party(){ //rediskey:派对热度 $redisPartyRankList = $this->redis->zRevRange("partyRank",0,-1,true); //rediskey:派对热度 $redisLiveRankList = $this->redis->zRevRange("liveRank",0,-1,true); $roomService = new RoomService(); if(!$redisPartyRankList) {//party $roomParams = ['room_type' => 1]; $roomPartyRes = $roomService->roomRedis($roomParams); $redisPartyRankList = $roomPartyRes['data']; } if(!$redisLiveRankList) {//live $roomParams = ['room_type' => 2]; $roomLiveRes = $roomService->roomRedis($roomParams); $redisLiveRankList = $roomLiveRes['data']; } } /** * 获取派对用户排序 */ public function getPartyUserRank() { $room_type = input('room_type',1); // 房间类型 $party_id = input("party_id");// 派对ID if (!$party_id) $this->error(__('Invalid parameters')); $res = []; // 获取今天 $day = date("Ymd"); // 获取本周第一天 $weekday = $this->firstOfWeek(date("Y-m-d H:i:s")); // 获取本月第一天 $monthday = date("Ym01"); $userModel = new \app\common\model\User(); // 获取条数 $num = 50; // 获取50条财富排行日记录 $getday = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$day."d",0,$num-1,true); $res['getRankListDay'] = $userModel->rankList($getday); // 获取50条财富排行日记录 $today = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true); $res['toRankListDay'] = $userModel->rankList($today); // 获取50条财富排行周记录 $getweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$weekday."w",0,$num-1,true); $res['getRankListWeek'] = $userModel->rankList($getweek); // 获取50条贡献排行周记录 $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$weekday."w",0,$num-1,true); $res['toRankListWeek'] = $userModel->rankList($toweek); // 获取50条财富排行月记录 $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$monthday."m",0,$num-1,true); $res['getRankListMonth'] = $userModel->rankList($toweek); // 获取50条贡献排行周记录 $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$monthday."m",0,$num-1,true); $res['toRankListMonth'] = $userModel->rankList($toweek); return $this->success("获取成功!",$res); } /** * 派对热度更新(已废弃) */ public function changeUserPartyhot() { $party_id = input('party_id',0,"intval"); // 派对ID $room_type = input('room_type',1); // 房间类型 $party_hot = input('party_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值) if (!$party_id || !$party_hot) { $this->error(__('Invalid parameters')); } // 更新热度 $this->redis->zIncrBy($this->roomTypeArr[$room_type]."Rank", $party_hot, $party_id); return $this->success("更新成功!"); } /** * 更新主持人和麦位前四位至首页房间排行 */ public function addUserPositionToParty() { $party_id = input('party_id',0,"intval"); // 直播间ID $user_id = input('user_id',0,"intval"); // 用户ID $upOrdown = input('upordown'); // 上下麦:1=上麦-1=下麦 $room_type = input('room_type',1); // 房间类型 $avatar = input('avatar'); // 用户头像 (upOrdown参数为-1时可以不传) $position = input('position'); // 麦位置:0=支持人,1=1号麦,2=2号麦,3=3号麦,4=4号麦 if (!$party_id || !$user_id || !in_array($position,[0,1,2,3,4,5,6,7,8]) || !in_array($upOrdown,[1,-1])) { $this->error(__('Invalid parameters')); } if($upOrdown == 1) { // 先完成所有麦上记录 \app\common\model\UserOnsiteTime::update(["status"=>2],["user_id"=>$user_id]); // 保存上麦记录 $data = []; $data["user_id"] = $user_id; $data["party_id"] = $party_id; $data["onsite_time"] = time(); \app\common\model\UserOnsiteTime::insert($data); } if($upOrdown == -1) { // 更新下麦时间 $update = []; $update["offsite_time"] = time(); $update["status"] = 2; \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]); } if(in_array($position,[5,6,7,8])) return $this->success("设置成功!"); $redisData = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id); if($redisData) { $partyInfo = json_decode($redisData,true); if($upOrdown == 1) { // 删掉已有头像,防止重复 $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:""; if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $avatar) unset($partyInfo["party_user"][$k]); $partyInfo["party_user"][$position] = $avatar; } else { if(isset($partyInfo["party_user"])) unset($partyInfo["party_user"][$position]); } // sort($partyInfo["party_user"]); $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo)); } return $this->success("设置成功!"); } /** * 获取派对在线人数列表 */ public function getOnlieList() { $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID if (!$party_id) { $this->error(__('Invalid parameters')); } $res = $this->redis->hGetAll("online_".$party_id); $user_ids = [];$userList = []; if($res) $user_ids = array_values($res); // 获取用户列表信息 if($user_ids){ $userList = Db::name('user')->alias('user')->field("user.id,user.avatar,user.nickname,user.level,user.gender,user.charm_level,user.wealth_level,charm.image as charm_image,charm.color as charm_color,wealth.image as wealth_image,wealth.color as wealth_color") ->join('user_config_charm charm','user.charm_level = charm.level','LEFT') ->join('user_config_wealth wealth','user.wealth_level = wealth.level','LEFT') ->where(["user.id"=>["in",$user_ids],'user.is_stealth'=>0]) ->autopage()->select(); $userList = list_domain_image($userList,['avatar','charm_image','wealth_image']); } $this->success("获取成功!",$userList); } /** 连续进房提示 * @param $user_id * @param $party_id * @return int */ private function getUserJoinPartyDays($user_id, $party_id) { $days = 1; $joinPartyDays = $this->redis->hGet("joinParty_days",$user_id."-".$party_id); if ($joinPartyDays){ $joinPartyDaysArr = explode('-',$joinPartyDays); $oldTime = (int)$joinPartyDaysArr[0]; $todayStartTime = strtotime(date('Y-m-d'));//今天0点 $yesterdayStartTime = strtotime(date("Y-m-d",strtotime("-1 day")));//昨天开始时间戳 $todayEndTime = $todayStartTime+24 * 60 * 60-1;//今天结束时间戳 //昨天登录过,+1 if ($yesterdayStartTime <= $oldTime && $oldTime < $todayStartTime){ $days = (int)$joinPartyDaysArr[1]; $days++; $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-'. $days); }elseif($todayStartTime <= $oldTime && $oldTime <= $todayEndTime){ //今天登录过 days $days = (int)$joinPartyDaysArr[1]; } else { $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1'); } }else{ $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1'); } return $days; } /** * 加入派对 */ public function joinParty() { $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID $room_type = input("room_type",1);// $party_pass = input("party_pass");// if (!$party_id || !in_array($room_type,[1,2])) { $this->error(__('Invalid parameters')); } $user_id = $this->auth->id; $this->redis->zAdd("party_user_".$party_id,$this->auth->u_id,$user_id); $this->redis->hSet("online_".$party_id,$user_id,$user_id); //新增的 // 判断当前用户是否存在在直播间通过切换过来的 $livingUserPartyId = $this->redis->hGet("livingUser", $user_id); if ($livingUserPartyId) { if($livingUserPartyId != $party_id){ // 扣除房间在线人数 $this->redis->HDel("online_" . $livingUserPartyId, $user_id); //首页用户头像 $this->redis->zRem("party_user_".$livingUserPartyId, $user_id); // 扣除在线用户在房间情况 $this->redis->hDel("livingUser", $user_id); } } // 记录在线用户在房间情况 $this->redis->hSet("livingUser", $user_id, $party_id); // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出 $lsetList = []; for($i=1;$i<=4;$i++) { $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i); $hgetlist = unserialize($hgetlist); if($hgetlist){ $restime = time() - intval($hgetlist["createtime"]); }else{ $restime = 0; } // 房管 if($hgetlist && $i == 1) $lsetList["manage_restime"] = ""; // 禁言 if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime; // 拉黑,管理员不能被拉黑 if($hgetlist && $i == 3 && $restime < $hgetlist["time"] && $this->auth->is_manager == 0) { $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"])); $this->error(__('您已被该房间拉黑,解除时间:'.$restime)); break; } // 踢出,管理员不能被踢出 if($hgetlist && $i == 4 && $restime < $hgetlist["time"] && $this->auth->is_manager == 0) { $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"])); $this->error(__('您已被该房间踢出,解除时间:'.$restime)); break; } } // 判断派对密码 $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id); if($partyInfo) { $partyInfo = json_decode($partyInfo,true); if($partyInfo["is_close"] == 1) $this->error("该房间已被关闭!"); //if($partyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!"); $partyManage = $this->redis->hGet("party_manage_".$party_id,$user_id."-1"); if(isset($partyInfo["party_pass"]) && $partyInfo["party_pass"] && $partyInfo["user_id"] != $user_id && empty($partyManage)) { if(($party_pass != $partyInfo["party_pass"] || strlen($party_pass) != 4) && $this->auth->is_manager == 0) { $this->error("派对密码不正确!"); } } // 公会管理员和工会长激活派对厅 $guildInfo = \app\common\model\Guild::where(['user_id' => $partyInfo['user_id'], 'status' => 1])->find(); if ($guildInfo) { // 获取公会管理员 $guildMemberInfo = \app\common\model\GuildMember::where(["user_id" => $user_id, "guild_id" => $guildInfo['id'], 'status' => 1])->find(); if ($guildMemberInfo && $guildMemberInfo['role'] == 0) { //$this->error("当前直播已结束!");//这种情况理论上不存在 } } // // 如果是房主自己进入房间,则更新用户 为在线状态 // if ($partyInfo["user_id"] == $user_id) { // if ($room_type == 2) // \app\common\model\User::update(["is_live" => 1], ["id" => $partyInfo["user_id"]]); // } } else { $this->error("派对信息获取失败!"); } // 获取用户魅力值 $users = $this->redis->zRange("hourCharm_".$party_id,0,-1,true); $u = []; if($users) { foreach($users as $k => $v) $u[] = [ "user_id"=>$k, "charm"=>$this->changeW($v) ]; } $lsetList["userCharm"] = $u; // 获取用户排行榜前三名头像 $heads = $this->redis->hGet("user_jewel_top3",$party_id); $lsetList["userJewelTop3"] = $heads?json_decode($heads,true):[]; // 获取是否被当前用户关注 $lsetList["is_follow"] = 0; $where = []; $where["user_id"] = $user_id; $where["party_id"] = $party_id; if(Db::name('party_follow')->where($where)->find()) { $lsetList["is_follow"] = 1; } if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) { // $partyInfo["type_name"] = $partyInfo["type_name"]; } else { $partyInfo["type_name"] = "普通房"; } $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]); $partyInfo['is_new'] = $partyInfo["status"] == 0 ? 1 : 0; $partyInfo['join_days'] = $this->getUserJoinPartyDays($user_id,$party_id); $lsetList["partyInfo"] = $partyInfo; return $this->success("加入成功!",$lsetList); } /** * 判断是否被禁言 */ public function isNotalk() { $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID $user_id = input('user_id',0,"intval"); // 用户ID 逻辑ID if (!$user_id || !$party_id) { $this->error(__('Invalid parameters')); } // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出 $lsetList = []; $lsetList["is_notalk"] = 0; $lsetList["notalk_time"] = 0; $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-2"); $hgetlist = unserialize($hgetlist); $restime = time() - intval($hgetlist["createtime"]); // 禁言 if($hgetlist && $restime < $hgetlist["time"]) { $lsetList["is_notalk"] = 1; $lsetList["notalk_time"] = $restime; } return $this->success("获取成功!",$lsetList); } /** * 退出派对 */ public function outParty() { $party_id = input('party_id',0,"intval"); // 直播间ID if (!$party_id) { $this->error(__('Invalid parameters')); } $user_u_id = $this->auth->u_id; $userId = $this->auth->id; $this->redis->zRem("party_user_".$party_id,$userId); // 处理online_party_id //$this->redis->connect($redisconfig["host"], $redisconfig["port"]); $this->redis->HDel("online_" . $party_id, $this->auth->id); // 扣除在线用户在房间情况 $this->redis->hDel("livingUser", $this->auth->id); // 更新房间在线状态 $partyInfo = $this->redis->get("party_" . $party_id); if ($partyInfo) { $partyInfo = json_decode($partyInfo, true); $memCount = count($this->redis->hGetAll("online_" . $party_id)); if ($memCount <= 0) { $partyInfo["is_online"] = 0; $this->redis->set("party_" . $party_id, json_encode($partyInfo)); \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]); } } $liveInfo = $this->redis->get("live_" . $party_id); if ($liveInfo) { $liveInfo = json_decode($liveInfo, true); $memCount = count($this->redis->hGetAll("online_" . $party_id)); if ($memCount <= 0) { $liveInfo["is_online"] = 0; $this->redis->set("live_" . $party_id, json_encode($liveInfo)); \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]); } } return $this->success("退出成功!",[]); } /** * 派对内搜索用户 */ public function searchUserParty() { $u_id = input('u_id',0,"intval"); // 用户u_id $party_id = input('party_id',0,"intval"); // 直播间ID // $userids = $this->redis->zRange("party_user_".$party_id,0,-1,true); // $useridArr = []; // if($userids)foreach($userids as $k => $v) $useridArr[$v] = $k; // 因为array_slip 不能交换数字啊 // $userid = isset($useridArr[$u_id])?$useridArr[$u_id]:0; $userModel = new \app\common\model\User(); $where = []; $where["u_id"] = $u_id; $userInfo = $userModel->field("id,u_id,avatar,nickname,level,gender")->where($where)->select(); return $this->success("查询成功!",$userInfo); } /** * 设置冠名 */ public function setNaming() { $party_id = input('party_id',0,"intval"); // 直播间ID $user_id = input('user_id',0,"intval"); // 用户ID $type = input('type',1); // 1:设置 0:取消设置 if (!$party_id || !$user_id || ($type != 1 && $type != 0)) { $this->error(__('Invalid parameters')); } $partyModel = new \app\common\model\Party(); $data = []; $where = []; $where["id"] = $party_id; $partyInfo = $partyModel->where(["id"=>$party_id])->find(); $getredisPartyInfo = $this->redis->get($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id); $redisPartyInfo = json_decode($getredisPartyInfo,true); if($type == 1) { $data["naming"] = $user_id; // 冠名 $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$user_id])->find($user_id); $redisPartyInfo["naming"] = $userInfo; } else { $data["naming"] = 0; $redisPartyInfo["naming"] = []; } $this->redis->set($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id,json_encode($redisPartyInfo)); $res = $partyModel->update($data,$where); if($res !== false) { $this->success("操作成功!"); } else { $this->error("网络错误,请稍后重试!"); } } /** * 派对关注 */ public function followparty() { $party_id = input('party_id',0,"intval"); // 直播间ID if (!$party_id) { $this->error(__('Invalid parameters')); } $user_id = $this->auth->id; // 添加记录 $data = []; $data["user_id"] = $user_id; $data["party_id"] = $party_id; if(Db::name('party_follow')->where($data)->find()) { $res = Db::name('party_follow')->where($data)->delete(); $this->success("已取消关注"); } $data["createtime"] = time(); $res = Db::name('party_follow')->insert($data); $this->success("已关注"); } /** * 派对收藏列表 */ public function cellectionPartyList() { $page = input('page',1); // 分页 $pageNum = input('pageNum',10); // 分页 // 分页搜索构建 $pageStart = ($page-1)*$pageNum; $partycellectionModel = new \app\common\model\PartyCellection(); $where = []; $where["a.user_id"] = $this->auth->id; $where["r.room_type"] = 1; $list = $partycellectionModel->alias("a") ->field("a.party_id,r.party_logo,r.party_hot,r.party_id as r_id,r.party_name,t.id as party_type_id,t.name as party_type") ->where($where) ->join("hx_party r","a.party_id = r.id") ->join("hx_party_type t","t.id = r.party_type","left") ->limit($pageStart,$pageNum) ->select(); if($list) { $users = []; foreach($list as $k => $v) { $users[$v["party_id"]] = $v["party_hot"]; } $partyModel = new \app\common\model\Party(); $resultInfo = $partyModel->getPatyInfoByPartyId($users,"party",0,0,1,0,20,0); $this->success("获取成功!",$resultInfo); } return $this->success("获取成功!",$list); } /** * 获取派对信息 */ public function getPartyInfo() { $party_id = input('party_id',0,"intval"); // 直播间ID if (!$party_id) { $this->error(__('Invalid parameters')); } $user_id = $this->auth->id; $partyModel = new \app\common\model\Party(); $userModel = new \app\common\model\User(); // 获取主体信息 $where = []; $where["a.id"] = $party_id; $partyInfo = $partyModel->alias("a") ->field("a.id,a.user_id,a.party_id,a.party_name,a.party_hot,a.party_logo,rt.id as party_type,rt.name as type_name,a.party_notice,a.party_notice_detail") ->join("hx_party_type rt","rt.id = a.party_type", "left") ->where($where) ->find(); if($partyInfo) { $mod = isset($partyInfo["party_type"])?intval($partyInfo["party_type"])%5:1; $partyInfo["party_type_color"] = $mod == 0?5:$mod; if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) { // $partyInfo["type_name"] = $partyInfo["type_name"]; } else { $partyInfo["type_name"] = "普通房"; } // 获取是否被当前用户关注 $partyInfo["is_follow"] = 0; $where = []; $where["user_id"] = $user_id; $where["party_id"] = $party_id; if(Db::name('party_follow')->where($where)->find()) { $partyInfo["is_follow"] = 1; } // 获取房主信息 $where = []; $where["id"] = $partyInfo["user_id"]; $userInfo = $userModel->field("avatar,nickname")->where($where)->find(); // 获取技能信息 $partyInfo["userInfo"] = $userInfo; $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]); } $this->success("获取成功!",$partyInfo); } /** * 派对设置 */ public function setParty() { $party_id = input('party_id',0,"intval"); // 直播间ID $party_name = input('party_name'); // 派对名称 $party_logo = input('party_logo'); // 派对logo/封面 $party_pass = input('party_pass'); // 派对密码 //$party_type = input('party_type'); // 使用场景 $is_screen = input('is_screen'); // 是否关闭公屏:1=是,0=否 $on_model = input('on_model'); // 上麦模式:1=自由模式,2=麦序模式 $room_type = input('room_type',1); // 房间类型:1=派对,2=直播 $background = input('background'); // 派对背景 if (!$party_id || (!$party_name && !$party_logo && !$party_pass && !$party_type && !$is_screen && !$on_model && !$background)) { $this->error(__('Invalid parameters')); } if($party_pass && strlen($party_pass) != 4) { $this->error("房间密码必须为四位!"); } $partyModel = new \app\common\model\Party(); $data = []; $party_name && $data["party_name"] = $party_name; $party_logo && $data["party_logo"] = $party_logo; $data["party_pass"] = $party_pass; //$party_type && $data["party_type"] = $party_type; $is_screen && $data["is_screen"] = $is_screen; $on_model && $data["on_model"] = $on_model; $background && $data["background"] = $background; $where = []; $where["id"] = $party_id; $res = $partyModel->update($data,$where); if($res) { // 获取派对类型 //if($party_type) $data["type_name"] = \app\common\model\PartyType::where(["id"=>$party_type])->value("name"); // 存redis 房间信息 $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id); if($partyInfo) { $partyInfo = json_decode($partyInfo,true); $partyInfo = array_replace($partyInfo,$data); $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo)); } $this->success("房间设置成功!",$data); } else { $this->error("网络错误,请稍后重试!"); } } /** * 获取派对背景 */ public function getDefaultBackground() { $room_type = input('room_type',1); // 房间类型 if (!in_array($room_type,[1,2])) { $this->error(__('Invalid parameters')); } $this->success("获取成功!",\app\common\model\PartyBackground::where(["room_type"=>$room_type])->select()); } //腾讯im设置管理员 private function setGroupAdmin($user_id,$party_id,$status) { $tenIm = new Tenim(); return $tenIm->setUpIMAdmin($user_id, $party_id,$status); } /** * 派对管理设置 */ public function partyManageSet() { $party_id = input('party_id',0,"intval"); // 直播间ID $user_id = input('user_id',0,"intval"); // 用户ID $item = input('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出 $time = input('time'); // 限制时间(单位:秒):0=永久 if (!$party_id || !$user_id || !in_array($item,[1,2,3,4])) { $this->error(__('Invalid parameters')); } if($item>1 && $time<=0) { $this->error(__('时间设置有误')); } $partyUserId = \app\common\model\Party::where('id', $party_id)->value('user_id'); if ($partyUserId == $user_id && in_array($item, [2, 3, 4])) { $this->error("您当前无权限操作!"); } // 获取用户信息 $userInfo = \app\common\model\User::field("avatar,nickname,gender,level")->where(["id"=>$user_id])->find(); if(!$userInfo) $this->error("用户信息获取失败!"); // 贵族防踢。 $data = []; $data["user_id"] = $user_id; $data["avatar"] = $userInfo->avatar; $data["nickname"] = $userInfo->nickname; $data["gender"] = $userInfo->gender; $data["level"] = $userInfo->level; $data["item"] = $item; $data["time"] = $time; $data["createtime"] = time(); $res = $this->redis->hSet("party_manage_".$party_id,$user_id."-".$item,serialize($data)); if($res !== false) { $this->success("设置成功!",$data); } else { $this->error("网络错误,请稍后重试!"); } } /** * 派对管理设置列表 */ public function partyManageSetList() { $party_id = input('party_id',0,"intval"); // 直播间ID $item = input('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出 if (!$party_id || !in_array($item,[1,2,3,4])) { $this->error(__('Invalid parameters')); } $time = time(); $hget=$this->redis->hGetAll("party_manage_".$party_id); $list=array(); foreach($hget as $key=>$val) { if(substr($key,-2) == "-".$item){ array_push($list,unserialize($val)); } } if($list) { foreach($list as $k => $v) if(bcadd($v["createtime"],$v["time"]) <= $time && $v["item"] > 1) unset($list[$k]); if($list) $list = array_values($list); } $this->success("获取成功!",$list); } /** * 派对管理设置移除 */ public function partyManageSetDel() { $id = input('user_id',0,"intval"); // userid $party_id = input('party_id',0,"intval"); // 房间ID $item = input('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出 if (!$id || !in_array($item,[1,2,3,4])) { $this->error(__('Invalid parameters')); } $res = $this->redis->hDel("party_manage_".$party_id,$id."-".$item); if($res !== false) { $this->success("移除成功!",$res); } else { $this->error("网络错误,请稍后重试!"); } } /** * 更新派对公告 */ public function savePartyNotice() { $party_id = input('party_id',0,"intval"); // 直播间ID $room_type = input('room_type',1); // 房间类型:1=派对2=直播 $party_notice = input('party_notice'); // 公告标题 $party_notice_detail = input('party_notice_detail'); // 公告内容 if (!$party_id || !$party_notice || !$party_notice_detail) { $this->error(__('Invalid parameters')); } $partyModel = new \app\common\model\Party(); $where = []; $where["id"] = $party_id; $data = []; $data["party_notice"] = $party_notice; $data["party_notice_detail"] = $party_notice_detail; $res = $partyModel->update($data,$where); if($res) { // 存redis 房间信息 $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id); if($partyInfo) { $partyInfo = json_decode($partyInfo,true); $partyInfo = array_replace($partyInfo,$data); $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo)); } $this->success("更新成功!",$res); } else { $this->error("网络错误,请稍后重试!"); } } /** * 开始排麦 */ public function addLineUp() { $party_id = input('party_id',0,"intval"); // 直播间ID $mai_id = input('mai_id',"1"); // 麦位置 if (!$party_id) { $this->error(__('Invalid parameters')); } $userInfo = $this->auth->getUserinfo(); $userid = $userInfo['id']; // 获取 房间信息 $partyUser = \app\common\model\Party::where(['id'=>$party_id])->value("user_id"); if(!$partyUser) $this->error(__('房间信息未找到!')); // $is_home = $partyUser == $userInfo->id?1:0; $data = unserialize($this->redis->hGet("party_lineup",$party_id)); $data[$userid]["user_id"] = $userInfo['id']; $data[$userid]["avatar"] = $userInfo['avatar']; $data[$userid]["mai_id"] = $mai_id; $data[$userid]["nickname"] = $userInfo['nickname']; // $data["is_home"] = $is_home; $data[$userid]["level"] = $userInfo['level']; $data[$userid]["gender"] = $userInfo['gender']; $res = false; $data && $res = $this->redis->hSet("party_lineup",$party_id,serialize($data)); $data = array_values($data); if($res !== false) { $this->success("设置成功!",$data); } else { $this->error("网络错误,请稍后重试!"); } } /** * 取消排麦 */ public function cancelLineUp() { $party_id = input('party_id',0,"intval"); // 直播间ID $user_id = input('user_id',0); // 直播间ID $is_empty = input('is_empty',0,"intval"); // 是否清空排麦列表 1=清空,0=不清空 if (!$party_id) { $this->error(__('Invalid parameters')); } $userid = $this->auth->id; $data=unserialize($this->redis->hGet("party_lineup",$party_id)); if($data && $is_empty != 1) { foreach($data as $k => $v) { if($v["user_id"] == $user_id) { unset($data[$k]); break; } } } // unset($data[$userid]); if($is_empty == 1) $data = []; $this->redis->hSet("party_lineup",$party_id,serialize($data)); $this->success("移除成功!"); } /** * 排麦列表 */ public function lineUpList() { $party_id = input('party_id',0,"intval"); // 直播间ID if (!$party_id) { $this->error(__('Invalid parameters')); } $data = $this->redis->hGet("party_lineup",$party_id); $dataArr = unserialize($data); $datas = []; is_array($dataArr) && $datas=array_values($dataArr); if($datas) { $this->success("获取成功!",$datas); } else { $this->success("获取成功!",[]); } } /** * 获取派对类型 */ public function getPatyType() { $room_type = input("room_type",1);// if (!in_array($room_type,[1,2])) $this->error(__('Invalid parameters')); $partytypeModel = new \app\common\model\PartyType(); $partytypeList = $partytypeModel->where(["room_type"=>$room_type])->select(); $this->success("获取成功!",$partytypeList); } /** * 判断派对是否设置过密码 */ public function getPatyIspass() { $party_id = input("party_id");// 派对ID $room_type = input("room_type",1);// $is_miniprogram = input("is_miniprogram",0);// if (!$party_id) $this->error(__('Invalid parameters')); $user_id = $this->auth->id; // 存redis 房间信息 $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id); if($partyInfo) { $partyInfo = json_decode($partyInfo,true); } else { $partyModel = new \app\common\model\Party(); $partyInfo = $partyModel->field("id,user_id,party_pass")->where(["id"=>$party_id])->find(); if(!$partyInfo) { $this->error(__('房间信息获取失败!')); } } $data = []; $data["status"] = 0; $data["is_pass"] = $partyInfo["party_pass"]?1:0; $partyManage = $this->redis->hGet("party_manage_".$party_id,$user_id."-1"); if($partyInfo["user_id"] == $user_id || $this->auth->is_manager == 1 || !empty($partyManage)){ $data["is_pass"] = 0;} if($is_miniprogram == 1) { // 小程序单独处理 // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出 $lsetList = []; for($i=1;$i<=4;$i++) { $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i); $hgetlist = unserialize($hgetlist); $restime = time() - intval($hgetlist["createtime"]); // 房管 if($hgetlist && $i == 1) $lsetList["manage_restime"] = ""; // 禁言 if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime; // 拉黑 if($hgetlist && $i == 3 && $restime < $hgetlist["time"]) { $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"])); $lsetList["manage_black"] = $restime; $lsetList["tips"] = '您已被该房间拉黑,解除时间:'.$restime; // $this->error(__('您已被该房间拉黑,解除时间:'.$restime)); break; } // 踢出 if($hgetlist && $i == 4 && $restime < $hgetlist["time"]) { $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"])); $lsetList["manage_takeout"] = $restime; $lsetList["tips"] = '您已被该房间踢出,解除时间:'.$restime; // $this->error(__('您已被该房间踢出,解除时间:'.$restime)); break; } } if($lsetList) { $data["status"] = 1; $data["hgetlist"] = $lsetList; } } $this->success("获取成功!",$data); } /** * 获取房间gif表情分类 */ public function getPartyGifType() { $list = \app\common\model\PartyGifType::field("id,name,image")->order("weigh", "asc")->select(); $this->success("获取成功!", $list); } /** * 获取房间gif表情列表 */ public function getPartGifList() { $this->success("获取成功!",\app\common\model\PartyGif::field("id,gif_image")->order("weight","asc")->select()); } /** * 获取房间头像gif表情列表 */ public function getPartHeadgifList() { $this->success("获取成功!",\app\common\model\PartyHeadgif::field("id,name,gif_image")->order("weight","asc")->select()); } /** * 随机获取礼物盒礼物 */ private function getBoxGift($gift_box_type) { // 查询本奖池内礼物是否还有可抽礼物 $where = []; $where["Jackpot_id"] = $gift_box_type; $where["is_use"] = 0; $boxhasgift = \app\common\model\GiftBox::where($where)->select(); $giftcount = count($boxhasgift); $giftArr = []; foreach($boxhasgift as $k => $v) $giftArr[$v["id"]] = $v; if($giftcount > 1) { // 随机抽取$num个礼物 $giftids = array_rand($giftArr,1); \app\common\model\GiftBox::update(["is_use"=>1],["id"=>$giftids]); } if($giftcount == 1) { $giftids = $boxhasgift[0]["id"]; // 更新宝箱奖池全部礼物为未使用 \app\common\model\GiftBox::update(["is_use"=>0],["Jackpot_id"=>$gift_box_type]); } $giftInfo = $giftArr[$giftids]; return $giftInfo; } /** * 全麦/单独赠送礼物 */ public function giveGiftToYou() { // 是否背包赠送: 1=是,0=否 $is_back = input("is_back",0); // 接口防并发 if($is_back){ if (!$this->apiLimit(50, 1000)) { //1000毫秒50次 $this->error('休息一下吧'); } }else{ if (!$this->apiLimit(10, 1000)) { //1000毫秒10次 $this->error('休息一下吧'); } } //送礼物权限 /*if($this->auth->power['give_gift'] == 1){ $this->error('您已被限制:赠送礼物,请联系管理员'); }*/ //接收参数 $user_ids = input("user_id");// 赠送对象 $gift_id = input("gift_id");// 礼物ID。如果是背包赠送,则是gift_back表的id,这样不合理,但是已经不好改了 $party_id = input("party_id",0);// 派对ID $room_type = input('room_type',1); // 房间类型 $number = input("number");// 赠送数量 if (!$user_ids || !in_array($is_back,[0,1]) || !$gift_id || !$number || !in_array($room_type,[1,2])) { $this->error(__('Invalid parameters')); } if(!$party_id){ $this->error('只有房间内能送礼物'); } //处理参数 $user_id_arr = explode(",",$user_ids); $userCount = count($user_id_arr); $userauthid = $this->auth->id; $money_to_jewel = config('site.money_to_jewel') ?: 10; //余额兑换钻石 // 不可以赠送给自己 //if(in_array($userauthid,$user_id_arr)) $this->error("不可以赠送给自己!"); $backGiftId = 0; //背包礼物表的 gift_id $boxgiftInfo = []; //每个人都能得到的礼物价值 $giftValue = 0; //每个人都能得到的礼物价值 $getValue = 0; //热度值 $hotValue = 0; if($is_back == 1) { // 获取背包礼物信息 $giftInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find(); if(!$giftInfo){ $this->error("背包礼物获取失败"); } $backGiftId = $giftInfo['gift_id']; // 随机获取一个礼物 $allCount = $number*$userCount; $giftbackList = Db::name('gift_back')->field('id,value,name,image,gif_image,number')->where(["gift_id"=>$backGiftId,"user_id"=>$userauthid,'is_use'=>0])->limit($allCount)->order('id asc')->select(); $giftInfo = isset($giftbackList[0]) ? $giftbackList[0] : []; $giftcount = 0; $giftList = []; if(!empty($giftbackList)) { foreach($giftbackList as $k => $v) { $giftList[$k] = $v; $giftcount += $v["number"]; if($giftcount >= $allCount) { break; } } } if($giftcount < $allCount) { $this->error("背包数量不足"); } $giftValue = $giftInfo["value"] * $number; $getValue = $giftValue; $giftCountValue = $giftInfo["value"] * $number * $userCount; } else { // 获取礼物信息 $giftInfo = Db::name('gift')->field('id,value,name,image,special')->where('id',$gift_id)->find(); if (!$giftInfo) { $this->error("请选择礼物!"); } $giftValue = $giftInfo["value"] * $number; $getValue = $giftValue; // 判断如果是礼物盒则随机开礼物盒礼物 /*if($giftInfo['box_type'] > 0) { $boxgiftInfo = $this->getBoxGift($giftInfo['box_type']); $getValue = $boxgiftInfo["price"]; }*/ // 判断当前用户余额,这里不需要锁表,钱包操作会锁 $giftCountValue = $giftInfo["value"] * $number * $userCount; $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel'); if($user_jewel < $giftCountValue){ $this->error("您的钻石余额不足!"); } } //此时 $giftValue,$getValue,$hotValue是相等的(除了盒子礼物特殊) $hotValue = $getValue; //进行抽点设置 $partyInfo = null; if(!$party_id) { $platRate = 10; $guilderRate = 30; } else { $partyInfo = \app\common\model\Party::field("id,room_type,party_name,party_hot,user_id,platRate,guilderRate")->where(["id"=>$party_id])->find(); // 获取系统配置信息 $platRate = $partyInfo->platRate; // 平台抽成百分比 $guilderRate = $partyInfo->guilderRate; // 工会长抽成百分比 } //getValue削减 // $returnData = []; Db::startTrans(); try { // 获取当天零点 $day = date("Ymd"); // 获取本周第一天 $weekday = $this->firstOfWeek(date("Y-m-d H:i:s")); // 获取本月第一天 $monthday = date("Ym01"); $allVal = 0; $i = 0; //用户大循环 if($userCount > 1){ $touserInfo_arr = Db::name('user')->where('id','IN',$user_id_arr)->column('id,nickname,u_id'); } foreach($user_id_arr as $user_id) { // 获取赠送用户信息 if($userCount > 1){ $touserInfo = $touserInfo_arr[$user_id]; }else{ $touserInfo = Db::name('user')->field('nickname')->where('id',$user_id)->find(); } if($is_back == 1) { $b=0; foreach($giftList as $k => $v) { for($a=1;$a<=$v["number"];$a++) { $b++; $num = $v["number"] - $a; if($num > 0) { $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->setDec("number"); } else { // $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]); $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->delete(); } if($b == $number) break; } } $res2 = true; } else { $res1 = true; $res2 = true; // 扣除当前用户钻石余额 $rs_wallet = model('wallet')->lockChangeAccountRemain($userauthid, $giftValue, '-', 0, "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "金币!", 3,'jewel'); if($rs_wallet['status'] == false){ Db::rollback(); $this->error($rs_wallet['msg']); } } // 添加礼物赠送记录表 $data = []; $data["user_id"] = $userauthid; $data["user_to_id"] = $user_id; $data["party_id"] = $party_id; /*if($boxgiftInfo){ $data["gift_id"] = $boxgiftInfo['gift_id']; $data["gift_give_type"] = 2; $data["gift_name"] = $boxgiftInfo["gift_name"]; $data["gift_gif_image"] = $boxgiftInfo["image"]; $data["number"] = $number; $data["price"] = $boxgiftInfo["price"]; $data["value"] = $boxgiftInfo["price"] * $number; }else{*/ $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id; $data["gift_give_type"] = $is_back ? 1 : 2; $data["gift_name"] = $giftInfo["name"]; $data["gift_gif_image"] = $giftInfo["image"]; $data["number"] = $number; $data["price"] = $giftInfo["value"]; $data["value"] = $giftValue; /*}*/ //每个礼物都要计算平台抽成和房主抽成 $data['platvalue'] = bcmul($platRate/100 ,$data["value"],2);//平台抽成 $data['guildervalue'] = bcmul($guilderRate/100 ,$data["value"],2);// 工会长抽成 $data['guildermoney'] = bcdiv($data['guildervalue'],$money_to_jewel,2); $data['getvalue'] = bcsub(bcsub($data["value"] ,$data['platvalue'],2),$data['guildervalue'],2);//减去抽成剩余价值 $data['task_status'] = 0; $data["createtime"] = time(); $res5 = Db::name('gift_user_party')->insertGetId($data); //大礼物飘屏 //$this->bigGiftNotice($this->auth->nickname,$touserInfo['nickname'],$partyInfo,$data); // 添加获赠用户余额 /*if($data['getvalue'] > 0){ $getMoney = bcdiv($data['getvalue'],$money_to_jewel,2); if($getMoney > 0){ $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$this->auth->nickname}送你{$giftInfo['name']}x{$number}",101,'money'); if($rs_wallet['status'] == false){ Db::rollback(); $this->error($rs_wallet['msg']); } } }*/ // 增加房主抽成 /*if ($partyInfo && $data['guildervalue'] > 0) { $guilderMoney = bcdiv($data['guildervalue'],$money_to_jewel,2); if($guilderMoney > 0){ $rs_wallet = model('wallet')->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$this->auth->nickname}送礼物{$giftInfo['name']}x{$number}给{$touserInfo['nickname']},房间礼物抽成",102,'money'); if($rs_wallet['status'] == false){ Db::rollback(); $this->error($rs_wallet['msg']); } } }*/ if ($res1 && $res2 && $res5) { $i++; if($party_id > 0) { // 添加redis记录做财富排行榜日榜用 $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $hotValue, $user_id); // 添加redis记录做财富排行榜周榜用 $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $hotValue, $user_id); // 添加redis记录做财富排行榜月榜用 $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $hotValue, $user_id); // 添加redis记录做贡献排行榜日榜用 $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid); // 添加redis记录做贡献排行榜周榜用 $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid); // 添加redis记录做贡献排行榜月榜用 $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid); // tcp 更新用户魅力值 $this->updateUserCharm($party_id, $user_id, $hotValue); // 如果是主播,则添加魅力值记录做榜单统计 /*if($room_type == 2) { $data = []; $data["user_id"] = $user_id; $data["party_id"] = $party_id; $data["charm"] = $hotValue; $data["createtime"] = time(); \app\common\model\UserCharmRank::insert($data); }*/ } //用户经验升级 /*$getempirical = config("site.getempirical"); $getempirical = $getempirical * $hotValue; // 获取用户贵族信息 $noble = \app\common\model\User::getUserNoble($this->auth->id); if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) { $getempirical = $getempirical + $getempirical * ($noble["explain"]/100); } // 增加用户经验值 $res = \app\common\model\User::addEmpirical($this->auth->id,$getempirical); if ($res){ $this->auth->level = $res->level; } //增加被送礼物用户的魅力等级 $res_charm = \app\common\model\User::add_charm_level($user_id,$giftValue); // +exp \app\common\model\TaskLog::tofinish($this->auth->id,"OBHqCX4g",$number); */ // +message //\app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number." 价值 ".$giftValue ." 钻石"); $allVal = $allVal + $hotValue; } } //爆币 $res_baobi = $this->baobi($this->auth->id,$data["gift_id"],$giftCountValue); if($res_baobi === false){ Db::rollback(); $this->error('爆币失败'); }elseif($res_baobi === true){ $returnData["baobi"] = 0; }else{ //爆币了 $returnData["baobi"] = $res_baobi; } // 获取用户魅力值 $users = $this->redis->zRange("hourCharm_".$party_id,0,-1,true); $u = []; if($users) { foreach($users as $k => $v) $u[] = [ "user_id"=>$k, "charm"=>$this->changeW($v) ]; } $userCharm = $u; // tcp 更新房间热度 $partyHot = $this->updatePartyHot($partyInfo, $allVal, $room_type); // 如果是派对,则添加派对热度值记录做榜单统计 if($room_type == 1) { $data = []; $data["party_id"] = $party_id; $data["hot"] = $allVal; $data["createtime"] = time(); \app\common\model\PartyHot::insert($data); } //增加送礼用户的财富等级 // $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$allVal); // tcp 获取房间用户周前三名 $partyUserTop = $this->getPartyUserTop($party_id, $room_type); if($i == $userCount) { $returnData["userCharm"] = $userCharm; $returnData["partyHot"] = $this->changeW($partyHot); $returnData["partyUserTop"] = $partyUserTop; if($is_back != 1) { /*if($giftInfo['box_type'] > 0) { // 不是背包,宝箱中赠送 $returnData["box_type"] = $giftInfo['box_type']; $returnData["box_image"] = $giftInfo['image']; $returnData["image"] = $boxgiftInfo["image"]; $returnData["name"] = $boxgiftInfo["gift_name"]; $returnData["gif_image"] = $boxgiftInfo["special"]; } else {*/ $returnData["image"] = $giftInfo["image"]; $returnData["gif_image"] = $giftInfo["special"]; /*}*/ } else { $returnData["image"] = $giftInfo["image"]; $returnData["gif_image"] = $giftInfo["gif_image"]; } // 增加抽点记录 Db::commit(); $this->success("赠送成功!",$returnData); } else { $this->success("赠送失败!"); } } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } } //gift_id必须是爆币礼物才可以 public function baobi($user_id,$gift_id,$pay_total){ //奖项参数 $conf_arr = Db::name('gift_baobi_config')->where('gift_id',$gift_id)->select(); if(empty($conf_arr)){ return true; } //用户今天的爆币情况,与消费情况,不用区分具体哪个爆币礼物 $today_start = strtotime(date('Y-m-d')); $today_end = $today_start + 86399; $map = [ 'user_id' => $user_id, 'createtime' => ['BETWEEN',[$today_start,$today_end]], ]; $today_data = Db::name('gift_baobi_log')->field('IFNULL(sum(pay_total),0) as today_pay_total,IFNULL(sum(baobi_total),0) as today_baobi_total')->where($map)->find(); //dump($today_data); if($today_data['today_baobi_total'] >= $today_data['today_pay_total']){ //爆币比消费还高了,不用抽奖了,记录个日志,返回。这里的日志仅用来记录今日消费 sum(pay_total) $data = []; $data['user_id'] = $user_id; $data['gift_id'] = $gift_id; $data['baobi_id'] = 0; $data['beilv'] = 0; $data['rate'] = 0; $data['pay_total'] = $pay_total; $data['baobi_total'] = 0; $data['createtime'] = time(); $log_id = Db::name('gift_baobi_log')->insertGetId($data); if(!$log_id){ return false; } return true; } //概率新数组 $gailv = []; $bei = 100; //小数变整数 foreach ($conf_arr as $key=>$value) { $gailv[$value['id']] = $value['rate']*$bei; } //dump($gailv); //奖项新数组 $conf_column = []; foreach ($conf_arr as $key=>$value) { $conf_column[$value['id']] = $value; } //dump($conf_column); //抽 $rid = $this->getRand($gailv); //根据概率获取奖项id //dump($rid); //返回中奖结果 $result = $conf_column[$rid]; //爆币金额 $baobi_total = bcdiv(bcmul($pay_total,$result['beilv'],0),100,0); //写入爆币记录,不论有没有钱。全都写入其实是为了方便查日志,比在money_log里找范围更小,而且可删除 $data = []; $data['user_id'] = $user_id; $data['gift_id'] = $gift_id; $data['baobi_id'] = $result['id']; $data['beilv'] = $result['beilv']; $data['rate'] = $result['rate']; $data['pay_total'] = $pay_total; $data['baobi_total'] = $baobi_total; $data['createtime'] = time(); $log_id = Db::name('gift_baobi_log')->insertGetId($data); if(!$log_id){ return false; } //直接给用户钱 if($baobi_total > 0){ $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$baobi_total,'+',0,'送礼物爆币'.$result['beilv'].'%',21,'jewel'); if($rs_wallet['status'] === false){ return false; } //返回爆币金额 return $baobi_total; } //默认成功 return true; } //概率获得算法 private function getRand($proArr) { //概率数组的总概率精度 $proSum = array_sum($proArr); $key = rand(1, $proSum); // echo $key; $result = 0; $now = 0; foreach ($proArr as $k=>$v) { $now = $now + $v; if($key<=$now) { $result = $k; break; } } unset ($proArr); return $result; } //送礼完成之后,大礼物飘屏 private function bigGiftNotice($sender,$receiver,$partyInfo,$giftUserParty){ if ($giftUserParty['party_id'] > 0) { $is_big = Db::name('gift')->where('id',$giftUserParty['gift_id'])->value('is_big'); if ($is_big == 1) { $notice = new GatewayworkerTools(); $giftUserParty['gift_gif_image'] = localpath_to_netpath($giftUserParty['gift_gif_image']); $notice->sendBigGiftInParty($sender,$receiver,$partyInfo,$giftUserParty); } } } //某房间内礼物列表 public function getPartyGiftLog(){ $party_id = input('party_id',0); $list = Db::name('gift_user_party')->alias('log') ->field('log.gift_name,log.gift_gif_image,log.number,log.value,log.guildermoney,log.createtime,user.nickname as give_nickname,b.nickname as get_nickname') ->join('user','log.user_id = user.id','LEFT') ->join('user b','log.user_to_id = b.id','LEFT') ->where('log.party_id',$party_id)->order('log.id desc')->autopage()->select(); $list = list_domain_image($list,['gift_gif_image']); foreach($list as $key => &$val){ $val['createtime'] = get_last_time($val['createtime']); } $this->success('success',$list); } // /** // * 播放动效 // */ // public function play($party_id,$type,$value,$number) { // // 发送tcp // $tcpArr = []; // $tcpArr['type'] = "play"; // $tcpArr['data'] = [ // 'party_id' => $party_id, // 'type' => $type, // 'value' => $value, // 'number' => $number, // ]; // $tcpJson = json_encode($tcpArr); // $client = stream_socket_client(config("tcp")); // $buffer2 = base64_encode($tcpJson)."****"; // fwrite($client, $buffer2); // } /** * 更新派对信息(热度等) */ private function updatePartyHot($partyInfo,$hotValue,$room_type) { // $partyInfo = \app\common\model\Party::where(['id'=>$party_id])->find(); if(!$partyInfo) return $hotValue; $party_hot = $partyInfo->party_hot > 0 ? $partyInfo->party_hot:0; $party_hot_value = $party_hot + $hotValue; $party_hot_value = $party_hot_value > 0 ? $party_hot_value : 0; if($party_hot_value != $party_hot) { // 保存数据 $partyInfo->party_hot = $party_hot_value; $partyInfo->save(); // 更新redis 加入缓存排序 $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]); // 更新redis 加入缓存 $redPartyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]); if($redPartyInfo) { $redPartyInfo = json_decode($redPartyInfo,true); $redPartyInfo["party_hot"] = $party_hot_value; $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($redPartyInfo)); } // // 发送tcp // $tcpArr = []; // $tcpArr['type'] = "changeRoomHot"; // $tcpArr['data'] = [ // 'room_id' => $party_id, // 'value' => $party_hot_value, // ]; // $tcpJson = json_encode($tcpArr); // $client = stream_socket_client(config("tcp")); // $buffer2 = base64_encode($tcpJson)."****"; // fwrite($client, $buffer2); } return $party_hot_value; } /** * 用户赠送礼物后房间内用户排行,贡献榜前三名 */ private function getPartyUserTop($party_id,$room_type) { // 获取本周第一天 $weekday = $this->firstOfWeek(date("Y-m-d H:i:s")); // 获取当天零点 $day = date("Ymd"); $userModel = new \app\common\model\User(); // 获取条数 $num = 3; // 获取3条财富排行周记录 $getweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true); $userList = $userModel->rankList($getweek); $avatarArr = []; if($userList) { foreach($userList as $k => $v) { $v["jewel"] > 0 && $avatarArr[] = $v["avatar"]; } // 加入缓存做备份 $this->redis->hSet("user_jewel_top3",$party_id,json_encode($avatarArr)); // // 发送tcp // $tcpArr = []; // $tcpArr['type'] = "changeRoomUserTop"; // $tcpArr['data'] = [ // 'room_id' => $party_id, // 'user_avatar' => $avatarArr, // ]; // $tcpJson = json_encode($tcpArr); // $client = stream_socket_client(config("tcp")); // $buffer2 = base64_encode($tcpJson)."****"; // fwrite($client, $buffer2); } return $avatarArr; } /** * 用户赠送礼物后房间内用户魅力值增加 */ private function updateUserCharm($party_id,$user_id,$giftValue) { // 获取用户魅力值 $users = $this->redis->zRange("hourCharm_".$party_id,0,-1,true); if(!$users) $users[$user_id] = 0; if(isset($users[$user_id])) { $value = $users[$user_id] + $giftValue; } else { $value = $giftValue; } $this->redis->zAdd("hourCharm_".$party_id,$value,$user_id); return true; } /** * 单个房间魅力值清零 */ public function partyClearCharm() { $party_id = input("party_id");// 派对ID $userId = input("user_id", 0);// 用户ID if($party_id <= 0) { $this->error("请输入派对ID"); } if ($userId) { $this->redis->zRem("hourCharm_" . $party_id, $userId); } else { $this->redis->del("hourCharm_" . $party_id); } $this->success("操作成功!"); } /** * 获取音乐列表 */ public function getMusicList() { $this->success("获取成功!",\app\common\model\Music::select()); } //派对举报 public function report_party() { $type = input('type');// 类型描述 $content = input('content');// 内容 $images = input('images');// 图片 $party_id = input('party_id');// if (!$type || !$content || !$images || !$party_id) { $this->error("请完成举报内容!"); } $data = []; $data["user_id"] = $this->auth->id; $data["party_id"] = $party_id; $data["type"] = $type; $data["content"] = $content; $data["images"] = $images; $data["createtime"] = time(); $res = Db::name('party_report')->insertGetId($data); if ($res) { $this->success("举报内容提交成功!"); } else { $this->error("网络错误,请稍后重试!"); } } //============================定时任务==========================// /** * redis清理排行榜 并数据库备份 */ public function updateTops() { // 前一天日期 $yestaday = date("Ymd",bcsub(time(),86400)); // 上个周一 $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800))); // 上个月一号 $monty = bcsub(date("m"),1); if($monty < 10) $monty = "0".$monty; $premonth = date("Y").$monty."01"; // 获取所有派对ID $party_ids = \app\common\model\Party::where(["status"=>1])->column("id"); // 获取数据 $time = time(); if($party_ids) foreach($party_ids as $k => $v) { $keys = [ "party_jewel_get_".$v.":".$yestaday."d", "party_jewel_to_".$v.":".$yestaday."d", "party_jewel_get_".$v.":".$preweek."w", "party_jewel_to_".$v.":".$preweek."w", "party_jewel_get_".$v.":".$premonth."m", "party_jewel_to_".$v.":".$premonth."m", ]; foreach($keys as $key) { // 备份数据 $redisData = $this->redis->zRevRange($key,0,-1,true); if($redisData) { $keyInfo = \app\common\model\RedisTops::where(["key"=>$key])->find(); $data = ["party_id"=>$v,"key"=>$key,"value"=>json_encode($redisData),"createtime" => $time]; if(!$keyInfo) { $res = \app\common\model\RedisTops::insert($data); // 清理数据 $res && $this->redis->zRemRangeByRank($key,0,-1); } } } } } public function handleParty() { return false; $partyList = \app\common\model\Party::where('is_online',1)->select(); // $this->redis->del("online_" . 3);die; // $this->redis->hSet("online_" . 3, 4, 4);die; foreach ($partyList as $party){ $userId = $party['user_id']; $roomId = $party['id']; $this->redis->HDel("online_" . $roomId, $userId); // 扣除在线用户在房间情况 $this->redis->hDel("livingUser",$userId); // // 更新房间在线状态 $partyInfo = $this->redis->get("party_" . $roomId); if ($partyInfo) { $partyInfo = json_decode($partyInfo, true); $memCount = count($this->redis->hGetAll("online_" . $roomId)); if ($memCount <= 0) { dump($roomId.'--1---'); $partyInfo["is_online"] = 0; $this->redis->set("party_" . $roomId, json_encode($partyInfo)); \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]); } } $liveInfo = $this->redis->get("live_" . $roomId); if ($liveInfo) { $liveInfo = json_decode($liveInfo, true); $memCount = count($this->redis->hGetAll("online_" . $roomId)); if ($memCount <= 0) { dump($roomId.'---2--'); $liveInfo["is_online"] = 0; $this->redis->set("live_" . $roomId, json_encode($liveInfo)); \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]); }else{ dump($roomId.'---3--'); $this->redis->del("online_" . $roomId); $liveInfo["is_online"] = 0; $this->redis->set("live_" . $roomId, json_encode($liveInfo)); \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]); } } } $this->success("获取成功!"); } }