Party.php 99 KB

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