Party.php 104 KB

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