Party.php 93 KB

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