Party.php 102 KB

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