Party.php 104 KB

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