Party.php 106 KB

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