Party.php 76 KB

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