Party.php 103 KB

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