Party.php 94 KB

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