Party.php 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  1. <?php
  2. namespace app\api\controller;
  3. use addons\faqueue\library\QueueApi;
  4. use app\api\controller\Common;
  5. use app\common\model\GiftBox;
  6. use app\common\model\PartyJoin;
  7. use fast\Random;
  8. use app\common\controller\RedisLeaderboard;
  9. use Redis;
  10. use think\Db;
  11. use think\Exception;
  12. use think\exception\PDOException;
  13. use think\exception\ValidateException;
  14. use think\Request;
  15. /**
  16. * 派对信息接口
  17. */
  18. class Party extends Common
  19. {
  20. protected $noNeedLogin = ['updatePartyInfo', 'getPatyType', 'addUserPositionToParty', 'clearMoney', 'clearCharm', 'getPartyRankList', 'getDefaultBackground', 'getPartyGifType', 'getPartGifList', 'getPartHeadgifList', 'isNotalk', 'getMusicList', 'updateTops', 'handleParty'];
  21. protected $noNeedRight = ['*'];
  22. public function __construct(Request $request = null)
  23. {
  24. $this->roomTypeArr = [1=>"party",2=>"livebc"];
  25. parent::__construct($request);
  26. }
  27. /**
  28. * 创建/进入派对
  29. */
  30. public function createParty() {
  31. $room_type = $this->request->request('room_type',1); // 房间类型:1=派对,2=直播
  32. $party_name = $this->request->request('party_name',0);
  33. $party_type = $this->request->request('party_type',0); // 派对类型(情感互动,心动点单 等)语聊房的
  34. $cityname = input('cityname','');//直播城市
  35. $partyModel = new \app\common\model\Party();
  36. $user_id = $this->auth->id;
  37. //
  38. if($this->auth->idcard_status == -1){
  39. $this->error('请先申请实名认证');
  40. }
  41. if($this->auth->idcard_status == 0){
  42. $this->error('您已经提交实名认证,请等待审核');
  43. }
  44. if($this->auth->idcard_status == 2){
  45. $this->error('您的实名认证审核未通过,请重新申请');
  46. }
  47. // 判断派对是否存在
  48. $redis = new Redis();
  49. $redisconfig = config("redis");
  50. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  51. $where = [];
  52. $where["user_id"] = $user_id;
  53. $where["room_type"] = $room_type;
  54. $sqlPartyInfo = $partyModel->where($where)->find();
  55. if(!$sqlPartyInfo) { // sql中不存在派对信息
  56. $partylimit = config("site.roomLimit");
  57. $partycount = $partyModel->count("id");
  58. if($partycount >= $partylimit && $partylimit != -1) {
  59. $this->error("房间数量已达上线,请联系客服!");
  60. }
  61. $party_name = $party_name ? $party_name : $this->auth->username."的房间"; // 派对名称
  62. $party_logo = "/assets/api/party_logo.png"; // 派对logo
  63. $party_notice = "请编辑房间公告"; // 派对公告
  64. $party_notice_detail = "请编辑房间公告内容!"; // 派对公告详情
  65. $party_ids = $partyModel->column("party_id");
  66. // 创建派对ID (临时ID四位,派对数不超过8999)
  67. $party_id = $this->auth->getUinqueId(6,$party_ids);
  68. if($party_id > 999999) {
  69. $this->error("派对超限,请联系客服");
  70. }
  71. $data = [];
  72. $data["user_id"] = $this->auth->id;
  73. $data["room_type"] = $room_type;
  74. $data["party_id"] = $party_id;
  75. $data["party_hot"] = 0;
  76. $data["party_name"] = $party_name;
  77. $data["party_logo"] = $party_logo;
  78. $data["party_type"] = $room_type == 2 ? $party_type : 0;//目前直播需要,语聊不需要
  79. $data["party_notice"] = $party_notice;
  80. $data["party_notice_detail"] = $party_notice_detail;
  81. $data["is_online"] = 1;
  82. $data["status"] = 0;
  83. $data["is_recommend"] = 0;
  84. $data["createtime"] = time();
  85. $data["cityname"] = $cityname;
  86. $id = $partyModel->insertGetId($data);
  87. if(!$id) {
  88. $this->error("派对创建失败,请稍后重试!");
  89. }
  90. $data["id"] = $id;
  91. if($room_type == 2) {
  92. \app\common\model\User::update(["is_livebc"=>1],["id"=>$user_id]);
  93. }
  94. $partyInfo = $partyModel->where(["id"=>$id])->find();
  95. $partyInfo["is_new"] = 1;
  96. // 冠名
  97. $userInfo = [];
  98. if($partyInfo["naming"]) {
  99. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$partyInfo["naming"]])->find();
  100. }
  101. // 头像
  102. if($partyInfo["user_id"] > 0 && $room_type == 2) {
  103. $partyInfo["avatar"] = \app\common\model\User::where(["id"=>$partyInfo["user_id"]])->value("avatar");
  104. }
  105. // 派对类型
  106. $partyTypeName = "普通房";
  107. if($partyInfo["party_type"]) {
  108. $partyTypeName = \app\common\model\PartyType::where(["id"=>$partyInfo["party_type"]])->value("name");
  109. }
  110. $partyInfo["naming"] = $userInfo;
  111. $partyInfo["type_name"] = $partyTypeName;
  112. // 加入缓存排序
  113. $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  114. // 加入缓存
  115. $redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($partyInfo));
  116. if($room_type != 2) {
  117. // 创建公会
  118. \app\common\model\Guild::createGuild($partyInfo["id"], $partyInfo["party_name"], $user_id);
  119. \app\common\model\User::update(["is_guild" => 3], ["id" => $user_id]);
  120. }
  121. } else {
  122. if($sqlPartyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  123. if($sqlPartyInfo["status"] == 0){
  124. // 清楚对应的记录
  125. $redis->del($this->roomTypeArr[$room_type]."_".$sqlPartyInfo["id"]);
  126. // 删除派对记录
  127. \app\common\model\Party::where('id',$sqlPartyInfo["id"])->delete();
  128. $this->error("创建失败,请重试");
  129. }
  130. // if($sqlPartyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
  131. // 如果是房主并且房间处于离线状态下激活
  132. if ($sqlPartyInfo['user_id'] != $user_id) {
  133. $this->error("当前直播已结束!");
  134. }
  135. $partyInfo = $sqlPartyInfo;
  136. $partyInfo["is_new"] = 0;
  137. }
  138. // 获取用户魅力值
  139. $users = $redis->zRange("hourCharm_".$partyInfo->id,0,-1,true);
  140. $u = [];
  141. if($users) {
  142. foreach($users as $k => $v) $u[] = [
  143. "user_id"=>$k,
  144. "charm"=>$this->changeW($v)
  145. ];
  146. }
  147. $partyInfo["userCharm"] = $u;
  148. $this->success("获取成功!",$partyInfo);
  149. }
  150. /**
  151. * 更新房间状态为正常
  152. */
  153. public function savePartyStatus() {
  154. $party_id = $this->request->request('party_id'); // 派对ID
  155. $room_type = $this->request->request('room_type',1);; // 房间类型:1=派对,2=直播
  156. if (!$party_id || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
  157. $user_id = $this->auth->id;
  158. $partyInfo = \app\common\model\Party::get($party_id);
  159. if($partyInfo->status < 0) $this->error("当前状态不支持变更!");
  160. if($partyInfo->user_id !== $user_id) $this->error("当前账号异常,非房主禁止操作!");
  161. $partyInfo->status = 1;
  162. $partyInfo->updatetime = time();
  163. $res = $partyInfo->save();
  164. if($res) {
  165. // 更新redis 加入缓存排序
  166. $redis = new Redis();
  167. $redisconfig = config("redis");
  168. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  169. // 更新redis 加入缓存
  170. $redPartyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]);
  171. if($redPartyInfo) {
  172. $redPartyInfo = json_decode($redPartyInfo,true);
  173. $redPartyInfo["status"] = 1;
  174. $redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($redPartyInfo));
  175. }
  176. $this->success("更新房间状态成功!");
  177. } else {
  178. $this->error("网络错误,请稍后重试!");
  179. }
  180. }
  181. /**
  182. * 删除派对(群解散)
  183. * 前端没用到
  184. */
  185. public function closeParty() {
  186. $party_id = $this->request->request('party_id'); // 派对ID
  187. $room_type = $this->request->request('room_type',1); // 房间类型:1=派对,2=直播
  188. if (!$party_id || !in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
  189. $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
  190. if(!$partyInfo) $this->error(__('派对不存在!'));
  191. $res = $partyInfo->delete();
  192. if($res !== false) {
  193. // redis 删除
  194. $redis = new Redis();
  195. $redisconfig = config("redis");
  196. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  197. $redis->zRem($this->roomTypeArr[$room_type]."Rank",$party_id);
  198. $redis->del($this->roomTypeArr[$room_type]."_".$party_id);
  199. $this->success("删除成功!");
  200. } else {
  201. $this->error("删除失败!");
  202. }
  203. }
  204. /**
  205. * 获取派对列表排序
  206. */
  207. public function getPartyRankList() {
  208. $thispage = $this->request->request('thispage',1,"intval"); // 当前页数
  209. $pagenum = $this->request->request('pagenum',10,"intval"); // 每页显示条数0=不做分页
  210. $type_id = $this->request->request('type_id'); // 派对类型
  211. $room_type = 1; // 房间类型:1=派对,2=直播
  212. $is_recommend = $this->request->request('is_recommend'); // 推荐0=否1=是
  213. $all = $this->request->request('all'); // 全部分类0=否1=是
  214. $index = $this->request->request('index',0); // 全部分类0=否1=是
  215. //更新ip,设备id
  216. //非原创,注释
  217. //if($is_recommend == 1) controller('api/User')->changeDeviceIp();
  218. $start = ($thispage-1)*$pagenum;
  219. $end = $start+($pagenum-1);
  220. // 获取排序
  221. $redis = new Redis();
  222. $redisconfig = config("redis");
  223. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  224. $redisPartyRankList = $redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
  225. $partyModel = new \app\common\model\Party();
  226. if(!$redisPartyRankList) {
  227. $userModel = new \app\common\model\User();
  228. // 直接从数据库获取所有数据
  229. $where = [];
  230. $where["a.status"] = 1;
  231. $where["a.is_online"] = 1;
  232. $where["a.room_type"] = $room_type;
  233. $sqlPartyList = $partyModel->alias("a")->field("a.*,b.name as type_name")
  234. ->join("party_type b","a.party_type = b.id","left")->where($where)->select();
  235. $userList = $userModel->field("id,nickname,avatar")->where(["status"=>1])->select();
  236. $userInfoArr = [];
  237. if($userList) foreach($userList as $k => $v) $userInfoArr[$v["id"]] = $v;
  238. if($sqlPartyList) {
  239. foreach($sqlPartyList as $k => $v) {
  240. // 加入缓存排序
  241. $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $v['party_hot'], $v["id"]);
  242. // 设置冠名
  243. $sqlPartyList[$k]["naming"] = isset($userInfoArr[$v["naming"]])?$userInfoArr[$v["naming"]]:[];
  244. // 设置房主头像
  245. $sqlPartyList[$k]["avatar"] = isset($userInfoArr[$v["user_id"]])?$userInfoArr[$v["user_id"]]["avatar"]:[];
  246. // 加入缓存
  247. $redis->set($this->roomTypeArr[$room_type]."_".$v["id"],json_encode($v));
  248. }
  249. $redisPartyRankList = $redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
  250. }
  251. }
  252. if($redisPartyRankList) {
  253. $resultInfo = $partyModel->getPatyInfoByPartyId($redisPartyRankList,$this->roomTypeArr[$room_type],$type_id,$is_recommend,$all,$start,$end,$index);
  254. $this->success("获取成功!",$resultInfo);
  255. }else{
  256. $this->success("获取成功!",[]);
  257. }
  258. }
  259. /**
  260. * 获取派对用户排序
  261. */
  262. public function getPartyUserRank() {
  263. $room_type = 1;
  264. $party_id = $this->request->request("party_id");// 派对ID
  265. if (!$party_id) $this->error(__('Invalid parameters'));
  266. $redis = new Redis();
  267. $redisconfig = config("redis");
  268. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  269. $res = [];
  270. // 获取今天
  271. $day = date("Ymd");
  272. // 获取本周第一天
  273. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  274. // 获取本月第一天
  275. $monthday = date("Ym01");
  276. $userModel = new \app\common\model\User();
  277. // 获取条数
  278. $num = 50;
  279. // 获取50条财富排行日记录
  280. $getday = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$day."d",0,$num-1,true);
  281. $res['getRankListDay'] = $userModel->rankList($getday);
  282. // 获取50条财富排行日记录
  283. $today = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true);
  284. $res['toRankListDay'] = $userModel->rankList($today);
  285. // 获取50条财富排行周记录
  286. $getweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$weekday."w",0,$num-1,true);
  287. $res['getRankListWeek'] = $userModel->rankList($getweek);
  288. // 获取50条贡献排行周记录
  289. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$weekday."w",0,$num-1,true);
  290. $res['toRankListWeek'] = $userModel->rankList($toweek);
  291. // 获取50条财富排行月记录
  292. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$monthday."m",0,$num-1,true);
  293. $res['getRankListMonth'] = $userModel->rankList($toweek);
  294. // 获取50条贡献排行周记录
  295. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$monthday."m",0,$num-1,true);
  296. $res['toRankListMonth'] = $userModel->rankList($toweek);
  297. return $this->success("获取成功!",$res);
  298. }
  299. /**
  300. * 派对热度更新(已废弃)
  301. */
  302. public function changeUserPartyhot() {
  303. $party_id = $this->request->request('party_id',0,"intval"); // 派对ID
  304. $room_type = 1; // 房间类型
  305. $party_hot = $this->request->request('party_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值)
  306. if (!$party_id || !$party_hot) {
  307. $this->error(__('Invalid parameters'));
  308. }
  309. $redis = new Redis();
  310. $redisconfig = config("redis");
  311. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  312. // 更新热度
  313. $redis->zIncrBy($this->roomTypeArr[$room_type]."Rank", $party_hot, $party_id);
  314. return $this->success("更新成功!");
  315. }
  316. /**
  317. * 更新主持人和麦位前四位至首页房间排行
  318. */
  319. public function addUserPositionToParty() {
  320. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  321. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
  322. $upOrdown = $this->request->request('upordown'); // 上下麦:1=上麦-1=下麦
  323. $room_type = 1; // 房间类型
  324. $avatar = $this->request->request('avatar'); // 用户头像 (upOrdown参数为-1时可以不传)
  325. $position = $this->request->request('position'); // 麦位置:0=支持人,1=1号麦,2=2号麦,3=3号麦,4=4号麦
  326. if (!$party_id || !$user_id || !in_array($position,[0,1,2,3,4,5,6,7,8]) || !in_array($upOrdown,[1,-1])) {
  327. $this->error(__('Invalid parameters'));
  328. }
  329. if($upOrdown == 1) {
  330. // 先完成所有麦上记录
  331. \app\common\model\UserOnsiteTime::update(["status"=>2],["user_id"=>$user_id]);
  332. // 保存上麦记录
  333. $data = [];
  334. $data["user_id"] = $user_id;
  335. $data["party_id"] = $party_id;
  336. $data["onsite_time"] = time();
  337. \app\common\model\UserOnsiteTime::insert($data);
  338. }
  339. if($upOrdown == -1) {
  340. // 更新下麦时间
  341. $update = [];
  342. $update["offsite_time"] = time();
  343. $update["status"] = 2;
  344. \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
  345. }
  346. if(in_array($position,[5,6,7,8])) return $this->success("设置成功!");
  347. $redis = new Redis();
  348. $redisconfig = config("redis");
  349. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  350. $redisData = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  351. if($redisData) {
  352. $partyInfo = json_decode($redisData,true);
  353. if($upOrdown == 1) {
  354. // 删掉已有头像,防止重复
  355. $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
  356. if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $avatar) unset($partyInfo["party_user"][$k]);
  357. $partyInfo["party_user"][$position] = $avatar;
  358. } else {
  359. if(isset($partyInfo["party_user"])) unset($partyInfo["party_user"][$position]);
  360. }
  361. // sort($partyInfo["party_user"]);
  362. $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  363. }
  364. return $this->success("设置成功!");
  365. }
  366. /**
  367. * 获取派对在线人数列表
  368. */
  369. public function getOnlieList() {
  370. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
  371. $page = $this->request->request('page',1); // 分页
  372. $pageNum = $this->request->request('pageNum',10); // 分页
  373. // 分页搜索构建
  374. $pageStart = ($page-1)*$pageNum;
  375. if (!$party_id) {
  376. $this->error(__('Invalid parameters'));
  377. }
  378. $redis = new Redis();
  379. $redisconfig = config("redis");
  380. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  381. $res = $redis->hGetAll("online_".$party_id);
  382. $user_ids = [];$userList = [];
  383. if($res) $user_ids = array_values($res);
  384. // 获取用户列表信息
  385. $user_ids && $userList = \app\common\model\User::field("id,avatar,nickname,level,gender")->where(["id"=>["in",$user_ids]])->limit($pageStart,$pageNum)->select();
  386. $this->success("获取成功!",$userList);
  387. }
  388. /** 连续进房提示
  389. * @param $user_id
  390. * @param $party_id
  391. * @param $redis
  392. * @return int
  393. */
  394. private function getUserJoinPartyDays($user_id, $party_id,$redis)
  395. {
  396. $days = 1;
  397. $joinPartyDays = $redis->hGet("joinParty_days",$user_id."-".$party_id);
  398. if ($joinPartyDays){
  399. $joinPartyDaysArr = explode('-',$joinPartyDays);
  400. $oldTime = (int)$joinPartyDaysArr[0];
  401. $todayStartTime = strtotime(date('Y-m-d'));//今天0点
  402. $yesterdayStartTime = strtotime(date("Y-m-d",strtotime("-1 day")));//昨天开始时间戳
  403. $todayEndTime = $todayStartTime+24 * 60 * 60-1;//今天结束时间戳
  404. //昨天登录过,+1
  405. if ($yesterdayStartTime <= $oldTime && $oldTime < $todayStartTime){
  406. $days = (int)$joinPartyDaysArr[1];
  407. $days++;
  408. $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-'. $days);
  409. }elseif($todayStartTime <= $oldTime && $oldTime <= $todayEndTime){
  410. //今天登录过 days
  411. $days = (int)$joinPartyDaysArr[1];
  412. } else {
  413. $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
  414. }
  415. }else{
  416. $redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
  417. }
  418. return $days;
  419. }
  420. /**
  421. * 加入派对
  422. */
  423. public function joinParty() {
  424. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
  425. $room_type = $this->request->request('room_type',1); // 房间类型:1=派对2=直播
  426. $party_pass = $this->request->request("party_pass");//
  427. if (!$party_id || !in_array($room_type,[1,2])) {
  428. $this->error(__('Invalid parameters'));
  429. }
  430. $user_id = $this->auth->id;
  431. $redis = new Redis();
  432. $redisconfig = config("redis");
  433. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  434. $redis->zAdd("party_user_".$party_id,$this->auth->username,$user_id);
  435. // 判断当前用户是否存在在直播间通过切换过来的
  436. $livingUserPartyId = $redis->hGet("livingUser", $user_id);
  437. if ($livingUserPartyId) {
  438. if($livingUserPartyId != $party_id){
  439. // 扣除房间在线人数
  440. $redis->HDel("online_" . $livingUserPartyId, $user_id);
  441. // 扣除在线用户在房间情况
  442. $redis->hDel("livingUser", $user_id);
  443. }
  444. }
  445. // 记录在线用户在房间情况
  446. $redis->hSet("livingUser", $user_id, $party_id);
  447. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  448. $lsetList = [];
  449. if($room_type == 1){
  450. for($i=1;$i<=4;$i++) {
  451. $hgetlist = $redis->hGet("party_manage_".$party_id,$user_id."-".$i);
  452. $hgetlist = unserialize($hgetlist);
  453. $restime = time() - intval($hgetlist["createtime"]);
  454. // 房管
  455. if($hgetlist && $i == 1) $lsetList["manage_restime"] = "";
  456. // 禁言
  457. if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime;
  458. // 拉黑
  459. if($hgetlist && $i == 3 && $restime < $hgetlist["time"]) {
  460. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  461. $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
  462. break;
  463. }
  464. // 踢出
  465. if($hgetlist && $i == 4 && $restime < $hgetlist["time"]) {
  466. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  467. $this->error(__('您已被该房间踢出,解除时间:'.$restime));
  468. break;
  469. }
  470. }
  471. }
  472. // 判断派对密码
  473. $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  474. if($partyInfo) {
  475. $partyInfo = json_decode($partyInfo,true);
  476. if($partyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  477. // if($partyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
  478. if(isset($partyInfo["party_pass"]) && $partyInfo["party_pass"] && $partyInfo["user_id"] != $user_id && $room_type == 1) {
  479. if(($party_pass != $partyInfo["party_pass"] || strlen($party_pass) != 4)) {
  480. $this->error("派对密码不正确!");
  481. }
  482. }
  483. // 公会管理员和工会长激活派对厅
  484. /*$guildInfo = \app\common\model\Guild::where(['user_id' => $partyInfo['user_id'], 'status' => 1])->find();
  485. if ($guildInfo) {
  486. // 获取公会管理员
  487. $guildMemberInfo = \app\common\model\GuildMember::where(["user_id" => $user_id, "guild_id" => $guildInfo['id'], 'status' => 1])->find();
  488. if ($guildMemberInfo && $guildMemberInfo['role'] == 0 && $partyInfo['is_online'] == 0) {
  489. $this->error("当前直播已结束!");
  490. }
  491. }*/
  492. // // 如果是房主自己进入房间,则更新用户 为在线状态
  493. // if ($partyInfo["user_id"] == $user_id) {
  494. // if ($room_type == 2)
  495. // \app\common\model\User::update(["is_livebc" => 1], ["id" => $partyInfo["user_id"]]);
  496. // }
  497. } else {
  498. $this->error("派对信息获取失败!");
  499. }
  500. // 获取用户魅力值
  501. $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
  502. $u = [];
  503. if($users) {
  504. foreach($users as $k => $v) $u[] = [
  505. "user_id"=>$k,
  506. "charm"=>$this->changeW($v)
  507. ];
  508. }
  509. $lsetList["userCharm"] = $u;
  510. // 获取用户排行榜前三名头像
  511. $heads = $redis->hGet("user_jewel_top3",$party_id);
  512. $lsetList["userJewelTop3"] = $heads?json_decode($heads,true):[];
  513. // 判断当前用户是否收藏了此房间
  514. $cellection = $redis->hGet("room_cellection",$user_id."-".$party_id);
  515. $lsetList["is_cellection"] = $cellection?1:0;
  516. if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  517. // $partyInfo["type_name"] = $partyInfo["type_name"];
  518. } else {
  519. $partyInfo["type_name"] = "普通房";
  520. }
  521. $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]);
  522. $partyInfo['is_new'] = $partyInfo["status"] == 0 ? 1 : 0;
  523. $partyInfo['join_days'] = $this->getUserJoinPartyDays($user_id,$party_id,$redis);
  524. $lsetList["partyInfo"] = $partyInfo;
  525. return $this->success("加入成功!",$lsetList);
  526. }
  527. /**
  528. * 判断是否被禁言
  529. */
  530. public function isNotalk() {
  531. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID 逻辑ID
  532. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID 逻辑ID
  533. if (!$user_id || !$party_id) {
  534. $this->error(__('Invalid parameters'));
  535. }
  536. $redis = new Redis();
  537. $redisconfig = config("redis");
  538. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  539. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  540. $lsetList = [];
  541. $lsetList["is_notalk"] = 0;
  542. $lsetList["notalk_time"] = 0;
  543. $hgetlist = $redis->hGet("party_manage_".$party_id,$user_id."-2");
  544. $hgetlist = unserialize($hgetlist);
  545. $restime = time() - intval($hgetlist["createtime"]);
  546. // 禁言
  547. if($hgetlist && $restime < $hgetlist["time"]) {
  548. $lsetList["is_notalk"] = 1;
  549. $lsetList["notalk_time"] = $restime;
  550. }
  551. return $this->success("获取成功!",$lsetList);
  552. }
  553. /**
  554. * 退出派对
  555. */
  556. public function outParty() {
  557. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  558. if (!$party_id) {
  559. $this->error(__('Invalid parameters'));
  560. }
  561. $user_username = $this->auth->username;
  562. $redis = new Redis();
  563. $redisconfig = config("redis");
  564. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  565. $redis->zRem("party_user_".$party_id,$user_username);
  566. // 处理online_party_id
  567. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  568. $redis->HDel("online_" . $party_id, $this->auth->id);
  569. // 扣除在线用户在房间情况
  570. $redis->hDel("livingUser", $this->auth->id);
  571. // 更新房间在线状态
  572. $partyInfo = $redis->get("party_" . $party_id);
  573. if ($partyInfo) {
  574. $partyInfo = json_decode($partyInfo, true);
  575. $memCount = count($redis->hGetAll("online_" . $party_id));
  576. if ($memCount <= 0) {
  577. $partyInfo["is_online"] = 0;
  578. $redis->set("party_" . $party_id, json_encode($partyInfo));
  579. \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
  580. }
  581. }
  582. $liveInfo = $redis->get("livebc_" . $party_id);
  583. if ($liveInfo) {
  584. $liveInfo = json_decode($liveInfo, true);
  585. $liveInfo["is_online"] = 0;
  586. $redis->set("livebc_" . $party_id, json_encode($liveInfo));
  587. \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
  588. $rs_user = Db::name('user')->where('id',$this->auth->id)->update(['is_livebc'=>0]);
  589. //准备返回数据,刷礼物的总值,刷礼物人数,榜一信息
  590. //清空房间排行榜
  591. $redis->del('livebc_jewel_to_' . $party_id);
  592. $redis->hDel("user_jewel_top3",$party_id);
  593. }
  594. return $this->success("退出成功!",[]);
  595. }
  596. /**
  597. * 派对内搜索用户
  598. */
  599. public function searchUserParty() {
  600. $username = $this->request->request('username',0,"intval"); // 用户
  601. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  602. $userModel = new \app\common\model\User();
  603. $where = [];
  604. $where["username"] = $username;
  605. $userInfo = $userModel->field("id,username,avatar,nickname,level,gender")->where($where)->select();
  606. return $this->success("查询成功!",$userInfo);
  607. }
  608. /**
  609. * 设置冠名
  610. */
  611. public function setNaming() {
  612. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  613. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
  614. $type = $this->request->request('type',1); // 1:设置 0:取消设置
  615. if (!$party_id || !$user_id || ($type != 1 && $type != 0)) {
  616. $this->error(__('Invalid parameters'));
  617. }
  618. $redis = new Redis();
  619. $redisconfig = config("redis");
  620. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  621. $partyModel = new \app\common\model\Party();
  622. $data = [];
  623. $where = [];
  624. $where["id"] = $party_id;
  625. $partyInfo = $partyModel->where(["id"=>$party_id])->find();
  626. $getredisPartyInfo = $redis->get($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id);
  627. $redisPartyInfo = json_decode($getredisPartyInfo,true);
  628. if($type == 1) {
  629. $data["naming"] = $user_id;
  630. // 冠名
  631. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$user_id])->find($user_id);
  632. $redisPartyInfo["naming"] = $userInfo;
  633. } else {
  634. $data["naming"] = 0;
  635. $redisPartyInfo["naming"] = [];
  636. }
  637. $redis->set($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id,json_encode($redisPartyInfo));
  638. $res = $partyModel->update($data,$where);
  639. if($res !== false) {
  640. $this->success("操作成功!");
  641. } else {
  642. $this->error("网络错误,请稍后重试!");
  643. }
  644. }
  645. /**
  646. * 派对收藏
  647. */
  648. public function cellectionParty() {
  649. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  650. if (!$party_id) {
  651. $this->error(__('Invalid parameters'));
  652. }
  653. $user_id = $this->auth->id;
  654. $redis = new Redis();
  655. $redisconfig = config("redis");
  656. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  657. $partycellectionModel = new \app\common\model\PartyCellection();
  658. // 添加记录
  659. $data = [];
  660. $data["user_id"] = $user_id;
  661. $data["party_id"] = $party_id;
  662. if($partycellectionModel->where($data)->find()) {
  663. $res = $partycellectionModel->where($data)->delete();
  664. $redis->hDel("room_cellection",$user_id."-".$party_id);
  665. return $this->success("取消收藏成功!",$res);
  666. }
  667. $data["createtime"] = time();
  668. $res = $partycellectionModel->insert($data);
  669. $redis->hSet("room_cellection",$user_id."-".$party_id,1);
  670. return $this->success("收藏成功!",$res);
  671. }
  672. /**
  673. * 派对收藏列表
  674. */
  675. public function cellectionPartyList() {
  676. $page = $this->request->request('page',1); // 分页
  677. $pageNum = $this->request->request('pageNum',10); // 分页
  678. // 分页搜索构建
  679. $pageStart = ($page-1)*$pageNum;
  680. $partycellectionModel = new \app\common\model\PartyCellection();
  681. $userModel = new \app\common\model\User();
  682. $where = [];
  683. $where["a.user_id"] = $this->auth->id;
  684. $where["r.room_type"] = 1;
  685. $list = $partycellectionModel->alias("a")
  686. ->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")
  687. ->where($where)
  688. ->join("party r","a.party_id = r.id")
  689. ->join("party_type t","t.id = r.party_type","left")
  690. ->limit($pageStart,$pageNum)
  691. ->select();
  692. if($list) {
  693. // $redis = new Redis();
  694. // $redisconfig = config("redis");
  695. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  696. // // 获取本周第一天
  697. // $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  698. // // 获取redis 中 用户排行榜前五名
  699. // foreach($list as $k => $v) {
  700. // $getweek = $redis->zRevRange("party_jewel_get_".$v["r_id"].":".$weekday,0,4,true);
  701. // $userlist = $userModel->rankList($getweek);
  702. // if($userlist) {
  703. // $users = [];
  704. // foreach($userlist as $m => $n) {
  705. // $users[] = $n["avatar"];
  706. // }
  707. // $list[$k]["users"] = $users;
  708. // } else {
  709. // $list[$k]["users"] = [];
  710. // }
  711. //
  712. // $mod = isset($v["party_type_id"])?intval($v["party_type_id"])%5:1;
  713. // $list[$k]["party_type_color"] = $mod == 0?5:$mod;
  714. // }
  715. $users = [];
  716. foreach($list as $k => $v) {
  717. $users[$v["party_id"]] = $v["party_hot"];
  718. }
  719. $partyModel = new \app\common\model\Party();
  720. $resultInfo = $partyModel->getPatyInfoByPartyId($users,"party",0,0,1,0,20,0);
  721. $this->success("获取成功!",$resultInfo);
  722. }
  723. return $this->success("获取成功!",$list);
  724. }
  725. /**
  726. * 获取派对信息
  727. */
  728. public function getPartyInfo() {
  729. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  730. if (!$party_id) {
  731. $this->error(__('Invalid parameters'));
  732. }
  733. $user_id = $this->auth->id;
  734. $partyModel = new \app\common\model\Party();
  735. $partycellectionModel = new \app\common\model\PartyCellection();
  736. $userModel = new \app\common\model\User();
  737. // 获取主体信息
  738. $where = [];
  739. $where["a.id"] = $party_id;
  740. $partyInfo = $partyModel->alias("a")
  741. ->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")
  742. ->join("party_type rt","rt.id = a.party_type", "left")
  743. ->where($where)
  744. ->find();
  745. if($partyInfo) {
  746. $mod = isset($partyInfo["party_type"])?intval($partyInfo["party_type"])%5:1;
  747. $partyInfo["party_type_color"] = $mod == 0?5:$mod;
  748. if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  749. // $partyInfo["type_name"] = $partyInfo["type_name"];
  750. } else {
  751. $partyInfo["type_name"] = "普通房";
  752. }
  753. // 获取是否被当前用户收藏
  754. $partyInfo["is_sellection"] = 0;
  755. $where = [];
  756. $where["user_id"] = $user_id;
  757. $where["party_id"] = $party_id;
  758. if($partycellectionModel->where($where)->find()) {
  759. $partyInfo["is_sellection"] = 1;
  760. }
  761. // 获取房主信息
  762. $where = [];
  763. $where["id"] = $partyInfo["user_id"];
  764. $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
  765. // 获取技能信息
  766. /*$skillList = Model("ViewUserSkill")->getSkillInfo($partyInfo["user_id"]);
  767. $userInfo["skill"] = implode("/",$skillList);*/
  768. $userInfo["skill"] = [];
  769. $partyInfo["userInfo"] = $userInfo;
  770. $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]);
  771. }
  772. $this->success("获取成功!",$partyInfo);
  773. }
  774. /**
  775. * 派对设置
  776. */
  777. public function setParty() {
  778. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  779. $party_name = $this->request->request('party_name'); // 派对名称
  780. $party_logo = $this->request->request('party_logo'); // 派对logo/封面
  781. $party_pass = $this->request->request('party_pass'); // 派对密码
  782. $party_type = $this->request->request('party_type'); // 使用场景
  783. $is_screen = $this->request->request('is_screen'); // 是否关闭公屏:1=是,0=否
  784. $on_model = $this->request->request('on_model'); // 上麦模式:1=自由模式,2=麦序模式
  785. $room_type = 1; // 房间类型:1=派对,2=直播
  786. $background = $this->request->request('background'); // 派对背景
  787. if (!$party_id || (!$party_name && !$party_logo && !$party_pass && !$party_type && !$is_screen && !$on_model && !$background)) {
  788. $this->error(__('Invalid parameters'));
  789. }
  790. if($party_pass && strlen($party_pass) != 4) {
  791. $this->error("房间密码必须为四位!");
  792. }
  793. $partyModel = new \app\common\model\Party();
  794. $data = [];
  795. $party_name && $data["party_name"] = $party_name;
  796. $party_logo && $data["party_logo"] = $party_logo;
  797. $data["party_pass"] = $party_pass;
  798. $party_type && $data["party_type"] = $party_type;
  799. $is_screen && $data["is_screen"] = $is_screen;
  800. $on_model && $data["on_model"] = $on_model;
  801. $background && $data["background"] = $background;
  802. $where = [];
  803. $where["id"] = $party_id;
  804. $res = $partyModel->update($data,$where);
  805. if($res) {
  806. // 获取派对类型
  807. if($party_type) $data["type_name"] = \app\common\model\PartyType::where(["id"=>$party_type])->value("name");
  808. // 存redis 房间信息
  809. $redis = new Redis();
  810. $redisconfig = config("redis");
  811. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  812. $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  813. if($partyInfo) {
  814. $partyInfo = json_decode($partyInfo,true);
  815. $partyInfo = array_replace($partyInfo,$data);
  816. $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  817. }
  818. $this->success("房间设置成功!",$data);
  819. } else {
  820. $this->error("网络错误,请稍后重试!");
  821. }
  822. }
  823. /**
  824. * 获取派对背景
  825. */
  826. public function getDefaultBackground() {
  827. $list = Db::name('party_background')->select();
  828. $list = list_domain_image($list,['image']);
  829. $this->success("success",$list);
  830. }
  831. //腾讯im设置管理员
  832. private function setGroupAdmin($user_id,$party_id,$status)
  833. {
  834. $tenIm = new Tenim();
  835. return $tenIm->setUpIMAdmin($user_id, $party_id,$status);
  836. }
  837. /**
  838. * 派对管理设置
  839. */
  840. public function partyManageSet() {
  841. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  842. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
  843. $item = $this->request->request('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  844. $time = $this->request->request('time'); // 限制时间(单位:秒):0=永久
  845. if (!$party_id || !$user_id || !in_array($item,[1,2,3,4])) {
  846. $this->error(__('Invalid parameters'));
  847. }
  848. if($item>1 && $time<=0) {
  849. $this->error(__('时间设置有误'));
  850. }
  851. $partyUserId = \app\common\model\Party::where('id', $party_id)->value('user_id');
  852. if ($partyUserId == $user_id && in_array($item, [2, 3, 4])) {
  853. $this->error("您当前无权限操作!");
  854. }
  855. // 获取用户信息
  856. $userInfo = \app\common\model\User::field("noble,avatar,nickname,gender,level")->where(["id"=>$user_id])->find();
  857. if(!$userInfo) $this->error("用户信息获取失败!");
  858. // 国王防踢。
  859. /*$noble_no = \app\common\model\NobleLevel::where(["id"=>$userInfo['noble']])->value("level_no");
  860. if(($item == 3 || $item == 4) && $noble_no == "p08PCcNB") {
  861. $this->error("对方已开通国王贵族,踢出房间失败!");
  862. }*/
  863. $redis = new Redis();
  864. $redisconfig = config("redis");
  865. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  866. $data = [];
  867. $data["user_id"] = $user_id;
  868. $data["avatar"] = $userInfo->avatar;
  869. $data["nickname"] = $userInfo->nickname;
  870. $data["gender"] = $userInfo->gender;
  871. $data["level"] = $userInfo->level;
  872. $data["item"] = $item;
  873. $data["time"] = $time;
  874. $data["createtime"] = time();
  875. $res = $redis->hSet("party_manage_".$party_id,$user_id."-".$item,serialize($data));
  876. if ($item==1) {
  877. $this->setGroupAdmin($user_id,$party_id,1);
  878. }
  879. if($res !== false) {
  880. $this->success("设置成功!",$data);
  881. } else {
  882. $this->error("网络错误,请稍后重试!");
  883. }
  884. }
  885. /**
  886. * 派对管理设置列表
  887. */
  888. public function partyManageSetList() {
  889. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  890. $item = $this->request->request('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  891. if (!$party_id || !in_array($item,[1,2,3,4])) {
  892. $this->error(__('Invalid parameters'));
  893. }
  894. $time = time();
  895. $redis = new Redis();
  896. $redisconfig = config("redis");
  897. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  898. $hget=$redis->hGetAll("party_manage_".$party_id);
  899. $list=array();
  900. foreach($hget as $key=>$val) {
  901. if(substr($key,-2) == "-".$item){
  902. array_push($list,unserialize($val));
  903. }
  904. }
  905. if($list) {
  906. foreach($list as $k => $v) if(bcadd($v["createtime"],$v["time"]) <= $time && $v["item"] > 1) unset($list[$k]);
  907. if($list) $list = array_values($list);
  908. }
  909. $this->success("获取成功!",$list);
  910. }
  911. /**
  912. * 派对管理设置移除
  913. */
  914. public function partyManageSetDel() {
  915. $id = $this->request->request('user_id',0,"intval"); // userid
  916. $party_id = $this->request->request('party_id',0,"intval"); // 房间ID
  917. $item = $this->request->request('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  918. if (!$id || !in_array($item,[1,2,3,4])) {
  919. $this->error(__('Invalid parameters'));
  920. }
  921. $redis = new Redis();
  922. $redisconfig = config("redis");
  923. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  924. $res = $redis->hDel("party_manage_".$party_id,$id."-".$item);
  925. if ($item==1) {
  926. $this->setGroupAdmin($id,$party_id,0);
  927. }
  928. if($res !== false) {
  929. $this->success("移除成功!",$res);
  930. } else {
  931. $this->error("网络错误,请稍后重试!");
  932. }
  933. }
  934. /**
  935. * 更新派对公告
  936. */
  937. public function savePartyNotice() {
  938. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  939. $room_type = $this->request->request('room_type',1); // 房间类型:1=派对2=直播
  940. $party_notice = $this->request->request('party_notice'); // 公告标题
  941. $party_notice_detail = $this->request->request('party_notice_detail'); // 公告内容
  942. if (!$party_id || !$party_notice || !$party_notice_detail) {
  943. $this->error(__('Invalid parameters'));
  944. }
  945. $partyModel = new \app\common\model\Party();
  946. $where = [];
  947. $where["id"] = $party_id;
  948. $data = [];
  949. $data["party_notice"] = $party_notice;
  950. $data["party_notice_detail"] = $party_notice_detail;
  951. $res = $partyModel->update($data,$where);
  952. if($res) {
  953. // 存redis 房间信息
  954. $redis = new Redis();
  955. $redisconfig = config("redis");
  956. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  957. $partyInfo = $redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  958. if($partyInfo) {
  959. $partyInfo = json_decode($partyInfo,true);
  960. $partyInfo = array_replace($partyInfo,$data);
  961. $redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  962. }
  963. $this->success("更新成功!",$res);
  964. } else {
  965. $this->error("网络错误,请稍后重试!");
  966. }
  967. }
  968. /**
  969. * 开始排麦
  970. */
  971. public function addLineUp() {
  972. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  973. $mai_id = $this->request->request('mai_id',"1"); // 麦位置
  974. if (!$party_id) {
  975. $this->error(__('Invalid parameters'));
  976. }
  977. $userInfo = $this->auth->getUserinfo();
  978. $userid = $userInfo['id'];
  979. // 获取 房间信息
  980. $partyUser = \app\common\model\Party::where(['id'=>$party_id])->value("user_id");
  981. if(!$partyUser) $this->error(__('房间信息未找到!'));
  982. // $is_home = $partyUser == $userInfo->id?1:0;
  983. $redis = new Redis();
  984. $redisconfig = config("redis");
  985. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  986. $data = unserialize($redis->hGet("party_lineup",$party_id));
  987. $data[$userid]["user_id"] = $userInfo['id'];
  988. $data[$userid]["avatar"] = $userInfo['avatar'];
  989. $data[$userid]["mai_id"] = $mai_id;
  990. $data[$userid]["nickname"] = $userInfo['nickname'];
  991. // $data["is_home"] = $is_home;
  992. $data[$userid]["level"] = $userInfo['level'];
  993. $data[$userid]["gender"] = $userInfo['gender'];
  994. $res = false;
  995. $data && $res = $redis->hSet("party_lineup",$party_id,serialize($data));
  996. $data = array_values($data);
  997. if($res !== false) {
  998. $this->success("设置成功!",$data);
  999. } else {
  1000. $this->error("网络错误,请稍后重试!");
  1001. }
  1002. }
  1003. /**
  1004. * 取消排麦
  1005. */
  1006. public function cancelLineUp() {
  1007. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  1008. $user_id = $this->request->request('user_id',0); // 直播间ID
  1009. $is_empty = $this->request->request('is_empty',0,"intval"); // 是否清空排麦列表 1=清空,0=不清空
  1010. if (!$party_id) {
  1011. $this->error(__('Invalid parameters'));
  1012. }
  1013. $userid = $this->auth->id;
  1014. $redis = new Redis();
  1015. $redisconfig = config("redis");
  1016. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1017. $data=unserialize($redis->hGet("party_lineup",$party_id));
  1018. if($data && $is_empty != 1) {
  1019. foreach($data as $k => $v) {
  1020. if($v["user_id"] == $user_id) {
  1021. unset($data[$k]);
  1022. break;
  1023. }
  1024. }
  1025. }
  1026. // unset($data[$userid]);
  1027. if($is_empty == 1) $data = [];
  1028. $redis->hSet("party_lineup",$party_id,serialize($data));
  1029. $this->success("移除成功!");
  1030. }
  1031. /**
  1032. * 排麦列表
  1033. */
  1034. public function lineUpList() {
  1035. $party_id = $this->request->request('party_id',0,"intval"); // 直播间ID
  1036. if (!$party_id) {
  1037. $this->error(__('Invalid parameters'));
  1038. }
  1039. $redis = new Redis();
  1040. $redisconfig = config("redis");
  1041. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1042. $data = $redis->hGet("party_lineup",$party_id);
  1043. $dataArr = unserialize($data);
  1044. $datas = [];
  1045. is_array($dataArr) && $datas=array_values($dataArr);
  1046. if($datas) {
  1047. $this->success("获取成功!",$datas);
  1048. } else {
  1049. $this->success("获取成功!",[]);
  1050. }
  1051. }
  1052. /**
  1053. * 获取派对类型
  1054. */
  1055. public function getPatyType() {
  1056. $room_type = 1;
  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. }