Party.php 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use Redis;
  5. use think\Db;
  6. use think\Exception;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use app\common\service\RoomService;
  10. use app\common\library\Easemob;
  11. require_once EXTEND_PATH . 'shengwang/src/RtcTokenBuilder2.php';
  12. require_once EXTEND_PATH . 'shengwang/src/RtmTokenBuilder2.php';
  13. /**
  14. *
  15. * 派对信息接口
  16. */
  17. class Party extends Api
  18. {
  19. protected $noNeedLogin = ['test_party_attr','updatePartyInfo','getPatyType','shengwang_token','addUserPositionToParty', 'clearMoney','clearCharm','getPartyRankList','getDefaultBackground','getPartGifList','getPartHeadgifList','isNotalk','getMusicList','updateTops','handleParty'];
  20. protected $noNeedRight = ['*'];
  21. protected $redis;
  22. public function __construct()
  23. {
  24. $this->roomTypeArr = [1=>"party",2=>"live"];
  25. //redis
  26. $redis = new Redis();
  27. $redisconfig = config("redis");
  28. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  29. if ($redisconfig['redis_pwd']) {
  30. $redis->auth($redisconfig['redis_pwd']);
  31. }
  32. if($redisconfig['redis_selectdb'] > 0){
  33. $redis->select($redisconfig['redis_selectdb']);
  34. }
  35. $this->redis = $redis;
  36. parent::__construct();
  37. }
  38. /**
  39. * 创建/进入派对
  40. */
  41. public function createParty() {
  42. if(!$this->apiLimit()){
  43. $this->error('访问频繁,请稍后重试');
  44. }
  45. $party_logo = input('party_logo');
  46. $party_name = input('party_name');
  47. $seatnum = input('seatnum');
  48. $is_public = input('is_public',1); //是否公开:1=是,0=否
  49. //$room_type = input('room_type',2); // 房间类型:1=公会房,2=K歌房
  50. $room_type = 2; // 房间类型:1=公会房,2=K歌房
  51. $partyModel = new \app\common\model\Party();
  52. $user_id = $this->auth->id;
  53. // 开直播时,判断当前用户是否为主播
  54. $anchorInfo = [];
  55. if($room_type == 2) {
  56. /*$anchorInfo = \app\common\model\UserAnchor::where(["user_id"=>$user_id])->find();
  57. $anchorInfo || $this->error("您还未申请主播!");
  58. $anchorInfo->status == 0 && $this->error("您的主播申请信息还未受理,请耐心等待!");
  59. $anchorInfo->status == 2 && $this->error("您的主播申请未通过,请重新提交!");*/
  60. }
  61. // 1。实名认证
  62. $userAuthInfo = \app\common\model\UserAuth::where(["user_id"=>$user_id])->find();
  63. if($userAuthInfo) {
  64. if($userAuthInfo->status == 0) {
  65. $this->error("您的实名认证还在这审核中...,请耐心等待!");
  66. } elseif($userAuthInfo->status == 2) {
  67. $this->error("您的实名认证审核未通过,请重新审核!");
  68. }
  69. } else {
  70. $this->error("请先申请实名认证!");
  71. }
  72. if($room_type == 1) {
  73. // 2。开厅申请
  74. $authInfo = model('Guild')->where(["user_id"=>$user_id])->order('id desc')->find();
  75. if($authInfo) {
  76. if($authInfo->status == 0) {
  77. $this->error("您的公会申请还在这审核中...,请耐心等待!");
  78. } elseif($authInfo->status == -1) {
  79. $this->error("您的公会申请已解散,请重新申请!");
  80. }
  81. } else {
  82. $this->error("请先申请公会!");
  83. }
  84. }
  85. // 判断派对是否存在
  86. $where = [];
  87. $where["user_id"] = $user_id;
  88. $where["room_type"] = $room_type;
  89. $sqlPartyInfo = $partyModel->where($where)->find();
  90. if(!$sqlPartyInfo) { // sql中不存在派对信息
  91. $party_type = 8; // 派对类型(情感互动,心动点单 等)
  92. $party_notice = "请编辑房间公告"; // 派对公告
  93. $party_notice_detail = "请编辑房间公告内容!"; // 派对公告详情
  94. $party_ids = $partyModel->column("party_id");
  95. // 创建派对ID (临时ID四位,派对数不超过8999)
  96. $party_id = $this->auth->getUinqueId(5,$party_ids);
  97. if($party_id > 99999) {
  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. $party_type = 7;
  111. }
  112. //创建房间,初始化环信
  113. $easemob_room_id = $this->easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type);
  114. //写入派对
  115. Db::startTrans();
  116. $data = [];
  117. $data["user_id"] = $this->auth->id;
  118. $data["room_type"] = $room_type;
  119. $data["party_id"] = $party_id;
  120. $data["party_hot"] = 0;
  121. $data["party_name"] = $party_name;
  122. $data["party_logo"] = $party_logo;
  123. $data["party_type"] = $party_type;
  124. $data["party_notice"] = $party_notice;
  125. $data["party_notice_detail"] = $party_notice_detail;
  126. $data["is_online"] = 1;
  127. $data["status"] = 1;//直接正常状态
  128. $data["is_recommend"] = 0;
  129. $data["createtime"] = time();
  130. $data["easemob_room_id"] = $easemob_room_id;
  131. if($room_type == 1){
  132. $data["platRate"] = config('site.party_party_platrate');
  133. $data["guilderRate"] = config('site.party_party_guilderrate');
  134. }else{
  135. $data["platRate"] = config('site.party_live_platrate');
  136. $data["guilderRate"] = config('site.party_live_guilderrate');
  137. }
  138. $id = $partyModel->insertGetId($data);
  139. if(!$id) {
  140. Db::rollback();
  141. $this->error("房间创建失败,请稍后重试!");
  142. }
  143. $data["id"] = $id;
  144. if($room_type == 2) {
  145. \app\common\model\User::update(["is_live"=>1],["id"=>$user_id]);
  146. }
  147. $partyInfo = $partyModel->where(["id"=>$id])->find();
  148. $partyInfo["is_new"] = 1;
  149. // 冠名
  150. /*$userInfo = [];
  151. if($partyInfo["naming"]) {
  152. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$partyInfo["naming"]])->find();
  153. }*/
  154. // 头像
  155. /*if($partyInfo["user_id"] > 0 && $room_type == 2) {
  156. $partyInfo["avatar"] = \app\common\model\User::where(["id"=>$partyInfo["user_id"]])->value("avatar");
  157. }*/
  158. $partyInfo["avatar"] = $party_logo;
  159. // 派对类型
  160. $partyTypeName = "普通房";
  161. if($partyInfo["party_type"]) {
  162. $partyTypeName = \app\common\model\PartyType::where(["id"=>$partyInfo["party_type"]])->value("name");
  163. }
  164. $partyInfo["naming"] = [];
  165. $partyInfo["type_name"] = $partyTypeName;
  166. // 加入缓存排序,rediskey:派对热度
  167. $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  168. // 加入缓存,rediskey:派对详情
  169. $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($partyInfo));
  170. if($room_type != 2) {
  171. //更新家族 party_id
  172. $guildWhere['user_id'] = $user_id;
  173. $guildWhere['status'] = 1;
  174. $guild = model('Guild')->where($guildWhere)->order('id desc')->find();
  175. if (!empty($guild)) {
  176. $rs_guild = model('Guild')->where($guildWhere)->update(['party_id'=>$partyInfo['id']]);
  177. if($rs_guild === false){
  178. Db::rollback();
  179. $this->error('创建房间失败');
  180. }
  181. }
  182. }
  183. Db::commit();
  184. } else {
  185. $partyInfo = $sqlPartyInfo;
  186. //初始化房间
  187. if($partyInfo['status'] == 3){
  188. $update = [
  189. 'status' => 1,
  190. 'party_hot' => 0,
  191. 'party_logo' => $party_logo,
  192. 'party_name' => $party_name,
  193. 'seatnum' => $seatnum,
  194. 'is_public' => $is_public,
  195. 'party_notice' => '请编辑房间公告',
  196. 'party_notice_detail' => '请编辑房间公告内容',
  197. 'is_recommend' => 0,
  198. 'is_close' => 0,
  199. 'party_pass' => '',
  200. 'is_screen' => 0,
  201. 'on_model' => 1,
  202. 'background' => '',
  203. ];
  204. if($room_type == 1){
  205. $update["platRate"] = config('site.party_party_platrate');
  206. $update["guilderRate"] = config('site.party_party_guilderrate');
  207. }else{
  208. $update["platRate"] = config('site.party_live_platrate');
  209. $update["guilderRate"] = config('site.party_live_guilderrate');
  210. }
  211. $matedata = [
  212. 'seatnum'=> $seatnum,//麦位数量
  213. //'waitsing_list' => json_encode([]),//已点歌曲列表
  214. 'party_name' => $party_name,//房名字
  215. 'party_logo' => $party_logo,//logo
  216. 'is_public' => $is_public,//是否公开
  217. 'room_type' => $room_type,//房间类型
  218. 'online_user_num' => 0,//当前房间在线的人
  219. //'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  220. 'background' => '', //房间背景
  221. 'party_notice' => $update['party_notice'],
  222. 'party_notice_detail' => $update['party_notice_detail'],
  223. ];
  224. }else{
  225. $update = [
  226. 'status' => 1,
  227. 'party_logo' => $party_logo,
  228. 'party_name' => $party_name,
  229. 'is_public' => $is_public,
  230. ];
  231. $matedata = [
  232. 'party_name' => $party_name,//房名字
  233. 'party_logo' => $party_logo,//logo
  234. 'is_public' => $is_public,//是否公开
  235. ];
  236. }
  237. //没有房间号的给创建一个
  238. if(empty($partyInfo['easemob_room_id'])){
  239. $easemob_room_id = $this->easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type);
  240. $update['easemob_room_id'] = $easemob_room_id;
  241. }else{
  242. $easemob_room_id = $partyInfo['easemob_room_id'];
  243. }
  244. Db::name('party')->where('id',$partyInfo['id'])->update($update);
  245. $partyInfo = $partyModel->where(["id"=>$partyInfo['id']])->find();
  246. $partyInfo["is_new"] = 0;
  247. // 加入缓存,rediskey:派对详情
  248. $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo['id'],json_encode($partyInfo));
  249. //[环信]初始化房间,初始化自定义属性
  250. $easemob = new Easemob();
  251. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$matedata);
  252. }
  253. //声网三个token
  254. $partyInfo['shengwang_token'] = $this->shengwang_token_private($easemob_room_id);
  255. //帮房主走一次joinparty
  256. $this->creater_joinParty($partyInfo['id'],$easemob_room_id,$partyInfo['user_id']);
  257. $this->success("获取成功!",$partyInfo);
  258. }
  259. public function test2()
  260. {
  261. $list = Db::name('party')->order('id asc')->select();
  262. foreach($list as $key => $partyInfo){
  263. dump($partyInfo['id']);
  264. //去环信建个群组
  265. $easemob_room_id = $this->easemob_createroom_new($partyInfo['party_name'],$partyInfo['party_logo'],8,$partyInfo['is_public'],$partyInfo['room_type'],$partyInfo['user_id']);
  266. $update['easemob_room_id'] = $easemob_room_id;
  267. Db::name('party')->where('id',$partyInfo['id'])->update($update);
  268. $partyInfo = Db::name('party')->where(["id"=>$partyInfo['id']])->find();
  269. $partyInfo["is_new"] = 0;
  270. // 加入缓存,rediskey:派对详情
  271. $room_type = $partyInfo['room_type'];
  272. $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo['id'],json_encode($partyInfo));
  273. dump($easemob_room_id);
  274. }
  275. }
  276. //创建房间,初始化环信
  277. private function easemob_createroom_new($party_name,$party_logo,$seatnum,$is_public,$room_type,$auth_uid){
  278. //[环信]创建聊天室
  279. $easemob = new Easemob();
  280. $easemob_room_id = $easemob->room_createRoom($party_name,$party_name,$auth_uid);
  281. if(empty($easemob_room_id)){
  282. $this->error('创建房间失败');
  283. }
  284. //[环信]初始化房间,初始化自定义属性
  285. $matedata = [
  286. 'seatnum'=> $seatnum,//麦位数量
  287. 'waitsing_list' => json_encode([]),//已点歌曲列表
  288. 'party_name' => $party_name,//房名字
  289. 'party_logo' => $party_logo,//logo
  290. 'is_public' => $is_public,//是否公开
  291. 'room_type' => $room_type,//房间类型
  292. 'online_user_num' => 0,//当前房间在线的人
  293. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  294. 'background' => '', //房间背景
  295. //'party_notice' => '请编辑房间公告',
  296. //'party_notice_detail' => '请编辑房间公告内容',
  297. ];
  298. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$auth_uid,$matedata);
  299. //[环信]初始化N个麦位
  300. $seatdata = [];
  301. for($i=0;$i<$seatnum;$i++){
  302. $seat = [
  303. 'charm' => 0, //红心,魅力值
  304. 'isMaster' => false, // 是否是房主
  305. 'headUrl' => '', // 头像
  306. 'userNo' => '', // 座位上用户no
  307. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  308. 'name' => '', // 座位上用户昵称
  309. 'seatIndex' => -1, // 座位编号
  310. 'chorusSongCode' => '', // 是否合唱
  311. 'isAudioMuted' => 1, // 是否静音
  312. 'isVideoMuted' => 0, // 是否开启视频
  313. 'checked' => false, // 用于送礼物选择用户
  314. 'isUsed' => true, // 用于送礼物选择用户
  315. 'gender' => 1, //性别
  316. ];
  317. //创建完房间就进入,所以房主直接在麦位0
  318. /*if($i == 0){
  319. $seat['isMaster'] = true;
  320. $seat['headUrl'] = localpath_to_netpath($this->auth->avatar);
  321. $seat['userNo'] = $this->auth->id;
  322. $seat['rtcUid'] = $this->auth->id;
  323. $seat['name'] = $this->auth->nickname;
  324. $seat['seatIndex'] = $i;
  325. }*/
  326. $seatdata['seat'.$i] = json_encode($seat);
  327. }
  328. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$auth_uid,$seatdata);
  329. return $easemob_room_id;
  330. }
  331. private function easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type){
  332. //[环信]创建聊天室
  333. $easemob = new Easemob();
  334. $easemob_room_id = $easemob->room_createRoom($party_name,$party_name,$this->auth->id);
  335. if(empty($easemob_room_id)){
  336. $this->error('创建房间失败');
  337. }
  338. //[环信]初始化房间,初始化自定义属性
  339. $matedata = [
  340. 'seatnum'=> $seatnum,//麦位数量
  341. 'waitsing_list' => json_encode([]),//已点歌曲列表
  342. 'party_name' => $party_name,//房名字
  343. 'party_logo' => $party_logo,//logo
  344. 'is_public' => $is_public,//是否公开
  345. 'room_type' => $room_type,//房间类型
  346. 'online_user_num' => 0,//当前房间在线的人
  347. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  348. 'background' => '', //房间背景
  349. //'party_notice' => '请编辑房间公告',
  350. //'party_notice_detail' => '请编辑房间公告内容',
  351. ];
  352. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$matedata);
  353. //[环信]初始化N个麦位
  354. $seatdata = [];
  355. for($i=0;$i<$seatnum;$i++){
  356. $seat = [
  357. 'charm' => 0, //红心,魅力值
  358. 'isMaster' => false, // 是否是房主
  359. 'headUrl' => '', // 头像
  360. 'userNo' => '', // 座位上用户no
  361. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  362. 'name' => '', // 座位上用户昵称
  363. 'seatIndex' => -1, // 座位编号
  364. 'chorusSongCode' => '', // 是否合唱
  365. 'isAudioMuted' => 1, // 是否静音
  366. 'isVideoMuted' => 0, // 是否开启视频
  367. 'checked' => false, // 用于送礼物选择用户
  368. 'isUsed' => true, // 用于送礼物选择用户
  369. 'gender' => 1, //性别
  370. ];
  371. //创建完房间就进入,所以房主直接在麦位0
  372. /*if($i == 0){
  373. $seat['isMaster'] = true;
  374. $seat['headUrl'] = localpath_to_netpath($this->auth->avatar);
  375. $seat['userNo'] = $this->auth->id;
  376. $seat['rtcUid'] = $this->auth->id;
  377. $seat['name'] = $this->auth->nickname;
  378. $seat['seatIndex'] = $i;
  379. }*/
  380. $seatdata['seat'.$i] = json_encode($seat);
  381. }
  382. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$seatdata);
  383. return $easemob_room_id;
  384. }
  385. //声网3个token
  386. public function shengwang_token(){
  387. $party_id = input('party_id'); // 派对ID
  388. $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
  389. if(!$partyInfo) {
  390. $this->error(__('派对不存在!'));
  391. }
  392. $rs = $this->shengwang_token_private($partyInfo['easemob_room_id']);
  393. $this->success(1,$rs);
  394. }
  395. //声网3个token
  396. private function shengwang_token_private($easemob_room_id){
  397. $config = config('shengwang');
  398. $appId = $config['app_id'];
  399. $appCertificate = $config['app_zhengshu'];;
  400. $channelName = $easemob_room_id;
  401. $uid = 0; //任何人都可进入
  402. $uidStr = '0';//任何人都可进入
  403. $tokenExpirationInSeconds = 86400;
  404. $privilegeExpirationInSeconds = 86400;
  405. //rtc token
  406. $rtc_token = \RtcTokenBuilder2::buildTokenWithUid($appId, $appCertificate, $channelName, $uid, \RtcTokenBuilder2::ROLE_PUBLISHER, $tokenExpirationInSeconds, $privilegeExpirationInSeconds);
  407. //chorus token
  408. $chorus_token = \RtcTokenBuilder2::buildTokenWithUid($appId, $appCertificate, $channelName.'_ex', $uid, \RtcTokenBuilder2::ROLE_PUBLISHER, $tokenExpirationInSeconds, $privilegeExpirationInSeconds);
  409. //rtm token
  410. $user = '0';
  411. $expireTimeInSeconds = 86400;
  412. $rtm_token = \RtmTokenBuilder2::buildToken($appId, $appCertificate, $user, $expireTimeInSeconds);
  413. $result = [
  414. 'rtc_token' => $rtc_token,
  415. 'chorus_token' => $chorus_token,
  416. 'rtm_token' => $rtm_token,
  417. ];
  418. //dump($result);
  419. return $result;
  420. }
  421. /**
  422. * 解散房间
  423. */
  424. public function closeParty() {
  425. $party_id = input('party_id'); // 派对ID
  426. $room_type = input('room_type',1); // 房间类型:1=派对,2=直播
  427. if (!$party_id || !in_array($room_type,[1,2])) {
  428. $this->error(__('Invalid parameters'));
  429. }
  430. $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
  431. if(!$partyInfo) {
  432. $this->error(__('派对不存在!'));
  433. }
  434. $room_type = $partyInfo['room_type'];
  435. if($partyInfo['user_id'] != $this->auth->id){
  436. $this->error('房主才能解散');
  437. }
  438. //房间从首页列表消失
  439. $update = [
  440. 'status' => 3,
  441. ];
  442. Db::name('party')->where('id',$party_id)->update($update);
  443. // 存redis 房间信息
  444. $redispartyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  445. if($redispartyInfo) {
  446. $redispartyInfo = json_decode($redispartyInfo,true);
  447. $redispartyInfo = array_replace($redispartyInfo,$update);
  448. $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($redispartyInfo));
  449. }
  450. //用户魅力榜,清空
  451. $this->redis->zRemRangeByRank("hourCharm_".$party_id, 0,-1);
  452. //用户贡献榜,清空
  453. $this->redis->zRemRangeByRank("hourWealth_".$party_id, 0,-1);
  454. //贡献榜前三,设置为空
  455. $this->redis->hSet("user_jewel_top3",$party_id,json_encode([]));
  456. //排麦列表,清空
  457. $this->redis->hSet("party_lineup",$party_id,serialize([]));
  458. //清空房间
  459. $user_ids = $this->redis->hGetAll("online_".$party_id);
  460. if(!empty($user_ids)){
  461. foreach($user_ids as $key => $userId){
  462. $this->redis->HDel("online_" . $party_id, $userId);
  463. $this->redis->zRem("party_user_".$party_id,$userId); //新加的
  464. $this->redis->hDel("livingUser", $userId);
  465. }
  466. }
  467. //全部下麦
  468. $key = "party_seat_".$party_id;
  469. $seats = $this->redis->hGetAll($key);
  470. foreach($seats as $seat_num => $seatuserid){
  471. $this->redis->HDel($key, $seat_num);
  472. }
  473. //环信操作
  474. $easemob_room_id = $partyInfo['easemob_room_id'];
  475. if(!empty($easemob_room_id)){
  476. ///初始化房间信息,重置自定义属性
  477. $matedata = [
  478. 'waitsing_list' => json_encode([]), //清空房间待唱列表
  479. 'online_user_num' => 0, //当前房间在线的人
  480. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  481. ];
  482. $easemob = new Easemob();
  483. $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  484. //重置麦位
  485. $seatnum = $partyInfo['seatnum'];
  486. $seatdata = [];
  487. for($i=0;$i<$seatnum;$i++){
  488. $seat = [
  489. 'charm' => 0, //红心,魅力值
  490. 'isMaster' => false, // 是否是房主
  491. 'headUrl' => '', // 头像
  492. 'userNo' => '', // 座位上用户no
  493. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  494. 'name' => '', // 座位上用户昵称
  495. 'seatIndex' => -1, // 座位编号
  496. 'chorusSongCode' => '', // 是否合唱
  497. 'isAudioMuted' => 1, // 是否静音
  498. 'isVideoMuted' => 0, // 是否开启视频
  499. 'checked' => false, // 用于送礼物选择用户
  500. 'isUsed' => true, // 用于送礼物选择用户
  501. 'gender' => 1, //性别
  502. ];
  503. $seatdata['seat'.$i] = json_encode($seat);
  504. }
  505. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$seatdata);
  506. //清空房间人,聊天室
  507. //分页获取聊天室成员列表
  508. $usernames = $easemob->room_listRoomMembers($easemob_room_id,0);
  509. //批量移除聊天室成员
  510. $user_ids = [];
  511. if(!empty($usernames)){
  512. foreach($usernames as $username){
  513. foreach($username as $key => $val)
  514. {
  515. $user_ids[] = $val;
  516. }
  517. }
  518. }
  519. if(!empty($user_ids)){
  520. $easemob->room_removeRoomMembers($easemob_room_id,$user_ids);
  521. }
  522. }
  523. $this->success();
  524. }
  525. /**
  526. * 获取派对列表排序
  527. */
  528. public function getPartyList() {
  529. $order = input('orderby','new');
  530. $this->reset_all_party();
  531. //
  532. $orderby = 'party.party_hot desc, party.id desc';
  533. $where = [];
  534. if($order == 'recommend'){
  535. $where['party.is_recommend'] = 1;
  536. }
  537. if($order == 'follow'){
  538. $follow_party_ids = Db::name('party_follow')->where('user_id',$this->auth->id)->column('party_id');
  539. $where['party.id'] = ['IN',$follow_party_ids];
  540. }
  541. $where['party.status'] = 1; //正常
  542. //$where['party.is_online'] = 1; //在线
  543. $where['party.is_close'] = 0; //未关闭
  544. $where['party.is_public'] = 1; //是否公开:1=是,0=否
  545. $field = 'party.id,party.user_id,party.room_type,party.party_id,party.party_name,party.party_hot,
  546. party.party_logo,party.party_pass,party.party_type,party.easemob_room_id,type.name as type_name';
  547. $list = Db::name('party')->alias('party')
  548. ->join('party_type type','party.party_type = type.id','LEFT')
  549. ->field($field)->where($where)->order($orderby)->autopage()->select();
  550. $list = list_domain_image($list,['party_logo']);
  551. $resdata = [];
  552. if (!empty($list)) {
  553. $roomService = new RoomService();
  554. foreach($list as $k => $v) {
  555. $mod = isset($v["party_type"]) ? intval($v["party_type"])%5 : 1;
  556. //房间用户列表
  557. $userDataResult = $roomService->getPartyUserList(['party_id'=>$v['id']]);
  558. $userDataRes = isset($userDataResult['data']) ? $userDataResult['data'] : [];
  559. $partyUserList = [
  560. 'user_list' => isset($userDataRes['member_list']) ? $userDataRes['member_list'] : [],
  561. 'online_num' => isset($userDataRes['online_num']) ? $userDataRes['online_num'] : 0,
  562. ];
  563. $resdata[] = [
  564. "id" => $v["id"],
  565. "user_id" => $v["user_id"],
  566. "room_type" => $v['room_type'],
  567. "party_id" => $v["party_id"],
  568. "r_id" => $v["party_id"],
  569. "party_name" => $v["party_name"],
  570. "party_hot" => $v['party_hot'],
  571. "party_logo" => $v["party_logo"],
  572. "party_pass" => $v["party_pass"] ? $v["party_pass"]:"",
  573. "party_type" => $v['type_name'],
  574. "party_type_color" => $mod == 0?5:$mod,
  575. "easemob_room_id" => $v['easemob_room_id'],
  576. "party_user" => $partyUserList,
  577. ];
  578. }
  579. }
  580. $this->success("获取成功!",$resdata);
  581. }
  582. //把所有房间的信息,都放到redis里去
  583. private function reset_all_party(){
  584. //rediskey:派对热度
  585. $redisPartyRankList = $this->redis->zRevRange("partyRank",0,-1,true);
  586. //rediskey:派对热度
  587. $redisLiveRankList = $this->redis->zRevRange("liveRank",0,-1,true);
  588. $roomService = new RoomService();
  589. if(!$redisPartyRankList) {//party
  590. $roomParams = ['room_type' => 1];
  591. $roomPartyRes = $roomService->roomRedis($roomParams);
  592. $redisPartyRankList = $roomPartyRes['data'];
  593. }
  594. if(!$redisLiveRankList) {//live
  595. $roomParams = ['room_type' => 2];
  596. $roomLiveRes = $roomService->roomRedis($roomParams);
  597. $redisLiveRankList = $roomLiveRes['data'];
  598. }
  599. }
  600. /**
  601. * 获取派对用户排序,(日榜,周榜,月榜) X (魅力榜,财富榜)
  602. */
  603. /*public function getPartyUserRank_old() {
  604. $room_type = input('room_type',1); // 房间类型
  605. $party_id = input("party_id");// 派对ID
  606. if (!$party_id) $this->error(__('Invalid parameters'));
  607. $res = [];
  608. // 获取今天
  609. $day = date("Ymd");
  610. // 获取本周第一天
  611. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  612. // 获取本月第一天
  613. $monthday = date("Ym01");
  614. $userModel = new \app\common\model\User();
  615. // 获取条数
  616. $num = 50;
  617. // 获取50条魅力排行日记录
  618. $getday = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$day."d",0,$num-1,true);
  619. $res['getRankListDay'] = $userModel->rankList($getday);
  620. // 获取50条财富排行日记录
  621. $today = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true);
  622. $res['toRankListDay'] = $userModel->rankList($today);
  623. // 获取50条魅力排行周记录
  624. $getweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$weekday."w",0,$num-1,true);
  625. $res['getRankListWeek'] = $userModel->rankList($getweek);
  626. // 获取50条财富排行周记录
  627. $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$weekday."w",0,$num-1,true);
  628. $res['toRankListWeek'] = $userModel->rankList($toweek);
  629. // 获取50条魅力排行月记录
  630. $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$monthday."m",0,$num-1,true);
  631. $res['getRankListMonth'] = $userModel->rankList($toweek);
  632. // 获取50条财富排行月记录
  633. $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$monthday."m",0,$num-1,true);
  634. $res['toRankListMonth'] = $userModel->rankList($toweek);
  635. return $this->success("获取成功!",$res);
  636. }*/
  637. //获取派对用户排序(魅力榜,财富榜),有数值的在上,0的在下
  638. public function getPartyUserRank() {
  639. $party_id = input("party_id");// 派对ID
  640. if (!$party_id) $this->error(__('Invalid parameters'));
  641. $userModel = new \app\common\model\User();
  642. // 获取所有魅力排行
  643. $getday = $this->redis->zRevRange("hourCharm_".$party_id,0,-1,true);
  644. $res['getRankListDay'] = $userModel->rankList($getday);
  645. // 获取所有财富排行
  646. $today = $this->redis->zRevRange("hourWealth_".$party_id,0,-1,true);
  647. $res['toRankListDay'] = $userModel->rankList($today);
  648. return $this->success("获取成功!",$res);
  649. }
  650. /**
  651. * 派对热度更新(已废弃)
  652. */
  653. /*public function changeUserPartyhot() {
  654. $party_id = input('party_id',0,"intval"); // 派对ID
  655. $room_type = input('room_type',1); // 房间类型
  656. $party_hot = input('party_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值)
  657. if (!$party_id || !$party_hot) {
  658. $this->error(__('Invalid parameters'));
  659. }
  660. // 更新热度
  661. $this->redis->zIncrBy($this->roomTypeArr[$room_type]."Rank", $party_hot, $party_id);
  662. return $this->success("更新成功!");
  663. }*/
  664. /**
  665. * 更新主持人和麦位前四位至首页房间排行
  666. */
  667. public function addUserPositionToParty() {
  668. $party_id = input('party_id',0,"intval"); // 直播间ID
  669. $user_id = input('user_id',0,"intval"); // 用户ID
  670. $upOrdown = input('upordown'); // 上下麦:1=上麦-1=下麦
  671. $room_type = input('room_type',1); // 房间类型
  672. $avatar = input('avatar'); // 用户头像 (upOrdown参数为-1时可以不传)
  673. $position = input('position'); // 麦位置:0=支持人,1=1号麦,2=2号麦,3=3号麦,4=4号麦
  674. if (!$party_id || !$user_id || !in_array($position,[0,1,2,3,4,5,6,7,8]) || !in_array($upOrdown,[1,-1])) {
  675. $this->error(__('Invalid parameters'));
  676. }
  677. if($upOrdown == 1) {
  678. // 先完成所有麦上记录
  679. \app\common\model\UserOnsiteTime::update(["status"=>2],["user_id"=>$user_id]);
  680. // 保存上麦记录
  681. $data = [];
  682. $data["user_id"] = $user_id;
  683. $data["party_id"] = $party_id;
  684. $data["onsite_time"] = time();
  685. \app\common\model\UserOnsiteTime::insert($data);
  686. }
  687. if($upOrdown == -1) {
  688. // 更新下麦时间
  689. $update = [];
  690. $update["offsite_time"] = time();
  691. $update["status"] = 2;
  692. \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
  693. }
  694. if(in_array($position,[5,6,7,8])) return $this->success("设置成功!");
  695. $redisData = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  696. if($redisData) {
  697. $partyInfo = json_decode($redisData,true);
  698. if($upOrdown == 1) {
  699. // 删掉已有头像,防止重复
  700. $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
  701. if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $avatar) unset($partyInfo["party_user"][$k]);
  702. $partyInfo["party_user"][$position] = $avatar;
  703. } else {
  704. if(isset($partyInfo["party_user"])) unset($partyInfo["party_user"][$position]);
  705. }
  706. // sort($partyInfo["party_user"]);
  707. $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  708. }
  709. return $this->success("设置成功!");
  710. }
  711. /**
  712. * 获取派对在线人数列表
  713. */
  714. public function getOnlieList() {
  715. $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
  716. if (!$party_id) {
  717. $this->error(__('Invalid parameters'));
  718. }
  719. $res = $this->redis->hGetAll("online_".$party_id);
  720. $user_ids = [];$userList = [];
  721. if($res) $user_ids = array_values($res);
  722. // 获取用户列表信息
  723. if($user_ids){
  724. $userList = Db::name('user')->alias('user')->field("user.id,user.avatar,user.nickname,user.level,user.gender,user.charm_level,user.wealth_level,charm.image as charm_image,charm.color as charm_color,wealth.image as wealth_image,wealth.color as wealth_color")
  725. ->join('user_config_charm charm','user.charm_level = charm.level','LEFT')
  726. ->join('user_config_wealth wealth','user.wealth_level = wealth.level','LEFT')
  727. ->where(["user.id"=>["in",$user_ids],'user.is_stealth'=>0])
  728. ->autopage()->select();
  729. $userList = list_domain_image($userList,['avatar','charm_image','wealth_image']);
  730. }
  731. $this->success("获取成功!",$userList);
  732. }
  733. /** 连续进房提示
  734. * @param $user_id
  735. * @param $party_id
  736. * @return int
  737. */
  738. private function getUserJoinPartyDays($user_id, $party_id)
  739. {
  740. $days = 1;
  741. $joinPartyDays = $this->redis->hGet("joinParty_days",$user_id."-".$party_id);
  742. if ($joinPartyDays){
  743. $joinPartyDaysArr = explode('-',$joinPartyDays);
  744. $oldTime = (int)$joinPartyDaysArr[0];
  745. $todayStartTime = strtotime(date('Y-m-d'));//今天0点
  746. $yesterdayStartTime = strtotime(date("Y-m-d",strtotime("-1 day")));//昨天开始时间戳
  747. $todayEndTime = $todayStartTime+24 * 60 * 60-1;//今天结束时间戳
  748. //昨天登录过,+1
  749. if ($yesterdayStartTime <= $oldTime && $oldTime < $todayStartTime){
  750. $days = (int)$joinPartyDaysArr[1];
  751. $days++;
  752. $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-'. $days);
  753. }elseif($todayStartTime <= $oldTime && $oldTime <= $todayEndTime){
  754. //今天登录过 days
  755. $days = (int)$joinPartyDaysArr[1];
  756. } else {
  757. $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
  758. }
  759. }else{
  760. $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
  761. }
  762. return $days;
  763. }
  764. /**
  765. * 加入派对
  766. */
  767. public function joinParty() {
  768. $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
  769. $room_type = input("room_type",1);//
  770. $party_pass = input("party_pass");//
  771. if (!$party_id || !in_array($room_type,[1,2])) {
  772. $this->error(__('Invalid parameters'));
  773. }
  774. $user_id = $this->auth->id;
  775. // 判断派对密码
  776. $partyInfo = Db::name('party')->where('id',$party_id)->find();
  777. if($partyInfo) {
  778. if($partyInfo["status"] != 1) $this->error("该房间已被解散!");
  779. // if($partyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  780. //if($partyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
  781. $partyManage = $this->redis->hGet("party_manage_".$party_id,$user_id."-1");
  782. if(isset($partyInfo["party_pass"]) && $partyInfo["party_pass"] && $partyInfo["user_id"] != $user_id && empty($partyManage)) {
  783. if(($party_pass != $partyInfo["party_pass"] || strlen($party_pass) != 4) && $this->auth->is_manager == 0) {
  784. $this->error("派对密码不正确!");
  785. }
  786. }
  787. // 公会管理员和公会长激活派对厅
  788. /* $guildInfo = \app\common\model\Guild::where(['user_id' => $partyInfo['user_id'], 'status' => 1])->find();
  789. if ($guildInfo) {
  790. // 获取公会管理员
  791. $guildMemberInfo = \app\common\model\GuildMember::where(["user_id" => $user_id, "guild_id" => $guildInfo['id'], 'status' => 1])->find();
  792. if ($guildMemberInfo && $guildMemberInfo['role'] == 0 && $partyInfo['is_online'] == 0) {
  793. //$this->error("当前直播已结束!");//这种情况理论上不存在
  794. }
  795. }*/
  796. // // 如果是房主自己进入房间,则更新用户 为在线状态
  797. // if ($partyInfo["user_id"] == $user_id) {
  798. // if ($room_type == 2)
  799. // \app\common\model\User::update(["is_live" => 1], ["id" => $partyInfo["user_id"]]);
  800. // }
  801. } else {
  802. $this->error("派对信息获取失败!");
  803. }
  804. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  805. $lsetList = [];
  806. for($i=1;$i<=4;$i++) {
  807. $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i);
  808. $hgetlist = unserialize($hgetlist);
  809. if($hgetlist){
  810. $restime = time() - intval($hgetlist["createtime"]);
  811. }else{
  812. $restime = 0;
  813. }
  814. // 房管
  815. if($hgetlist && $i == 1) {
  816. $lsetList["manage_restime"] = "";
  817. //重新设置成管理员
  818. $this->setGroupAdmin($partyInfo['easemob_room_id'],$user_id,1);
  819. }
  820. // 禁言
  821. if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) {
  822. $lsetList["manage_notalk"] = $restime;
  823. }
  824. // 拉黑,管理员不能被拉黑
  825. if($hgetlist && $i == 3 && $restime < $hgetlist["time"] && $this->auth->is_manager == 0) {
  826. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  827. $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
  828. break;
  829. }
  830. // 踢出,管理员不能被踢出
  831. if($hgetlist && $i == 4 && $restime < $hgetlist["time"] && $this->auth->is_manager == 0) {
  832. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  833. $this->error(__('您已被该房间踢出,解除时间:'.$restime));
  834. break;
  835. }
  836. }
  837. //下移
  838. $this->redis->zAdd("party_user_".$party_id,$user_id,$user_id);
  839. $this->redis->hSet("online_".$party_id,$user_id,$user_id); //新增的
  840. // 判断当前用户是否存在在直播间通过切换过来的
  841. $livingUserPartyId = $this->redis->hGet("livingUser", $user_id);
  842. if ($livingUserPartyId) {
  843. if($livingUserPartyId != $party_id){
  844. // 扣除房间在线人数
  845. $this->redis->HDel("online_" . $livingUserPartyId, $user_id);
  846. //首页用户头像
  847. $this->redis->zRem("party_user_".$livingUserPartyId, $user_id);
  848. // 扣除在线用户在房间情况
  849. $this->redis->hDel("livingUser", $user_id);
  850. }
  851. }
  852. // 记录在线用户在房间情况
  853. $this->redis->hSet("livingUser", $user_id, $party_id);
  854. //下移
  855. // 获取用户魅力值。大概率没用了
  856. // 获取用户排行榜前三名头像。大概率没用了
  857. // 获取是否被当前用户关注
  858. $lsetList["is_follow"] = 0;
  859. $where = [];
  860. $where["user_id"] = $user_id;
  861. $where["party_id"] = $party_id;
  862. if(Db::name('party_follow')->where($where)->find()) {
  863. $lsetList["is_follow"] = 1;
  864. }
  865. if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  866. // $partyInfo["type_name"] = $partyInfo["type_name"];
  867. } else {
  868. $partyInfo["type_name"] = "普通房";
  869. }
  870. $partyInfo["party_hot"] = changeW($partyInfo["party_hot"]);
  871. $partyInfo['is_new'] = $partyInfo["status"] == 0 ? 1 : 0;
  872. $partyInfo['join_days'] = $this->getUserJoinPartyDays($user_id,$party_id);
  873. $lsetList["partyInfo"] = $partyInfo;
  874. //声网三个token
  875. $lsetList['shengwang_token'] = $this->shengwang_token_private($partyInfo['easemob_room_id']);
  876. //[环信] 更新聊天室自定义属性 在线用户人数
  877. $easemob = new Easemob();
  878. $matedata = [
  879. 'online_user_num' => count($this->redis->hGetAll("online_".$party_id)),
  880. ];
  881. $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  882. return $this->success("加入成功!",$lsetList);
  883. }
  884. private function creater_joinParty($party_id,$easemob_room_id,$party_user_id) {
  885. $user_id = $this->auth->id;
  886. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  887. $lsetList = [];
  888. for($i=1;$i<=4;$i++) {
  889. $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i);
  890. $hgetlist = unserialize($hgetlist);
  891. // 房管
  892. if($hgetlist && $i == 1) {
  893. $lsetList["manage_restime"] = "";
  894. //重新设置成管理员
  895. $this->setGroupAdmin($easemob_room_id,$user_id,1);
  896. }
  897. }
  898. //下移
  899. $this->redis->zAdd("party_user_".$party_id,$user_id,$user_id);
  900. $this->redis->hSet("online_".$party_id,$user_id,$user_id); //新增的
  901. // 判断当前用户是否存在在直播间通过切换过来的
  902. $livingUserPartyId = $this->redis->hGet("livingUser", $user_id);
  903. if ($livingUserPartyId) {
  904. if($livingUserPartyId != $party_id){
  905. // 扣除房间在线人数
  906. $this->redis->HDel("online_" . $livingUserPartyId, $user_id);
  907. //首页用户头像
  908. $this->redis->zRem("party_user_".$livingUserPartyId, $user_id);
  909. // 扣除在线用户在房间情况
  910. $this->redis->hDel("livingUser", $user_id);
  911. }
  912. }
  913. // 记录在线用户在房间情况
  914. $this->redis->hSet("livingUser", $user_id, $party_id);
  915. //下移
  916. //[环信] 更新聊天室自定义属性 在线用户人数
  917. $easemob = new Easemob();
  918. $matedata = [
  919. 'online_user_num' => count($this->redis->hGetAll("online_".$party_id)),
  920. ];
  921. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_user_id,$matedata);
  922. return true;
  923. }
  924. /**
  925. * 判断是否被禁言
  926. */
  927. public function isNotalk() {
  928. $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
  929. $user_id = input('user_id',0,"intval"); // 用户ID 逻辑ID
  930. if (!$user_id || !$party_id) {
  931. $this->error(__('Invalid parameters'));
  932. }
  933. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  934. $lsetList = [];
  935. $lsetList["is_notalk"] = 0;
  936. $lsetList["notalk_time"] = 0;
  937. $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-2");
  938. if(!$hgetlist){
  939. return $this->success("获取成功!",$lsetList);
  940. }
  941. $hgetlist = unserialize($hgetlist);
  942. $restime = time() - intval($hgetlist["createtime"]);
  943. // 禁言
  944. if($hgetlist && $restime < $hgetlist["time"]) {
  945. $lsetList["is_notalk"] = 1;
  946. $lsetList["notalk_time"] = $restime;//已经禁了N秒
  947. }
  948. return $this->success("获取成功!",$lsetList);
  949. }
  950. /**
  951. * 退出派对
  952. */
  953. public function outParty() {
  954. $party_id = input('party_id',0,"intval"); // 直播间ID
  955. $room_type = input("room_type",1);//
  956. if (!$party_id) {
  957. $this->error(__('Invalid parameters'));
  958. }
  959. $userId = $this->auth->id;
  960. $this->redis->zRem("party_user_".$party_id,$userId);
  961. // 处理online_party_id
  962. $this->redis->HDel("online_" . $party_id, $this->auth->id);
  963. // 扣除在线用户在房间情况
  964. $this->redis->hDel("livingUser", $this->auth->id);
  965. //下麦
  966. $key = "party_seat_".$party_id;
  967. $seats = $this->redis->hGetAll($key);
  968. foreach($seats as $seat_num => $seatuserid){
  969. if($seatuserid == $userId){
  970. $this->redis->HDel($key, $seat_num);
  971. }
  972. }
  973. // 更新房间在线状态
  974. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type].'_'.$party_id);
  975. if ($partyInfo) {
  976. $partyInfo = json_decode($partyInfo, true);
  977. $memCount = count($this->redis->hGetAll("online_" . $party_id));
  978. if ($memCount <= 0) {
  979. $partyInfo["is_online"] = 0;
  980. $this->redis->set($this->roomTypeArr[$room_type].'_'.$party_id,json_encode($partyInfo));
  981. \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
  982. }
  983. //[环信] 更新聊天室自定义属性 在线用户人数
  984. $easemob = new Easemob();
  985. $matedata = [
  986. 'online_user_num' => count($this->redis->hGetAll("online_".$party_id)),
  987. ];
  988. $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  989. //清除这个人点的歌,前台自己处理了
  990. /*$easemob_room_id = $partyInfo['easemob_room_id'];
  991. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  992. $singlist = json_decode($singlist['waitsing_list'],true);
  993. if(!empty($singlist)){
  994. foreach($singlist as $key => $sing){
  995. if($sing['userNo'] == $userId){
  996. unset($singlist[$key]);
  997. }
  998. }
  999. //重新排序
  1000. $singlist = $this->waitsing_array_column_sort($singlist);
  1001. //重新设置
  1002. $matedata = [
  1003. 'waitsing_list' => json_encode($singlist),
  1004. ];
  1005. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$matedata);
  1006. }*/
  1007. }
  1008. $this->success("退出成功!",[]);
  1009. }
  1010. /**
  1011. * 派对内搜索用户
  1012. */
  1013. public function searchUserParty() {
  1014. $u_id = input('u_id',0,"intval"); // 用户u_id
  1015. $party_id = input('party_id',0,"intval"); // 直播间ID
  1016. // $userids = $this->redis->zRange("party_user_".$party_id,0,-1,true);
  1017. // $useridArr = [];
  1018. // if($userids)foreach($userids as $k => $v) $useridArr[$v] = $k; // 因为array_slip 不能交换数字啊
  1019. // $userid = isset($useridArr[$u_id])?$useridArr[$u_id]:0;
  1020. $userModel = new \app\common\model\User();
  1021. $where = [];
  1022. $where["u_id"] = $u_id;
  1023. $userInfo = $userModel->field("id,u_id,avatar,nickname,level,gender")->where($where)->select();
  1024. return $this->success("查询成功!",$userInfo);
  1025. }
  1026. /**
  1027. * 设置冠名
  1028. */
  1029. public function setNaming() {
  1030. $party_id = input('party_id',0,"intval"); // 直播间ID
  1031. $user_id = input('user_id',0,"intval"); // 用户ID
  1032. $type = input('type',1); // 1:设置 0:取消设置
  1033. if (!$party_id || !$user_id || ($type != 1 && $type != 0)) {
  1034. $this->error(__('Invalid parameters'));
  1035. }
  1036. $partyModel = new \app\common\model\Party();
  1037. $data = [];
  1038. $where = [];
  1039. $where["id"] = $party_id;
  1040. $partyInfo = $partyModel->where(["id"=>$party_id])->find();
  1041. $getredisPartyInfo = $this->redis->get($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id);
  1042. $redisPartyInfo = json_decode($getredisPartyInfo,true);
  1043. if($type == 1) {
  1044. $data["naming"] = $user_id;
  1045. // 冠名
  1046. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$user_id])->find($user_id);
  1047. $redisPartyInfo["naming"] = $userInfo;
  1048. } else {
  1049. $data["naming"] = 0;
  1050. $redisPartyInfo["naming"] = [];
  1051. }
  1052. $this->redis->set($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id,json_encode($redisPartyInfo));
  1053. $res = $partyModel->update($data,$where);
  1054. if($res !== false) {
  1055. $this->success("操作成功!");
  1056. } else {
  1057. $this->error("网络错误,请稍后重试!");
  1058. }
  1059. }
  1060. /**
  1061. * 派对关注
  1062. */
  1063. public function followparty() {
  1064. $party_id = input('party_id',0,"intval"); // 直播间ID
  1065. if (!$party_id) {
  1066. $this->error(__('Invalid parameters'));
  1067. }
  1068. $user_id = $this->auth->id;
  1069. // 添加记录
  1070. $data = [];
  1071. $data["user_id"] = $user_id;
  1072. $data["party_id"] = $party_id;
  1073. if(Db::name('party_follow')->where($data)->find()) {
  1074. $res = Db::name('party_follow')->where($data)->delete();
  1075. $this->success("已取消关注");
  1076. }
  1077. $data["createtime"] = time();
  1078. $res = Db::name('party_follow')->insert($data);
  1079. $this->success("已关注");
  1080. }
  1081. /**
  1082. * 获取派对信息
  1083. */
  1084. public function getPartyInfo() {
  1085. $party_id = input('party_id',0,"intval"); // 直播间ID
  1086. if (!$party_id) {
  1087. $this->error(__('Invalid parameters'));
  1088. }
  1089. $user_id = $this->auth->id;
  1090. $partyModel = new \app\common\model\Party();
  1091. $userModel = new \app\common\model\User();
  1092. // 获取主体信息
  1093. $where = [];
  1094. $where["a.id"] = $party_id;
  1095. $partyInfo = $partyModel->alias("a")
  1096. ->field("a.id,a.user_id,a.party_id,a.party_name,a.party_hot,a.party_logo,a.party_notice,a.party_notice_detail,a.on_model,a.background,a.is_public,
  1097. rt.id as party_type,rt.name as type_name")
  1098. ->join("hx_party_type rt","rt.id = a.party_type", "left")
  1099. ->where($where)
  1100. ->find();
  1101. if($partyInfo) {
  1102. $mod = isset($partyInfo["party_type"])?intval($partyInfo["party_type"])%5:1;
  1103. $partyInfo["party_type_color"] = $mod == 0?5:$mod;
  1104. if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  1105. // $partyInfo["type_name"] = $partyInfo["type_name"];
  1106. } else {
  1107. $partyInfo["type_name"] = "普通房";
  1108. }
  1109. // 获取是否被当前用户关注
  1110. $partyInfo["is_follow"] = 0;
  1111. $where = [];
  1112. $where["user_id"] = $user_id;
  1113. $where["party_id"] = $party_id;
  1114. if(Db::name('party_follow')->where($where)->find()) {
  1115. $partyInfo["is_follow"] = 1;
  1116. }
  1117. // 获取房主信息
  1118. $where = [];
  1119. $where["id"] = $partyInfo["user_id"];
  1120. $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
  1121. // 获取技能信息
  1122. $partyInfo["userInfo"] = $userInfo;
  1123. $partyInfo["party_hot"] = changeW($partyInfo["party_hot"]);
  1124. }
  1125. $this->success("获取成功!",$partyInfo);
  1126. }
  1127. /**
  1128. * 派对设置
  1129. */
  1130. public function setParty() {
  1131. $party_id = input('party_id',0,"intval"); // 直播间ID
  1132. $room_type = input('room_type',1); // 房间类型:1=公会,2=个人k歌
  1133. if (!$party_id || !$room_type) {
  1134. $this->error(__('Invalid parameters'));
  1135. }
  1136. // 派对名称
  1137. // 派对logo/封面
  1138. // 派对密码
  1139. // 使用场景
  1140. // 是否关闭公屏:1=是,0=否
  1141. // 上麦模式:1=自由模式,2=麦序模式
  1142. // 派对背景
  1143. // 是否公开:1=是,0=否
  1144. $field_array = ['party_name','party_logo','party_pass'/*,'party_type'*/,'is_screen','on_model','background','is_public','party_notice','party_notice_detail'];
  1145. $data = [];
  1146. foreach($field_array as $key => $field){
  1147. //前端传不了post,改了
  1148. if(!request()->has($field,'post')){
  1149. continue;
  1150. }
  1151. /*if(!input('?'.$field)){
  1152. continue;
  1153. }*/
  1154. $newone = input($field);
  1155. $data[$field] = $newone;
  1156. }
  1157. if(isset($data['party_pass'])){
  1158. if(empty($data['party_pass'])){
  1159. $data['party_pass'] = '';
  1160. }
  1161. if(!empty($data['party_pass']) && strlen($data['party_pass']) != 4) {
  1162. $this->error("房间密码必须为四位!");
  1163. }
  1164. }
  1165. $partyModel = new \app\common\model\Party();
  1166. $where = [];
  1167. $where["id"] = $party_id;
  1168. $res = $partyModel->update($data,$where);
  1169. if($res) {
  1170. // 获取派对类型
  1171. //if($party_type) $data["type_name"] = \app\common\model\PartyType::where(["id"=>$party_type])->value("name");
  1172. // 存redis 房间信息
  1173. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  1174. if($partyInfo) {
  1175. $partyInfo = json_decode($partyInfo,true);
  1176. $partyInfo = array_replace($partyInfo,$data);
  1177. $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  1178. }
  1179. //[环信]初始化房间,初始化自定义属性
  1180. $matedata = [];
  1181. if(isset($data['party_name'])){
  1182. $matedata['party_name'] = $data['party_name'];
  1183. }
  1184. if(isset($data['party_logo'])){
  1185. $matedata['party_logo'] = $data['party_logo'];
  1186. }
  1187. if(isset($data['is_public'])){
  1188. $matedata['is_public'] = $data['is_public'];
  1189. }
  1190. if(isset($data['background'])){
  1191. $matedata['background'] = $data['background'];
  1192. }
  1193. if(isset($data['party_notice'])){
  1194. $matedata['party_notice'] = $data['party_notice'];
  1195. }
  1196. if(isset($data['party_notice_detail'])){
  1197. $matedata['party_notice_detail'] = $data['party_notice_detail'];
  1198. }
  1199. if(!empty($matedata)){
  1200. $easemob = new Easemob();
  1201. $rs = $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  1202. }
  1203. $this->success("房间设置成功!",$data);
  1204. } else {
  1205. $this->error("网络错误,请稍后重试!");
  1206. }
  1207. }
  1208. /**
  1209. * 获取派对背景
  1210. */
  1211. public function getDefaultBackground() {
  1212. $room_type = input('room_type',1); // 房间类型
  1213. if (!in_array($room_type,[1,2])) {
  1214. $this->error(__('Invalid parameters'));
  1215. }
  1216. $this->success("获取成功!",\app\common\model\PartyBackground::where(["room_type"=>$room_type])->select());
  1217. }
  1218. //环信聊天室设置管理员,type=1设置,type=0移除
  1219. private function setGroupAdmin($easemob_room_id,$user_id,$type = 1)
  1220. {
  1221. $easemob = new Easemob();
  1222. if($type == 1){
  1223. $easemob->room_promoteRoomAdmin($easemob_room_id,$user_id);
  1224. }else{
  1225. $easemob->room_demoteRoomAdmin($easemob_room_id,$user_id);
  1226. }
  1227. }
  1228. /**
  1229. * 派对管理设置
  1230. */
  1231. public function partyManageSet() {
  1232. $party_id = input('party_id',0,"intval"); // 直播间ID
  1233. $user_id = input('user_id',0,"intval"); // 用户ID
  1234. $item = input('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  1235. $time = input('time'); // 限制时间(单位:秒):0=永久
  1236. if (!$party_id || !$user_id || !in_array($item,[1,2,3,4])) {
  1237. $this->error(__('Invalid parameters'));
  1238. }
  1239. //2=禁言,3=拉黑,4=踢出 ,都要有时间
  1240. if($item>1 && $time<=0) {
  1241. $this->error(__('时间设置有误'));
  1242. }
  1243. //不能对房主恶意操作
  1244. $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
  1245. if ($partyinfo['user_id'] == $user_id && in_array($item, [2, 3, 4])) {
  1246. $this->error("您当前无权限操作!");
  1247. }
  1248. //只有房主才能加管理员
  1249. if($item == 1 && $this->auth->id != $partyinfo['user_id']){
  1250. $this->error('只有房主才能设置管理员');
  1251. }
  1252. // 获取用户信息
  1253. $userInfo = \app\common\model\User::field("avatar,nickname,gender")->where(["id"=>$user_id])->find();
  1254. if(!$userInfo) $this->error("用户信息获取失败!");
  1255. $data = [];
  1256. $data["user_id"] = $user_id;
  1257. $data["avatar"] = $userInfo->avatar;
  1258. $data["nickname"] = $userInfo->nickname;
  1259. $data["gender"] = $userInfo->gender;
  1260. $data["item"] = $item;
  1261. $data["time"] = $time;
  1262. $data["createtime"] = time();
  1263. $res = $this->redis->hSet("party_manage_".$party_id,$user_id."-".$item,serialize($data));
  1264. if ($item==1) {
  1265. $this->setGroupAdmin($partyinfo['easemob_room_id'],$user_id,1);
  1266. }
  1267. if($res !== false) {
  1268. $this->success("设置成功!",$data);
  1269. } else {
  1270. $this->error("网络错误,请稍后重试!");
  1271. }
  1272. }
  1273. /**
  1274. * 派对管理设置列表
  1275. * 仅用来给前端展示,禁言的鉴权在isNotalk接口,拉黑与提出的鉴权在joinparty接口
  1276. */
  1277. public function partyManageSetList() {
  1278. $party_id = input('party_id',0,"intval"); // 直播间ID
  1279. $item = input('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  1280. if (!$party_id || !in_array($item,[1,2,3,4])) {
  1281. $this->error(__('Invalid parameters'));
  1282. }
  1283. $time = time();
  1284. $hget=$this->redis->hGetAll("party_manage_".$party_id);
  1285. $list=array();
  1286. foreach($hget as $key=>$val) {
  1287. if(substr($key,-2) == "-".$item){
  1288. array_push($list,unserialize($val));
  1289. }
  1290. }
  1291. if($list) {
  1292. foreach($list as $k => $v) if(bcadd($v["createtime"],$v["time"]) <= $time && $v["item"] > 1) unset($list[$k]);
  1293. if($list) $list = array_values($list);
  1294. }
  1295. $this->success("获取成功!",$list);
  1296. }
  1297. /**
  1298. * 派对管理设置移除
  1299. */
  1300. public function partyManageSetDel() {
  1301. $id = input('user_id',0,"intval"); // userid
  1302. $party_id = input('party_id',0,"intval"); // 房间ID
  1303. $item = input('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  1304. if (!$id || !in_array($item,[1,2,3,4])) {
  1305. $this->error(__('Invalid parameters'));
  1306. }
  1307. $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
  1308. //只有房主才能加管理员
  1309. if($item == 1 && $this->auth->id != $partyinfo['user_id']){
  1310. $this->error('只有房主才能免除管理员');
  1311. }
  1312. $res = $this->redis->hDel("party_manage_".$party_id,$id."-".$item);
  1313. if ($item==1) {
  1314. $this->setGroupAdmin($partyinfo['easemob_room_id'],$id,0);
  1315. }
  1316. if($res !== false) {
  1317. $this->success("移除成功!",$res);
  1318. } else {
  1319. $this->error("网络错误,请稍后重试!");
  1320. }
  1321. }
  1322. /**
  1323. * 更新派对公告
  1324. */
  1325. public function savePartyNotice() {
  1326. $party_id = input('party_id',0,"intval"); // 直播间ID
  1327. $room_type = input('room_type',1); // 房间类型:1=派对2=直播
  1328. $party_notice = input('party_notice'); // 公告标题
  1329. $party_notice_detail = input('party_notice_detail'); // 公告内容
  1330. if (!$party_id || !$party_notice || !$party_notice_detail) {
  1331. $this->error(__('Invalid parameters'));
  1332. }
  1333. $partyModel = new \app\common\model\Party();
  1334. $where = [];
  1335. $where["id"] = $party_id;
  1336. $data = [];
  1337. $data["party_notice"] = $party_notice;
  1338. $data["party_notice_detail"] = $party_notice_detail;
  1339. $res = $partyModel->update($data,$where);
  1340. if($res) {
  1341. // 存redis 房间信息
  1342. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  1343. if($partyInfo) {
  1344. $partyInfo = json_decode($partyInfo,true);
  1345. $partyInfo = array_replace($partyInfo,$data);
  1346. $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  1347. }
  1348. $this->success("更新成功!",$res);
  1349. } else {
  1350. $this->error("网络错误,请稍后重试!");
  1351. }
  1352. }
  1353. /**
  1354. * 开始排麦
  1355. */
  1356. public function addLineUp() {
  1357. $party_id = input('party_id',0,"intval"); // 直播间ID
  1358. $mai_id = input('mai_id',"1"); // 麦位置
  1359. if (!$party_id) {
  1360. $this->error(__('Invalid parameters'));
  1361. }
  1362. $userInfo = $this->auth->getUserinfo();
  1363. $userid = $userInfo['id'];
  1364. // 获取 房间信息
  1365. $partyUser = \app\common\model\Party::where(['id'=>$party_id])->value("user_id");
  1366. if(!$partyUser) $this->error(__('房间信息未找到!'));
  1367. // $is_home = $partyUser == $userInfo->id?1:0;
  1368. $data = unserialize($this->redis->hGet("party_lineup",$party_id));
  1369. $data[$userid]["user_id"] = $userInfo['id'];
  1370. $data[$userid]["avatar"] = $userInfo['avatar'];
  1371. $data[$userid]["mai_id"] = $mai_id;
  1372. $data[$userid]["nickname"] = $userInfo['nickname'];
  1373. // $data["is_home"] = $is_home;
  1374. // $data[$userid]["level"] = $userInfo['level'];
  1375. $data[$userid]["gender"] = $userInfo['gender'];
  1376. $res = false;
  1377. $data && $res = $this->redis->hSet("party_lineup",$party_id,serialize($data));
  1378. $data = array_values($data);
  1379. if($res !== false) {
  1380. $this->success("设置成功!",$data);
  1381. } else {
  1382. $this->error("网络错误,请稍后重试!");
  1383. }
  1384. }
  1385. /**
  1386. * 取消排麦
  1387. */
  1388. public function cancelLineUp() {
  1389. $party_id = input('party_id',0,"intval"); // 直播间ID
  1390. $user_id = input('user_id',0); // 直播间ID
  1391. $is_empty = input('is_empty',0,"intval"); // 是否清空排麦列表 1=清空,0=不清空
  1392. $is_baomai = input('is_baomai',0,'intval'); //是否抱麦
  1393. if (!$party_id) {
  1394. $this->error(__('Invalid parameters'));
  1395. }
  1396. $data=unserialize($this->redis->hGet("party_lineup",$party_id));
  1397. $baomai_user = [];
  1398. if($data && $is_empty != 1) {
  1399. foreach($data as $k => $v) {
  1400. if($v["user_id"] == $user_id) {
  1401. $baomai_user = $v;
  1402. unset($data[$k]);
  1403. break;
  1404. }
  1405. }
  1406. }
  1407. // unset($data[$userid]);
  1408. if($is_empty == 1) $data = [];
  1409. $this->redis->hSet("party_lineup",$party_id,serialize($data));
  1410. //抱麦
  1411. if($is_empty != 1 && $is_baomai == 1 && !empty($baomai_user)){
  1412. $this->baomai($party_id,$user_id,$baomai_user);
  1413. }
  1414. //
  1415. $datas = [];
  1416. is_array($data) && $datas=array_values($data);
  1417. if($datas) {
  1418. $this->success("移除成功!",$datas);
  1419. } else {
  1420. $this->success("移除成功!",[]);
  1421. }
  1422. }
  1423. private function baomai($party_id,$user_id,$baomai_user){
  1424. $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
  1425. if(empty($partyinfo)){
  1426. return false;
  1427. }
  1428. //获取所有麦位,假设8个
  1429. $easemob = new Easemob();
  1430. $seatlist = $easemob->room_getRoomCustomAttribute($partyinfo['easemob_room_id'],['seat0','seat1','seat2','seat3','seat4','seat5','seat6','seat7']);
  1431. $newseat = [
  1432. 'charm' => 0, //红心,魅力值
  1433. 'isMaster' => false, // 是否是房主
  1434. 'headUrl' => $baomai_user['avatar'], // 头像
  1435. 'userNo' => $baomai_user['user_id'], // 座位上用户no
  1436. 'rtcUid' => $baomai_user['user_id'], // 座位上用户id,与rtc的userId一致
  1437. 'name' => $baomai_user['nickname'], // 座位上用户昵称
  1438. 'seatIndex' => -1, // 座位编号
  1439. 'chorusSongCode' => '', // 是否合唱
  1440. 'isAudioMuted' => 1, // 是否静音
  1441. 'isVideoMuted' => 0, // 是否开启视频
  1442. 'checked' => false, // 用于送礼物选择用户
  1443. 'isUsed' => true, // 用于送礼物选择用户
  1444. 'gender' => $baomai_user['gender'], //性别
  1445. ];
  1446. if(empty($seatlist)){
  1447. $newseat['seatIndex'] = 0;
  1448. $matedata['seat0'] = $newseat;
  1449. }else{
  1450. //第一遍轮询麦位,尝试找到这个人,如果找到在任何一个麦位,立刻return
  1451. foreach($seatlist as $k => $seat){
  1452. $seat = json_decode($seat,true);
  1453. //找到当前用户所在的麦位
  1454. if(isset($seat['rtcUid']) && !empty($seat['rtcUid']) && $seat['rtcUid'] == $user_id){
  1455. return true;
  1456. }
  1457. }
  1458. //第二遍轮询麦位,找一个空麦位,放进去
  1459. $matedata = [];
  1460. foreach($seatlist as $k => $seat){
  1461. $seat = json_decode($seat,true);
  1462. //找到当前用户所在的麦位
  1463. if(isset($seat['rtcUid']) && empty($seat['rtcUid'])){
  1464. //dump(substr($k,-1));
  1465. $newseat['seatIndex'] = substr($k,-1);
  1466. //初始化这个麦位
  1467. $matedata = [
  1468. $k => json_encode($newseat),
  1469. ];
  1470. }
  1471. }
  1472. }
  1473. //重置房间在线人数
  1474. if(!empty($matedata)){
  1475. $easemob->room_setRoomCustomAttributeForced($partyinfo['easemob_room_id'],$partyinfo['user_id'],$matedata);
  1476. }
  1477. return true;
  1478. }
  1479. /**
  1480. * 排麦列表
  1481. */
  1482. public function lineUpList() {
  1483. $party_id = input('party_id',0,"intval"); // 直播间ID
  1484. if (!$party_id) {
  1485. $this->error(__('Invalid parameters'));
  1486. }
  1487. $data = $this->redis->hGet("party_lineup",$party_id);
  1488. $dataArr = unserialize($data);
  1489. $datas = [];
  1490. is_array($dataArr) && $datas=array_values($dataArr);
  1491. if($datas) {
  1492. $this->success("获取成功!",$datas);
  1493. } else {
  1494. $this->success("获取成功!",[]);
  1495. }
  1496. }
  1497. /**
  1498. * 获取派对类型
  1499. */
  1500. public function getPatyType() {
  1501. $room_type = input("room_type",1);//
  1502. if (!in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
  1503. $partytypeModel = new \app\common\model\PartyType();
  1504. $partytypeList = $partytypeModel->where(["room_type"=>$room_type])->select();
  1505. $this->success("获取成功!",$partytypeList);
  1506. }
  1507. /**
  1508. * 判断派对是否设置过密码
  1509. */
  1510. public function getPatyIspass() {
  1511. $party_id = input("party_id");// 派对ID
  1512. $room_type = input("room_type",1);//
  1513. $is_miniprogram = input("is_miniprogram",0);//
  1514. if (!$party_id) $this->error(__('Invalid parameters'));
  1515. $user_id = $this->auth->id;
  1516. // 存redis 房间信息
  1517. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  1518. if($partyInfo) {
  1519. $partyInfo = json_decode($partyInfo,true);
  1520. } else {
  1521. $partyModel = new \app\common\model\Party();
  1522. $partyInfo = $partyModel->field("id,user_id,party_pass")->where(["id"=>$party_id])->find();
  1523. if(!$partyInfo) {
  1524. $this->error(__('房间信息获取失败!'));
  1525. }
  1526. }
  1527. $data = [];
  1528. $data["status"] = 0;
  1529. $data["is_pass"] = $partyInfo["party_pass"]?1:0;
  1530. $partyManage = $this->redis->hGet("party_manage_".$party_id,$user_id."-1");
  1531. if($partyInfo["user_id"] == $user_id || $this->auth->is_manager == 1 || !empty($partyManage)){ $data["is_pass"] = 0;}
  1532. if($is_miniprogram == 1) { // 小程序单独处理
  1533. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  1534. $lsetList = [];
  1535. for($i=1;$i<=4;$i++) {
  1536. $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i);
  1537. $hgetlist = unserialize($hgetlist);
  1538. $restime = time() - intval($hgetlist["createtime"]);
  1539. // 房管
  1540. if($hgetlist && $i == 1) $lsetList["manage_restime"] = "";
  1541. // 禁言
  1542. if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime;
  1543. // 拉黑
  1544. if($hgetlist && $i == 3 && $restime < $hgetlist["time"]) {
  1545. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  1546. $lsetList["manage_black"] = $restime;
  1547. $lsetList["tips"] = '您已被该房间拉黑,解除时间:'.$restime;
  1548. // $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
  1549. break;
  1550. }
  1551. // 踢出
  1552. if($hgetlist && $i == 4 && $restime < $hgetlist["time"]) {
  1553. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  1554. $lsetList["manage_takeout"] = $restime;
  1555. $lsetList["tips"] = '您已被该房间踢出,解除时间:'.$restime;
  1556. // $this->error(__('您已被该房间踢出,解除时间:'.$restime));
  1557. break;
  1558. }
  1559. }
  1560. if($lsetList) {
  1561. $data["status"] = 1;
  1562. $data["hgetlist"] = $lsetList;
  1563. }
  1564. }
  1565. $this->success("获取成功!",$data);
  1566. }
  1567. /**
  1568. * 获取房间gif表情分类
  1569. */
  1570. public function getPartyGifType()
  1571. {
  1572. $list = \app\common\model\PartyGifType::field("id,name,image")->order("weigh", "asc")->select();
  1573. $this->success("获取成功!", $list);
  1574. }
  1575. /**
  1576. * 获取房间gif表情列表
  1577. */
  1578. public function getPartGifList() {
  1579. $this->success("获取成功!",\app\common\model\PartyGif::field("id,gif_image")->order("weight","asc")->select());
  1580. }
  1581. /**
  1582. * 获取房间头像gif表情列表
  1583. */
  1584. public function getPartHeadgifList() {
  1585. $this->success("获取成功!",\app\common\model\PartyHeadgif::field("id,name,gif_image")->order("weight","asc")->select());
  1586. }
  1587. /**
  1588. * 全麦/单独赠送礼物
  1589. */
  1590. public function giveGiftToYou() {
  1591. // 是否背包赠送: 1=是,0=否
  1592. $is_back = input("is_back",0);
  1593. // 接口防并发
  1594. if($is_back){
  1595. if (!$this->apiLimit(50, 1000)) {
  1596. //1000毫秒50次
  1597. $this->error('休息一下吧');
  1598. }
  1599. }else{
  1600. if (!$this->apiLimit(10, 1000)) {
  1601. //1000毫秒10次
  1602. $this->error('休息一下吧');
  1603. }
  1604. }
  1605. //送礼物权限
  1606. /*if($this->auth->power['give_gift'] == 1){
  1607. $this->error('您已被限制:赠送礼物,请联系管理员');
  1608. }*/
  1609. //接收参数
  1610. $user_ids = input("user_id");// 赠送对象用户id
  1611. $seat_ids = input("seat_id");// 赠送对象麦位id
  1612. $gift_id = input("gift_id",0,'intval');// 礼物ID。如果是背包赠送,则是gift_back表的id,这样不合理,但是已经不好改了
  1613. $party_id = input("party_id",0,'intval');// 派对ID
  1614. $room_type = input('room_type',1,'intval'); // 房间类型
  1615. $number = input("number",1,'intval');// 赠送数量
  1616. if (!$user_ids || $seat_ids == '' || !in_array($is_back,[0,1]) || !$gift_id || !$number || !in_array($room_type,[1,2]))
  1617. {
  1618. $this->error(__('Invalid parameters'));
  1619. }
  1620. if(!$party_id){
  1621. $this->error('只有房间内能送礼物');
  1622. }
  1623. //处理参数
  1624. $user_id_arr = explode(",",$user_ids);
  1625. $seat_id_arr = explode(",",$seat_ids);
  1626. $userCount = count($user_id_arr);
  1627. $seatCount = count($seat_id_arr);
  1628. if($userCount != $seatCount){
  1629. $this->error();
  1630. }
  1631. $userauthid = $this->auth->id;
  1632. $money_to_jewel = config('site.money_to_jewel') ?: 10000; //余额兑换金币
  1633. // 不可以赠送给自己
  1634. if(in_array($userauthid,$user_id_arr)) $this->error("不可以赠送给自己!");
  1635. $backGiftId = 0; //背包礼物表的 gift_id
  1636. $boxgiftInfo = [];
  1637. //每个人都能得到的礼物价值
  1638. $giftValue = 0;
  1639. //每个人都能得到的礼物价值
  1640. $getValue = 0;
  1641. //热度值
  1642. $hotValue = 0;
  1643. Db::startTrans();
  1644. if($is_back == 1) {
  1645. // 获取背包礼物信息
  1646. $giftbackInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
  1647. if(!$giftbackInfo){
  1648. $this->error("背包礼物获取失败");
  1649. }
  1650. $backGiftId = $giftbackInfo['gift_id'];
  1651. // 随机获取一个礼物
  1652. $allCount = $number*$userCount;
  1653. $giftbackList = Db::name('gift_back')->field('id,value,name,image,gif_image,number')->where(["gift_id"=>$backGiftId,"user_id"=>$userauthid,'is_use'=>0])->limit($allCount)->order('id asc')->lock(true)->select();
  1654. if(count($giftbackList) < $allCount)
  1655. {
  1656. Db::rollback();
  1657. $this->error("背包数量不足");
  1658. }
  1659. $giftInfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
  1660. $giftInfo['is_baobi'] = 0;
  1661. $giftValue = $giftInfo["value"] * $number;
  1662. $giftCountValue = $giftValue * $userCount;
  1663. } else {
  1664. // 获取礼物信息
  1665. $giftInfo = Db::name('gift')->field('id,value,name,image,special as gif_image,is_baobi')->where('id',$gift_id)->find();
  1666. if (!$giftInfo) {
  1667. $this->error("请选择礼物!");
  1668. }
  1669. $giftValue = $giftInfo["value"] * $number;
  1670. // 判断当前用户余额,这里不需要锁表,钱包操作会锁
  1671. $giftCountValue = $giftValue * $userCount;
  1672. $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
  1673. if($user_jewel < $giftCountValue){
  1674. $this->error("您的金币余额不足!");
  1675. }
  1676. }
  1677. //进行抽点设置
  1678. $partyInfo = null;
  1679. $partyInfo = \app\common\model\Party::field("id,room_type,party_name,party_hot,user_id,platRate,guilderRate,easemob_room_id")->where(["id"=>$party_id])->find();
  1680. // 获取系统配置信息
  1681. $platRate = $partyInfo->platRate; // 平台抽成百分比
  1682. $guilderRate = $partyInfo->guilderRate; // 房主抽成百分比,包括公会房主,普通房房主
  1683. //爆币礼物单独比例
  1684. if($is_back != 1){
  1685. if(isset($giftInfo['is_baobi']) && $giftInfo['is_baobi'] == 1){
  1686. $platRate = config('site.baobi_gift_platrate');
  1687. $guilderRate = config('site.baobi_gift_guilderrate');
  1688. }
  1689. }
  1690. //getValue削减
  1691. //
  1692. $returnData = [];
  1693. try {
  1694. $i = 0;
  1695. //用户大循环
  1696. foreach($user_id_arr as $user_id_key => $user_id) {
  1697. if($is_back == 1) {
  1698. $giftback_ids = array_column($giftbackList,'id');
  1699. if($user_id_key == 0){
  1700. //第一个人就全给扣掉
  1701. $res1 = Db::name('gift_back')->where('id','IN',$giftback_ids)->delete();
  1702. if(!$res1){
  1703. Db::rollback();
  1704. $this->error('背包送礼物失败');
  1705. }
  1706. }else{
  1707. //其他人的时候就不用扣了
  1708. $res1 = true;
  1709. }
  1710. } else {
  1711. $res1 = true;
  1712. // 扣除当前用户金币余额
  1713. $rs_wallet = model('wallet')->lockChangeAccountRemain($userauthid, $giftValue, '-', 0, "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "金币!", 3,'jewel');
  1714. if($rs_wallet['status'] == false){
  1715. Db::rollback();
  1716. $this->error($rs_wallet['msg']);
  1717. }
  1718. }
  1719. // 添加礼物赠送记录表
  1720. $data = [];
  1721. $data["user_id"] = $userauthid;
  1722. $data["user_to_id"] = $user_id;
  1723. $data["seat_num"] = $seat_id_arr[$user_id_key];
  1724. $data["party_id"] = $party_id;
  1725. $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id;
  1726. $data["gift_give_type"] = $is_back ? 1 : 2;
  1727. $data["gift_name"] = $giftInfo["name"];
  1728. $data["gift_gif_image"] = $giftInfo["image"];
  1729. $data["number"] = $number;
  1730. $data["price"] = $giftInfo["value"];
  1731. $data["value"] = $giftValue;
  1732. //每个礼物都要计算平台抽成和房主抽成
  1733. $data['platvalue'] = bcmul($platRate/100 ,$data["value"],2);//平台抽成
  1734. $data['guildervalue'] = bcmul($guilderRate/100 ,$data["value"],2);// 房主抽成
  1735. $data['guildermoney'] = bcdiv($data['guildervalue'],$money_to_jewel,2);
  1736. $data['getvalue'] = bcsub(bcsub($data["value"] ,$data['platvalue'],2),$data['guildervalue'],2);//减去抽成剩余价值
  1737. $data['task_status'] = 0;
  1738. $data["createtime"] = time();
  1739. $res5 = Db::name('gift_user_party')->insertGetId($data);
  1740. if(!$res5){
  1741. Db::rollback();
  1742. $this->error('送礼物失败');
  1743. }
  1744. //大礼物飘屏
  1745. //$this->bigGiftNotice($this->auth->nickname,$touserInfo['nickname'],$partyInfo,$data);
  1746. // 添加获赠用户余额
  1747. if($data['getvalue'] > 0){
  1748. $getMoney = bcdiv($data['getvalue'],$money_to_jewel,2);
  1749. if($getMoney > 0){
  1750. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$this->auth->nickname}送你{$giftInfo['name']}x{$number}",101,'money');
  1751. if($rs_wallet['status'] == false){
  1752. Db::rollback();
  1753. $this->error($rs_wallet['msg']);
  1754. }
  1755. }
  1756. }
  1757. // 增加房主抽成
  1758. if ($partyInfo && $data['guildervalue'] > 0) {
  1759. $guilderMoney = bcdiv($data['guildervalue'],$money_to_jewel,2);
  1760. if($guilderMoney > 0){
  1761. $touserInfo = Db::name('user')->field('nickname')->where('id',$user_id)->find();
  1762. $rs_wallet = model('wallet')->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$this->auth->nickname}送礼物{$giftInfo['name']}x{$number}给{$touserInfo['nickname']},房间礼物抽成",102,'money');
  1763. if($rs_wallet['status'] == false){
  1764. Db::rollback();
  1765. $this->error($rs_wallet['msg']);
  1766. }
  1767. }
  1768. }
  1769. if ($res1 && $res5) {
  1770. $i++;
  1771. if($party_id > 0) {
  1772. // 添加redis记录做魅力排行榜日榜用
  1773. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $giftValue, $user_id);
  1774. // 添加redis记录做魅力排行榜周榜用
  1775. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $giftValue, $user_id);
  1776. // 添加redis记录做魅力排行榜月榜用
  1777. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $giftValue, $user_id);
  1778. // 添加redis记录做财富排行榜日榜用
  1779. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
  1780. // 添加redis记录做财富排行榜周榜用
  1781. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
  1782. // 添加redis记录做财富排行榜月榜用
  1783. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
  1784. // tcp 更新用户魅力值
  1785. $this->redis->zIncrBy("hourCharm_".$party_id, $giftValue, $user_id);
  1786. // tcp 更新用户贡献值
  1787. $this->redis->zIncrBy("hourWealth_".$party_id, $giftValue, $userauthid);
  1788. }
  1789. //用户经验升级
  1790. /*
  1791. //增加被送礼物用户的魅力等级
  1792. $res_charm = \app\common\model\User::add_charm_level($user_id,$giftValue);
  1793. */
  1794. // +message
  1795. \app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number." 价值 ".$giftValue ." 金币");
  1796. }
  1797. }
  1798. //爆币,背包不爆币
  1799. if($is_back != 1){
  1800. if(isset($giftInfo['is_baobi']) && $giftInfo['is_baobi'] == 1){
  1801. $res_baobi = $this->baobi($this->auth->id,$data["gift_id"],$giftCountValue,$giftInfo["value"]);
  1802. if($res_baobi === false){
  1803. Db::rollback();
  1804. $this->error('爆币失败');
  1805. }elseif($res_baobi === true){
  1806. $returnData['baobi_total'] = 0;
  1807. $returnData['baobi_beilv'] = 0;
  1808. }else{
  1809. //爆币了
  1810. $returnData['baobi_total'] = $res_baobi['baobi_total'];
  1811. $returnData['baobi_beilv'] = $res_baobi['baobi_beilv'];
  1812. }
  1813. }
  1814. }
  1815. //增加送礼用户的财富等级
  1816. $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$giftCountValue);
  1817. // tcp 获取房间用户周前三名
  1818. $this->getPartyUserTop($partyInfo, $room_type);
  1819. if($i == $userCount) {
  1820. $returnData["image"] = $giftInfo["image"];
  1821. $returnData["gif_image"] = $giftInfo["gif_image"];
  1822. // 增加抽点记录
  1823. Db::commit();
  1824. $this->success("赠送成功!",$returnData);
  1825. } else {
  1826. $this->success("赠送失败!");
  1827. }
  1828. } catch (ValidateException $e) {
  1829. Db::rollback();
  1830. $this->error($e->getMessage());
  1831. } catch (PDOException $e) {
  1832. Db::rollback();
  1833. $this->error($e->getMessage());
  1834. } catch (Exception $e) {
  1835. Db::rollback();
  1836. $this->error($e->getMessage());
  1837. }
  1838. }
  1839. //gift_id必须是爆币礼物才可以
  1840. public function baobi($user_id,$gift_id,$pay_total,$giftInfo_value){
  1841. //奖项参数
  1842. $conf_arr = Db::name('gift_baobi_config')->where('gift_id',$gift_id)->select();
  1843. if(empty($conf_arr)){
  1844. return true;
  1845. }
  1846. //用户今天的爆币情况,与消费情况,不用区分具体哪个爆币礼物
  1847. $today_start = strtotime(date('Y-m-d')) - (86400 * 3);
  1848. $today_end = time();
  1849. $map = [
  1850. //'user_id' => $user_id,
  1851. 'createtime' => ['BETWEEN',[$today_start,$today_end]],
  1852. ];
  1853. $today_data = Db::name('gift_baobi_log')->field('IFNULL(sum(pay_total),0) as today_pay_total,IFNULL(sum(baobi_total),0) as today_baobi_total')->where($map)->find();
  1854. //select IFNULL(sum(pay_total),0) as today_pay_total,IFNULL(sum(baobi_total),0) as today_baobi_total from hx_gift_baobi_log where user_id = 1001 and createtime BETWEEN 1703088000 and 1703174399
  1855. //dump($today_data);
  1856. $baobi_zhisun_bili = config('site.baobi_zhisun_bili');
  1857. if($today_data['today_baobi_total'] >= bcdiv(bcmul($today_data['today_pay_total'],$baobi_zhisun_bili,2),100,2)){
  1858. //爆币比消费还高了,不用抽奖了,记录个日志,返回。这里的日志仅用来记录今日消费 sum(pay_total)
  1859. $data = [];
  1860. $data['user_id'] = $user_id;
  1861. $data['gift_id'] = $gift_id;
  1862. $data['baobi_id'] = 0;
  1863. $data['beilv'] = 0;
  1864. $data['rate'] = 0;
  1865. $data['pay_total'] = $pay_total;
  1866. $data['baobi_total'] = 0;
  1867. $data['createtime'] = time();
  1868. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  1869. if(!$log_id){
  1870. return false;
  1871. }
  1872. return true;
  1873. }
  1874. //概率新数组
  1875. $gailv = [];
  1876. $bei = 100; //小数变整数
  1877. foreach ($conf_arr as $key=>$value)
  1878. {
  1879. $gailv[$value['id']] = $value['rate']*$bei;
  1880. }
  1881. //dump($gailv);
  1882. //奖项新数组
  1883. $conf_column = [];
  1884. foreach ($conf_arr as $key=>$value)
  1885. {
  1886. $conf_column[$value['id']] = $value;
  1887. }
  1888. //dump($conf_column);
  1889. //抽
  1890. $rid = $this->getRand($gailv); //根据概率获取奖项id
  1891. //dump($rid);
  1892. //返回中奖结果
  1893. $result = $conf_column[$rid];
  1894. //爆币金额
  1895. $baobi_total = bcmul($giftInfo_value,$result['beilv'],0);
  1896. //写入爆币记录,不论有没有钱。全都写入其实是为了方便查日志,比在money_log里找范围更小,而且可删除
  1897. $data = [];
  1898. $data['user_id'] = $user_id;
  1899. $data['gift_id'] = $gift_id;
  1900. $data['baobi_id'] = $result['id'];
  1901. $data['beilv'] = $result['beilv'];
  1902. $data['rate'] = $result['rate'];
  1903. $data['pay_total'] = $pay_total;
  1904. $data['baobi_total'] = $baobi_total;
  1905. $data['createtime'] = time();
  1906. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  1907. if(!$log_id){
  1908. return false;
  1909. }
  1910. //直接给用户钱
  1911. if($baobi_total > 0){
  1912. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$baobi_total,'+',0,'送礼物爆币'.$result['beilv'].'倍',21,'jewel');
  1913. if($rs_wallet['status'] === false){
  1914. return false;
  1915. }
  1916. //返回爆币金额
  1917. return [
  1918. 'baobi_total' => $baobi_total,
  1919. 'baobi_beilv' => $result['beilv'],
  1920. ];
  1921. }
  1922. //默认成功
  1923. return true;
  1924. }
  1925. //概率获得算法
  1926. private function getRand($proArr) {
  1927. //概率数组的总概率精度
  1928. $proSum = array_sum($proArr);
  1929. $key = rand(1, $proSum);
  1930. // echo $key;
  1931. $result = 0;
  1932. $now = 0;
  1933. foreach ($proArr as $k=>$v)
  1934. {
  1935. $now = $now + $v;
  1936. if($key<=$now)
  1937. {
  1938. $result = $k;
  1939. break;
  1940. }
  1941. }
  1942. unset ($proArr);
  1943. return $result;
  1944. }
  1945. //某房间内礼物列表
  1946. public function getPartyGiftLog(){
  1947. $party_id = input('party_id',0);
  1948. $list = Db::name('gift_user_party')->alias('log')
  1949. ->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')
  1950. ->join('user','log.user_id = user.id','LEFT')
  1951. ->join('user b','log.user_to_id = b.id','LEFT')
  1952. ->where('log.party_id',$party_id)->order('log.id desc')->autopage()->select();
  1953. $list = list_domain_image($list,['gift_gif_image']);
  1954. foreach($list as $key => &$val){
  1955. $val['createtime'] = get_last_time($val['createtime']);
  1956. }
  1957. $this->success('success',$list);
  1958. }
  1959. // /**
  1960. // * 播放动效
  1961. // */
  1962. // public function play($party_id,$type,$value,$number) {
  1963. // // 发送tcp
  1964. // $tcpArr = [];
  1965. // $tcpArr['type'] = "play";
  1966. // $tcpArr['data'] = [
  1967. // 'party_id' => $party_id,
  1968. // 'type' => $type,
  1969. // 'value' => $value,
  1970. // 'number' => $number,
  1971. // ];
  1972. // $tcpJson = json_encode($tcpArr);
  1973. // $client = stream_socket_client(config("tcp"));
  1974. // $buffer2 = base64_encode($tcpJson)."****";
  1975. // fwrite($client, $buffer2);
  1976. // }
  1977. /**
  1978. * 更新派对信息(热度等)
  1979. */
  1980. private function updatePartyHot($partyInfo,$giftValue,$room_type) {
  1981. // $partyInfo = \app\common\model\Party::where(['id'=>$party_id])->find();
  1982. if(!$partyInfo) return $giftValue;
  1983. $party_hot = $partyInfo->party_hot > 0 ? $partyInfo->party_hot:0;
  1984. $party_hot_value = $party_hot + $giftValue;
  1985. $party_hot_value = $party_hot_value > 0 ? $party_hot_value : 0;
  1986. if($party_hot_value != $party_hot) {
  1987. // 保存数据
  1988. $partyInfo->party_hot = $party_hot_value;
  1989. $partyInfo->save();
  1990. // 更新redis 加入缓存排序
  1991. $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  1992. // 更新redis 加入缓存
  1993. $redPartyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]);
  1994. if($redPartyInfo) {
  1995. $redPartyInfo = json_decode($redPartyInfo,true);
  1996. $redPartyInfo["party_hot"] = $party_hot_value;
  1997. $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($redPartyInfo));
  1998. }
  1999. }
  2000. return $party_hot_value;
  2001. }
  2002. /**
  2003. * 用户赠送礼物后房间内用户排行,财富榜前三名
  2004. */
  2005. private function getPartyUserTop($party_info,$room_type) {
  2006. // 获取当天零点
  2007. $userModel = new \app\common\model\User();
  2008. // 获取条数
  2009. $num = 3;
  2010. // 获取3条财富排行记录
  2011. $getweek = $this->redis->zRevRange("hourWealth_".$party_info['id'],0,$num-1,true);
  2012. $userList = $userModel->rankList($getweek);
  2013. $avatarArr = [];
  2014. if($userList) {
  2015. foreach($userList as $k => $v) {
  2016. $v["jewel"] > 0 && $avatarArr[] = $v;
  2017. }
  2018. // 加入缓存做备份
  2019. $this->redis->hSet("user_jewel_top3",$party_info['id'],json_encode($avatarArr));
  2020. }
  2021. return $avatarArr;
  2022. //[环信]更新财富榜前3。放到异步了
  2023. }
  2024. /**
  2025. * 单个房间魅力值清零
  2026. */
  2027. public function partyClearCharm() {
  2028. $party_id = input("party_id");// 派对ID
  2029. $userId = input("user_id", 0);// 用户ID
  2030. if($party_id <= 0) {
  2031. $this->error("请输入派对ID");
  2032. }
  2033. if ($userId) {
  2034. $this->redis->zRem("hourCharm_" . $party_id, $userId);
  2035. } else {
  2036. $this->redis->del("hourCharm_" . $party_id);
  2037. }
  2038. $this->success("操作成功!");
  2039. }
  2040. //获取某房间内某个用户的魅力值
  2041. //上麦时候初始化小红心用的
  2042. public function getPartyUserCharm(){
  2043. $party_id = input("party_id");// 派对ID
  2044. $userId = input("user_id", 0);// 用户ID
  2045. $seat_num = input('seat_num',0);//麦位数字0-7
  2046. $key = "party_seat_".$party_id;
  2047. $seat_userid = $this->redis->hGet($key,$seat_num);
  2048. if($seat_userid){
  2049. $this->error('麦上有人了');
  2050. }
  2051. //轮询下麦
  2052. $key = "party_seat_".$party_id;
  2053. $seats = $this->redis->hGetAll($key);
  2054. foreach($seats as $seatnum => $seatuserid){
  2055. if($seatuserid == $userId){
  2056. $this->redis->HDel($key, $seatnum);
  2057. }
  2058. }
  2059. //上麦
  2060. $this->redis->hSet($key,$seat_num,$userId);
  2061. //获取魅力值
  2062. $users = $this->redis->zRevRange("hourCharm_".$party_id,0,-1,true);
  2063. $charm = 0;
  2064. if($users) {
  2065. foreach($users as $k => $v){
  2066. if($userId == $k){
  2067. $charm = changeW($v);
  2068. }
  2069. }
  2070. }
  2071. $this->success(1,$charm);
  2072. }
  2073. //下麦
  2074. public function seat_down(){
  2075. $party_id = input("party_id");// 派对ID
  2076. $userId = input("user_id", 0);// 用户ID
  2077. /*$seat_num = input('seat_num',0);//麦位数字0-7
  2078. $key = "party_seat_".$party_id;
  2079. $this->redis->HDel($key, $seat_num);*/
  2080. //轮询下麦
  2081. $key = "party_seat_".$party_id;
  2082. $seats = $this->redis->hGetAll($key);
  2083. foreach($seats as $seat_num => $seatuserid){
  2084. if($seatuserid == $userId){
  2085. $this->redis->HDel($key, $seat_num);
  2086. }
  2087. }
  2088. $this->success(1);
  2089. }
  2090. /**
  2091. * 获取音乐列表
  2092. */
  2093. public function getMusicList() {
  2094. $this->success("获取成功!",\app\common\model\Music::select());
  2095. }
  2096. //派对举报
  2097. public function report_party() {
  2098. $type = input('type');// 类型描述
  2099. $content = input('content');// 内容
  2100. $images = input('images');// 图片
  2101. $party_id = input('party_id');//
  2102. if (!$type || !$content || !$images || !$party_id) {
  2103. $this->error("请完成举报内容!");
  2104. }
  2105. $data = [];
  2106. $data["user_id"] = $this->auth->id;
  2107. $data["party_id"] = $party_id;
  2108. $data["type"] = $type;
  2109. $data["content"] = $content;
  2110. $data["images"] = $images;
  2111. $data["createtime"] = time();
  2112. $res = Db::name('party_report')->insertGetId($data);
  2113. if ($res) {
  2114. $this->success("举报内容提交成功!");
  2115. } else {
  2116. $this->error("网络错误,请稍后重试!");
  2117. }
  2118. }
  2119. //=============待唱歌曲操作=================
  2120. //待唱,列表
  2121. public function waitsing_list(){
  2122. $party_id = input('party_id');
  2123. $party_info = Db::name('party')->where('id',$party_id)->find();
  2124. if(empty($party_info)){
  2125. $this->error();
  2126. }
  2127. $easemob_room_id = $party_info['easemob_room_id'];
  2128. $easemob = new Easemob();
  2129. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  2130. if(empty($singlist)){
  2131. //默认为空
  2132. $singlist['waitsing_list'] = '[]';
  2133. }
  2134. $singlist = json_decode($singlist['waitsing_list'],true);
  2135. //重新排序
  2136. $singlist = $this->waitsing_array_column_sort($singlist);
  2137. $this->success(1,$singlist);
  2138. }
  2139. //待唱,增加
  2140. public function waitsing_add(){
  2141. //查找派对
  2142. $party_id = input('party_id');
  2143. $party_info = Db::name('party')->where('id',$party_id)->find();
  2144. if(empty($party_info)){
  2145. $this->error();
  2146. }
  2147. $easemob_room_id = $party_info['easemob_room_id'];
  2148. //接收新歌
  2149. $sing_json = input('sing_json','','htmlspecialchars_decode');
  2150. $sing_json = json_decode($sing_json,true);
  2151. $sing_json['sing_no'] = createUniqueNo('s');//附加新参数:歌曲编号
  2152. $sing_json['sort'] = time();//附加新参数:排序
  2153. //获取已有列表
  2154. $easemob = new Easemob();
  2155. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  2156. if(empty($singlist)){
  2157. //默认为空
  2158. $singlist['waitsing_list'] = '[]';
  2159. }
  2160. $singlist = json_decode($singlist['waitsing_list'],true);
  2161. //加入新歌
  2162. $singlist[] = $sing_json;
  2163. //重新排序
  2164. $singlist = $this->waitsing_array_column_sort($singlist);
  2165. //重新设置
  2166. $matedata = [
  2167. 'waitsing_list' => json_encode($singlist),
  2168. ];
  2169. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
  2170. //返回最新歌曲列表
  2171. $this->success(1,$singlist);
  2172. }
  2173. //根据歌曲sort字段,正序排列
  2174. private function waitsing_array_column_sort($data){
  2175. if(empty($data)){
  2176. return $data;
  2177. }
  2178. //dump($data);
  2179. $sort = array_column($data,'sort');
  2180. $sing_no = array_column($data,'sing_no');
  2181. $a = array_multisort($sort,SORT_ASC,$sing_no,$data);
  2182. //dump($data);exit;
  2183. return $data;
  2184. }
  2185. //待唱,删除
  2186. public function waitsing_delete(){
  2187. //查找派对
  2188. $party_id = input('party_id');
  2189. $party_info = Db::name('party')->where('id',$party_id)->find();
  2190. if(empty($party_info)){
  2191. $this->error();
  2192. }
  2193. $easemob_room_id = $party_info['easemob_room_id'];
  2194. //需要删除的编号
  2195. $sing_no = input('sing_no','');
  2196. //获取已有列表
  2197. $easemob = new Easemob();
  2198. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  2199. if(empty($singlist)){
  2200. //默认为空
  2201. $singlist['waitsing_list'] = '[]';
  2202. }
  2203. $singlist = json_decode($singlist['waitsing_list'],true);
  2204. //轮询,找到,删除
  2205. foreach($singlist as $key => $sing){
  2206. if($sing['sing_no'] == $sing_no){
  2207. unset($singlist[$key]);
  2208. }
  2209. }
  2210. //重新排序
  2211. $singlist = $this->waitsing_array_column_sort($singlist);
  2212. //重新设置
  2213. $matedata = [
  2214. 'waitsing_list' => json_encode($singlist),
  2215. ];
  2216. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
  2217. //返回最新歌曲列表
  2218. $this->success(1,$singlist);
  2219. }
  2220. //待唱,置顶
  2221. public function waitsing_top(){
  2222. //查找派对
  2223. $party_id = input('party_id');
  2224. $party_info = Db::name('party')->where('id',$party_id)->find();
  2225. if(empty($party_info)){
  2226. $this->error();
  2227. }
  2228. $easemob_room_id = $party_info['easemob_room_id'];
  2229. //需要置顶的编号
  2230. $sing_no = input('sing_no','');
  2231. //获取已有列表
  2232. $easemob = new Easemob();
  2233. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  2234. if(empty($singlist)){
  2235. //默认为空
  2236. $singlist['waitsing_list'] = '[]';
  2237. }
  2238. $singlist = json_decode($singlist['waitsing_list'],true);
  2239. //轮询,找到,改排序
  2240. if(!empty($singlist)){
  2241. //拿到所有排序字段,排列,取第0个
  2242. $sort = array_column($singlist,'sort');
  2243. ksort($sort);
  2244. foreach($singlist as $key => &$sing){
  2245. if($sing['sing_no'] == $sing_no){
  2246. $sing['sort'] = $sort[0] - 1;
  2247. }
  2248. }
  2249. }
  2250. //重新排序
  2251. $singlist = $this->waitsing_array_column_sort($singlist);
  2252. //重新设置
  2253. $matedata = [
  2254. 'waitsing_list' => json_encode($singlist),
  2255. ];
  2256. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
  2257. //返回最新歌曲列表
  2258. $this->success(1,$singlist);
  2259. }
  2260. //轮询所有房间,挨个初始化自定义属性
  2261. public function room_task(){
  2262. $ids = Db::name('party')->select();
  2263. $easemob = new Easemob();
  2264. foreach($ids as $key => $party){
  2265. //[环信]初始化房间
  2266. $matedata = [
  2267. 'seatnum'=> $party['seatnum'],//麦位数量
  2268. 'waitsing_list' => json_encode([]),//已点歌曲列表
  2269. 'party_name' => $party['party_name'],//房名字
  2270. 'party_logo' => localpath_to_netpath($party['party_logo']),//logo
  2271. 'is_public' => $party['is_public'],//是否公开
  2272. 'room_type' => $party['room_type'],//房间类型
  2273. 'online_user_num' => 0,//当前房间在线的人
  2274. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  2275. 'background' => '',//背景图
  2276. ];
  2277. $rs = $easemob->room_setRoomCustomAttributeForced($party['easemob_room_id'],$party['user_id'],$matedata);
  2278. //[环信]初始化N个麦位
  2279. $seatdata = [];
  2280. for($i=0;$i<$party['seatnum'];$i++){
  2281. $seat = [
  2282. 'charm' => 0, //红心,魅力值
  2283. 'isMaster' => false, // 是否是房主
  2284. 'headUrl' => '', // 头像
  2285. 'userNo' => '', // 座位上用户no
  2286. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  2287. 'name' => '', // 座位上用户昵称
  2288. 'seatIndex' => -1, // 座位编号
  2289. 'chorusSongCode' => '', // 是否合唱
  2290. 'isAudioMuted' => 1, // 是否静音
  2291. 'isVideoMuted' => 0, // 是否开启视频
  2292. 'checked' => false, // 用于送礼物选择用户
  2293. 'isUsed' => true, // 用于送礼物选择用户
  2294. 'gender' => 1, //性别
  2295. ];
  2296. //创建完房间就进入,所以房主直接在麦位1
  2297. $owner = Db::name('user')->where('id',$party['user_id'])->find();
  2298. if($i == 0){
  2299. $seat['isMaster'] = true;
  2300. $seat['headUrl'] = localpath_to_netpath($owner['avatar']);
  2301. $seat['userNo'] = $owner['id'];
  2302. $seat['rtcUid'] = $owner['id'];
  2303. $seat['name'] = $owner['nickname'];
  2304. $seat['seatIndex']= $i;
  2305. }
  2306. $seatdata['seat'.$i] = json_encode($seat);
  2307. }
  2308. $rs = $easemob->room_setRoomCustomAttributeForced($party['easemob_room_id'],$party['user_id'],$seatdata);
  2309. }
  2310. }
  2311. //显示聊天室所有自定义属性,自用工具
  2312. public function test_party_attr(){
  2313. $party_id = input('party_id',0);
  2314. $party_info = Db::name('party')->where('id',$party_id)->find();
  2315. $easemob = new Easemob();
  2316. $field = [
  2317. 'seatnum',
  2318. 'waitsing_list',
  2319. 'party_name',
  2320. 'party_logo',
  2321. 'is_public',
  2322. 'room_type',
  2323. 'online_user_num',
  2324. 'wealth_top3_userlist',
  2325. 'background',
  2326. 'party_notice',
  2327. 'party_notice_detail',
  2328. ];
  2329. for($i=0;$i<$party_info['seatnum'];$i++){
  2330. $field[] = 'seat'.$i;
  2331. }
  2332. $all_attr = $easemob->room_getRoomCustomAttribute($party_info['easemob_room_id'],$field);
  2333. dump($all_attr);
  2334. echo '<hr>';
  2335. foreach($all_attr as $key => $val){
  2336. echo $key;
  2337. echo '<br>';
  2338. $val = json_decode($val,true);
  2339. dump($val);
  2340. echo '<hr>';
  2341. }
  2342. //
  2343. /*$matedata = [
  2344. 'background' => '1.jpg',
  2345. ];
  2346. $rs = $easemob->room_setRoomCustomAttributeForced($party_info['easemob_room_id'],$party_info['user_id'],$matedata);
  2347. dump($rs);*/
  2348. }
  2349. //============================定时任务==========================//
  2350. /**
  2351. * redis清理排行榜 并数据库备份
  2352. */
  2353. /*public function updateTops() {
  2354. // 前一天日期
  2355. $yestaday = date("Ymd",bcsub(time(),86400));
  2356. // 上个周一
  2357. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  2358. // 上个月一号
  2359. $monty = bcsub(date("m"),1);
  2360. if($monty < 10) $monty = "0".$monty;
  2361. $premonth = date("Y").$monty."01";
  2362. // 获取所有派对ID
  2363. $party_ids = \app\common\model\Party::where(["status"=>1])->column("id");
  2364. // 获取数据
  2365. $time = time();
  2366. if($party_ids) foreach($party_ids as $k => $v) {
  2367. $keys = [
  2368. "party_jewel_get_".$v.":".$yestaday."d",
  2369. "party_jewel_to_".$v.":".$yestaday."d",
  2370. "party_jewel_get_".$v.":".$preweek."w",
  2371. "party_jewel_to_".$v.":".$preweek."w",
  2372. "party_jewel_get_".$v.":".$premonth."m",
  2373. "party_jewel_to_".$v.":".$premonth."m",
  2374. ];
  2375. foreach($keys as $key) {
  2376. // 备份数据
  2377. $redisData = $this->redis->zRevRange($key,0,-1,true);
  2378. if($redisData) {
  2379. $keyInfo = \app\common\model\RedisTops::where(["key"=>$key])->find();
  2380. $data = ["party_id"=>$v,"key"=>$key,"value"=>json_encode($redisData),"createtime" => $time];
  2381. if(!$keyInfo) {
  2382. $res = \app\common\model\RedisTops::insert($data);
  2383. // 清理数据
  2384. $res && $this->redis->zRemRangeByRank($key,0,-1);
  2385. }
  2386. }
  2387. }
  2388. }
  2389. }*/
  2390. public function handleParty() {
  2391. return false;
  2392. $partyList = \app\common\model\Party::where('is_online',1)->select();
  2393. // $this->redis->del("online_" . 3);die;
  2394. // $this->redis->hSet("online_" . 3, 4, 4);die;
  2395. foreach ($partyList as $party){
  2396. $userId = $party['user_id'];
  2397. $roomId = $party['id'];
  2398. $this->redis->HDel("online_" . $roomId, $userId);
  2399. $this->redis->zRem("party_user_".$roomId, $userId);
  2400. // 扣除在线用户在房间情况
  2401. $this->redis->hDel("livingUser",$userId);
  2402. // // 更新房间在线状态
  2403. $partyInfo = $this->redis->get("party_" . $roomId);
  2404. if ($partyInfo) {
  2405. $partyInfo = json_decode($partyInfo, true);
  2406. $memCount = count($this->redis->hGetAll("online_" . $roomId));
  2407. if ($memCount <= 0) {
  2408. dump($roomId.'--1---');
  2409. $partyInfo["is_online"] = 0;
  2410. $this->redis->set("party_" . $roomId, json_encode($partyInfo));
  2411. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  2412. }
  2413. }
  2414. $liveInfo = $this->redis->get("live_" . $roomId);
  2415. if ($liveInfo) {
  2416. $liveInfo = json_decode($liveInfo, true);
  2417. $memCount = count($this->redis->hGetAll("online_" . $roomId));
  2418. if ($memCount <= 0) {
  2419. dump($roomId.'---2--');
  2420. $liveInfo["is_online"] = 0;
  2421. $this->redis->set("live_" . $roomId, json_encode($liveInfo));
  2422. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  2423. }else{
  2424. dump($roomId.'---3--');
  2425. $this->redis->del("online_" . $roomId);
  2426. $liveInfo["is_online"] = 0;
  2427. $this->redis->set("live_" . $roomId, json_encode($liveInfo));
  2428. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  2429. }
  2430. }
  2431. }
  2432. $this->success("获取成功!");
  2433. }
  2434. }