Party.php 93 KB

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