Party.php 98 KB

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