Party.php 104 KB

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