Party.php 103 KB

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