Party.php 104 KB

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