123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986 |
- <?php
- namespace app\api\controller;
- use addons\faqueue\library\QueueApi;
- use app\api\controller\Common;
- use app\common\model\GiftBox;
- use app\common\model\PartyJoin;
- use fast\Random;
- use app\common\controller\RedisLeaderboard;
- use Redis;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use think\Request;
- /**
- * 派对信息接口
- */
- class Party extends Common
- {
- protected $noNeedLogin = ['updatePartyInfo', 'getPatyType', 'addUserPositionToParty', 'clearMoney', 'clearCharm', 'getPartyRankList', 'getDefaultBackground', 'getPartyGifType', 'getPartGifList', 'getPartHeadgifList', 'isNotalk', 'getMusicList', 'updateTops', 'handleParty'];
- protected $noNeedRight = ['*'];
- public function __construct(Request $request = null)
- {
- $this->roomTypeArr = [1=>"party",2=>"livebc"];
- parent::__construct($request);
- }
- /**
- * 创建/进入派对
- */
- public function createParty() {
- $room_type = $this->request->request('room_type',1); // 房间类型:1=派对,2=直播
- $party_name = $this->request->request('party_name',0);
- $party_type = $this->request->request('party_type',0); // 派对类型(情感互动,心动点单 等)语聊房的
- $cityname = input('cityname','');//直播城市
- $partyModel = new \app\common\model\Party();
- $user_id = $this->auth->id;
- //
- if($this->auth->idcard_status == -1){
- $this->error('请先申请实名认证');
- }
- if($this->auth->idcard_status == 0){
- $this->error('您已经提交实名认证,请等待审核');
- }
- if($this->auth->idcard_status == 2){
- $this->error('您的实名认证审核未通过,请重新申请');
- }
- // 判断派对是否存在
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $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->count("id");
- if($partycount >= $partylimit && $partylimit != -1) {
- $this->error("房间数量已达上线,请联系客服!");
- }
- $party_name = $party_name ? $party_name : $this->auth->username."的房间"; // 派对名称
- $party_logo = "/assets/api/party_logo.png"; // 派对logo
- $party_notice = "请编辑房间公告"; // 派对公告
- $party_notice_detail = "请编辑房间公告内容!"; // 派对公告详情
- $party_ids = $partyModel->column("party_id");
- // 创建派对ID (临时ID四位,派对数不超过8999)
- $party_id = $this->auth->getUinqueId(6,$party_ids);
- if($party_id > 999999) {
- $this->error("派对超限,请联系客服");
- }
- $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"] = $room_type == 2 ? $party_type : 0;//目前直播需要,语聊不需要
- $data["party_notice"] = $party_notice;
- $data["party_notice_detail"] = $party_notice_detail;
- $data["is_online"] = 1;
- $data["status"] = 0;
- $data["is_recommend"] = 0;
- $data["createtime"] = time();
- $data["cityname"] = $cityname;
- $id = $partyModel->insertGetId($data);
- if(!$id) {
- $this->error("派对创建失败,请稍后重试!");
- }
- $data["id"] = $id;
- if($room_type == 2) {
- \app\common\model\User::update(["is_livebc"=>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;
- // 加入缓存排序
- $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
- // 加入缓存
- $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], ["id" => $user_id]);
- }
- } else {
- if($sqlPartyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
- if($sqlPartyInfo["status"] == 0){
- // 清楚对应的记录
- $redis->del($this->roomTypeArr[$room_type]."_".$sqlPartyInfo["id"]);
- // 删除派对记录
- \app\common\model\Party::where('id',$sqlPartyInfo["id"])->delete();
- $this->error("创建失败,请重试");
- }
- // if($sqlPartyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
- // 如果是房主并且房间处于离线状态下激活
- if ($sqlPartyInfo['user_id'] != $user_id) {
- $this->error("当前直播已结束!");
- }
- $partyInfo = $sqlPartyInfo;
- $partyInfo["is_new"] = 0;
- //修改类型
- //目前直播需要,语聊不需要
- if($room_type == 2){
- Db::name('party')->where('id',$partyInfo['id'])->update(['party_type'=>$party_type]);
- $partyInfo["party_type"] = $party_type;
- }
- }
- if($room_type == 2){
- //清空房间排行榜
- $redis->del('livebc_jewel_to_' . $partyInfo['id']);
- $redis->del('livebc_jewel_get_' . $partyInfo['id']);
- $redis->hDel("user_jewel_top3",$partyInfo['id']);
- }
- // 获取用户魅力值
- $users = $redis->zRange("hourCharm_".$partyInfo->id,0,-1,true);
- $u = [];
- if($users) {
- foreach($users as $k => $v) $u[] = [
- "user_id"=>$k,
- "charm"=>$this->changeW($v)
- ];
- }
- $partyInfo["userCharm"] = $u;
- $this->success("获取成功!",$partyInfo);
- }
- /**
- * 更新房间状态为正常
- */
- public function savePartyStatus() {
- $party_id = $this->request->request('party_id'); // 派对ID
- $room_type = $this->request->request('room_type',1);; // 房间类型:1=派对,2=直播
- if (!$party_id || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
- $user_id = $this->auth->id;
- $partyInfo = \app\common\model\Party::get($party_id);
- if($partyInfo->status < 0) $this->error("当前状态不支持变更!");
- if($partyInfo->user_id !== $user_id) $this->error("当前账号异常,非房主禁止操作!");
- /*$partyInfo->status = 1;
- $partyInfo->updatetime = time();*/
- $res = Db::name('party')->where('id',$party_id)->update(['status'=>1,'updatetime'=>time()]);
- if($res !== false) {
- // 更新redis 加入缓存排序
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- // 更新redis 加入缓存
- $redPartyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]);
- if($redPartyInfo) {
- $redPartyInfo = json_decode($redPartyInfo,true);
- $redPartyInfo["status"] = 1;
- $redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($redPartyInfo));
- }
- $this->success("更新房间状态成功!");
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 删除派对(群解散)
- * 前端没用到
- */
- public function closeParty() {
- $party_id = $this->request->request('party_id'); // 派对ID
- $room_type = $this->request->request('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 删除
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $redis->zRem($this->roomTypeArr[$room_type]."Rank",$party_id);
- $redis->del($this->roomTypeArr[$room_type]."_".$party_id);
- $this->success("删除成功!");
- } else {
- $this->error("删除失败!");
- }
- }
- /**
- * 获取派对列表排序
- */
- public function getPartyRankList() {
- $thispage = $this->request->request('thispage',1,"intval"); // 当前页数
- $pagenum = $this->request->request('pagenum',10,"intval"); // 每页显示条数0=不做分页
- $type_id = $this->request->request('type_id'); // 派对类型
- $room_type = 1; // 房间类型:1=派对,2=直播
- $is_recommend = $this->request->request('is_recommend'); // 推荐0=否1=是
- $all = $this->request->request('all'); // 全部分类0=否1=是
- $index = $this->request->request('index',0); // 全部分类0=否1=是
- //更新ip,设备id
- //非原创,注释
- //if($is_recommend == 1) controller('api/User')->changeDeviceIp();
- $start = ($thispage-1)*$pagenum;
- $end = $start+($pagenum-1);
- // 获取排序
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $redisPartyRankList = $redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
- $partyModel = new \app\common\model\Party();
- if(!$redisPartyRankList) {
- $userModel = new \app\common\model\User();
- // 直接从数据库获取所有数据
- $where = [];
- $where["a.status"] = 1;
- $where["a.is_online"] = 1;
- $where["a.room_type"] = $room_type;
- $sqlPartyList = $partyModel->alias("a")->field("a.*,b.name as type_name")
- ->join("party_type b","a.party_type = b.id","left")->where($where)->select();
- $userList = $userModel->field("id,nickname,avatar")->where(["status"=>1])->select();
- $userInfoArr = [];
- if($userList) foreach($userList as $k => $v) $userInfoArr[$v["id"]] = $v;
- if($sqlPartyList) {
- foreach($sqlPartyList as $k => $v) {
- // 加入缓存排序
- $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $v['party_hot'], $v["id"]);
- // 设置冠名
- $sqlPartyList[$k]["naming"] = isset($userInfoArr[$v["naming"]])?$userInfoArr[$v["naming"]]:[];
- // 设置房主头像
- $sqlPartyList[$k]["avatar"] = isset($userInfoArr[$v["user_id"]])?$userInfoArr[$v["user_id"]]["avatar"]:[];
- // 加入缓存
- $redis->set($this->roomTypeArr[$room_type]."_".$v["id"],json_encode($v));
- }
- $redisPartyRankList = $redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
- }
- }
- if($redisPartyRankList) {
- $resultInfo = $partyModel->getPatyInfoByPartyId($redisPartyRankList,$this->roomTypeArr[$room_type],$type_id,$is_recommend,$all,$start,$end,$index);
- $this->success("获取成功!",$resultInfo);
- }else{
- $this->success("获取成功!",[]);
- }
- }
- /**
- * 获取派对用户排序
- */
- public function getPartyUserRank() {
- $room_type = 1;
- $party_id = $this->request->request("party_id");// 派对ID
- if (!$party_id) $this->error(__('Invalid parameters'));
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $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 = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$day."d",0,$num-1,true);
- $res['getRankListDay'] = $userModel->rankList($getday);
- // 获取50条财富排行日记录
- $today = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true);
- $res['toRankListDay'] = $userModel->rankList($today);
- // 获取50条财富排行周记录
- $getweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$weekday."w",0,$num-1,true);
- $res['getRankListWeek'] = $userModel->rankList($getweek);
- // 获取50条贡献排行周记录
- $toweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$weekday."w",0,$num-1,true);
- $res['toRankListWeek'] = $userModel->rankList($toweek);
- // 获取50条财富排行月记录
- $toweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$monthday."m",0,$num-1,true);
- $res['getRankListMonth'] = $userModel->rankList($toweek);
- // 获取50条贡献排行周记录
- $toweek = $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 = $this->request->request('party_id',0,"intval"); // 派对ID
- $room_type = 1; // 房间类型
- $party_hot = $this->request->request('party_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值)
- if (!$party_id || !$party_hot) {
- $this->error(__('Invalid parameters'));
- }
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- // 更新热度
- $redis->zIncrBy($this->roomTypeArr[$room_type]."Rank", $party_hot, $party_id);
- return $this->success("更新成功!");
- }
- /**
- * 更新主持人和麦位前四位至首页房间排行
- */
- public function addUserPositionToParty() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
- $upOrdown = $this->request->request('upordown'); // 上下麦:1=上麦-1=下麦
- $room_type = 1; // 房间类型
- $avatar = $this->request->request('avatar'); // 用户头像 (upOrdown参数为-1时可以不传)
- $position = $this->request->request('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("设置成功!");
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $redisData = $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"]);
- $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
- }
- return $this->success("设置成功!");
- }
- /**
- * 获取派对在线人数列表
- */
- public function getOnlieList() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
- $page = $this->request->request('page',1); // 分页
- $pageNum = $this->request->request('pageNum',10); // 分页
- // 分页搜索构建
- $pageStart = ($page-1)*$pageNum;
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $res = $redis->hGetAll("online_".$party_id);
- $user_ids = [];$userList = [];
- if($res) $user_ids = array_values($res);
- // 获取用户列表信息
- $user_ids && $userList = \app\common\model\User::field("id,avatar,nickname,level,gender")->where(["id"=>["in",$user_ids]])->limit($pageStart,$pageNum)->select();
- $this->success("获取成功!",$userList);
- }
- /** 连续进房提示
- * @param $user_id
- * @param $party_id
- * @param $redis
- * @return int
- */
- private function getUserJoinPartyDays($user_id, $party_id,$redis)
- {
- $days = 1;
- $joinPartyDays = $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++;
- $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-'. $days);
- }elseif($todayStartTime <= $oldTime && $oldTime <= $todayEndTime){
- //今天登录过 days
- $days = (int)$joinPartyDaysArr[1];
- } else {
- $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
- }
- }else{
- $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
- }
- return $days;
- }
- /**
- * 加入派对
- */
- public function joinParty() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
- $room_type = $this->request->request('room_type',1); // 房间类型:1=派对2=直播
- $party_pass = $this->request->request("party_pass");//
- if (!$party_id || !in_array($room_type,[1,2])) {
- $this->error(__('Invalid parameters'));
- }
- $user_id = $this->auth->id;
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $redis->zAdd("party_user_".$party_id,$this->auth->username,$user_id);
- // 判断当前用户是否存在在直播间通过切换过来的
- $livingUserPartyId = $redis->hGet("livingUser", $user_id);
- if ($livingUserPartyId) {
- if($livingUserPartyId != $party_id){
- // 扣除房间在线人数
- $redis->HDel("online_" . $livingUserPartyId, $user_id);
- // 扣除在线用户在房间情况
- $redis->hDel("livingUser", $user_id);
- }
- }
- // 记录在线用户在房间情况
- $redis->hSet("livingUser", $user_id, $party_id);
- // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
- $lsetList = [];
- if($room_type == 1){
- for($i=1;$i<=4;$i++) {
- $hgetlist = $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"]));
- $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
- break;
- }
- // 踢出
- if($hgetlist && $i == 4 && $restime < $hgetlist["time"]) {
- $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
- $this->error(__('您已被该房间踢出,解除时间:'.$restime));
- break;
- }
- }
- }
- // 判断派对密码
- $partyInfo = $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("该房间为预创建房间,请联系管理员正式开通!");
- if(isset($partyInfo["party_pass"]) && $partyInfo["party_pass"] && $partyInfo["user_id"] != $user_id && $room_type == 1) {
- if(($party_pass != $partyInfo["party_pass"] || strlen($party_pass) != 4)) {
- $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 && $partyInfo['is_online'] == 0) {
- $this->error("当前直播已结束!");
- }
- }*/
- // // 如果是房主自己进入房间,则更新用户 为在线状态
- // if ($partyInfo["user_id"] == $user_id) {
- // if ($room_type == 2)
- // \app\common\model\User::update(["is_livebc" => 1], ["id" => $partyInfo["user_id"]]);
- // }
- } else {
- $this->error("派对信息获取失败!");
- }
- // 获取用户魅力值
- $users = $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 = $redis->hGet("user_jewel_top3",$party_id);
- $lsetList["userJewelTop3"] = $heads?json_decode($heads,true):[];
- // 判断当前用户是否收藏了此房间
- $cellection = $redis->hGet("room_cellection",$user_id."-".$party_id);
- $lsetList["is_cellection"] = $cellection?1:0;
- 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,$redis);
- $lsetList["partyInfo"] = $partyInfo;
- return $this->success("加入成功!",$lsetList);
- }
- /**
- * 判断是否被禁言
- */
- public function isNotalk() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
- $user_id = $this->request->request('user_id',0,"intval"); // 用户ID 逻辑ID
- if (!$user_id || !$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
- $lsetList = [];
- $lsetList["is_notalk"] = 0;
- $lsetList["notalk_time"] = 0;
- $hgetlist = $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);
- }
- /**
- * 退出派对
- * 前端没用到,转移到trtc_callback了
- */
- public function outParty() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $user_username = $this->auth->username;
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $redis->zRem("party_user_".$party_id,$user_username);
- // 处理online_party_id
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $redis->HDel("online_" . $party_id, $this->auth->id);
- // 扣除在线用户在房间情况
- $redis->hDel("livingUser", $this->auth->id);
- // 更新房间在线状态
- $partyInfo = $redis->get("party_" . $party_id);
- if ($partyInfo) {
- $partyInfo = json_decode($partyInfo, true);
- $memCount = count($redis->hGetAll("online_" . $party_id));
- if ($memCount <= 0) {
- $partyInfo["is_online"] = 0;
- $redis->set("party_" . $party_id, json_encode($partyInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
- }
- }
- $liveInfo = $redis->get("livebc_" . $party_id);
- if ($liveInfo) {
- $liveInfo = json_decode($liveInfo, true);
- $liveInfo["is_online"] = 0;
- $redis->set("livebc_" . $party_id, json_encode($liveInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
- $rs_user = Db::name('user')->where('id',$this->auth->id)->update(['is_livebc'=>0]);
- //准备返回数据,刷礼物的总值,刷礼物人数,榜一信息
- //清空房间排行榜
- $redis->del('livebc_jewel_to_' . $party_id);
- $redis->hDel("user_jewel_top3",$party_id);
- }
- return $this->success("退出成功!",[]);
- }
- /**
- * 派对内搜索用户
- */
- public function searchUserParty() {
- $username = $this->request->request('username',0,"intval"); // 用户
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- $userModel = new \app\common\model\User();
- $where = [];
- $where["username"] = $username;
- $userInfo = $userModel->field("id,username,avatar,nickname,level,gender")->where($where)->select();
- return $this->success("查询成功!",$userInfo);
- }
- /**
- * 设置冠名
- */
- public function setNaming() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
- $type = $this->request->request('type',1); // 1:设置 0:取消设置
- if (!$party_id || !$user_id || ($type != 1 && $type != 0)) {
- $this->error(__('Invalid parameters'));
- }
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $partyModel = new \app\common\model\Party();
- $data = [];
- $where = [];
- $where["id"] = $party_id;
- $partyInfo = $partyModel->where(["id"=>$party_id])->find();
- $getredisPartyInfo = $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"] = [];
- }
- $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 cellectionParty() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $user_id = $this->auth->id;
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $partycellectionModel = new \app\common\model\PartyCellection();
- // 添加记录
- $data = [];
- $data["user_id"] = $user_id;
- $data["party_id"] = $party_id;
- if($partycellectionModel->where($data)->find()) {
- $res = $partycellectionModel->where($data)->delete();
- $redis->hDel("room_cellection",$user_id."-".$party_id);
- return $this->success("取消收藏成功!",$res);
- }
- $data["createtime"] = time();
- $res = $partycellectionModel->insert($data);
- $redis->hSet("room_cellection",$user_id."-".$party_id,1);
- return $this->success("收藏成功!",$res);
- }
- /**
- * 派对收藏列表
- */
- public function cellectionPartyList() {
- $page = $this->request->request('page',1); // 分页
- $pageNum = $this->request->request('pageNum',10); // 分页
- // 分页搜索构建
- $pageStart = ($page-1)*$pageNum;
- $partycellectionModel = new \app\common\model\PartyCellection();
- $userModel = new \app\common\model\User();
- $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("party r","a.party_id = r.id")
- ->join("party_type t","t.id = r.party_type","left")
- ->limit($pageStart,$pageNum)
- ->select();
- if($list) {
- // $redis = new Redis();
- // $redisconfig = config("redis");
- // $redis->connect($redisconfig["host"], $redisconfig["port"]);
- // // 获取本周第一天
- // $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
- // // 获取redis 中 用户排行榜前五名
- // foreach($list as $k => $v) {
- // $getweek = $redis->zRevRange("party_jewel_get_".$v["r_id"].":".$weekday,0,4,true);
- // $userlist = $userModel->rankList($getweek);
- // if($userlist) {
- // $users = [];
- // foreach($userlist as $m => $n) {
- // $users[] = $n["avatar"];
- // }
- // $list[$k]["users"] = $users;
- // } else {
- // $list[$k]["users"] = [];
- // }
- //
- // $mod = isset($v["party_type_id"])?intval($v["party_type_id"])%5:1;
- // $list[$k]["party_type_color"] = $mod == 0?5:$mod;
- // }
- $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 = $this->request->request('party_id',0,"intval"); // 直播间ID
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $user_id = $this->auth->id;
- $partyModel = new \app\common\model\Party();
- $partycellectionModel = new \app\common\model\PartyCellection();
- $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("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_sellection"] = 0;
- $where = [];
- $where["user_id"] = $user_id;
- $where["party_id"] = $party_id;
- if($partycellectionModel->where($where)->find()) {
- $partyInfo["is_sellection"] = 1;
- }
- // 获取房主信息
- $where = [];
- $where["id"] = $partyInfo["user_id"];
- $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
- // 获取技能信息
- /*$skillList = Model("ViewUserSkill")->getSkillInfo($partyInfo["user_id"]);
- $userInfo["skill"] = implode("/",$skillList);*/
- $userInfo["skill"] = [];
- $partyInfo["userInfo"] = $userInfo;
- $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]);
- }
- $this->success("获取成功!",$partyInfo);
- }
- /**
- * 派对设置
- */
- public function setParty() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- $party_name = $this->request->request('party_name'); // 派对名称
- $party_logo = $this->request->request('party_logo'); // 派对logo/封面
- $party_pass = $this->request->request('party_pass'); // 派对密码
- $party_type = $this->request->request('party_type'); // 使用场景
- $is_screen = $this->request->request('is_screen'); // 是否关闭公屏:1=是,0=否
- $on_model = $this->request->request('on_model'); // 上麦模式:1=自由模式,2=麦序模式
- $room_type = 1; // 房间类型:1=派对,2=直播
- $background = $this->request->request('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 房间信息
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
- if($partyInfo) {
- $partyInfo = json_decode($partyInfo,true);
- $partyInfo = array_replace($partyInfo,$data);
- $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
- }
- $this->success("房间设置成功!",$data);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 获取派对背景
- */
- public function getDefaultBackground() {
- $list = Db::name('party_background')->select();
- $list = list_domain_image($list,['image']);
- $this->success("success",$list);
- }
- //腾讯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 = $this->request->request('party_id',0,"intval"); // 直播间ID
- $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
- $item = $this->request->request('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
- $time = $this->request->request('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("noble,avatar,nickname,gender,level")->where(["id"=>$user_id])->find();
- if(!$userInfo) $this->error("用户信息获取失败!");
- // 国王防踢。
- /*$noble_no = \app\common\model\NobleLevel::where(["id"=>$userInfo['noble']])->value("level_no");
- if(($item == 3 || $item == 4) && $noble_no == "p08PCcNB") {
- $this->error("对方已开通国王贵族,踢出房间失败!");
- }*/
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $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 = $redis->hSet("party_manage_".$party_id,$user_id."-".$item,serialize($data));
- if ($item==1) {
- $this->setGroupAdmin($user_id,$party_id,1);
- }
- if($res !== false) {
- $this->success("设置成功!",$data);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 派对管理设置列表
- */
- public function partyManageSetList() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- $item = $this->request->request('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
- if (!$party_id || !in_array($item,[1,2,3,4])) {
- $this->error(__('Invalid parameters'));
- }
- $time = time();
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $hget=$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 = $this->request->request('user_id',0,"intval"); // userid
- $party_id = $this->request->request('party_id',0,"intval"); // 房间ID
- $item = $this->request->request('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
- if (!$id || !in_array($item,[1,2,3,4])) {
- $this->error(__('Invalid parameters'));
- }
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $res = $redis->hDel("party_manage_".$party_id,$id."-".$item);
- if ($item==1) {
- $this->setGroupAdmin($id,$party_id,0);
- }
- if($res !== false) {
- $this->success("移除成功!",$res);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 更新派对公告
- */
- public function savePartyNotice() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- $room_type = $this->request->request('room_type',1); // 房间类型:1=派对2=直播
- $party_notice = $this->request->request('party_notice'); // 公告标题
- $party_notice_detail = $this->request->request('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 房间信息
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
- if($partyInfo) {
- $partyInfo = json_decode($partyInfo,true);
- $partyInfo = array_replace($partyInfo,$data);
- $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
- }
- $this->success("更新成功!",$res);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 开始排麦
- */
- public function addLineUp() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- $mai_id = $this->request->request('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;
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $data = unserialize($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 = $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 = $this->request->request('party_id',0,"intval"); // 直播间ID
- $user_id = $this->request->request('user_id',0); // 直播间ID
- $is_empty = $this->request->request('is_empty',0,"intval"); // 是否清空排麦列表 1=清空,0=不清空
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $userid = $this->auth->id;
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $data=unserialize($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 = [];
- $redis->hSet("party_lineup",$party_id,serialize($data));
- $this->success("移除成功!");
- }
- /**
- * 排麦列表
- */
- public function lineUpList() {
- $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $data = $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 = 1;
- $partytypeModel = new \app\common\model\PartyType();
- $partytypeList = $partytypeModel->where(["room_type"=>$room_type])->select();
- $this->success("获取成功!",$partytypeList);
- }
- /**
- * 判断派对是否设置过密码
- */
- public function getPatyIspass() {
- $party_id = $this->request->request("party_id");// 派对ID
- $room_type = 1;
- $is_miniprogram = 0;//
- if (!$party_id) $this->error(__('Invalid parameters'));
- $user_id = $this->auth->id;
- // 存redis 房间信息
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $partyInfo = $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;
- if($partyInfo["user_id"] == $user_id || $this->auth->mobile == '18560505277') $data["is_pass"] = 0;
- $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()
- {
- // $type_id = $this->request->request("type_id", 1);// 类型ID
- $list = \app\common\model\PartyGif::field("id,gif_image")->order("weight", "asc")->select();
- $this->success("获取成功!", $list);
- }
- /**
- * 获取房间头像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() {
- // 接口防并发
- if (!$this->apiLimit(1, 1000)) {
- $this->error(__('Operation frequently'));
- }
- //
- $user_ids = $this->request->request("user_id");// 赠送对象
- $gift_id = $this->request->request("gift_id");// 礼物ID
- $party_id = $this->request->request("party_id",0);// 派对ID
- $number = $this->request->request("number");// 赠送数量
- $room_type = 1; // 房间类型
- $is_back = 0;// 是否背包赠送: 1=是,0=否
- if (!$user_ids || !$gift_id || !$number)
- {
- $this->error(__('Invalid parameters'));
- }
- //
- $user_id_arr = explode(',',$user_ids);
- $userCount = count($user_id_arr);
- $userauthid = $this->auth->id;
- // 获取礼物信息
- $giftInfo = Db::name('gift')->where('id',$gift_id)->find();
- if (!$giftInfo) {$this->error("请选择礼物!");}
- $giftValue = $giftInfo["value"] * $number;
- $giftCountValue = $giftInfo["value"] * $number * $userCount;
- // 判断当前用户余额
- $user_gold = model('wallet')->getWallet($userauthid,'gold');
- if($user_gold < $giftCountValue) {$this->error("您的金币余额不足!");}
- if($party_id){
- $partyInfo = \app\common\model\Party::field("user_id")->where(["id"=>$party_id])->find();
- if(!$partyInfo){
- $this->error('不存在的语聊间');
- }
- }
- $returnData = [];
- Db::startTrans();
- try {
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
- // 获取当天零点
- $day = date("Ymd");
- // 获取本周第一天
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
- // 获取本月第一天
- $monthday = date("Ym01");
- $allVal = 0;
- $i = 0;
- foreach($user_id_arr as $user_id) {
- // 添加礼物赠送记录表
- $data = [];
- $data["user_id"] = $userauthid;
- $data["user_to_id"] = $user_id;
- $data["party_id"] = $party_id;
- $data["gift_id"] = $gift_id;
- $data["gift_give_type"] = 2;
- $data["gift_name"] = $giftInfo["name"];
- $data["gift_gif_image"] = $giftInfo["image"];
- $data["number"] = $number;
- $data["price"] = $giftInfo["value"];
- $data["value"] = $giftValue;
- $data["createtime"] = time();
- $log_id = Db::name('gift_user_party')->insertGetId($data);
- if(!$log_id){
- Db::rollback();
- $this->error('赠送失败');
- }
- if($giftValue > 0){
- // 扣除当前用户钻石余额
- $wallet_rs = model('wallet')->lockChangeAccountRemain($userauthid,'gold',-$giftValue,51,'赠送礼物:'.$giftInfo["name"],'gift_user_party',$log_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- // 添加赠送用户余额
- $money_to_gold = config('site.money_to_gold');
- $gift_plat_scale = config('site.gift_plat_scale');
- $giftmoney = bcdiv($giftValue,$money_to_gold,2);
- $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
- $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,52,'获得礼物:'.$giftInfo["name"],'gift_user_party',$log_id);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- }
- $res6 = true;
- if ($res6) {
- $i++;
- if($party_id > 0) {
- // 添加redis记录做财富排行榜日榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $giftValue, $user_id);
- // 添加redis记录做财富排行榜周榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $giftValue, $user_id);
- // 添加redis记录做财富排行榜月榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $giftValue, $user_id);
- // 添加redis记录做贡献排行榜日榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
- // 添加redis记录做贡献排行榜周榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
- // 添加redis记录做贡献排行榜月榜用
- $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
- // tcp 更新用户魅力值
- $this->updateUserCharm($party_id, $user_id, $giftValue);
- }
- $allVal = $allVal + $giftValue;
- }
- }
- // 获取用户魅力值
- $users = $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($party_id, $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);
- }
- // tcp 获取房间用户周前三名
- $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
- if($i == $userCount) {
- $returnData["userCharm"] = $userCharm;
- $returnData["partyHot"] = $this->changeW($partyHot);
- $returnData["partyUserTop"] = $partyUserTop;
- $returnData["image"] = one_domain_image($giftInfo["image"]);
- $returnData["gif_image"] = one_domain_image($giftInfo["special"]);
- 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());
- }
- }
- /**
- * 随机获取礼物盒礼物(复数版)
- * @params $gift_box_type 奖池id
- * @params $userCount 用户数量
- * @params $number 礼物数量
- */
- private function getBoxesGift($gift_box_type,$userCount,$number,&$oldGiftArr=[]) {
- // 查询本奖池内礼物是否还有可抽礼物
- $where = [];
- $where["Jackpot_id"] = $gift_box_type;
- $where["is_use"] = 0;
- $limit = $userCount*$number;
- $boxhasgift = \app\common\model\GiftBox::where($where)->orderRaw('rand()')->limit($limit)->select();
- $giftcount = count($boxhasgift);
- $giftArr = [];
- foreach($boxhasgift as $k => $v) $giftArr[$v["id"]] = $v;
- unset($boxhasgift);
- foreach ($giftArr as $id => $v) {
- $list[] = ['id' => $id, 'is_use' => 1];
- }
- $oldGiftArr = array_merge($giftArr,$oldGiftArr);
- $is_update = TRUE;
- if($giftcount<$limit){
- $newNum = $limit - $giftcount;
- \app\common\model\GiftBox::update(["is_use"=>0],["Jackpot_id"=>$gift_box_type]);
- $this->getBoxesGift($gift_box_type,1,$newNum,$oldGiftArr);
- $is_update = FALSE;
- }
- if ($is_update==TRUE) {
- $gifBoxObject = new GiftBox();
- $gifBoxObject->isUpdate()->saveAll($list);
- }
- return $oldGiftArr;
- }
- /**
- * 用户声币余额变更数据(宝箱专供)
- */
- private function boxAddUserSoundcoinLog($user_id, $money, $mode, $before, $detail, $type = 1, $objId = 0)
- {
- if ($mode == "+") {
- $balance = $before + $money;
- } else {
- $balance = $before - $money;
- }
- // 添加当前用户钻石流水记录
- $data = [];
- $data["user_id"] = $user_id;
- $data['type'] = $type;
- $data['obj_id'] = $objId;
- $data["value"] = $money;
- $data["mode"] = $mode;
- $data["before"] = $before;
- $data["balance"] = $balance;
- $data["detail"] = $detail;
- $data["createtime"] = time();
- return $data;
- }
- /**
- * 增加抽点数据
- */
- private function addUserProfitLogList($user_id,$party_id,$getValue,$platValue,$guilderValue)
- {
- $data = [];
- $data["user_id"] = $user_id;
- $data["party_id"] = $party_id?$party_id:0;
- $data["gift_value"] = $getValue;
- $data["plat_value"] = $platValue;
- $data["guilder_value"] = $guilderValue;
- $data["createtime"] = time();
- return $data;
- }
- //每个玩家获得的礼物,对象处理成数组
- private function checkAllUserBoxGiftInfo(&$allUserBoxGiftInfo)
- {
- $giftNotice = config("site.giftNotice");
- foreach ($allUserBoxGiftInfo as $k => $v){
- $data = [];
- foreach ($v as $vv){
- $money = $vv['price'] / 100 ;
- if($money<$giftNotice){
- $vv['only_gif_image'] = '';
- }
- unset($vv['price']);
- $data[] = $vv;
- }
- $allUserBoxGiftInfo[$k] = $data;
- }
- }
- /**
- * 消费开通贵族
- * @param Redis $redis
- * @param $user_id
- * @param $giftCountValue
- * @return void
- */
- private function checkBeNoble(Redis $redis,$user_id,$giftCountValue)
- {
- $user_level = $this->auth->level;
- $user_noble = $this->auth->noble;
- $getNobleLevelList = $this->getNobleLevelList($redis);
- $getUserRenew = $this->changeUserRenew($redis,$user_id,$giftCountValue);
- $getNobleLevelList = array_reverse($getNobleLevelList);
- //总消费
- $renewcount = $this->auth->renewcount;
- $renewcount += $giftCountValue;
- //是否可升级
- $maxNoble = $getNobleLevelList[0]['id'];
- $updateNoble = FALSE;
- $userNobleLeve = 0;
- $nobleLeveRenew2 = 0;
- if($user_noble < $maxNoble) {
- foreach ($getNobleLevelList as $NobleLeve) {
- if ($getUserRenew >= $NobleLeve['renew2'] && //月消费 >= 爵位保级消费
- $user_level >= $NobleLeve['need_level'] && //玩家等级 > 爵位等级要求
- $user_noble < $NobleLeve['id'] && //玩家等级 < 升级的爵位等级
- $renewcount >= $NobleLeve['first2'] //总消费 >= 爵位低消
- ) {
- $updateNoble = TRUE;
- $userNobleLeve = $NobleLeve['id'];
- $nobleLeveRenew2 = $NobleLeve['renew2'];
- break;
- }
- }
- }
- if ($updateNoble===TRUE){
- // echo $NobleLeve['name'].PHP_EOL.$getUserRenew.PHP_EOL.$renewcount.PHP_EOL.$user_level.PHP_EOL.$user_noble;
- controller('api/Noble')->checkBeNoble2(config('token')['key'], $userNobleLeve);
- //月保级消费清空
- //溢出的积分算入保级消费
- $shengRenew = $getUserRenew-$nobleLeveRenew2;
- $redis->set('user_renew2_'.$user_id,$shengRenew);
- }
- }
- /**
- * 获取贵族等级列表
- * @param Redis $redis
- * @param bool $again 是否重新生成redis
- * @return bool|mixed|\PDOStatement|string|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function checkGetNobleLevelList($tokenKey='')
- {
- if ($tokenKey!==config('token')['key']) return;
- $redis = new \Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
- return $this->getNobleLevelList($redis);
- }
- private function getNobleLevelList(Redis $redis, bool $again=FALSE)
- {
- if ($again===TRUE) $redis->del('noble_level_list');
- $getNobleLevelList = $redis->get('noble_level_list');
- if ($getNobleLevelList){
- $getNobleLevelList = unserialize($getNobleLevelList);
- }else{
- // $getNobleLevelList = Db::name('noble_level')->where('is_show',1)
- $getNobleLevelList = \app\common\model\NobleLevel::where('is_show',1)
- // ->field('id,name,first2,renew2,need_level,level_no,tqgq,qftz,qfdh,gptz')
- ->select();
- $redis->set("noble_level_list", serialize($getNobleLevelList));
- }
- return $getNobleLevelList;
- }
- /**
- * 获取玩家月消费
- * @param Redis $redis
- * @param $user_id
- * @param $giftCountValue
- * @return false|mixed|string
- */
- private function changeUserRenew(Redis $redis, $user_id,$giftCountValue){
- $user_renew2 = $redis->get('user_renew2_'.$user_id);
- $giftCountValue = (int)$giftCountValue;
- if ($user_renew2){
- // $user_renew2 = $redis->incrBy('user_renew_'.$user_id,$giftCountValue);
- $user_renew2 = $redis->incrBy('user_renew2_'.$user_id,$giftCountValue);
- }else{
- //月消费(每30天清空)
- // $redis->set('user_renew_'.$user_id,$giftCountValue);
- //月保级消费(每30天清空||每次升级贵族清空)
- $redis->set('user_renew2_'.$user_id,$giftCountValue);
- $user_renew2 = $giftCountValue;
- }
- return $user_renew2;
- }
- // /**
- // * 播放动效
- // */
- // 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($party_id,$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 加入缓存排序
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
- // 更新redis 加入缓存
- $redPartyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]);
- if($redPartyInfo) {
- $redPartyInfo = json_decode($redPartyInfo,true);
- $redPartyInfo["party_hot"] = $party_hot_value;
- $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) {
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- // 获取本周第一天
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
- // 获取当天零点
- $day = date("Ymd");
- $userModel = new \app\common\model\User();
- // 获取条数
- $num = 3;
- // 获取3条财富排行周记录
- $getweek = $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"];
- }
- // 加入缓存做备份
- $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) {
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- // 获取用户魅力值
- $users = $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;
- }
- $redis->zAdd("hourCharm_".$party_id,$value,$user_id);
- return true;
- }
- /**
- * 单个房间魅力值清零
- */
- public function partyClearCharm() {
- $party_id = $this->request->request("party_id");// 派对ID
- $userId = $this->request->request("user_id", 0);// 用户ID
- if($party_id <= 0) {
- $this->error("请输入派对ID");
- }
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($userId) {
- $redis->zRem("hourCharm_" . $party_id, $userId);
- } else {
- $redis->del("hourCharm_" . $party_id);
- }
- $this->success("操作成功!");
- }
- /**
- * 获取音乐列表
- */
- public function getMusicList() {
- $this->success("获取成功!",\app\common\model\Music::select());
- }
- //============================定时任务==========================//
- /**
- * 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";
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- // 获取所有派对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 = $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 && $redis->zRemRangeByRank($key,0,-1);
- }
- }
- }
- }
- }
- public function handleParty() {
- return false;
- $partyList = \app\common\model\Party::where('is_online',1)->select();
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- // $redis->del("online_" . 3);die;
- // $redis->hSet("online_" . 3, 4, 4);die;
- foreach ($partyList as $party){
- $userId = $party['user_id'];
- $roomId = $party['id'];
- $redis->HDel("online_" . $roomId, $userId);
- // 扣除在线用户在房间情况
- $redis->hDel("livingUser",$userId);
- // // 更新房间在线状态
- $partyInfo = $redis->get("party_" . $roomId);
- if ($partyInfo) {
- $partyInfo = json_decode($partyInfo, true);
- $memCount = count($redis->hGetAll("online_" . $roomId));
- if ($memCount <= 0) {
- dump($roomId.'--1---');
- $partyInfo["is_online"] = 0;
- $redis->set("party_" . $roomId, json_encode($partyInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
- }
- }
- $liveInfo = $redis->get("live_" . $roomId);
- if ($liveInfo) {
- $liveInfo = json_decode($liveInfo, true);
- $memCount = count($redis->hGetAll("online_" . $roomId));
- if ($memCount <= 0) {
- dump($roomId.'---2--');
- $liveInfo["is_online"] = 0;
- $redis->set("live_" . $roomId, json_encode($liveInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
- }else{
- dump($roomId.'---3--');
- $redis->del("online_" . $roomId);
- $liveInfo["is_online"] = 0;
- $redis->set("live_" . $roomId, json_encode($liveInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
- }
- }
- }
- $this->success("获取成功!");
- }
- }
|