Party.php 99 KB

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