Party.php 75 KB

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