Party.php 95 KB

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