Party.php 105 KB

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