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