Party.php 74 KB

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