Party.php 99 KB

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