Party.php 75 KB

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