Party.php 100 KB

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