Party.php 93 KB

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