Party.php 105 KB

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