Party.php 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  1. <?php
  2. namespace app\api\controller;
  3. use addons\faqueue\library\QueueApi;
  4. use app\api\controller\Common;
  5. use app\common\model\GiftBox;
  6. use app\common\model\PartyJoin;
  7. use fast\Random;
  8. use app\common\controller\RedisLeaderboard;
  9. use Redis;
  10. use think\Db;
  11. use think\Exception;
  12. use think\exception\PDOException;
  13. use think\exception\ValidateException;
  14. use think\Request;
  15. /**
  16. * 派对信息接口
  17. */
  18. class Party extends Common
  19. {
  20. protected $noNeedLogin = ['updatePartyInfo', 'getPatyType', 'addUserPositionToParty', 'clearMoney', 'clearCharm', 'getPartyRankList', 'getDefaultBackground', 'getPartyGifType', 'getPartGifList', 'getPartHeadgifList', 'isNotalk', 'getMusicList', 'updateTops', 'handleParty'];
  21. protected $noNeedRight = ['*'];
  22. public function __construct(Request $request = null)
  23. {
  24. $this->roomTypeArr = [1=>"party",2=>"livebc"];
  25. parent::__construct($request);
  26. }
  27. /**
  28. * 创建/进入派对
  29. */
  30. public function createParty() {
  31. $room_type = $this->request->request('room_type',1); // 房间类型:1=派对,2=直播
  32. $party_name = $this->request->request('party_name',0);
  33. $party_type = $this->request->request('party_type',0); // 派对类型(情感互动,心动点单 等)语聊房的
  34. $cityname = input('cityname','');//直播城市
  35. $partyModel = new \app\common\model\Party();
  36. $user_id = $this->auth->id;
  37. //
  38. if($this->auth->idcard_status == -1){
  39. $this->error('请先申请实名认证');
  40. }
  41. if($this->auth->idcard_status == 0){
  42. $this->error('您已经提交实名认证,请等待审核');
  43. }
  44. if($this->auth->idcard_status == 2){
  45. $this->error('您的实名认证审核未通过,请重新申请');
  46. }
  47. // 判断派对是否存在
  48. $redis = new Redis();
  49. $redisconfig = config("redis");
  50. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  51. $where = [];
  52. $where["user_id"] = $user_id;
  53. $where["room_type"] = $room_type;
  54. $sqlPartyInfo = $partyModel->where($where)->find();
  55. if(!$sqlPartyInfo) { // sql中不存在派对信息
  56. $partylimit = config("site.roomLimit");
  57. $partycount = $partyModel->count("id");
  58. if($partycount >= $partylimit && $partylimit != -1) {
  59. $this->error("房间数量已达上线,请联系客服!");
  60. }
  61. $party_name = $party_name ? $party_name : $this->auth->username."的房间"; // 派对名称
  62. $party_logo = "/assets/api/party_logo.png"; // 派对logo
  63. $party_notice = "请编辑房间公告"; // 派对公告
  64. $party_notice_detail = "请编辑房间公告内容!"; // 派对公告详情
  65. $party_ids = $partyModel->column("party_id");
  66. // 创建派对ID (临时ID四位,派对数不超过8999)
  67. $party_id = $this->auth->getUinqueId(6,$party_ids);
  68. if($party_id > 999999) {
  69. $this->error("派对超限,请联系客服");
  70. }
  71. $data = [];
  72. $data["user_id"] = $this->auth->id;
  73. $data["room_type"] = $room_type;
  74. $data["party_id"] = $party_id;
  75. $data["party_hot"] = 0;
  76. $data["party_name"] = $party_name;
  77. $data["party_logo"] = $party_logo;
  78. $data["party_type"] = $room_type == 2 ? $party_type : 0;//目前直播需要,语聊不需要
  79. $data["party_notice"] = $party_notice;
  80. $data["party_notice_detail"] = $party_notice_detail;
  81. $data["is_online"] = 1;
  82. $data["status"] = 0;
  83. $data["is_recommend"] = 0;
  84. $data["createtime"] = time();
  85. $data["cityname"] = $cityname;
  86. $id = $partyModel->insertGetId($data);
  87. if(!$id) {
  88. $this->error("派对创建失败,请稍后重试!");
  89. }
  90. $data["id"] = $id;
  91. if($room_type == 2) {
  92. \app\common\model\User::update(["is_livebc"=>1],["id"=>$user_id]);
  93. }
  94. $partyInfo = $partyModel->where(["id"=>$id])->find();
  95. $partyInfo["is_new"] = 1;
  96. // 冠名
  97. $userInfo = [];
  98. if($partyInfo["naming"]) {
  99. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$partyInfo["naming"]])->find();
  100. }
  101. // 头像
  102. if($partyInfo["user_id"] > 0 && $room_type == 2) {
  103. $partyInfo["avatar"] = \app\common\model\User::where(["id"=>$partyInfo["user_id"]])->value("avatar");
  104. }
  105. // 派对类型
  106. $partyTypeName = "普通房";
  107. if($partyInfo["party_type"]) {
  108. $partyTypeName = \app\common\model\PartyType::where(["id"=>$partyInfo["party_type"]])->value("name");
  109. }
  110. $partyInfo["naming"] = $userInfo;
  111. $partyInfo["type_name"] = $partyTypeName;
  112. // 加入缓存排序
  113. $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  114. // 加入缓存
  115. $redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($partyInfo));
  116. if($room_type != 2) {
  117. // 创建公会
  118. \app\common\model\Guild::createGuild($partyInfo["id"], $partyInfo["party_name"], $user_id);
  119. \app\common\model\User::update(["is_guild" => 3], ["id" => $user_id]);
  120. }
  121. } else {
  122. if($sqlPartyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  123. if($sqlPartyInfo["status"] == 0){
  124. // 清楚对应的记录
  125. $redis->del($this->roomTypeArr[$room_type]."_".$sqlPartyInfo["id"]);
  126. // 删除派对记录
  127. \app\common\model\Party::where('id',$sqlPartyInfo["id"])->delete();
  128. $this->error("创建失败,请重试");
  129. }
  130. // if($sqlPartyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
  131. // 如果是房主并且房间处于离线状态下激活
  132. if ($sqlPartyInfo['user_id'] != $user_id) {
  133. $this->error("当前直播已结束!");
  134. }
  135. $partyInfo = $sqlPartyInfo;
  136. $partyInfo["is_new"] = 0;
  137. //修改类型
  138. //目前直播需要,语聊不需要
  139. if($room_type == 2){
  140. Db::name('party')->where('id',$partyInfo['id'])->update(['party_type'=>$party_type]);
  141. $partyInfo["party_type"] = $party_type;
  142. }
  143. }
  144. if($room_type == 2){
  145. //清空房间排行榜
  146. $redis->del('livebc_jewel_to_' . $partyInfo['id']);
  147. $redis->del('livebc_jewel_get_' . $partyInfo['id']);
  148. $redis->hDel("user_jewel_top3",$partyInfo['id']);
  149. }
  150. // 获取用户魅力值
  151. $users = $redis->zRange("hourCharm_".$partyInfo->id,0,-1,true);
  152. $u = [];
  153. if($users) {
  154. foreach($users as $k => $v) $u[] = [
  155. "user_id"=>$k,
  156. "charm"=>$this->changeW($v)
  157. ];
  158. }
  159. $partyInfo["userCharm"] = $u;
  160. $this->success("获取成功!",$partyInfo);
  161. }
  162. /**
  163. * 更新房间状态为正常
  164. */
  165. public function savePartyStatus() {
  166. $party_id = $this->request->request('party_id'); // 派对ID
  167. $room_type = $this->request->request('room_type',1);; // 房间类型:1=派对,2=直播
  168. if (!$party_id || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
  169. $user_id = $this->auth->id;
  170. $partyInfo = \app\common\model\Party::get($party_id);
  171. if($partyInfo->status < 0) $this->error("当前状态不支持变更!");
  172. if($partyInfo->user_id !== $user_id) $this->error("当前账号异常,非房主禁止操作!");
  173. /*$partyInfo->status = 1;
  174. $partyInfo->updatetime = time();*/
  175. $res = Db::name('party')->where('id',$party_id)->update(['status'=>1,'updatetime'=>time()]);
  176. if($res !== false) {
  177. // 更新redis 加入缓存排序
  178. $redis = new Redis();
  179. $redisconfig = config("redis");
  180. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  181. // 更新redis 加入缓存
  182. $redPartyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]);
  183. if($redPartyInfo) {
  184. $redPartyInfo = json_decode($redPartyInfo,true);
  185. $redPartyInfo["status"] = 1;
  186. $redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($redPartyInfo));
  187. }
  188. $this->success("更新房间状态成功!");
  189. } else {
  190. $this->error("网络错误,请稍后重试!");
  191. }
  192. }
  193. /**
  194. * 删除派对(群解散)
  195. * 前端没用到
  196. */
  197. public function closeParty() {
  198. $party_id = $this->request->request('party_id'); // 派对ID
  199. $room_type = $this->request->request('room_type',1); // 房间类型:1=派对,2=直播
  200. if (!$party_id || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
  201. $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
  202. if(!$partyInfo) $this->error(__('派对不存在!'));
  203. $res = $partyInfo->delete();
  204. if($res !== false) {
  205. // redis 删除
  206. $redis = new Redis();
  207. $redisconfig = config("redis");
  208. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  209. $redis->zRem($this->roomTypeArr[$room_type]."Rank",$party_id);
  210. $redis->del($this->roomTypeArr[$room_type]."_".$party_id);
  211. $this->success("删除成功!");
  212. } else {
  213. $this->error("删除失败!");
  214. }
  215. }
  216. /**
  217. * 获取派对列表排序
  218. */
  219. public function getPartyRankList() {
  220. $thispage = $this->request->request('thispage',1,"intval"); // 当前页数
  221. $pagenum = $this->request->request('pagenum',10,"intval"); // 每页显示条数0=不做分页
  222. $type_id = $this->request->request('type_id'); // 派对类型
  223. $room_type = 1; // 房间类型:1=派对,2=直播
  224. $is_recommend = $this->request->request('is_recommend'); // 推荐0=否1=是
  225. $all = $this->request->request('all'); // 全部分类0=否1=是
  226. $index = $this->request->request('index',0); // 全部分类0=否1=是
  227. //更新ip,设备id
  228. //非原创,注释
  229. //if($is_recommend == 1) controller('api/User')->changeDeviceIp();
  230. $start = ($thispage-1)*$pagenum;
  231. $end = $start+($pagenum-1);
  232. // 获取排序
  233. $redis = new Redis();
  234. $redisconfig = config("redis");
  235. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  236. $redisPartyRankList = $redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
  237. $partyModel = new \app\common\model\Party();
  238. if(!$redisPartyRankList) {
  239. $userModel = new \app\common\model\User();
  240. // 直接从数据库获取所有数据
  241. $where = [];
  242. $where["a.status"] = 1;
  243. $where["a.is_online"] = 1;
  244. $where["a.room_type"] = $room_type;
  245. $sqlPartyList = $partyModel->alias("a")->field("a.*,b.name as type_name")
  246. ->join("party_type b","a.party_type = b.id","left")->where($where)->select();
  247. $userList = $userModel->field("id,nickname,avatar")->where(["status"=>1])->select();
  248. $userInfoArr = [];
  249. if($userList) foreach($userList as $k => $v) $userInfoArr[$v["id"]] = $v;
  250. if($sqlPartyList) {
  251. foreach($sqlPartyList as $k => $v) {
  252. // 加入缓存排序
  253. $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $v['party_hot'], $v["id"]);
  254. // 设置冠名
  255. $sqlPartyList[$k]["naming"] = isset($userInfoArr[$v["naming"]])?$userInfoArr[$v["naming"]]:[];
  256. // 设置房主头像
  257. $sqlPartyList[$k]["avatar"] = isset($userInfoArr[$v["user_id"]])?$userInfoArr[$v["user_id"]]["avatar"]:[];
  258. // 加入缓存
  259. $redis->set($this->roomTypeArr[$room_type]."_".$v["id"],json_encode($v));
  260. }
  261. $redisPartyRankList = $redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
  262. }
  263. }
  264. if($redisPartyRankList) {
  265. $resultInfo = $partyModel->getPatyInfoByPartyId($redisPartyRankList,$this->roomTypeArr[$room_type],$type_id,$is_recommend,$all,$start,$end,$index);
  266. $this->success("获取成功!",$resultInfo);
  267. }else{
  268. $this->success("获取成功!",[]);
  269. }
  270. }
  271. /**
  272. * 获取派对用户排序
  273. */
  274. public function getPartyUserRank() {
  275. $room_type = 1;
  276. $party_id = $this->request->request("party_id");// 派对ID
  277. if (!$party_id) $this->error(__('Invalid parameters'));
  278. $redis = new Redis();
  279. $redisconfig = config("redis");
  280. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  281. $res = [];
  282. // 获取今天
  283. $day = date("Ymd");
  284. // 获取本周第一天
  285. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  286. // 获取本月第一天
  287. $monthday = date("Ym01");
  288. $userModel = new \app\common\model\User();
  289. // 获取条数
  290. $num = 50;
  291. // 获取50条财富排行日记录
  292. $getday = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$day."d",0,$num-1,true);
  293. $res['getRankListDay'] = $userModel->rankList($getday);
  294. // 获取50条财富排行日记录
  295. $today = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true);
  296. $res['toRankListDay'] = $userModel->rankList($today);
  297. // 获取50条财富排行周记录
  298. $getweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$weekday."w",0,$num-1,true);
  299. $res['getRankListWeek'] = $userModel->rankList($getweek);
  300. // 获取50条贡献排行周记录
  301. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$weekday."w",0,$num-1,true);
  302. $res['toRankListWeek'] = $userModel->rankList($toweek);
  303. // 获取50条财富排行月记录
  304. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$monthday."m",0,$num-1,true);
  305. $res['getRankListMonth'] = $userModel->rankList($toweek);
  306. // 获取50条贡献排行周记录
  307. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$monthday."m",0,$num-1,true);
  308. $res['toRankListMonth'] = $userModel->rankList($toweek);
  309. return $this->success("获取成功!",$res);
  310. }
  311. /**
  312. * 派对热度更新(已废弃)
  313. */
  314. public function changeUserPartyhot() {
  315. $party_id = $this->request->request('party_id',0,"intval"); // 派对ID
  316. $room_type = 1; // 房间类型
  317. $party_hot = $this->request->request('party_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值)
  318. if (!$party_id || !$party_hot) {
  319. $this->error(__('Invalid parameters'));
  320. }
  321. $redis = new Redis();
  322. $redisconfig = config("redis");
  323. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  324. // 更新热度
  325. $redis->zIncrBy($this->roomTypeArr[$room_type]."Rank", $party_hot, $party_id);
  326. return $this->success("更新成功!");
  327. }
  328. /**
  329. * 更新主持人和麦位前四位至首页房间排行
  330. */
  331. public function addUserPositionToParty() {
  332. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  333. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
  334. $upOrdown = $this->request->request('upordown'); // 上下麦:1=上麦-1=下麦
  335. $room_type = 1; // 房间类型
  336. $avatar = $this->request->request('avatar'); // 用户头像 (upOrdown参数为-1时可以不传)
  337. $position = $this->request->request('position'); // 麦位置:0=支持人,1=1号麦,2=2号麦,3=3号麦,4=4号麦
  338. if (!$party_id || !$user_id || !in_array($position,[0,1,2,3,4,5,6,7,8]) || !in_array($upOrdown,[1,-1])) {
  339. $this->error(__('Invalid parameters'));
  340. }
  341. if($upOrdown == 1) {
  342. // 先完成所有麦上记录
  343. \app\common\model\UserOnsiteTime::update(["status"=>2],["user_id"=>$user_id]);
  344. // 保存上麦记录
  345. $data = [];
  346. $data["user_id"] = $user_id;
  347. $data["party_id"] = $party_id;
  348. $data["onsite_time"] = time();
  349. \app\common\model\UserOnsiteTime::insert($data);
  350. }
  351. if($upOrdown == -1) {
  352. // 更新下麦时间
  353. $update = [];
  354. $update["offsite_time"] = time();
  355. $update["status"] = 2;
  356. \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
  357. }
  358. if(in_array($position,[5,6,7,8])) return $this->success("设置成功!");
  359. $redis = new Redis();
  360. $redisconfig = config("redis");
  361. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  362. $redisData = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  363. if($redisData) {
  364. $partyInfo = json_decode($redisData,true);
  365. if($upOrdown == 1) {
  366. // 删掉已有头像,防止重复
  367. $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
  368. if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $avatar) unset($partyInfo["party_user"][$k]);
  369. $partyInfo["party_user"][$position] = $avatar;
  370. } else {
  371. if(isset($partyInfo["party_user"])) unset($partyInfo["party_user"][$position]);
  372. }
  373. // sort($partyInfo["party_user"]);
  374. $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  375. }
  376. return $this->success("设置成功!");
  377. }
  378. /**
  379. * 获取派对在线人数列表
  380. */
  381. public function getOnlieList() {
  382. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
  383. $page = $this->request->request('page',1); // 分页
  384. $pageNum = $this->request->request('pageNum',10); // 分页
  385. // 分页搜索构建
  386. $pageStart = ($page-1)*$pageNum;
  387. if (!$party_id) {
  388. $this->error(__('Invalid parameters'));
  389. }
  390. $redis = new Redis();
  391. $redisconfig = config("redis");
  392. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  393. $res = $redis->hGetAll("online_".$party_id);
  394. $user_ids = [];$userList = [];
  395. if($res) $user_ids = array_values($res);
  396. // 获取用户列表信息
  397. $user_ids && $userList = \app\common\model\User::field("id,avatar,nickname,level,gender")->where(["id"=>["in",$user_ids]])->limit($pageStart,$pageNum)->select();
  398. $this->success("获取成功!",$userList);
  399. }
  400. /** 连续进房提示
  401. * @param $user_id
  402. * @param $party_id
  403. * @param $redis
  404. * @return int
  405. */
  406. private function getUserJoinPartyDays($user_id, $party_id,$redis)
  407. {
  408. $days = 1;
  409. $joinPartyDays = $redis->hGet("joinParty_days",$user_id."-".$party_id);
  410. if ($joinPartyDays){
  411. $joinPartyDaysArr = explode('-',$joinPartyDays);
  412. $oldTime = (int)$joinPartyDaysArr[0];
  413. $todayStartTime = strtotime(date('Y-m-d'));//今天0点
  414. $yesterdayStartTime = strtotime(date("Y-m-d",strtotime("-1 day")));//昨天开始时间戳
  415. $todayEndTime = $todayStartTime+24 * 60 * 60-1;//今天结束时间戳
  416. //昨天登录过,+1
  417. if ($yesterdayStartTime <= $oldTime && $oldTime < $todayStartTime){
  418. $days = (int)$joinPartyDaysArr[1];
  419. $days++;
  420. $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-'. $days);
  421. }elseif($todayStartTime <= $oldTime && $oldTime <= $todayEndTime){
  422. //今天登录过 days
  423. $days = (int)$joinPartyDaysArr[1];
  424. } else {
  425. $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
  426. }
  427. }else{
  428. $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
  429. }
  430. return $days;
  431. }
  432. /**
  433. * 加入派对
  434. */
  435. public function joinParty() {
  436. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
  437. $room_type = $this->request->request('room_type',1); // 房间类型:1=派对2=直播
  438. $party_pass = $this->request->request("party_pass");//
  439. if (!$party_id || !in_array($room_type,[1,2])) {
  440. $this->error(__('Invalid parameters'));
  441. }
  442. $user_id = $this->auth->id;
  443. $redis = new Redis();
  444. $redisconfig = config("redis");
  445. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  446. $redis->zAdd("party_user_".$party_id,$this->auth->username,$user_id);
  447. // 判断当前用户是否存在在直播间通过切换过来的
  448. $livingUserPartyId = $redis->hGet("livingUser", $user_id);
  449. if ($livingUserPartyId) {
  450. if($livingUserPartyId != $party_id){
  451. // 扣除房间在线人数
  452. $redis->HDel("online_" . $livingUserPartyId, $user_id);
  453. // 扣除在线用户在房间情况
  454. $redis->hDel("livingUser", $user_id);
  455. }
  456. }
  457. // 记录在线用户在房间情况
  458. $redis->hSet("livingUser", $user_id, $party_id);
  459. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  460. $lsetList = [];
  461. if($room_type == 1){
  462. for($i=1;$i<=4;$i++) {
  463. $hgetlist = $redis->hGet("party_manage_".$party_id,$user_id."-".$i);
  464. $hgetlist = unserialize($hgetlist);
  465. $restime = time() - intval($hgetlist["createtime"]);
  466. // 房管
  467. if($hgetlist && $i == 1) $lsetList["manage_restime"] = "";
  468. // 禁言
  469. if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime;
  470. // 拉黑
  471. if($hgetlist && $i == 3 && $restime < $hgetlist["time"]) {
  472. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  473. $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
  474. break;
  475. }
  476. // 踢出
  477. if($hgetlist && $i == 4 && $restime < $hgetlist["time"]) {
  478. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  479. $this->error(__('您已被该房间踢出,解除时间:'.$restime));
  480. break;
  481. }
  482. }
  483. }
  484. // 判断派对密码
  485. $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  486. if($partyInfo) {
  487. $partyInfo = json_decode($partyInfo,true);
  488. if($partyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  489. // if($partyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
  490. if(isset($partyInfo["party_pass"]) && $partyInfo["party_pass"] && $partyInfo["user_id"] != $user_id && $room_type == 1) {
  491. if(($party_pass != $partyInfo["party_pass"] || strlen($party_pass) != 4)) {
  492. $this->error("派对密码不正确!");
  493. }
  494. }
  495. // 公会管理员和工会长激活派对厅
  496. /*$guildInfo = \app\common\model\Guild::where(['user_id' => $partyInfo['user_id'], 'status' => 1])->find();
  497. if ($guildInfo) {
  498. // 获取公会管理员
  499. $guildMemberInfo = \app\common\model\GuildMember::where(["user_id" => $user_id, "guild_id" => $guildInfo['id'], 'status' => 1])->find();
  500. if ($guildMemberInfo && $guildMemberInfo['role'] == 0 && $partyInfo['is_online'] == 0) {
  501. $this->error("当前直播已结束!");
  502. }
  503. }*/
  504. // // 如果是房主自己进入房间,则更新用户 为在线状态
  505. // if ($partyInfo["user_id"] == $user_id) {
  506. // if ($room_type == 2)
  507. // \app\common\model\User::update(["is_livebc" => 1], ["id" => $partyInfo["user_id"]]);
  508. // }
  509. } else {
  510. $this->error("派对信息获取失败!");
  511. }
  512. // 获取用户魅力值
  513. $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
  514. $u = [];
  515. if($users) {
  516. foreach($users as $k => $v) $u[] = [
  517. "user_id"=>$k,
  518. "charm"=>$this->changeW($v)
  519. ];
  520. }
  521. $lsetList["userCharm"] = $u;
  522. // 获取用户排行榜前三名头像
  523. $heads = $redis->hGet("user_jewel_top3",$party_id);
  524. $lsetList["userJewelTop3"] = $heads?json_decode($heads,true):[];
  525. // 判断当前用户是否收藏了此房间
  526. $cellection = $redis->hGet("room_cellection",$user_id."-".$party_id);
  527. $lsetList["is_cellection"] = $cellection?1:0;
  528. if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  529. // $partyInfo["type_name"] = $partyInfo["type_name"];
  530. } else {
  531. $partyInfo["type_name"] = "普通房";
  532. }
  533. $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]);
  534. $partyInfo['is_new'] = $partyInfo["status"] == 0 ? 1 : 0;
  535. $partyInfo['join_days'] = $this->getUserJoinPartyDays($user_id,$party_id,$redis);
  536. $lsetList["partyInfo"] = $partyInfo;
  537. return $this->success("加入成功!",$lsetList);
  538. }
  539. /**
  540. * 判断是否被禁言
  541. */
  542. public function isNotalk() {
  543. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
  544. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID 逻辑ID
  545. if (!$user_id || !$party_id) {
  546. $this->error(__('Invalid parameters'));
  547. }
  548. $redis = new Redis();
  549. $redisconfig = config("redis");
  550. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  551. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  552. $lsetList = [];
  553. $lsetList["is_notalk"] = 0;
  554. $lsetList["notalk_time"] = 0;
  555. $hgetlist = $redis->hGet("party_manage_".$party_id,$user_id."-2");
  556. $hgetlist = unserialize($hgetlist);
  557. $restime = time() - intval($hgetlist["createtime"]);
  558. // 禁言
  559. if($hgetlist && $restime < $hgetlist["time"]) {
  560. $lsetList["is_notalk"] = 1;
  561. $lsetList["notalk_time"] = $restime;
  562. }
  563. return $this->success("获取成功!",$lsetList);
  564. }
  565. /**
  566. * 退出派对
  567. * 前端没用到,转移到trtc_callback了
  568. */
  569. public function outParty() {
  570. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  571. if (!$party_id) {
  572. $this->error(__('Invalid parameters'));
  573. }
  574. $user_username = $this->auth->username;
  575. $redis = new Redis();
  576. $redisconfig = config("redis");
  577. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  578. $redis->zRem("party_user_".$party_id,$user_username);
  579. // 处理online_party_id
  580. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  581. $redis->HDel("online_" . $party_id, $this->auth->id);
  582. // 扣除在线用户在房间情况
  583. $redis->hDel("livingUser", $this->auth->id);
  584. // 更新房间在线状态
  585. $partyInfo = $redis->get("party_" . $party_id);
  586. if ($partyInfo) {
  587. $partyInfo = json_decode($partyInfo, true);
  588. $memCount = count($redis->hGetAll("online_" . $party_id));
  589. if ($memCount <= 0) {
  590. $partyInfo["is_online"] = 0;
  591. $redis->set("party_" . $party_id, json_encode($partyInfo));
  592. \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
  593. }
  594. }
  595. $liveInfo = $redis->get("livebc_" . $party_id);
  596. if ($liveInfo) {
  597. $liveInfo = json_decode($liveInfo, true);
  598. $liveInfo["is_online"] = 0;
  599. $redis->set("livebc_" . $party_id, json_encode($liveInfo));
  600. \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
  601. $rs_user = Db::name('user')->where('id',$this->auth->id)->update(['is_livebc'=>0]);
  602. //准备返回数据,刷礼物的总值,刷礼物人数,榜一信息
  603. //清空房间排行榜
  604. $redis->del('livebc_jewel_to_' . $party_id);
  605. $redis->hDel("user_jewel_top3",$party_id);
  606. }
  607. return $this->success("退出成功!",[]);
  608. }
  609. /**
  610. * 派对内搜索用户
  611. */
  612. public function searchUserParty() {
  613. $username = $this->request->request('username',0,"intval"); // 用户
  614. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  615. $userModel = new \app\common\model\User();
  616. $where = [];
  617. $where["username"] = $username;
  618. $userInfo = $userModel->field("id,username,avatar,nickname,level,gender")->where($where)->select();
  619. return $this->success("查询成功!",$userInfo);
  620. }
  621. /**
  622. * 设置冠名
  623. */
  624. public function setNaming() {
  625. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  626. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
  627. $type = $this->request->request('type',1); // 1:设置 0:取消设置
  628. if (!$party_id || !$user_id || ($type != 1 && $type != 0)) {
  629. $this->error(__('Invalid parameters'));
  630. }
  631. $redis = new Redis();
  632. $redisconfig = config("redis");
  633. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  634. $partyModel = new \app\common\model\Party();
  635. $data = [];
  636. $where = [];
  637. $where["id"] = $party_id;
  638. $partyInfo = $partyModel->where(["id"=>$party_id])->find();
  639. $getredisPartyInfo = $redis->get($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id);
  640. $redisPartyInfo = json_decode($getredisPartyInfo,true);
  641. if($type == 1) {
  642. $data["naming"] = $user_id;
  643. // 冠名
  644. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$user_id])->find($user_id);
  645. $redisPartyInfo["naming"] = $userInfo;
  646. } else {
  647. $data["naming"] = 0;
  648. $redisPartyInfo["naming"] = [];
  649. }
  650. $redis->set($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id,json_encode($redisPartyInfo));
  651. $res = $partyModel->update($data,$where);
  652. if($res !== false) {
  653. $this->success("操作成功!");
  654. } else {
  655. $this->error("网络错误,请稍后重试!");
  656. }
  657. }
  658. /**
  659. * 派对收藏
  660. */
  661. public function cellectionParty() {
  662. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  663. if (!$party_id) {
  664. $this->error(__('Invalid parameters'));
  665. }
  666. $user_id = $this->auth->id;
  667. $redis = new Redis();
  668. $redisconfig = config("redis");
  669. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  670. $partycellectionModel = new \app\common\model\PartyCellection();
  671. // 添加记录
  672. $data = [];
  673. $data["user_id"] = $user_id;
  674. $data["party_id"] = $party_id;
  675. if($partycellectionModel->where($data)->find()) {
  676. $res = $partycellectionModel->where($data)->delete();
  677. $redis->hDel("room_cellection",$user_id."-".$party_id);
  678. return $this->success("取消收藏成功!",$res);
  679. }
  680. $data["createtime"] = time();
  681. $res = $partycellectionModel->insert($data);
  682. $redis->hSet("room_cellection",$user_id."-".$party_id,1);
  683. return $this->success("收藏成功!",$res);
  684. }
  685. /**
  686. * 派对收藏列表
  687. */
  688. public function cellectionPartyList() {
  689. $page = $this->request->request('page',1); // 分页
  690. $pageNum = $this->request->request('pageNum',10); // 分页
  691. // 分页搜索构建
  692. $pageStart = ($page-1)*$pageNum;
  693. $partycellectionModel = new \app\common\model\PartyCellection();
  694. $userModel = new \app\common\model\User();
  695. $where = [];
  696. $where["a.user_id"] = $this->auth->id;
  697. $where["r.room_type"] = 1;
  698. $list = $partycellectionModel->alias("a")
  699. ->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")
  700. ->where($where)
  701. ->join("party r","a.party_id = r.id")
  702. ->join("party_type t","t.id = r.party_type","left")
  703. ->limit($pageStart,$pageNum)
  704. ->select();
  705. if($list) {
  706. // $redis = new Redis();
  707. // $redisconfig = config("redis");
  708. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  709. // // 获取本周第一天
  710. // $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  711. // // 获取redis 中 用户排行榜前五名
  712. // foreach($list as $k => $v) {
  713. // $getweek = $redis->zRevRange("party_jewel_get_".$v["r_id"].":".$weekday,0,4,true);
  714. // $userlist = $userModel->rankList($getweek);
  715. // if($userlist) {
  716. // $users = [];
  717. // foreach($userlist as $m => $n) {
  718. // $users[] = $n["avatar"];
  719. // }
  720. // $list[$k]["users"] = $users;
  721. // } else {
  722. // $list[$k]["users"] = [];
  723. // }
  724. //
  725. // $mod = isset($v["party_type_id"])?intval($v["party_type_id"])%5:1;
  726. // $list[$k]["party_type_color"] = $mod == 0?5:$mod;
  727. // }
  728. $users = [];
  729. foreach($list as $k => $v) {
  730. $users[$v["party_id"]] = $v["party_hot"];
  731. }
  732. $partyModel = new \app\common\model\Party();
  733. $resultInfo = $partyModel->getPatyInfoByPartyId($users,"party",0,0,1,0,20,0);
  734. $this->success("获取成功!",$resultInfo);
  735. }
  736. return $this->success("获取成功!",$list);
  737. }
  738. /**
  739. * 获取派对信息
  740. */
  741. public function getPartyInfo() {
  742. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  743. if (!$party_id) {
  744. $this->error(__('Invalid parameters'));
  745. }
  746. $user_id = $this->auth->id;
  747. $partyModel = new \app\common\model\Party();
  748. $partycellectionModel = new \app\common\model\PartyCellection();
  749. $userModel = new \app\common\model\User();
  750. // 获取主体信息
  751. $where = [];
  752. $where["a.id"] = $party_id;
  753. $partyInfo = $partyModel->alias("a")
  754. ->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")
  755. ->join("party_type rt","rt.id = a.party_type", "left")
  756. ->where($where)
  757. ->find();
  758. if($partyInfo) {
  759. $mod = isset($partyInfo["party_type"])?intval($partyInfo["party_type"])%5:1;
  760. $partyInfo["party_type_color"] = $mod == 0?5:$mod;
  761. if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  762. // $partyInfo["type_name"] = $partyInfo["type_name"];
  763. } else {
  764. $partyInfo["type_name"] = "普通房";
  765. }
  766. // 获取是否被当前用户收藏
  767. $partyInfo["is_sellection"] = 0;
  768. $where = [];
  769. $where["user_id"] = $user_id;
  770. $where["party_id"] = $party_id;
  771. if($partycellectionModel->where($where)->find()) {
  772. $partyInfo["is_sellection"] = 1;
  773. }
  774. // 获取房主信息
  775. $where = [];
  776. $where["id"] = $partyInfo["user_id"];
  777. $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
  778. // 获取技能信息
  779. /*$skillList = Model("ViewUserSkill")->getSkillInfo($partyInfo["user_id"]);
  780. $userInfo["skill"] = implode("/",$skillList);*/
  781. $userInfo["skill"] = [];
  782. $partyInfo["userInfo"] = $userInfo;
  783. $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]);
  784. }
  785. $this->success("获取成功!",$partyInfo);
  786. }
  787. /**
  788. * 派对设置
  789. */
  790. public function setParty() {
  791. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  792. $party_name = $this->request->request('party_name'); // 派对名称
  793. $party_logo = $this->request->request('party_logo'); // 派对logo/封面
  794. $party_pass = $this->request->request('party_pass'); // 派对密码
  795. $party_type = $this->request->request('party_type'); // 使用场景
  796. $is_screen = $this->request->request('is_screen'); // 是否关闭公屏:1=是,0=否
  797. $on_model = $this->request->request('on_model'); // 上麦模式:1=自由模式,2=麦序模式
  798. $room_type = 1; // 房间类型:1=派对,2=直播
  799. $background = $this->request->request('background'); // 派对背景
  800. if (!$party_id || (!$party_name && !$party_logo && !$party_pass && !$party_type && !$is_screen && !$on_model && !$background)) {
  801. $this->error(__('Invalid parameters'));
  802. }
  803. if($party_pass && strlen($party_pass) != 4) {
  804. $this->error("房间密码必须为四位!");
  805. }
  806. $partyModel = new \app\common\model\Party();
  807. $data = [];
  808. $party_name && $data["party_name"] = $party_name;
  809. $party_logo && $data["party_logo"] = $party_logo;
  810. $data["party_pass"] = $party_pass;
  811. $party_type && $data["party_type"] = $party_type;
  812. $is_screen && $data["is_screen"] = $is_screen;
  813. $on_model && $data["on_model"] = $on_model;
  814. $background && $data["background"] = $background;
  815. $where = [];
  816. $where["id"] = $party_id;
  817. $res = $partyModel->update($data,$where);
  818. if($res) {
  819. // 获取派对类型
  820. if($party_type) $data["type_name"] = \app\common\model\PartyType::where(["id"=>$party_type])->value("name");
  821. // 存redis 房间信息
  822. $redis = new Redis();
  823. $redisconfig = config("redis");
  824. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  825. $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  826. if($partyInfo) {
  827. $partyInfo = json_decode($partyInfo,true);
  828. $partyInfo = array_replace($partyInfo,$data);
  829. $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  830. }
  831. $this->success("房间设置成功!",$data);
  832. } else {
  833. $this->error("网络错误,请稍后重试!");
  834. }
  835. }
  836. /**
  837. * 获取派对背景
  838. */
  839. public function getDefaultBackground() {
  840. $list = Db::name('party_background')->select();
  841. $list = list_domain_image($list,['image']);
  842. $this->success("success",$list);
  843. }
  844. //腾讯im设置管理员
  845. private function setGroupAdmin($user_id,$party_id,$status)
  846. {
  847. $tenIm = new Tenim();
  848. return $tenIm->setUpIMAdmin($user_id, $party_id,$status);
  849. }
  850. /**
  851. * 派对管理设置
  852. */
  853. public function partyManageSet() {
  854. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  855. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
  856. $item = $this->request->request('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  857. $time = $this->request->request('time'); // 限制时间(单位:秒):0=永久
  858. if (!$party_id || !$user_id || !in_array($item,[1,2,3,4])) {
  859. $this->error(__('Invalid parameters'));
  860. }
  861. if($item>1 && $time<=0) {
  862. $this->error(__('时间设置有误'));
  863. }
  864. $partyUserId = \app\common\model\Party::where('id', $party_id)->value('user_id');
  865. if ($partyUserId == $user_id && in_array($item, [2, 3, 4])) {
  866. $this->error("您当前无权限操作!");
  867. }
  868. // 获取用户信息
  869. $userInfo = \app\common\model\User::field("noble,avatar,nickname,gender,level")->where(["id"=>$user_id])->find();
  870. if(!$userInfo) $this->error("用户信息获取失败!");
  871. // 国王防踢。
  872. /*$noble_no = \app\common\model\NobleLevel::where(["id"=>$userInfo['noble']])->value("level_no");
  873. if(($item == 3 || $item == 4) && $noble_no == "p08PCcNB") {
  874. $this->error("对方已开通国王贵族,踢出房间失败!");
  875. }*/
  876. $redis = new Redis();
  877. $redisconfig = config("redis");
  878. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  879. $data = [];
  880. $data["user_id"] = $user_id;
  881. $data["avatar"] = $userInfo->avatar;
  882. $data["nickname"] = $userInfo->nickname;
  883. $data["gender"] = $userInfo->gender;
  884. $data["level"] = $userInfo->level;
  885. $data["item"] = $item;
  886. $data["time"] = $time;
  887. $data["createtime"] = time();
  888. $res = $redis->hSet("party_manage_".$party_id,$user_id."-".$item,serialize($data));
  889. if ($item==1) {
  890. $this->setGroupAdmin($user_id,$party_id,1);
  891. }
  892. if($res !== false) {
  893. $this->success("设置成功!",$data);
  894. } else {
  895. $this->error("网络错误,请稍后重试!");
  896. }
  897. }
  898. /**
  899. * 派对管理设置列表
  900. */
  901. public function partyManageSetList() {
  902. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  903. $item = $this->request->request('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  904. if (!$party_id || !in_array($item,[1,2,3,4])) {
  905. $this->error(__('Invalid parameters'));
  906. }
  907. $time = time();
  908. $redis = new Redis();
  909. $redisconfig = config("redis");
  910. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  911. $hget=$redis->hGetAll("party_manage_".$party_id);
  912. $list=array();
  913. foreach($hget as $key=>$val) {
  914. if(substr($key,-2) == "-".$item){
  915. array_push($list,unserialize($val));
  916. }
  917. }
  918. if($list) {
  919. foreach($list as $k => $v) if(bcadd($v["createtime"],$v["time"]) <= $time && $v["item"] > 1) unset($list[$k]);
  920. if($list) $list = array_values($list);
  921. }
  922. $this->success("获取成功!",$list);
  923. }
  924. /**
  925. * 派对管理设置移除
  926. */
  927. public function partyManageSetDel() {
  928. $id = $this->request->request('user_id',0,"intval"); // userid
  929. $party_id = $this->request->request('party_id',0,"intval"); // 房间ID
  930. $item = $this->request->request('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  931. if (!$id || !in_array($item,[1,2,3,4])) {
  932. $this->error(__('Invalid parameters'));
  933. }
  934. $redis = new Redis();
  935. $redisconfig = config("redis");
  936. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  937. $res = $redis->hDel("party_manage_".$party_id,$id."-".$item);
  938. if ($item==1) {
  939. $this->setGroupAdmin($id,$party_id,0);
  940. }
  941. if($res !== false) {
  942. $this->success("移除成功!",$res);
  943. } else {
  944. $this->error("网络错误,请稍后重试!");
  945. }
  946. }
  947. /**
  948. * 更新派对公告
  949. */
  950. public function savePartyNotice() {
  951. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  952. $room_type = $this->request->request('room_type',1); // 房间类型:1=派对2=直播
  953. $party_notice = $this->request->request('party_notice'); // 公告标题
  954. $party_notice_detail = $this->request->request('party_notice_detail'); // 公告内容
  955. if (!$party_id || !$party_notice || !$party_notice_detail) {
  956. $this->error(__('Invalid parameters'));
  957. }
  958. $partyModel = new \app\common\model\Party();
  959. $where = [];
  960. $where["id"] = $party_id;
  961. $data = [];
  962. $data["party_notice"] = $party_notice;
  963. $data["party_notice_detail"] = $party_notice_detail;
  964. $res = $partyModel->update($data,$where);
  965. if($res) {
  966. // 存redis 房间信息
  967. $redis = new Redis();
  968. $redisconfig = config("redis");
  969. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  970. $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  971. if($partyInfo) {
  972. $partyInfo = json_decode($partyInfo,true);
  973. $partyInfo = array_replace($partyInfo,$data);
  974. $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  975. }
  976. $this->success("更新成功!",$res);
  977. } else {
  978. $this->error("网络错误,请稍后重试!");
  979. }
  980. }
  981. /**
  982. * 开始排麦
  983. */
  984. public function addLineUp() {
  985. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  986. $mai_id = $this->request->request('mai_id',"1"); // 麦位置
  987. if (!$party_id) {
  988. $this->error(__('Invalid parameters'));
  989. }
  990. $userInfo = $this->auth->getUserinfo();
  991. $userid = $userInfo['id'];
  992. // 获取 房间信息
  993. $partyUser = \app\common\model\Party::where(['id'=>$party_id])->value("user_id");
  994. if(!$partyUser) $this->error(__('房间信息未找到!'));
  995. // $is_home = $partyUser == $userInfo->id?1:0;
  996. $redis = new Redis();
  997. $redisconfig = config("redis");
  998. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  999. $data = unserialize($redis->hGet("party_lineup",$party_id));
  1000. $data[$userid]["user_id"] = $userInfo['id'];
  1001. $data[$userid]["avatar"] = $userInfo['avatar'];
  1002. $data[$userid]["mai_id"] = $mai_id;
  1003. $data[$userid]["nickname"] = $userInfo['nickname'];
  1004. // $data["is_home"] = $is_home;
  1005. $data[$userid]["level"] = $userInfo['level'];
  1006. $data[$userid]["gender"] = $userInfo['gender'];
  1007. $res = false;
  1008. $data && $res = $redis->hSet("party_lineup",$party_id,serialize($data));
  1009. $data = array_values($data);
  1010. if($res !== false) {
  1011. $this->success("设置成功!",$data);
  1012. } else {
  1013. $this->error("网络错误,请稍后重试!");
  1014. }
  1015. }
  1016. /**
  1017. * 取消排麦
  1018. */
  1019. public function cancelLineUp() {
  1020. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  1021. $user_id = $this->request->request('user_id',0); // 直播间ID
  1022. $is_empty = $this->request->request('is_empty',0,"intval"); // 是否清空排麦列表 1=清空,0=不清空
  1023. if (!$party_id) {
  1024. $this->error(__('Invalid parameters'));
  1025. }
  1026. $userid = $this->auth->id;
  1027. $redis = new Redis();
  1028. $redisconfig = config("redis");
  1029. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1030. $data=unserialize($redis->hGet("party_lineup",$party_id));
  1031. if($data && $is_empty != 1) {
  1032. foreach($data as $k => $v) {
  1033. if($v["user_id"] == $user_id) {
  1034. unset($data[$k]);
  1035. break;
  1036. }
  1037. }
  1038. }
  1039. // unset($data[$userid]);
  1040. if($is_empty == 1) $data = [];
  1041. $redis->hSet("party_lineup",$party_id,serialize($data));
  1042. $this->success("移除成功!");
  1043. }
  1044. /**
  1045. * 排麦列表
  1046. */
  1047. public function lineUpList() {
  1048. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  1049. if (!$party_id) {
  1050. $this->error(__('Invalid parameters'));
  1051. }
  1052. $redis = new Redis();
  1053. $redisconfig = config("redis");
  1054. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1055. $data = $redis->hGet("party_lineup",$party_id);
  1056. $dataArr = unserialize($data);
  1057. $datas = [];
  1058. is_array($dataArr) && $datas=array_values($dataArr);
  1059. if($datas) {
  1060. $this->success("获取成功!",$datas);
  1061. } else {
  1062. $this->success("获取成功!",[]);
  1063. }
  1064. }
  1065. /**
  1066. * 获取派对类型
  1067. */
  1068. public function getPatyType() {
  1069. $room_type = 1;
  1070. $partytypeModel = new \app\common\model\PartyType();
  1071. $partytypeList = $partytypeModel->where(["room_type"=>$room_type])->select();
  1072. $this->success("获取成功!",$partytypeList);
  1073. }
  1074. /**
  1075. * 判断派对是否设置过密码
  1076. */
  1077. public function getPatyIspass() {
  1078. $party_id = $this->request->request("party_id");// 派对ID
  1079. $room_type = 1;
  1080. $is_miniprogram = 0;//
  1081. if (!$party_id) $this->error(__('Invalid parameters'));
  1082. $user_id = $this->auth->id;
  1083. // 存redis 房间信息
  1084. $redis = new Redis();
  1085. $redisconfig = config("redis");
  1086. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1087. $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  1088. if($partyInfo) {
  1089. $partyInfo = json_decode($partyInfo,true);
  1090. } else {
  1091. $partyModel = new \app\common\model\Party();
  1092. $partyInfo = $partyModel->field("id,user_id,party_pass")->where(["id"=>$party_id])->find();
  1093. if(!$partyInfo) {
  1094. $this->error(__('房间信息获取失败!'));
  1095. }
  1096. }
  1097. $data = [];
  1098. $data["status"] = 0;
  1099. $data["is_pass"] = $partyInfo["party_pass"]?1:0;
  1100. if($partyInfo["user_id"] == $user_id || $this->auth->mobile == '18560505277') $data["is_pass"] = 0;
  1101. $this->success("获取成功!",$data);
  1102. }
  1103. /**
  1104. * 获取房间gif表情分类
  1105. */
  1106. /*public function getPartyGifType()
  1107. {
  1108. $list = \app\common\model\PartyGifType::field("id,name,image")->order("weigh", "asc")->select();
  1109. $this->success("获取成功!", $list);
  1110. }*/
  1111. /**
  1112. * 获取房间gif表情列表
  1113. */
  1114. public function getPartGifList()
  1115. {
  1116. // $type_id = $this->request->request("type_id", 1);// 类型ID
  1117. $list = \app\common\model\PartyGif::field("id,gif_image")->order("weight", "asc")->select();
  1118. $this->success("获取成功!", $list);
  1119. }
  1120. /**
  1121. * 获取房间头像gif表情列表
  1122. */
  1123. public function getPartHeadgifList() {
  1124. $this->success("获取成功!",\app\common\model\PartyHeadgif::field("id,name,gif_image")->order("weight","asc")->select());
  1125. }
  1126. /**
  1127. * 随机获取礼物盒礼物
  1128. */
  1129. /*private function getBoxGift($gift_box_type) {
  1130. // 查询本奖池内礼物是否还有可抽礼物
  1131. $where = [];
  1132. $where["Jackpot_id"] = $gift_box_type;
  1133. $where["is_use"] = 0;
  1134. $boxhasgift = \app\common\model\GiftBox::where($where)->select();
  1135. $giftcount = count($boxhasgift);
  1136. $giftArr = [];
  1137. foreach($boxhasgift as $k => $v) $giftArr[$v["id"]] = $v;
  1138. if($giftcount > 1) {
  1139. // 随机抽取$num个礼物
  1140. $giftids = array_rand($giftArr,1);
  1141. \app\common\model\GiftBox::update(["is_use"=>1],["id"=>$giftids]);
  1142. }
  1143. if($giftcount == 1) {
  1144. $giftids = $boxhasgift[0]["id"];
  1145. // 更新宝箱奖池全部礼物为未使用
  1146. \app\common\model\GiftBox::update(["is_use"=>0],["Jackpot_id"=>$gift_box_type]);
  1147. }
  1148. $giftInfo = $giftArr[$giftids];
  1149. return $giftInfo;
  1150. }*/
  1151. /**
  1152. * 全麦/单独赠送礼物
  1153. */
  1154. public function giveGiftToYou() {
  1155. // 接口防并发
  1156. if (!$this->apiLimit(1, 1000)) {
  1157. $this->error(__('Operation frequently'));
  1158. }
  1159. //
  1160. $user_ids = $this->request->request("user_id");// 赠送对象
  1161. $gift_id = $this->request->request("gift_id");// 礼物ID
  1162. $party_id = $this->request->request("party_id",0);// 派对ID
  1163. $number = $this->request->request("number");// 赠送数量
  1164. $room_type = 1; // 房间类型
  1165. $is_back = 0;// 是否背包赠送: 1=是,0=否
  1166. if (!$user_ids || !$gift_id || !$number)
  1167. {
  1168. $this->error(__('Invalid parameters'));
  1169. }
  1170. //
  1171. $user_id_arr = explode(',',$user_ids);
  1172. $userCount = count($user_id_arr);
  1173. $userauthid = $this->auth->id;
  1174. // 获取礼物信息
  1175. $giftInfo = Db::name('gift')->where('id',$gift_id)->find();
  1176. if (!$giftInfo) {$this->error("请选择礼物!");}
  1177. $giftValue = $giftInfo["value"] * $number;
  1178. $giftCountValue = $giftInfo["value"] * $number * $userCount;
  1179. // 判断当前用户余额
  1180. $user_gold = model('wallet')->getWallet($userauthid,'gold');
  1181. if($user_gold < $giftCountValue) {$this->error("您的金币余额不足!");}
  1182. if($party_id){
  1183. $partyInfo = \app\common\model\Party::field("user_id")->where(["id"=>$party_id])->find();
  1184. if(!$partyInfo){
  1185. $this->error('不存在的语聊间');
  1186. }
  1187. }
  1188. $returnData = [];
  1189. Db::startTrans();
  1190. try {
  1191. $redis = new Redis();
  1192. $redisconfig = config("redis");
  1193. $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
  1194. // 获取当天零点
  1195. $day = date("Ymd");
  1196. // 获取本周第一天
  1197. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  1198. // 获取本月第一天
  1199. $monthday = date("Ym01");
  1200. $allVal = 0;
  1201. $i = 0;
  1202. foreach($user_id_arr as $user_id) {
  1203. // 添加礼物赠送记录表
  1204. $data = [];
  1205. $data["user_id"] = $userauthid;
  1206. $data["user_to_id"] = $user_id;
  1207. $data["party_id"] = $party_id;
  1208. $data["gift_id"] = $gift_id;
  1209. $data["gift_give_type"] = 2;
  1210. $data["gift_name"] = $giftInfo["name"];
  1211. $data["gift_gif_image"] = $giftInfo["image"];
  1212. $data["number"] = $number;
  1213. $data["price"] = $giftInfo["value"];
  1214. $data["value"] = $giftValue;
  1215. $data["createtime"] = time();
  1216. $log_id = Db::name('gift_user_party')->insertGetId($data);
  1217. if(!$log_id){
  1218. Db::rollback();
  1219. $this->error('赠送失败');
  1220. }
  1221. if($giftValue > 0){
  1222. // 扣除当前用户钻石余额
  1223. $wallet_rs = model('wallet')->lockChangeAccountRemain($userauthid,'gold',-$giftValue,51,'赠送礼物:'.$giftInfo["name"],'gift_user_party',$log_id);
  1224. if($wallet_rs['status'] === false){
  1225. Db::rollback();
  1226. $this->error($wallet_rs['msg']);
  1227. }
  1228. // 添加赠送用户余额
  1229. $money_to_gold = config('site.money_to_gold');
  1230. $gift_plat_scale = config('site.gift_plat_scale');
  1231. $giftmoney = bcdiv($giftValue,$money_to_gold,2);
  1232. $money = bcdiv(bcmul($giftmoney,100 - $gift_plat_scale,2),100,2);
  1233. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,'money',$money,52,'获得礼物:'.$giftInfo["name"],'gift_user_party',$log_id);
  1234. if($wallet_rs['status'] === false){
  1235. Db::rollback();
  1236. $this->error($wallet_rs['msg']);
  1237. }
  1238. }
  1239. $res6 = true;
  1240. if ($res6) {
  1241. $i++;
  1242. if($party_id > 0) {
  1243. // 添加redis记录做财富排行榜日榜用
  1244. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $giftValue, $user_id);
  1245. // 添加redis记录做财富排行榜周榜用
  1246. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $giftValue, $user_id);
  1247. // 添加redis记录做财富排行榜月榜用
  1248. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $giftValue, $user_id);
  1249. // 添加redis记录做贡献排行榜日榜用
  1250. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
  1251. // 添加redis记录做贡献排行榜周榜用
  1252. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
  1253. // 添加redis记录做贡献排行榜月榜用
  1254. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
  1255. // tcp 更新用户魅力值
  1256. $this->updateUserCharm($party_id, $user_id, $giftValue);
  1257. }
  1258. $allVal = $allVal + $giftValue;
  1259. }
  1260. }
  1261. // 获取用户魅力值
  1262. $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
  1263. $u = [];
  1264. if($users) {
  1265. foreach($users as $k => $v) $u[] = [
  1266. "user_id"=>$k,
  1267. "charm"=>$this->changeW($v)
  1268. ];
  1269. }
  1270. $userCharm = $u;
  1271. // tcp 更新房间热度
  1272. $partyHot = $this->updatePartyHot($party_id, $allVal, $room_type);
  1273. // 如果是派对,则添加派对热度值记录做榜单统计
  1274. if($room_type == 1) {
  1275. $data = [];
  1276. $data["party_id"] = $party_id;
  1277. $data["hot"] = $allVal;
  1278. $data["createtime"] = time();
  1279. \app\common\model\PartyHot::insert($data);
  1280. }
  1281. // tcp 获取房间用户周前三名
  1282. $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
  1283. if($i == $userCount) {
  1284. $returnData["userCharm"] = $userCharm;
  1285. $returnData["partyHot"] = $this->changeW($partyHot);
  1286. $returnData["partyUserTop"] = $partyUserTop;
  1287. $returnData["image"] = one_domain_image($giftInfo["image"]);
  1288. $returnData["gif_image"] = one_domain_image($giftInfo["special"]);
  1289. Db::commit();
  1290. $this->success("赠送成功!",$returnData);
  1291. } else {
  1292. $this->success("赠送失败!");
  1293. }
  1294. } catch (ValidateException $e) {
  1295. Db::rollback();
  1296. $this->error($e->getMessage());
  1297. } catch (PDOException $e) {
  1298. Db::rollback();
  1299. $this->error($e->getMessage());
  1300. } catch (Exception $e) {
  1301. Db::rollback();
  1302. $this->error($e->getMessage());
  1303. }
  1304. }
  1305. /**
  1306. * 随机获取礼物盒礼物(复数版)
  1307. * @params $gift_box_type 奖池id
  1308. * @params $userCount 用户数量
  1309. * @params $number 礼物数量
  1310. */
  1311. private function getBoxesGift($gift_box_type,$userCount,$number,&$oldGiftArr=[]) {
  1312. // 查询本奖池内礼物是否还有可抽礼物
  1313. $where = [];
  1314. $where["Jackpot_id"] = $gift_box_type;
  1315. $where["is_use"] = 0;
  1316. $limit = $userCount*$number;
  1317. $boxhasgift = \app\common\model\GiftBox::where($where)->orderRaw('rand()')->limit($limit)->select();
  1318. $giftcount = count($boxhasgift);
  1319. $giftArr = [];
  1320. foreach($boxhasgift as $k => $v) $giftArr[$v["id"]] = $v;
  1321. unset($boxhasgift);
  1322. foreach ($giftArr as $id => $v) {
  1323. $list[] = ['id' => $id, 'is_use' => 1];
  1324. }
  1325. $oldGiftArr = array_merge($giftArr,$oldGiftArr);
  1326. $is_update = TRUE;
  1327. if($giftcount<$limit){
  1328. $newNum = $limit - $giftcount;
  1329. \app\common\model\GiftBox::update(["is_use"=>0],["Jackpot_id"=>$gift_box_type]);
  1330. $this->getBoxesGift($gift_box_type,1,$newNum,$oldGiftArr);
  1331. $is_update = FALSE;
  1332. }
  1333. if ($is_update==TRUE) {
  1334. $gifBoxObject = new GiftBox();
  1335. $gifBoxObject->isUpdate()->saveAll($list);
  1336. }
  1337. return $oldGiftArr;
  1338. }
  1339. /**
  1340. * 用户声币余额变更数据(宝箱专供)
  1341. */
  1342. private function boxAddUserSoundcoinLog($user_id, $money, $mode, $before, $detail, $type = 1, $objId = 0)
  1343. {
  1344. if ($mode == "+") {
  1345. $balance = $before + $money;
  1346. } else {
  1347. $balance = $before - $money;
  1348. }
  1349. // 添加当前用户钻石流水记录
  1350. $data = [];
  1351. $data["user_id"] = $user_id;
  1352. $data['type'] = $type;
  1353. $data['obj_id'] = $objId;
  1354. $data["value"] = $money;
  1355. $data["mode"] = $mode;
  1356. $data["before"] = $before;
  1357. $data["balance"] = $balance;
  1358. $data["detail"] = $detail;
  1359. $data["createtime"] = time();
  1360. return $data;
  1361. }
  1362. /**
  1363. * 增加抽点数据
  1364. */
  1365. private function addUserProfitLogList($user_id,$party_id,$getValue,$platValue,$guilderValue)
  1366. {
  1367. $data = [];
  1368. $data["user_id"] = $user_id;
  1369. $data["party_id"] = $party_id?$party_id:0;
  1370. $data["gift_value"] = $getValue;
  1371. $data["plat_value"] = $platValue;
  1372. $data["guilder_value"] = $guilderValue;
  1373. $data["createtime"] = time();
  1374. return $data;
  1375. }
  1376. //每个玩家获得的礼物,对象处理成数组
  1377. private function checkAllUserBoxGiftInfo(&$allUserBoxGiftInfo)
  1378. {
  1379. $giftNotice = config("site.giftNotice");
  1380. foreach ($allUserBoxGiftInfo as $k => $v){
  1381. $data = [];
  1382. foreach ($v as $vv){
  1383. $money = $vv['price'] / 100 ;
  1384. if($money<$giftNotice){
  1385. $vv['only_gif_image'] = '';
  1386. }
  1387. unset($vv['price']);
  1388. $data[] = $vv;
  1389. }
  1390. $allUserBoxGiftInfo[$k] = $data;
  1391. }
  1392. }
  1393. /**
  1394. * 消费开通贵族
  1395. * @param Redis $redis
  1396. * @param $user_id
  1397. * @param $giftCountValue
  1398. * @return void
  1399. */
  1400. private function checkBeNoble(Redis $redis,$user_id,$giftCountValue)
  1401. {
  1402. $user_level = $this->auth->level;
  1403. $user_noble = $this->auth->noble;
  1404. $getNobleLevelList = $this->getNobleLevelList($redis);
  1405. $getUserRenew = $this->changeUserRenew($redis,$user_id,$giftCountValue);
  1406. $getNobleLevelList = array_reverse($getNobleLevelList);
  1407. //总消费
  1408. $renewcount = $this->auth->renewcount;
  1409. $renewcount += $giftCountValue;
  1410. //是否可升级
  1411. $maxNoble = $getNobleLevelList[0]['id'];
  1412. $updateNoble = FALSE;
  1413. $userNobleLeve = 0;
  1414. $nobleLeveRenew2 = 0;
  1415. if($user_noble < $maxNoble) {
  1416. foreach ($getNobleLevelList as $NobleLeve) {
  1417. if ($getUserRenew >= $NobleLeve['renew2'] && //月消费 >= 爵位保级消费
  1418. $user_level >= $NobleLeve['need_level'] && //玩家等级 > 爵位等级要求
  1419. $user_noble < $NobleLeve['id'] && //玩家等级 < 升级的爵位等级
  1420. $renewcount >= $NobleLeve['first2'] //总消费 >= 爵位低消
  1421. ) {
  1422. $updateNoble = TRUE;
  1423. $userNobleLeve = $NobleLeve['id'];
  1424. $nobleLeveRenew2 = $NobleLeve['renew2'];
  1425. break;
  1426. }
  1427. }
  1428. }
  1429. if ($updateNoble===TRUE){
  1430. // echo $NobleLeve['name'].PHP_EOL.$getUserRenew.PHP_EOL.$renewcount.PHP_EOL.$user_level.PHP_EOL.$user_noble;
  1431. controller('api/Noble')->checkBeNoble2(config('token')['key'], $userNobleLeve);
  1432. //月保级消费清空
  1433. //溢出的积分算入保级消费
  1434. $shengRenew = $getUserRenew-$nobleLeveRenew2;
  1435. $redis->set('user_renew2_'.$user_id,$shengRenew);
  1436. }
  1437. }
  1438. /**
  1439. * 获取贵族等级列表
  1440. * @param Redis $redis
  1441. * @param bool $again 是否重新生成redis
  1442. * @return bool|mixed|\PDOStatement|string|\think\Collection
  1443. * @throws \think\db\exception\DataNotFoundException
  1444. * @throws \think\db\exception\ModelNotFoundException
  1445. * @throws \think\exception\DbException
  1446. */
  1447. public function checkGetNobleLevelList($tokenKey='')
  1448. {
  1449. if ($tokenKey!==config('token')['key']) return;
  1450. $redis = new \Redis();
  1451. $redisconfig = config("redis");
  1452. $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
  1453. return $this->getNobleLevelList($redis);
  1454. }
  1455. private function getNobleLevelList(Redis $redis, bool $again=FALSE)
  1456. {
  1457. if ($again===TRUE) $redis->del('noble_level_list');
  1458. $getNobleLevelList = $redis->get('noble_level_list');
  1459. if ($getNobleLevelList){
  1460. $getNobleLevelList = unserialize($getNobleLevelList);
  1461. }else{
  1462. // $getNobleLevelList = Db::name('noble_level')->where('is_show',1)
  1463. $getNobleLevelList = \app\common\model\NobleLevel::where('is_show',1)
  1464. // ->field('id,name,first2,renew2,need_level,level_no,tqgq,qftz,qfdh,gptz')
  1465. ->select();
  1466. $redis->set("noble_level_list", serialize($getNobleLevelList));
  1467. }
  1468. return $getNobleLevelList;
  1469. }
  1470. /**
  1471. * 获取玩家月消费
  1472. * @param Redis $redis
  1473. * @param $user_id
  1474. * @param $giftCountValue
  1475. * @return false|mixed|string
  1476. */
  1477. private function changeUserRenew(Redis $redis, $user_id,$giftCountValue){
  1478. $user_renew2 = $redis->get('user_renew2_'.$user_id);
  1479. $giftCountValue = (int)$giftCountValue;
  1480. if ($user_renew2){
  1481. // $user_renew2 = $redis->incrBy('user_renew_'.$user_id,$giftCountValue);
  1482. $user_renew2 = $redis->incrBy('user_renew2_'.$user_id,$giftCountValue);
  1483. }else{
  1484. //月消费(每30天清空)
  1485. // $redis->set('user_renew_'.$user_id,$giftCountValue);
  1486. //月保级消费(每30天清空||每次升级贵族清空)
  1487. $redis->set('user_renew2_'.$user_id,$giftCountValue);
  1488. $user_renew2 = $giftCountValue;
  1489. }
  1490. return $user_renew2;
  1491. }
  1492. // /**
  1493. // * 播放动效
  1494. // */
  1495. // public function play($party_id,$type,$value,$number) {
  1496. // // 发送tcp
  1497. // $tcpArr = [];
  1498. // $tcpArr['type'] = "play";
  1499. // $tcpArr['data'] = [
  1500. // 'party_id' => $party_id,
  1501. // 'type' => $type,
  1502. // 'value' => $value,
  1503. // 'number' => $number,
  1504. // ];
  1505. // $tcpJson = json_encode($tcpArr);
  1506. // $client = stream_socket_client(config("tcp"));
  1507. // $buffer2 = base64_encode($tcpJson)."****";
  1508. // fwrite($client, $buffer2);
  1509. // }
  1510. /**
  1511. * 更新派对信息(热度等)
  1512. */
  1513. private function updatePartyHot($party_id,$hotValue,$room_type) {
  1514. $partyInfo = \app\common\model\Party::where(['id'=>$party_id])->find();
  1515. if(!$partyInfo) return $hotValue;
  1516. $party_hot = $partyInfo->party_hot > 0 ? $partyInfo->party_hot:0;
  1517. $party_hot_value = $party_hot + $hotValue;
  1518. $party_hot_value = $party_hot_value > 0 ? $party_hot_value : 0;
  1519. if($party_hot_value != $party_hot) {
  1520. // 保存数据
  1521. $partyInfo->party_hot = $party_hot_value;
  1522. $partyInfo->save();
  1523. // 更新redis 加入缓存排序
  1524. $redis = new Redis();
  1525. $redisconfig = config("redis");
  1526. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1527. $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  1528. // 更新redis 加入缓存
  1529. $redPartyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]);
  1530. if($redPartyInfo) {
  1531. $redPartyInfo = json_decode($redPartyInfo,true);
  1532. $redPartyInfo["party_hot"] = $party_hot_value;
  1533. $redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($redPartyInfo));
  1534. }
  1535. // // 发送tcp
  1536. // $tcpArr = [];
  1537. // $tcpArr['type'] = "changeRoomHot";
  1538. // $tcpArr['data'] = [
  1539. // 'room_id' => $party_id,
  1540. // 'value' => $party_hot_value,
  1541. // ];
  1542. // $tcpJson = json_encode($tcpArr);
  1543. // $client = stream_socket_client(config("tcp"));
  1544. // $buffer2 = base64_encode($tcpJson)."****";
  1545. // fwrite($client, $buffer2);
  1546. }
  1547. return $party_hot_value;
  1548. }
  1549. /**
  1550. * 用户赠送礼物后房间内用户排行,贡献榜前三名
  1551. */
  1552. private function getPartyUserTop($party_id,$room_type) {
  1553. $redis = new Redis();
  1554. $redisconfig = config("redis");
  1555. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1556. // 获取本周第一天
  1557. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  1558. // 获取当天零点
  1559. $day = date("Ymd");
  1560. $userModel = new \app\common\model\User();
  1561. // 获取条数
  1562. $num = 3;
  1563. // 获取3条财富排行周记录
  1564. $getweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true);
  1565. $userList = $userModel->rankList($getweek);
  1566. $avatarArr = [];
  1567. if($userList) {
  1568. foreach($userList as $k => $v) {
  1569. $v["jewel"] > 0 && $avatarArr[] = $v["avatar"];
  1570. }
  1571. // 加入缓存做备份
  1572. $redis->hSet("user_jewel_top3",$party_id,json_encode($avatarArr));
  1573. // // 发送tcp
  1574. // $tcpArr = [];
  1575. // $tcpArr['type'] = "changeRoomUserTop";
  1576. // $tcpArr['data'] = [
  1577. // 'room_id' => $party_id,
  1578. // 'user_avatar' => $avatarArr,
  1579. // ];
  1580. // $tcpJson = json_encode($tcpArr);
  1581. // $client = stream_socket_client(config("tcp"));
  1582. // $buffer2 = base64_encode($tcpJson)."****";
  1583. // fwrite($client, $buffer2);
  1584. }
  1585. return $avatarArr;
  1586. }
  1587. /**
  1588. * 用户赠送礼物后房间内用户魅力值增加
  1589. */
  1590. private function updateUserCharm($party_id,$user_id,$giftValue) {
  1591. $redis = new Redis();
  1592. $redisconfig = config("redis");
  1593. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1594. // 获取用户魅力值
  1595. $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
  1596. if(!$users) $users[$user_id] = 0;
  1597. if(isset($users[$user_id])) {
  1598. $value = $users[$user_id] + $giftValue;
  1599. } else {
  1600. $value = $giftValue;
  1601. }
  1602. $redis->zAdd("hourCharm_".$party_id,$value,$user_id);
  1603. return true;
  1604. }
  1605. /**
  1606. * 单个房间魅力值清零
  1607. */
  1608. public function partyClearCharm() {
  1609. $party_id = $this->request->request("party_id");// 派对ID
  1610. $userId = $this->request->request("user_id", 0);// 用户ID
  1611. if($party_id <= 0) {
  1612. $this->error("请输入派对ID");
  1613. }
  1614. $redis = new Redis();
  1615. $redisconfig = config("redis");
  1616. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1617. if ($userId) {
  1618. $redis->zRem("hourCharm_" . $party_id, $userId);
  1619. } else {
  1620. $redis->del("hourCharm_" . $party_id);
  1621. }
  1622. $this->success("操作成功!");
  1623. }
  1624. /**
  1625. * 获取音乐列表
  1626. */
  1627. public function getMusicList() {
  1628. $this->success("获取成功!",\app\common\model\Music::select());
  1629. }
  1630. //============================定时任务==========================//
  1631. /**
  1632. * redis清理排行榜 并数据库备份
  1633. */
  1634. public function updateTops() {
  1635. // 前一天日期
  1636. $yestaday = date("Ymd",bcsub(time(),86400));
  1637. // 上个周一
  1638. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1639. // 上个月一号
  1640. $monty = bcsub(date("m"),1);
  1641. if($monty < 10) $monty = "0".$monty;
  1642. $premonth = date("Y").$monty."01";
  1643. $redis = new Redis();
  1644. $redisconfig = config("redis");
  1645. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1646. // 获取所有派对ID
  1647. $party_ids = \app\common\model\Party::where(["status"=>1])->column("id");
  1648. // 获取数据
  1649. $time = time();
  1650. if($party_ids) foreach($party_ids as $k => $v) {
  1651. $keys = [
  1652. "party_jewel_get_".$v.":".$yestaday."d",
  1653. "party_jewel_to_".$v.":".$yestaday."d",
  1654. "party_jewel_get_".$v.":".$preweek."w",
  1655. "party_jewel_to_".$v.":".$preweek."w",
  1656. "party_jewel_get_".$v.":".$premonth."m",
  1657. "party_jewel_to_".$v.":".$premonth."m",
  1658. ];
  1659. foreach($keys as $key) {
  1660. // 备份数据
  1661. $redisData = $redis->zRevRange($key,0,-1,true);
  1662. if($redisData) {
  1663. $keyInfo = \app\common\model\RedisTops::where(["key"=>$key])->find();
  1664. $data = ["party_id"=>$v,"key"=>$key,"value"=>json_encode($redisData),"createtime" => $time];
  1665. if(!$keyInfo) {
  1666. $res = \app\common\model\RedisTops::insert($data);
  1667. // 清理数据
  1668. $res && $redis->zRemRangeByRank($key,0,-1);
  1669. }
  1670. }
  1671. }
  1672. }
  1673. }
  1674. public function handleParty() {
  1675. return false;
  1676. $partyList = \app\common\model\Party::where('is_online',1)->select();
  1677. $redis = new Redis();
  1678. $redisconfig = config("redis");
  1679. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1680. // $redis->del("online_" . 3);die;
  1681. // $redis->hSet("online_" . 3, 4, 4);die;
  1682. foreach ($partyList as $party){
  1683. $userId = $party['user_id'];
  1684. $roomId = $party['id'];
  1685. $redis->HDel("online_" . $roomId, $userId);
  1686. // 扣除在线用户在房间情况
  1687. $redis->hDel("livingUser",$userId);
  1688. // // 更新房间在线状态
  1689. $partyInfo = $redis->get("party_" . $roomId);
  1690. if ($partyInfo) {
  1691. $partyInfo = json_decode($partyInfo, true);
  1692. $memCount = count($redis->hGetAll("online_" . $roomId));
  1693. if ($memCount <= 0) {
  1694. dump($roomId.'--1---');
  1695. $partyInfo["is_online"] = 0;
  1696. $redis->set("party_" . $roomId, json_encode($partyInfo));
  1697. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  1698. }
  1699. }
  1700. $liveInfo = $redis->get("live_" . $roomId);
  1701. if ($liveInfo) {
  1702. $liveInfo = json_decode($liveInfo, true);
  1703. $memCount = count($redis->hGetAll("online_" . $roomId));
  1704. if ($memCount <= 0) {
  1705. dump($roomId.'---2--');
  1706. $liveInfo["is_online"] = 0;
  1707. $redis->set("live_" . $roomId, json_encode($liveInfo));
  1708. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  1709. }else{
  1710. dump($roomId.'---3--');
  1711. $redis->del("online_" . $roomId);
  1712. $liveInfo["is_online"] = 0;
  1713. $redis->set("live_" . $roomId, json_encode($liveInfo));
  1714. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  1715. }
  1716. }
  1717. }
  1718. $this->success("获取成功!");
  1719. }
  1720. }