Party.php 76 KB

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