Party.php 93 KB

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