Party.php 75 KB

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