Party.php 75 KB

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