Party.php 107 KB

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