Party.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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\Request;
  10. /**
  11. * 派对信息接口
  12. */
  13. class Party extends Common
  14. {
  15. protected $noNeedLogin = ['updatePartyInfo', 'getPatyType', 'addUserPositionToParty', 'clearMoney', 'clearCharm', 'getPartyRankList', 'getDefaultBackground', 'getPartGifList', 'getPartHeadgifList', 'isNotalk', 'getMusicList', 'updateTops'];
  16. protected $noNeedRight = ['*'];
  17. public function __construct(Request $request = null)
  18. {
  19. $this->roomTypeArr = [1 => "party", 2 => "live"];
  20. parent::__construct($request);
  21. }
  22. /**
  23. * 创建/进入派对
  24. */
  25. public function createParty()
  26. {
  27. $room_type = $this->request->request('room_type', 1); // 房间类型:1=派对,2=直播
  28. $partyModel = new \app\common\model\Party();
  29. $user_id = $this->auth->id;
  30. // 开直播时,判断当前用户是否为主播
  31. if ($room_type == 2) {
  32. $anchorInfo = \app\common\model\UserAnchor::where(["user_id" => $user_id])->find();
  33. $anchorInfo || $this->error("您还未申请主播!");
  34. $anchorInfo->status == 0 && $this->error("您的主播申请信息还未受理,请耐心等待!");
  35. $anchorInfo->status == 2 && $this->error("您的主播申请未通过,请重新提交!");
  36. }
  37. // 1。实名认证
  38. $userAuthInfo = \app\common\model\UserAuth::where(["user_id" => $user_id])->find();
  39. if ($userAuthInfo) {
  40. if ($userAuthInfo->status == 0) {
  41. $this->error("您的实名认证还在这审核中...,请耐心等待!");
  42. } elseif ($userAuthInfo->status == 2) {
  43. $this->error("您的实名认证审核未通过,请重新审核!");
  44. }
  45. } else {
  46. $this->error("请先申请实名认证!");
  47. }
  48. if ($room_type == 1) {
  49. // 2。开厅申请
  50. $authInfo = \app\common\model\GuildApply::where(["user_id" => $user_id])->find();
  51. if ($authInfo) {
  52. if ($authInfo->status == 0) {
  53. $this->error("您的开厅申请还在这审核中...,请耐心等待!");
  54. } elseif ($authInfo->status == -1) {
  55. $this->error("您的开厅申请审核未通过,请重新审核!");
  56. }
  57. } else {
  58. $this->error("请先申请开厅资质!");
  59. }
  60. }
  61. // 判断派对是否存在
  62. $redis = new Redis();
  63. $redisconfig = config("redis");
  64. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  65. $where = [];
  66. $where["user_id"] = $user_id;
  67. $where["room_type"] = $room_type;
  68. $sqlPartyInfo = $partyModel->where($where)->find();
  69. if (!$sqlPartyInfo) { // sql中不存在派对信息
  70. $partylimit = config("site.roomLimit");
  71. $partycount = $partyModel->count("id");
  72. if ($partycount >= $partylimit && $partylimit != -1) {
  73. $this->error("房间数量已达上线,请联系客服!");
  74. }
  75. $party_name = $this->auth->nickname . "的房间"; // 派对名称
  76. $party_logo = "/assets/api/party_logo.png"; // 派对logo
  77. $party_type = 0; // 派对类型(情感互动,心动点单 等)
  78. $party_notice = "请编辑房间公告"; // 派对公告
  79. $party_notice_detail = "请编辑房间公告内容!"; // 派对公告详情
  80. if (!$party_name || !$party_logo || !$party_notice) {
  81. $this->error(__('Invalid parameters'));
  82. }
  83. $party_ids = $partyModel->column("party_id");
  84. // 创建派对ID (临时ID四位,派对数不超过8999)
  85. $party_id = $this->auth->getUinqueId(4, $party_ids);
  86. if ($party_id > 9999) {
  87. $this->error("派对超限,请联系客服");
  88. }
  89. $data = [];
  90. $data["user_id"] = $this->auth->id;
  91. $data["room_type"] = $room_type;
  92. $data["party_id"] = $party_id;
  93. $data["party_hot"] = 0;
  94. $data["party_name"] = $party_name;
  95. $data["party_logo"] = $party_logo;
  96. $data["party_type"] = $party_type;
  97. $data["party_notice"] = $party_notice;
  98. $data["party_notice_detail"] = $party_notice_detail;
  99. $data["is_online"] = 1;
  100. $data["status"] = 0;
  101. $data["is_recommend"] = 0;
  102. $data["createtime"] = time();
  103. $id = $partyModel->insertGetId($data);
  104. if (!$id) {
  105. $this->error("派对创建失败,请稍后重试!");
  106. }
  107. $data["id"] = $id;
  108. if ($room_type == 2) {
  109. \app\common\model\User::update(["is_live" => 1], ["id" => $user_id]);
  110. }
  111. $partyInfo = $partyModel->where(["id" => $id])->find();
  112. $partyInfo["is_new"] = 1;
  113. // 冠名
  114. $userInfo = [];
  115. if ($partyInfo["naming"]) {
  116. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id" => $partyInfo["naming"]])->find();
  117. }
  118. // 头像
  119. if ($partyInfo["user_id"] > 0 && $room_type == 2) {
  120. $partyInfo["avatar"] = \app\common\model\User::where(["id" => $partyInfo["user_id"]])->value("avatar");
  121. }
  122. // 派对类型
  123. $partyTypeName = "普通房";
  124. if ($partyInfo["party_type"]) {
  125. $partyTypeName = \app\common\model\PartyType::where(["id" => $partyInfo["party_type"]])->value("name");
  126. }
  127. $partyInfo["naming"] = $userInfo;
  128. $partyInfo["type_name"] = $partyTypeName;
  129. // 加入缓存排序
  130. $redis->zAdd($this->roomTypeArr[$room_type] . "Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  131. // 加入缓存
  132. $redis->set($this->roomTypeArr[$room_type] . "_" . $partyInfo["id"], json_encode($partyInfo));
  133. if ($room_type != 2) {
  134. // 创建公会
  135. \app\common\model\Guild::createGuild($partyInfo["id"], $partyInfo["party_name"], $user_id);
  136. \app\common\model\User::update(["is_guild" => 3], ["id" => $user_id]);
  137. }
  138. } else {
  139. if ($sqlPartyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  140. if ($sqlPartyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
  141. $partyInfo = $sqlPartyInfo;
  142. $partyInfo["is_new"] = 0;
  143. }
  144. $this->success("获取成功!", $partyInfo);
  145. }
  146. /**
  147. * 更新房间状态为正常
  148. */
  149. public function savePartyStatus()
  150. {
  151. $party_id = $this->request->request('party_id'); // 派对ID
  152. $room_type = $this->request->request('room_type', 1); // 房间类型:1=派对,2=直播
  153. if (!$party_id || !in_array($room_type, [1, 2])) $this->error(__('Invalid parameters'));
  154. $user_id = $this->auth->id;
  155. $partyInfo = \app\common\model\Party::get($party_id);
  156. if ($partyInfo->status < 0) $this->error("当前状态不支持变更!");
  157. if ($partyInfo->user_id !== $user_id) $this->error("当前账号异常,非工会长禁止操作!");
  158. $partyInfo->status = 1;
  159. $res = $partyInfo->save();
  160. if ($res) {
  161. // 更新redis 加入缓存排序
  162. $redis = new Redis();
  163. $redisconfig = config("redis");
  164. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  165. // 更新redis 加入缓存
  166. $redPartyInfo = $redis->get($this->roomTypeArr[$room_type] . "_" . $partyInfo["id"]);
  167. if ($redPartyInfo) {
  168. $redPartyInfo = json_decode($redPartyInfo, true);
  169. $redPartyInfo["status"] = 1;
  170. $redis->set($this->roomTypeArr[$room_type] . "_" . $partyInfo["id"], json_encode($redPartyInfo));
  171. }
  172. $this->success("更新公会状态成功!");
  173. } else {
  174. $this->error("网络错误,请稍后重试!");
  175. }
  176. }
  177. /**
  178. * 删除派对(群解散)
  179. */
  180. public function closeParty()
  181. {
  182. $party_id = $this->request->request('party_id'); // 派对ID
  183. $room_type = $this->request->request('room_type', 1); // 房间类型:1=派对,2=直播
  184. if (!$party_id || !in_array($room_type, [1, 2])) $this->error(__('Invalid parameters'));
  185. $partyInfo = \app\common\model\Party::where(["id" => $party_id])->find();
  186. if (!$partyInfo) $this->error(__('派对不存在!'));
  187. $res = $partyInfo->delete();
  188. if ($res !== false) {
  189. // redis 删除
  190. $redis = new Redis();
  191. $redisconfig = config("redis");
  192. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  193. $redis->zRem($this->roomTypeArr[$room_type] . "Rank", $party_id);
  194. $redis->del($this->roomTypeArr[$room_type] . "_" . $party_id);
  195. $this->success("删除成功!");
  196. } else {
  197. $this->error("删除失败!");
  198. }
  199. }
  200. /**
  201. * 获取派对列表排序
  202. */
  203. public function getPartyRankList()
  204. {
  205. $thispage = $this->request->request('thispage', 1, "intval"); // 当前页数
  206. $pagenum = $this->request->request('pagenum', 10, "intval"); // 每页显示条数0=不做分页
  207. $type_id = $this->request->request('type_id'); // 派对类型
  208. $room_type = $this->request->request('room_type', 1); // 房间类型:1=派对,2=直播
  209. $is_recommend = $this->request->request('is_recommend'); // 推荐0=否1=是
  210. $all = $this->request->request('all'); // 全部分类0=否1=是
  211. $index = $this->request->request('index', 0); // 全部分类0=否1=是
  212. $start = ($thispage - 1) * $pagenum;
  213. $end = $start + ($pagenum - 1);
  214. // 获取排序
  215. $redis = new Redis();
  216. $redisconfig = config("redis");
  217. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  218. $redisPartyRankList = $redis->zRevRange($this->roomTypeArr[$room_type] . "Rank", 0, -1, true);
  219. $partyModel = new \app\common\model\Party();
  220. if (!$redisPartyRankList) {
  221. $userModel = new \app\common\model\User();
  222. // 直接从数据库获取所有数据
  223. $where = [];
  224. $where["a.status"] = 1;
  225. $where["a.room_type"] = $room_type;
  226. $sqlPartyList = $partyModel->alias("a")->field("a.*,b.name as type_name")
  227. ->join("hx_party_type b", "a.party_type = b.id", "left")->where($where)->select();
  228. $userList = $userModel->field("id,nickname,avatar")->where(["status" => "normal"])->select();
  229. $userInfoArr = [];
  230. if ($userList) foreach ($userList as $k => $v) $userInfoArr[$v["id"]] = $v;
  231. if ($sqlPartyList) {
  232. foreach ($sqlPartyList as $k => $v) {
  233. // 加入缓存排序
  234. $redis->zAdd($this->roomTypeArr[$room_type] . "Rank", $v['party_hot'], $v["id"]);
  235. // 设置冠名
  236. $sqlPartyList[$k]["naming"] = isset($userInfoArr[$v["naming"]]) ? $userInfoArr[$v["naming"]] : [];
  237. // 设置房主头像
  238. $sqlPartyList[$k]["avatar"] = isset($userInfoArr[$v["user_id"]]) ? $userInfoArr[$v["user_id"]]["avatar"] : [];
  239. // 加入缓存
  240. $redis->set($this->roomTypeArr[$room_type] . "_" . $v["id"], json_encode($v));
  241. }
  242. $redisPartyRankList = $redis->zRevRange($this->roomTypeArr[$room_type] . "Rank", 0, -1, true);
  243. }
  244. }
  245. if ($redisPartyRankList) {
  246. $resultInfo = $partyModel->getPatyInfoByPartyId($redisPartyRankList, $this->roomTypeArr[$room_type], $type_id, $is_recommend, $all, $start, $end, $index);
  247. $this->success("获取成功!", $resultInfo);
  248. } else {
  249. $this->success("获取成功!", []);
  250. }
  251. }
  252. /**
  253. * 获取派对用户排序
  254. */
  255. public function getPartyUserRank()
  256. {
  257. $room_type = $this->request->request('room_type', 1); // 房间类型
  258. $party_id = $this->request->request("party_id");// 派对ID
  259. if (!$party_id) $this->error(__('Invalid parameters'));
  260. $redis = new Redis();
  261. $redisconfig = config("redis");
  262. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  263. $res = [];
  264. // 获取今天
  265. $day = date("Ymd");
  266. // 获取本周第一天
  267. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  268. // 获取本月第一天
  269. $monthday = date("Ym01");
  270. $userModel = new \app\common\model\User();
  271. // 获取条数
  272. $num = 50;
  273. // 获取50条财富排行日记录
  274. $getday = $redis->zRevRange($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", 0, $num - 1, true);
  275. $res['getRankListDay'] = $userModel->rankList($getday);
  276. // 获取50条财富排行日记录
  277. $today = $redis->zRevRange($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", 0, $num - 1, true);
  278. $res['toRankListDay'] = $userModel->rankList($today);
  279. // 获取50条财富排行周记录
  280. $getweek = $redis->zRevRange($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", 0, $num - 1, true);
  281. $res['getRankListWeek'] = $userModel->rankList($getweek);
  282. // 获取50条贡献排行周记录
  283. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", 0, $num - 1, true);
  284. $res['toRankListWeek'] = $userModel->rankList($toweek);
  285. // 获取50条财富排行月记录
  286. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", 0, $num - 1, true);
  287. $res['getRankListMonth'] = $userModel->rankList($toweek);
  288. // 获取50条贡献排行周记录
  289. $toweek = $redis->zRevRange($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", 0, $num - 1, true);
  290. $res['toRankListMonth'] = $userModel->rankList($toweek);
  291. return $this->success("获取成功!", $res);
  292. }
  293. /**
  294. * 派对热度更新(已废弃)
  295. */
  296. public function changeUserPartyhot()
  297. {
  298. $party_id = $this->request->request('party_id', 0, "intval"); // 派对ID
  299. $room_type = $this->request->request('room_type', 1); // 房间类型
  300. $party_hot = $this->request->request('party_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值)
  301. if (!$party_id || !$party_hot) {
  302. $this->error(__('Invalid parameters'));
  303. }
  304. $redis = new Redis();
  305. $redisconfig = config("redis");
  306. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  307. // 更新热度
  308. $redis->zIncrBy($this->roomTypeArr[$room_type] . "Rank", $party_hot, $party_id);
  309. return $this->success("更新成功!");
  310. }
  311. /**
  312. * 更新主持人和麦位前四位至首页房间排行
  313. */
  314. public function addUserPositionToParty()
  315. {
  316. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  317. $user_id = $this->request->request('user_id', 0, "intval"); // 用户ID
  318. $upOrdown = $this->request->request('upordown'); // 上下麦:1=上麦-1=下麦
  319. $room_type = $this->request->request('room_type', 1); // 房间类型
  320. $avatar = $this->request->request('avatar'); // 用户头像 (upOrdown参数为-1时可以不传)
  321. $position = $this->request->request('position'); // 麦位置:0=支持人,1=1号麦,2=2号麦,3=3号麦,4=4号麦
  322. if (!$party_id || !$user_id || !in_array($position, [0, 1, 2, 3, 4, 5, 6, 7, 8]) || !in_array($upOrdown, [1, -1])) {
  323. $this->error(__('Invalid parameters'));
  324. }
  325. if ($upOrdown == 1) {
  326. // 先完成所有麦上记录
  327. \app\common\model\UserOnsiteTime::update(["status" => 2], ["user_id" => $user_id]);
  328. // 保存上麦记录
  329. $data = [];
  330. $data["user_id"] = $user_id;
  331. $data["party_id"] = $party_id;
  332. $data["onsite_time"] = time();
  333. \app\common\model\UserOnsiteTime::insert($data);
  334. }
  335. if ($upOrdown == -1) {
  336. // 更新下麦时间
  337. $update = [];
  338. $update["offsite_time"] = time();
  339. $update["status"] = 2;
  340. \app\common\model\UserOnsiteTime::update($update, ["user_id" => $user_id, "status" => 1]);
  341. }
  342. if (in_array($position, [5, 6, 7, 8])) return $this->success("设置成功!");
  343. $redis = new Redis();
  344. $redisconfig = config("redis");
  345. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  346. $redisData = $redis->get($this->roomTypeArr[$room_type] . "_" . $party_id);
  347. if ($redisData) {
  348. $partyInfo = json_decode($redisData, true);
  349. if ($upOrdown == 1) {
  350. // 删掉已有头像,防止重复
  351. $partyuser = isset($partyInfo["party_user"]) ? $partyInfo["party_user"] : "";
  352. if (is_array($partyuser)) foreach ($partyuser as $k => $v) if ($v === $avatar) unset($partyInfo["party_user"][$k]);
  353. $partyInfo["party_user"][$position] = $avatar;
  354. } else {
  355. if (isset($partyInfo["party_user"])) unset($partyInfo["party_user"][$position]);
  356. }
  357. // sort($partyInfo["party_user"]);
  358. $redis->set($this->roomTypeArr[$room_type] . "_" . $party_id, json_encode($partyInfo));
  359. }
  360. return $this->success("设置成功!");
  361. }
  362. /**
  363. * 获取派对在线人数列表
  364. */
  365. public function getOnlieList()
  366. {
  367. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID 逻辑ID
  368. $page = $this->request->request('page', 1); // 分页
  369. $pageNum = $this->request->request('pageNum', 10); // 分页
  370. // 分页搜索构建
  371. $pageStart = ($page - 1) * $pageNum;
  372. if (!$party_id) {
  373. $this->error(__('Invalid parameters'));
  374. }
  375. $redis = new Redis();
  376. $redisconfig = config("redis");
  377. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  378. $res = $redis->hGetAll("online_" . $party_id);
  379. $user_ids = [];
  380. $userList = [];
  381. if ($res) $user_ids = array_values($res);
  382. // 获取用户列表信息
  383. $user_ids && $userList = \app\common\model\User::field("id,avatar,nickname,level,gender")->where(["id" => ["in", $user_ids]])->limit($pageStart, $pageNum)->select();
  384. $this->success("获取成功!", $userList);
  385. }
  386. /**
  387. * 加入派对
  388. */
  389. public function joinParty()
  390. {
  391. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID 逻辑ID
  392. $room_type = $this->request->request("room_type", 1);//
  393. $party_pass = $this->request->request("party_pass");//
  394. if (!$party_id || !in_array($room_type, [1, 2])) {
  395. $this->error(__('Invalid parameters'));
  396. }
  397. $user_id = $this->auth->id;
  398. $redis = new Redis();
  399. $redisconfig = config("redis");
  400. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  401. $redis->zAdd("party_user_" . $party_id, $this->auth->u_id, $user_id);
  402. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  403. $lsetList = [];
  404. for ($i = 1; $i <= 4; $i++) {
  405. $hgetlist = $redis->hGet("party_manage_" . $party_id, $user_id . "-" . $i);
  406. $hgetlist = unserialize($hgetlist);
  407. $restime = time() - intval($hgetlist["createtime"]);
  408. // 房管
  409. if ($hgetlist && $i == 1) $lsetList["manage_restime"] = "";
  410. // 禁言
  411. if ($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime;
  412. // 拉黑
  413. if ($hgetlist && $i == 3 && $restime < $hgetlist["time"]) {
  414. $restime = date("Y-m-d H:i:s", ($hgetlist["time"] + $hgetlist["createtime"]));
  415. $this->error(__('您已被该房间拉黑,解除时间:' . $restime));
  416. break;
  417. }
  418. // 踢出
  419. if ($hgetlist && $i == 4 && $restime < $hgetlist["time"]) {
  420. $restime = date("Y-m-d H:i:s", ($hgetlist["time"] + $hgetlist["createtime"]));
  421. $this->error(__('您已被该房间踢出,解除时间:' . $restime));
  422. break;
  423. }
  424. }
  425. // 判断派对密码
  426. $partyInfo = $redis->get($this->roomTypeArr[$room_type] . "_" . $party_id);
  427. if ($partyInfo) {
  428. $partyInfo = json_decode($partyInfo, true);
  429. if ($partyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  430. if ($partyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
  431. if (isset($partyInfo["party_pass"]) && $partyInfo["party_pass"] && $partyInfo["user_id"] != $user_id) {
  432. if (($party_pass != $partyInfo["party_pass"] || strlen($party_pass) != 4) && $this->auth->mobile != '17353993050') {
  433. $this->error("派对密码不正确!");
  434. }
  435. }
  436. // 如果是房主自己进入房间,则更新用户 为在线状态
  437. if ($partyInfo["user_id"] == $user_id) {
  438. if ($room_type == 1)
  439. \app\common\model\Party::update(["is_online" => 1], ["id" => $partyInfo["id"]]);
  440. if ($room_type == 2)
  441. \app\common\model\User::update(["is_live" => 1], ["id" => $partyInfo["user_id"]]);
  442. }
  443. } else {
  444. $this->error("派对信息获取失败!");
  445. }
  446. // 获取用户魅力值
  447. $users = $redis->zRange("hourCharm_" . $party_id, 0, -1, true);
  448. $u = [];
  449. if ($users) {
  450. foreach ($users as $k => $v) $u[] = [
  451. "user_id" => $k,
  452. "charm" => $this->changeW($v)
  453. ];
  454. }
  455. $lsetList["userCharm"] = $u;
  456. // 获取用户排行榜前三名头像
  457. $heads = $redis->hGet("user_jewel_top3", $party_id);
  458. $lsetList["userJewelTop3"] = $heads ? json_decode($heads, true) : [];
  459. // 判断当前用户是否收藏了此房间
  460. $cellection = $redis->hGet("room_cellection", $user_id . "-" . $party_id);
  461. $lsetList["is_cellection"] = $cellection ? 1 : 0;
  462. if (isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  463. // $partyInfo["type_name"] = $partyInfo["type_name"];
  464. } else {
  465. $partyInfo["type_name"] = "普通房";
  466. }
  467. $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]);
  468. $lsetList["partyInfo"] = $partyInfo;
  469. return $this->success("加入成功!", $lsetList);
  470. }
  471. /**
  472. * 判断是否被禁言
  473. */
  474. public function isNotalk()
  475. {
  476. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID 逻辑ID
  477. $user_id = $this->request->request('user_id', 0, "intval"); // 用户ID 逻辑ID
  478. if (!$user_id || !$party_id) {
  479. $this->error(__('Invalid parameters'));
  480. }
  481. $redis = new Redis();
  482. $redisconfig = config("redis");
  483. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  484. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  485. $lsetList = [];
  486. $lsetList["is_notalk"] = 0;
  487. $lsetList["notalk_time"] = 0;
  488. $hgetlist = $redis->hGet("party_manage_" . $party_id, $user_id . "-2");
  489. $hgetlist = unserialize($hgetlist);
  490. $restime = time() - intval($hgetlist["createtime"]);
  491. // 禁言
  492. if ($hgetlist && $restime < $hgetlist["time"]) {
  493. $lsetList["is_notalk"] = 1;
  494. $lsetList["notalk_time"] = $restime;
  495. }
  496. return $this->success("获取成功!", $lsetList);
  497. }
  498. /**
  499. * 退出派对
  500. */
  501. public function outParty()
  502. {
  503. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  504. if (!$party_id) {
  505. $this->error(__('Invalid parameters'));
  506. }
  507. $user_u_id = $this->auth->u_id;
  508. $redis = new Redis();
  509. $redisconfig = config("redis");
  510. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  511. $redis->zRem("party_user_" . $party_id, $user_u_id);
  512. return $this->success("退出成功!", []);
  513. }
  514. /**
  515. * 派对内搜索用户
  516. */
  517. public function searchUserParty()
  518. {
  519. $u_id = $this->request->request('u_id', 0, "intval"); // 用户u_id
  520. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  521. $redis = new Redis();
  522. $redisconfig = config("redis");
  523. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  524. $userids = $redis->zRange("party_user_" . $party_id, 0, -1, true);
  525. $useridArr = [];
  526. if ($userids) foreach ($userids as $k => $v) $useridArr[$v] = $k; // 因为array_slip 不能交换数字啊
  527. $userid = isset($useridArr[$u_id]) ? $useridArr[$u_id] : 0;
  528. $userModel = new \app\common\model\User();
  529. $where = [];
  530. $where["id"] = $userid;
  531. $userInfo = $userModel->field("id,u_id,avatar,nickname,level,gender")->where($where)->select();
  532. return $this->success("查询成功!", $userInfo);
  533. }
  534. /**
  535. * 设置冠名
  536. */
  537. public function setNaming()
  538. {
  539. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  540. $user_id = $this->request->request('user_id', 0, "intval"); // 用户ID
  541. $type = $this->request->request('type', 1); // 1:设置 0:取消设置
  542. if (!$party_id || !$user_id || ($type != 1 && $type != 0)) {
  543. $this->error(__('Invalid parameters'));
  544. }
  545. $redis = new Redis();
  546. $redisconfig = config("redis");
  547. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  548. $partyModel = new \app\common\model\Party();
  549. $data = [];
  550. $where = [];
  551. $where["id"] = $party_id;
  552. $partyInfo = $partyModel->where(["id" => $party_id])->find();
  553. $getredisPartyInfo = $redis->get($this->roomTypeArr[$partyInfo->room_type] . '_' . $party_id);
  554. $redisPartyInfo = json_decode($getredisPartyInfo, true);
  555. if ($type == 1) {
  556. $data["naming"] = $user_id;
  557. // 冠名
  558. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id" => $user_id])->find($user_id);
  559. $redisPartyInfo["naming"] = $userInfo;
  560. } else {
  561. $data["naming"] = 0;
  562. $redisPartyInfo["naming"] = [];
  563. }
  564. $redis->set($this->roomTypeArr[$partyInfo->room_type] . '_' . $party_id, json_encode($redisPartyInfo));
  565. $res = $partyModel->update($data, $where);
  566. if ($res !== false) {
  567. $this->success("操作成功!");
  568. } else {
  569. $this->error("网络错误,请稍后重试!");
  570. }
  571. }
  572. /**
  573. * 派对收藏
  574. */
  575. public function cellectionParty()
  576. {
  577. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  578. if (!$party_id) {
  579. $this->error(__('Invalid parameters'));
  580. }
  581. $user_id = $this->auth->id;
  582. $redis = new Redis();
  583. $redisconfig = config("redis");
  584. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  585. $partycellectionModel = new \app\common\model\PartyCellection();
  586. // 添加记录
  587. $data = [];
  588. $data["user_id"] = $user_id;
  589. $data["party_id"] = $party_id;
  590. if ($partycellectionModel->where($data)->find()) {
  591. $res = $partycellectionModel->where($data)->delete();
  592. $redis->hDel("room_cellection", $user_id . "-" . $party_id);
  593. return $this->success("取消收藏成功!", $res);
  594. }
  595. $data["createtime"] = time();
  596. $res = $partycellectionModel->insert($data);
  597. $redis->hSet("room_cellection", $user_id . "-" . $party_id, 1);
  598. return $this->success("收藏成功!", $res);
  599. }
  600. /**
  601. * 派对收藏列表
  602. */
  603. public function cellectionPartyList()
  604. {
  605. $page = $this->request->request('page', 1); // 分页
  606. $pageNum = $this->request->request('pageNum', 10); // 分页
  607. // 分页搜索构建
  608. $pageStart = ($page - 1) * $pageNum;
  609. $partycellectionModel = new \app\common\model\PartyCellection();
  610. $userModel = new \app\common\model\User();
  611. $where = [];
  612. $where["a.user_id"] = $this->auth->id;
  613. $where["r.room_type"] = 1;
  614. $list = $partycellectionModel->alias("a")
  615. ->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")
  616. ->where($where)
  617. ->join("hx_party r", "a.party_id = r.id")
  618. ->join("hx_party_type t", "t.id = r.party_type", "left")
  619. ->limit($pageStart, $pageNum)
  620. ->select();
  621. if ($list) {
  622. // $redis = new Redis();
  623. // $redisconfig = config("redis");
  624. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  625. // // 获取本周第一天
  626. // $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  627. // // 获取redis 中 用户排行榜前五名
  628. // foreach($list as $k => $v) {
  629. // $getweek = $redis->zRevRange("party_jewel_get_".$v["r_id"].":".$weekday,0,4,true);
  630. // $userlist = $userModel->rankList($getweek);
  631. // if($userlist) {
  632. // $users = [];
  633. // foreach($userlist as $m => $n) {
  634. // $users[] = $n["avatar"];
  635. // }
  636. // $list[$k]["users"] = $users;
  637. // } else {
  638. // $list[$k]["users"] = [];
  639. // }
  640. //
  641. // $mod = isset($v["party_type_id"])?intval($v["party_type_id"])%5:1;
  642. // $list[$k]["party_type_color"] = $mod == 0?5:$mod;
  643. // }
  644. $users = [];
  645. foreach ($list as $k => $v) {
  646. $users[$v["party_id"]] = $v["party_hot"];
  647. }
  648. $partyModel = new \app\common\model\Party();
  649. $resultInfo = $partyModel->getPatyInfoByPartyId($users, "party", 0, 0, 1, 0, 20, 0);
  650. $this->success("获取成功!", $resultInfo);
  651. }
  652. return $this->success("获取成功!", $list);
  653. }
  654. /**
  655. * 获取派对信息
  656. */
  657. public function getPartyInfo()
  658. {
  659. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  660. if (!$party_id) {
  661. $this->error(__('Invalid parameters'));
  662. }
  663. $user_id = $this->auth->id;
  664. $partyModel = new \app\common\model\Party();
  665. $partycellectionModel = new \app\common\model\PartyCellection();
  666. $userModel = new \app\common\model\User();
  667. // 获取主体信息
  668. $where = [];
  669. $where["a.id"] = $party_id;
  670. $partyInfo = $partyModel->alias("a")
  671. ->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")
  672. ->join("hx_party_type rt", "rt.id = a.party_type", "left")
  673. ->where($where)
  674. ->find();
  675. if ($partyInfo) {
  676. $mod = isset($partyInfo["party_type"]) ? intval($partyInfo["party_type"]) % 5 : 1;
  677. $partyInfo["party_type_color"] = $mod == 0 ? 5 : $mod;
  678. if (isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  679. // $partyInfo["type_name"] = $partyInfo["type_name"];
  680. } else {
  681. $partyInfo["type_name"] = "普通房";
  682. }
  683. // 获取是否被当前用户收藏
  684. $partyInfo["is_sellection"] = 0;
  685. $where = [];
  686. $where["user_id"] = $user_id;
  687. $where["party_id"] = $party_id;
  688. if ($partycellectionModel->where($where)->find()) {
  689. $partyInfo["is_sellection"] = 1;
  690. }
  691. // 获取房主信息
  692. $where = [];
  693. $where["id"] = $partyInfo["user_id"];
  694. $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
  695. // 获取技能信息
  696. $skillList = Model("ViewUserSkill")->getSkillInfo($partyInfo["user_id"]);
  697. $userInfo["skill"] = implode("/", $skillList);
  698. $partyInfo["userInfo"] = $userInfo;
  699. $partyInfo["party_hot"] = $this->changeW($partyInfo["party_hot"]);
  700. }
  701. $this->success("获取成功!", $partyInfo);
  702. }
  703. /**
  704. * 派对设置
  705. */
  706. public function setParty()
  707. {
  708. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  709. $party_name = $this->request->request('party_name'); // 派对名称
  710. $party_logo = $this->request->request('party_logo'); // 派对logo/封面
  711. $party_pass = $this->request->request('party_pass'); // 派对密码
  712. $party_type = $this->request->request('party_type'); // 使用场景
  713. $is_screen = $this->request->request('is_screen'); // 是否关闭公屏:1=是,0=否
  714. $on_model = $this->request->request('on_model'); // 上麦模式:1=自由模式,2=麦序模式
  715. $room_type = $this->request->request('room_type', 1); // 房间类型:1=派对,2=直播
  716. $background = $this->request->request('background'); // 派对背景
  717. if (!$party_id || (!$party_name && !$party_logo && !$party_pass && !$party_type && !$is_screen && !$on_model && !$background)) {
  718. $this->error(__('Invalid parameters'));
  719. }
  720. if ($party_pass && strlen($party_pass) != 4) {
  721. $this->error("房间密码必须为四位!");
  722. }
  723. $partyModel = new \app\common\model\Party();
  724. $data = [];
  725. $party_name && $data["party_name"] = $party_name;
  726. $party_logo && $data["party_logo"] = $party_logo;
  727. $data["party_pass"] = $party_pass;
  728. $party_type && $data["party_type"] = $party_type;
  729. $is_screen && $data["is_screen"] = $is_screen;
  730. $on_model && $data["on_model"] = $on_model;
  731. $background && $data["background"] = $background;
  732. $where = [];
  733. $where["id"] = $party_id;
  734. $res = $partyModel->update($data, $where);
  735. if ($res) {
  736. // 获取派对类型
  737. if ($party_type) $data["type_name"] = \app\common\model\PartyType::where(["id" => $party_type])->value("name");
  738. // 存redis 房间信息
  739. $redis = new Redis();
  740. $redisconfig = config("redis");
  741. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  742. $partyInfo = $redis->get($this->roomTypeArr[$room_type] . "_" . $party_id);
  743. if ($partyInfo) {
  744. $partyInfo = json_decode($partyInfo, true);
  745. $partyInfo = array_replace($partyInfo, $data);
  746. $redis->set($this->roomTypeArr[$room_type] . "_" . $party_id, json_encode($partyInfo));
  747. }
  748. $this->success("房间设置成功!", $data);
  749. } else {
  750. $this->error("网络错误,请稍后重试!");
  751. }
  752. }
  753. /**
  754. * 获取派对背景
  755. */
  756. public function getDefaultBackground()
  757. {
  758. $room_type = $this->request->request('room_type', 1); // 房间类型
  759. if (!in_array($room_type, [1, 2])) {
  760. $this->error(__('Invalid parameters'));
  761. }
  762. $this->success("获取成功!", \app\common\model\PartyBackground::where(["room_type" => $room_type])->select());
  763. }
  764. /**
  765. * 派对管理设置
  766. */
  767. public function partyManageSet()
  768. {
  769. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  770. $user_id = $this->request->request('user_id', 0, "intval"); // 用户ID
  771. $item = $this->request->request('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  772. $time = $this->request->request('time'); // 限制时间(单位:秒):0=永久
  773. if (!$party_id || !$user_id || !in_array($item, [1, 2, 3, 4])) {
  774. $this->error(__('Invalid parameters'));
  775. }
  776. if ($item > 1 && $time <= 0) {
  777. $this->error(__('时间设置有误'));
  778. }
  779. // 获取用户信息
  780. $userInfo = \app\common\model\User::field("noble,avatar,nickname,gender,level")->where(["id" => $user_id])->find();
  781. if (!$userInfo) $this->error("用户信息获取失败!");
  782. // 国王防踢。
  783. $noble_no = \app\common\model\NobleLevel::where(["id" => $userInfo->noble])->value("level_no");
  784. if (($item == 3 || $item == 4) && $noble_no == "p08PCcNB") {
  785. $this->error("对方已开通国王贵族,踢出房间失败!");
  786. }
  787. $redis = new Redis();
  788. $redisconfig = config("redis");
  789. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  790. $data = [];
  791. $data["user_id"] = $user_id;
  792. $data["avatar"] = $userInfo->avatar;
  793. $data["nickname"] = $userInfo->nickname;
  794. $data["gender"] = $userInfo->gender;
  795. $data["level"] = $userInfo->level;
  796. $data["item"] = $item;
  797. $data["time"] = $time;
  798. $data["createtime"] = time();
  799. $res = $redis->hSet("party_manage_" . $party_id, $user_id . "-" . $item, serialize($data));
  800. if ($res !== false) {
  801. $this->success("设置成功!", $data);
  802. } else {
  803. $this->error("网络错误,请稍后重试!");
  804. }
  805. }
  806. /**
  807. * 派对管理设置列表
  808. */
  809. public function partyManageSetList()
  810. {
  811. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  812. $item = $this->request->request('item', 1, "intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  813. if (!$party_id || !in_array($item, [1, 2, 3, 4])) {
  814. $this->error(__('Invalid parameters'));
  815. }
  816. $time = time();
  817. $redis = new Redis();
  818. $redisconfig = config("redis");
  819. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  820. $hget = $redis->hGetAll("party_manage_" . $party_id);
  821. $list = array();
  822. foreach ($hget as $key => $val) {
  823. if (substr($key, -2) == "-" . $item) {
  824. array_push($list, unserialize($val));
  825. }
  826. }
  827. if ($list) {
  828. foreach ($list as $k => $v) if (bcadd($v["createtime"], $v["time"]) <= $time && $v["item"] > 1) unset($list[$k]);
  829. if ($list) $list = array_values($list);
  830. }
  831. $this->success("获取成功!", $list);
  832. }
  833. /**
  834. * 派对管理设置移除
  835. */
  836. public function partyManageSetDel()
  837. {
  838. $id = $this->request->request('user_id', 0, "intval"); // userid
  839. $party_id = $this->request->request('party_id', 0, "intval"); // 房间ID
  840. $item = $this->request->request('item', 1, "intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  841. if (!$id || !in_array($item, [1, 2, 3, 4])) {
  842. $this->error(__('Invalid parameters'));
  843. }
  844. $redis = new Redis();
  845. $redisconfig = config("redis");
  846. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  847. $res = $redis->hDel("party_manage_" . $party_id, $id . "-" . $item);
  848. if ($res !== false) {
  849. $this->success("移除成功!", $res);
  850. } else {
  851. $this->error("网络错误,请稍后重试!");
  852. }
  853. }
  854. /**
  855. * 更新派对公告
  856. */
  857. public function savePartyNotice()
  858. {
  859. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  860. $room_type = $this->request->request('room_type', 1); // 房间类型:1=派对2=直播
  861. $party_notice = $this->request->request('party_notice'); // 公告标题
  862. $party_notice_detail = $this->request->request('party_notice_detail'); // 公告内容
  863. if (!$party_id || !$party_notice || !$party_notice_detail) {
  864. $this->error(__('Invalid parameters'));
  865. }
  866. $partyModel = new \app\common\model\Party();
  867. $where = [];
  868. $where["id"] = $party_id;
  869. $data = [];
  870. $data["party_notice"] = $party_notice;
  871. $data["party_notice_detail"] = $party_notice_detail;
  872. $res = $partyModel->update($data, $where);
  873. if ($res) {
  874. // 存redis 房间信息
  875. $redis = new Redis();
  876. $redisconfig = config("redis");
  877. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  878. $partyInfo = $redis->get($this->roomTypeArr[$room_type] . "_" . $party_id);
  879. if ($partyInfo) {
  880. $partyInfo = json_decode($partyInfo, true);
  881. $partyInfo = array_replace($partyInfo, $data);
  882. $redis->set($this->roomTypeArr[$room_type] . "_" . $party_id, json_encode($partyInfo));
  883. }
  884. $this->success("更新成功!", $res);
  885. } else {
  886. $this->error("网络错误,请稍后重试!");
  887. }
  888. }
  889. /**
  890. * 开始排麦
  891. */
  892. public function addLineUp()
  893. {
  894. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  895. $mai_id = $this->request->request('mai_id', "1"); // 麦位置
  896. if (!$party_id) {
  897. $this->error(__('Invalid parameters'));
  898. }
  899. $userInfo = $this->auth->getUserinfo();
  900. $userid = $userInfo['id'];
  901. // 获取 房间信息
  902. $partyUser = \app\common\model\Party::where(['id' => $party_id])->value("user_id");
  903. if (!$partyUser) $this->error(__('房间信息未找到!'));
  904. // $is_home = $partyUser == $userInfo->id?1:0;
  905. $redis = new Redis();
  906. $redisconfig = config("redis");
  907. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  908. $data = unserialize($redis->hGet("party_lineup", $party_id));
  909. $data[$userid]["user_id"] = $userInfo['id'];
  910. $data[$userid]["avatar"] = $userInfo['avatar'];
  911. $data[$userid]["mai_id"] = $mai_id;
  912. $data[$userid]["nickname"] = $userInfo['nickname'];
  913. // $data["is_home"] = $is_home;
  914. $data[$userid]["level"] = $userInfo['level'];
  915. $data[$userid]["gender"] = $userInfo['gender'];
  916. $res = false;
  917. $data && $res = $redis->hSet("party_lineup", $party_id, serialize($data));
  918. $data = array_values($data);
  919. if ($res !== false) {
  920. $this->success("设置成功!", $data);
  921. } else {
  922. $this->error("网络错误,请稍后重试!");
  923. }
  924. }
  925. /**
  926. * 取消排麦
  927. */
  928. public function cancelLineUp()
  929. {
  930. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  931. $user_id = $this->request->request('user_id', 0); // 直播间ID
  932. $is_empty = $this->request->request('is_empty', 0, "intval"); // 是否清空排麦列表 1=清空,0=不清空
  933. if (!$party_id) {
  934. $this->error(__('Invalid parameters'));
  935. }
  936. $userid = $this->auth->id;
  937. $redis = new Redis();
  938. $redisconfig = config("redis");
  939. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  940. $data = unserialize($redis->hGet("party_lineup", $party_id));
  941. if ($data && $is_empty != 1) {
  942. foreach ($data as $k => $v) {
  943. if ($v["user_id"] == $user_id) {
  944. unset($data[$k]);
  945. break;
  946. }
  947. }
  948. }
  949. // unset($data[$userid]);
  950. if ($is_empty == 1) $data = [];
  951. $redis->hSet("party_lineup", $party_id, serialize($data));
  952. $this->success("移除成功!");
  953. }
  954. /**
  955. * 排麦列表
  956. */
  957. public function lineUpList()
  958. {
  959. $party_id = $this->request->request('party_id', 0, "intval"); // 直播间ID
  960. if (!$party_id) {
  961. $this->error(__('Invalid parameters'));
  962. }
  963. $redis = new Redis();
  964. $redisconfig = config("redis");
  965. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  966. $data = $redis->hGet("party_lineup", $party_id);
  967. $dataArr = unserialize($data);
  968. $datas = [];
  969. is_array($dataArr) && $datas = array_values($dataArr);
  970. if ($datas) {
  971. $this->success("获取成功!", $datas);
  972. } else {
  973. $this->success("获取成功!", []);
  974. }
  975. }
  976. /**
  977. * 获取派对类型
  978. */
  979. public function getPatyType()
  980. {
  981. $room_type = $this->request->request("room_type", 1);//
  982. if (!in_array($room_type, [1, 2])) $this->error(__('Invalid parameters'));
  983. $partytypeModel = new \app\common\model\PartyType();
  984. $partytypeList = $partytypeModel->where(["room_type" => $room_type])->select();
  985. $this->success("获取成功!", $partytypeList);
  986. }
  987. /**
  988. * 判断派对是否设置过密码
  989. */
  990. public function getPatyIspass()
  991. {
  992. $party_id = $this->request->request("party_id");// 派对ID
  993. $room_type = $this->request->request("room_type", 1);//
  994. $is_miniprogram = $this->request->request("is_miniprogram", 0);//
  995. if (!$party_id) $this->error(__('Invalid parameters'));
  996. $user_id = $this->auth->id;
  997. // 存redis 房间信息
  998. $redis = new Redis();
  999. $redisconfig = config("redis");
  1000. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1001. $partyInfo = $redis->get($this->roomTypeArr[$room_type] . "_" . $party_id);
  1002. if ($partyInfo) {
  1003. $partyInfo = json_decode($partyInfo, true);
  1004. } else {
  1005. $partyModel = new \app\common\model\Party();
  1006. $partyInfo = $partyModel->field("id,party_pass")->where(["id" => $party_id])->find();
  1007. if (!$partyInfo) {
  1008. $this->error(__('房间信息获取失败!'));
  1009. }
  1010. }
  1011. $data = [];
  1012. $data["status"] = 0;
  1013. $data["is_pass"] = $partyInfo["party_pass"] ? 1 : 0;
  1014. if ($partyInfo["user_id"] == $user_id || $this->auth->mobile == '17353993050') $data["is_pass"] = 0;
  1015. if ($is_miniprogram == 1) { // 小程序单独处理
  1016. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  1017. $lsetList = [];
  1018. for ($i = 1; $i <= 4; $i++) {
  1019. $hgetlist = $redis->hGet("party_manage_" . $party_id, $user_id . "-" . $i);
  1020. $hgetlist = unserialize($hgetlist);
  1021. $restime = time() - intval($hgetlist["createtime"]);
  1022. // 房管
  1023. if ($hgetlist && $i == 1) $lsetList["manage_restime"] = "";
  1024. // 禁言
  1025. if ($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime;
  1026. // 拉黑
  1027. if ($hgetlist && $i == 3 && $restime < $hgetlist["time"]) {
  1028. $restime = date("Y-m-d H:i:s", ($hgetlist["time"] + $hgetlist["createtime"]));
  1029. $lsetList["manage_black"] = $restime;
  1030. $lsetList["tips"] = '您已被该房间拉黑,解除时间:' . $restime;
  1031. // $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
  1032. break;
  1033. }
  1034. // 踢出
  1035. if ($hgetlist && $i == 4 && $restime < $hgetlist["time"]) {
  1036. $restime = date("Y-m-d H:i:s", ($hgetlist["time"] + $hgetlist["createtime"]));
  1037. $lsetList["manage_takeout"] = $restime;
  1038. $lsetList["tips"] = '您已被该房间踢出,解除时间:' . $restime;
  1039. // $this->error(__('您已被该房间踢出,解除时间:'.$restime));
  1040. break;
  1041. }
  1042. }
  1043. if ($lsetList) {
  1044. $data["status"] = 1;
  1045. $data["hgetlist"] = $lsetList;
  1046. }
  1047. }
  1048. $this->success("获取成功!", $data);
  1049. }
  1050. /**
  1051. * 获取房间gif表情列表
  1052. */
  1053. public function getPartGifList()
  1054. {
  1055. $this->success("获取成功!", \app\common\model\PartyGif::field("id,gif_image")->order("weight", "asc")->select());
  1056. }
  1057. /**
  1058. * 获取房间头像gif表情列表
  1059. */
  1060. public function getPartHeadgifList()
  1061. {
  1062. $this->success("获取成功!", \app\common\model\PartyHeadgif::field("id,name,gif_image")->order("weight", "asc")->select());
  1063. }
  1064. /**
  1065. * 随机获取礼物盒礼物
  1066. */
  1067. private function getBoxGift($gift_box_type)
  1068. {
  1069. // 查询本奖池内礼物是否还有可抽礼物
  1070. $where = [];
  1071. $where["Jackpot_id"] = $gift_box_type;
  1072. $where["is_use"] = 0;
  1073. $boxhasgift = \app\common\model\GiftBox::where($where)->select();
  1074. $giftcount = count($boxhasgift);
  1075. $giftArr = [];
  1076. foreach ($boxhasgift as $k => $v) $giftArr[$v["id"]] = $v;
  1077. if ($giftcount > 1) {
  1078. // 随机抽取$num个礼物
  1079. $giftids = array_rand($giftArr, 1);
  1080. \app\common\model\GiftBox::update(["is_use" => 1], ["id" => $giftids]);
  1081. }
  1082. if ($giftcount == 1) {
  1083. $giftids = $boxhasgift[0]["id"];
  1084. // 更新宝箱奖池全部礼物为未使用
  1085. \app\common\model\GiftBox::update(["is_use" => 0], ["Jackpot_id" => $gift_box_type]);
  1086. }
  1087. $giftInfo = $giftArr[$giftids];
  1088. return $giftInfo;
  1089. }
  1090. /**
  1091. * 全麦/单独赠送礼物
  1092. */
  1093. public function giveGiftToYou()
  1094. {
  1095. $user_ids = $this->request->request("user_id");// 赠送对象
  1096. $gift_id = $this->request->request("gift_id");// 礼物ID
  1097. $party_id = $this->request->request("party_id", 0);// 派对ID
  1098. $room_type = $this->request->request('room_type', 1); // 房间类型
  1099. $number = $this->request->request("number");// 赠送数量
  1100. $is_back = $this->request->request("is_back", 0);// 是否背包赠送: 1=是,0=否
  1101. if (!$user_ids || !in_array($is_back, [0, 1]) || !$gift_id || !$number || !in_array($room_type, [1, 2])) $this->error(__('Invalid parameters'));
  1102. $user_id_arr = explode(",", $user_ids);
  1103. $userCount = count($user_id_arr);
  1104. $userauthid = $this->auth->id;
  1105. $soundCoinRate = config("site.giftCoin"); // 声币兑换比例
  1106. $userModel = new \app\common\model\User();
  1107. if ($is_back == 1) {
  1108. // 获取背包礼物信息
  1109. $giftInfo = \app\common\model\GiftBack::get($gift_id);
  1110. if (!$giftInfo) $this->error("背包礼物获取失败");
  1111. // 随机获取一个礼物
  1112. $allCount = $number * $userCount;
  1113. $giftbackList = \app\common\model\GiftBack::where(["name" => $giftInfo->name, "user_id" => $userauthid, 'is_use' => 0])->limit($allCount)->select();
  1114. $giftInfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
  1115. $giftcount = 0;
  1116. $giftList = [];
  1117. if ($giftbackList) foreach ($giftbackList as $k => $v) {
  1118. $giftcount = $giftcount + $v["number"];
  1119. $giftList[$k] = $v;
  1120. if ($giftcount >= $allCount) {
  1121. break;
  1122. }
  1123. }
  1124. if ($giftcount < $allCount) $this->error("背包数量不足");
  1125. $giftValue = $giftInfo["value"] * $number;
  1126. $getValue = $giftInfo["value"] * $number;
  1127. } else {
  1128. // // 不可以赠送给自己
  1129. // if(in_array($userauthid,$user_id_arr)) $this->error("不可以赠送给自己!");
  1130. // 获取礼物信息
  1131. $giftModel = new \app\common\model\Gift();
  1132. $where = [];
  1133. $where["id"] = $gift_id;
  1134. $giftInfo = $giftModel->where($where)->find();
  1135. if (!$giftInfo) $this->error("请选择礼物!");
  1136. $giftValue = $giftInfo["value"] * $number;
  1137. $giftCountValue = $giftInfo["value"] * $number * $userCount;
  1138. $getValue = $giftValue;
  1139. // 判断如果是礼物盒则随机开礼物盒礼物
  1140. if ($giftInfo->box_type > 0) {
  1141. $boxgiftInfo = $this->getBoxGift($giftInfo->box_type);
  1142. $getValue = $boxgiftInfo["price"];
  1143. }
  1144. // 判断当前用户余额
  1145. $where = [];
  1146. $where["id"] = $userauthid;
  1147. $userInfo = $userModel->where($where)->find();
  1148. if (!$userInfo) $this->error("用户信息查询失败!");
  1149. if ($userInfo["jewel"] < $giftCountValue) $this->error("您的钻石余额不足!");
  1150. }
  1151. $hotValue = $getValue;
  1152. $getValue = round($getValue * ($soundCoinRate / 100));
  1153. // 转换统计
  1154. $progetValue = $hotValue - $getValue;
  1155. if ($progetValue > 0) {
  1156. $data = [];
  1157. $data["user_id"] = $user_ids;
  1158. $data["party_id"] = $party_id ? $party_id : 0;
  1159. $data["gift_value"] = $hotValue;
  1160. $data["plat_value"] = $hotValue - $getValue;
  1161. $data["createtime"] = time();
  1162. \app\common\model\UserChangeLog::insert($data);
  1163. }
  1164. // 转换声币后 再进行抽点设置
  1165. if (!$party_id) {
  1166. $platRate = 10;
  1167. $guilderRate = 30;
  1168. } else {
  1169. $partyInfo = \app\common\model\Party::field("platRate,guilderRate")->where(["id" => $party_id])->find();
  1170. // 获取系统配置信息
  1171. $platRate = $partyInfo->platRate; // 平台抽成百分比
  1172. $guilderRate = $partyInfo->guilderRate; // 工会长抽成百分比
  1173. }
  1174. $platValue = bcmul($platRate / 100, $getValue, 2);
  1175. $guilderValue = bcmul($guilderRate / 100, $getValue, 2);
  1176. $getValue = bcsub(bcsub($getValue, $platValue), $guilderValue);
  1177. $gif_image = $is_back==1?$giftInfo["gif_image"]:$giftInfo["special"];
  1178. $returnData = [];
  1179. Db::startTrans();
  1180. try {
  1181. $redis = new Redis();
  1182. $redisconfig = config("redis");
  1183. $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
  1184. // 事务处理余额与记录信息
  1185. $userjewellogModel = new \app\common\model\UserJewelLog();
  1186. $usersoundcoinlogModel = new \app\common\model\UserSoundcoinLog();
  1187. $giftuserpartyModel = new \app\common\model\GiftUserParty();
  1188. // 获取当天零点
  1189. $day = date("Ymd");
  1190. // 获取本周第一天
  1191. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  1192. // 获取本月第一天
  1193. $monthday = date("Ym01");
  1194. $allVal = 0;
  1195. $i = 0;
  1196. foreach ($user_id_arr as $user_id) {
  1197. // 获取赠送用户信息
  1198. $where = [];
  1199. $where["id"] = $user_id;
  1200. $touserInfo = $userModel->where($where)->find();
  1201. if ($is_back == 1) {
  1202. $b = 0;
  1203. foreach ($giftList as $k => $v) {
  1204. for ($a = 1; $a <= $v["number"]; $a++) {
  1205. $b++;
  1206. $num = $v["number"] - $a;
  1207. if ($num > 0) {
  1208. $res1 = \app\common\model\GiftBack::where(["id" => $v["id"]])->setDec("number");
  1209. } else {
  1210. $res1 = \app\common\model\GiftBack::update(["is_use" => 1, "use_time" => time()], ["id" => $v["id"]]);
  1211. }
  1212. if ($b == $number) break;
  1213. }
  1214. }
  1215. $res2 = true;
  1216. } else {
  1217. // 扣除当前用户钻石余额
  1218. $where = [];
  1219. $where["id"] = $userauthid;
  1220. $res1 = $userModel->where($where)->setDec("jewel", $giftValue);
  1221. // 添加当前用户钻石流水记录
  1222. $res2 = $userjewellogModel->addUserJewelLog($userauthid, $giftValue, "-", $userInfo["jewel"], "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!");
  1223. }
  1224. // 添加赠送用户声币余额
  1225. if ($soundCoinRate > 0) {
  1226. $where = [];
  1227. $where["id"] = $user_id;
  1228. $res3 = $userModel->where($where)->setInc("sound_coin", $getValue);
  1229. $getValue == 0 && $res3 = true;
  1230. // 添加赠送用户声币流水记录soundCoin
  1231. if($partyInfo) {
  1232. // 增加房主抽成
  1233. $where = [];
  1234. $where["id"] = $partyInfo->user_id;
  1235. $userModel->where($where)->setInc("sound_coin", $guilderValue);
  1236. // 添加赠送用户声币流水记录soundCoin
  1237. $usersoundcoinlogModel->addUserSoundcoinLog($partyInfo->user_id, $guilderValue, "+", $touserInfo["sound_coin"], "房间内获赠礼物:'" . $giftInfo["name"] . "',获得抽成" . $guilderValue . "声币!");
  1238. }
  1239. // }
  1240. // 类型:1=获赠礼物,2=兑换砖石,3=兑换余额,10000=系统调整
  1241. $res4 = $usersoundcoinlogModel->addUserSoundcoinLog($user_id, $getValue, "+", $touserInfo["sound_coin"], "获赠礼物:'" . $giftInfo["name"] . "',增加" . $getValue . "声币!", 1);
  1242. }
  1243. // 添加礼物赠送记录表
  1244. $data = [];
  1245. $data["user_id"] = $userauthid;
  1246. $data["user_to_id"] = $user_id;
  1247. $data["party_id"] = $party_id;
  1248. $data["gift_id"] = $gift_id;
  1249. $data["gift_give_type"] = $is_back ? 1 : 2;
  1250. $data["gift_name"] = $giftInfo["name"];
  1251. $data["gift_gif_image"] = $gif_image;
  1252. $data["number"] = $number;
  1253. $data["price"] = $giftInfo["value"];
  1254. $data["value"] = $giftValue;
  1255. $data["createtime"] = time();
  1256. $res5 = $giftuserpartyModel->insertGetId($data);
  1257. if ($res1 && $res2 && $res3 && $res4 && $res5) {
  1258. $i++;
  1259. if ($party_id > 0) {
  1260. // 添加redis记录做财富排行榜日榜用
  1261. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $hotValue, $user_id);
  1262. // 添加redis记录做财富排行榜周榜用
  1263. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $hotValue, $user_id);
  1264. // 添加redis记录做财富排行榜月榜用
  1265. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $hotValue, $user_id);
  1266. // 添加redis记录做贡献排行榜日榜用
  1267. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
  1268. // 添加redis记录做贡献排行榜周榜用
  1269. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
  1270. // 添加redis记录做贡献排行榜月榜用
  1271. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
  1272. // tcp 更新用户魅力值
  1273. $this->updateUserCharm($party_id, $user_id, $hotValue);
  1274. // 如果是主播,则添加魅力值记录做榜单统计
  1275. if ($room_type == 2) {
  1276. $data = [];
  1277. $data["user_id"] = $user_id;
  1278. $data["party_id"] = $party_id;
  1279. $data["charm"] = $hotValue;
  1280. $data["createtime"] = time();
  1281. \app\common\model\UserCharmRank::insert($data);
  1282. }
  1283. }
  1284. $getempirical = config("site.getempirical");
  1285. $getempirical = $getempirical * $hotValue;
  1286. // 获取用户贵族信息
  1287. $noble = \app\common\model\User::getUserNoble($this->auth->id);
  1288. if (isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
  1289. $getempirical = $getempirical + $getempirical * ($noble["explain"] / 100);
  1290. }
  1291. // 增加用户经验值
  1292. \app\common\model\User::addEmpirical($this->auth->id, $getempirical);
  1293. // +exp
  1294. \app\common\model\TaskLog::tofinish($this->auth->id, "OBHqCX4g", $number);
  1295. // +message
  1296. \app\common\model\Message::addMessage($user_id, "礼物通知", "收到 " . $this->auth->nickname . " 赠送的" . $giftInfo["name"] . " x" . $number);
  1297. $allVal = $allVal + $hotValue;
  1298. // // 剪掉背包礼物
  1299. // if($is_back == 1) {
  1300. // \app\common\model\GiftBack::update(["is_use"=>1],["id"=>$gift_id]);
  1301. // }
  1302. }
  1303. }
  1304. // 获取用户魅力值
  1305. $users = $redis->zRange("hourCharm_" . $party_id, 0, -1, true);
  1306. $u = [];
  1307. if ($users) {
  1308. foreach ($users as $k => $v) $u[] = [
  1309. "user_id" => $k,
  1310. "charm" => $this->changeW($v)
  1311. ];
  1312. }
  1313. $userCharm = $u;
  1314. // tcp 更新房间热度
  1315. $partyHot = $this->updatePartyHot($party_id, $allVal, $room_type);
  1316. // 如果是派对,则添加派对热度值记录做榜单统计
  1317. if ($room_type == 1) {
  1318. $data = [];
  1319. $data["party_id"] = $party_id;
  1320. $data["hot"] = $allVal;
  1321. $data["createtime"] = time();
  1322. \app\common\model\PartyHot::insert($data);
  1323. }
  1324. // tcp 获取房间用户周前三名
  1325. $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
  1326. if ($i == $userCount) {
  1327. $returnData["userCharm"] = $userCharm;
  1328. $returnData["partyHot"] = $this->changeW($partyHot);
  1329. $returnData["partyUserTop"] = $partyUserTop;
  1330. if ($is_back != 1) {
  1331. if ($giftInfo->box_type > 0) { // 不是背包,宝箱中赠送
  1332. $returnData["box_type"] = $giftInfo->box_type;
  1333. $returnData["box_image"] = $giftInfo->image;
  1334. $returnData["image"] = $boxgiftInfo["image"];
  1335. $returnData["name"] = $boxgiftInfo["gift_name"];
  1336. $returnData["gif_image"] = $boxgiftInfo["special"];
  1337. } else {
  1338. $returnData["image"] = $giftInfo["image"];
  1339. $returnData["gif_image"] = $giftInfo["special"];
  1340. }
  1341. } else {
  1342. $returnData["image"] = $giftInfo["image"];
  1343. $returnData["gif_image"] = $giftInfo["gif_image"];
  1344. }
  1345. // 增加抽点记录
  1346. $data = [];
  1347. $data["user_id"] = $user_ids;
  1348. $data["party_id"] = $party_id ? $party_id : 0;
  1349. $data["gift_value"] = $getValue;
  1350. $data["plat_value"] = $platValue;
  1351. $data["guilder_value"] = $guilderValue;
  1352. $data["createtime"] = time();
  1353. \app\common\model\UserProfitLog::insert($data);
  1354. Db::commit();
  1355. $this->success("赠送成功!", $returnData);
  1356. } else {
  1357. $this->success("赠送失败!");
  1358. }
  1359. } catch (ValidateException $e) {
  1360. Db::rollback();
  1361. $this->error($e->getMessage());
  1362. } catch (PDOException $e) {
  1363. Db::rollback();
  1364. $this->error($e->getMessage());
  1365. } catch (Exception $e) {
  1366. Db::rollback();
  1367. $this->error($e->getMessage());
  1368. }
  1369. }
  1370. // /**
  1371. // * 播放动效
  1372. // */
  1373. // public function play($party_id,$type,$value,$number) {
  1374. // // 发送tcp
  1375. // $tcpArr = [];
  1376. // $tcpArr['type'] = "play";
  1377. // $tcpArr['data'] = [
  1378. // 'party_id' => $party_id,
  1379. // 'type' => $type,
  1380. // 'value' => $value,
  1381. // 'number' => $number,
  1382. // ];
  1383. // $tcpJson = json_encode($tcpArr);
  1384. // $client = stream_socket_client(config("tcp"));
  1385. // $buffer2 = base64_encode($tcpJson)."****";
  1386. // fwrite($client, $buffer2);
  1387. // }
  1388. /**
  1389. * 更新派对信息(热度等)
  1390. */
  1391. private function updatePartyHot($party_id, $hotValue, $room_type)
  1392. {
  1393. $partyInfo = \app\common\model\Party::where(['id' => $party_id])->find();
  1394. if (!$partyInfo) return $hotValue;
  1395. $party_hot = $partyInfo->party_hot > 0 ? $partyInfo->party_hot : 0;
  1396. $party_hot_value = $party_hot + $hotValue;
  1397. $party_hot_value = $party_hot_value > 0 ? $party_hot_value : 0;
  1398. if ($party_hot_value != $party_hot) {
  1399. // 保存数据
  1400. $partyInfo->party_hot = $party_hot_value;
  1401. $partyInfo->save();
  1402. // 更新redis 加入缓存排序
  1403. $redis = new Redis();
  1404. $redisconfig = config("redis");
  1405. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1406. $redis->zAdd($this->roomTypeArr[$room_type] . "Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  1407. // 更新redis 加入缓存
  1408. $redPartyInfo = $redis->get($this->roomTypeArr[$room_type] . "_" . $partyInfo["id"]);
  1409. if ($redPartyInfo) {
  1410. $redPartyInfo = json_decode($redPartyInfo, true);
  1411. $redPartyInfo["party_hot"] = $party_hot_value;
  1412. $redis->set($this->roomTypeArr[$room_type] . "_" . $partyInfo["id"], json_encode($redPartyInfo));
  1413. }
  1414. // // 发送tcp
  1415. // $tcpArr = [];
  1416. // $tcpArr['type'] = "changeRoomHot";
  1417. // $tcpArr['data'] = [
  1418. // 'room_id' => $party_id,
  1419. // 'value' => $party_hot_value,
  1420. // ];
  1421. // $tcpJson = json_encode($tcpArr);
  1422. // $client = stream_socket_client(config("tcp"));
  1423. // $buffer2 = base64_encode($tcpJson)."****";
  1424. // fwrite($client, $buffer2);
  1425. }
  1426. return $party_hot_value;
  1427. }
  1428. /**
  1429. * 用户赠送礼物后房间内用户排行,贡献榜前三名
  1430. */
  1431. private function getPartyUserTop($party_id, $room_type)
  1432. {
  1433. $redis = new Redis();
  1434. $redisconfig = config("redis");
  1435. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1436. // 获取本周第一天
  1437. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  1438. // 获取当天零点
  1439. $day = date("Ymd");
  1440. $userModel = new \app\common\model\User();
  1441. // 获取条数
  1442. $num = 3;
  1443. // 获取3条财富排行周记录
  1444. $getweek = $redis->zRevRange($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", 0, $num - 1, true);
  1445. $userList = $userModel->rankList($getweek);
  1446. $avatarArr = [];
  1447. if ($userList) {
  1448. foreach ($userList as $k => $v) {
  1449. $v["jewel"] > 0 && $avatarArr[] = $v["avatar"];
  1450. }
  1451. // 加入缓存做备份
  1452. $redis->hSet("user_jewel_top3", $party_id, json_encode($avatarArr));
  1453. // // 发送tcp
  1454. // $tcpArr = [];
  1455. // $tcpArr['type'] = "changeRoomUserTop";
  1456. // $tcpArr['data'] = [
  1457. // 'room_id' => $party_id,
  1458. // 'user_avatar' => $avatarArr,
  1459. // ];
  1460. // $tcpJson = json_encode($tcpArr);
  1461. // $client = stream_socket_client(config("tcp"));
  1462. // $buffer2 = base64_encode($tcpJson)."****";
  1463. // fwrite($client, $buffer2);
  1464. }
  1465. return $avatarArr;
  1466. }
  1467. /**
  1468. * 用户赠送礼物后房间内用户魅力值增加
  1469. */
  1470. private function updateUserCharm($party_id, $user_id, $giftValue)
  1471. {
  1472. $redis = new Redis();
  1473. $redisconfig = config("redis");
  1474. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1475. // 获取用户魅力值
  1476. $users = $redis->zRange("hourCharm_" . $party_id, 0, -1, true);
  1477. if (!$users) $users[$user_id] = 0;
  1478. if (isset($users[$user_id])) {
  1479. $value = $users[$user_id] + $giftValue;
  1480. } else {
  1481. $value = $giftValue;
  1482. }
  1483. $redis->zAdd("hourCharm_" . $party_id, $value, $user_id);
  1484. return true;
  1485. }
  1486. /**
  1487. * 单个房间魅力值清零
  1488. */
  1489. public function partyClearCharm()
  1490. {
  1491. $party_id = $this->request->request("party_id");// 礼物ID
  1492. if ($party_id <= 0) {
  1493. $this->error("请输入派对ID");
  1494. }
  1495. $redis = new Redis();
  1496. $redisconfig = config("redis");
  1497. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1498. $res = $redis->del("hourCharm_" . $party_id);
  1499. $this->success("操作成功!");
  1500. }
  1501. /**
  1502. * 获取音乐列表
  1503. */
  1504. public function getMusicList()
  1505. {
  1506. $this->success("获取成功!", \app\common\model\Music::select());
  1507. }
  1508. //============================定时任务==========================//
  1509. /**
  1510. * redis清理排行榜 并数据库备份
  1511. */
  1512. public function updateTops()
  1513. {
  1514. // 前一天日期
  1515. $yestaday = date("Ymd", strtotime("-1 day"));
  1516. // 上个周一
  1517. $preweek = date("Ymd", strtotime("last Monday"));
  1518. // 上个月一号
  1519. $monty = bcsub(date("m"),1);
  1520. if($monty < 10) $monty = "0".$monty;
  1521. $premonth = date("Y").$monty."01";
  1522. $redis = new Redis();
  1523. $redisconfig = config("redis");
  1524. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  1525. // 获取所有派对ID
  1526. $party_ids = \app\common\model\Party::where(["status" => 1])->column("id");
  1527. // 获取数据
  1528. $time = time();
  1529. if ($party_ids) foreach ($party_ids as $k => $v) {
  1530. $keys = [
  1531. "party_jewel_get_" . $v . ":" . $yestaday . "d",
  1532. "party_jewel_to_" . $v . ":" . $yestaday . "d",
  1533. "party_jewel_get_" . $v . ":" . $preweek . "w",
  1534. "party_jewel_to_" . $v . ":" . $preweek . "w",
  1535. "party_jewel_get_" . $v . ":" . $premonth . "m",
  1536. "party_jewel_to_" . $v . ":" . $premonth . "m",
  1537. ];
  1538. foreach ($keys as $key) {
  1539. // 备份数据
  1540. $redisData = $redis->zRevRange($key, 0, -1, true);
  1541. if ($redisData) {
  1542. $keyInfo = \app\common\model\RedisTops::where(["key" => $key])->find();
  1543. $data = ["party_id" => $v, "key" => $key, "value" => json_encode($redisData), "createtime" => $time];
  1544. if (!$keyInfo) {
  1545. $res = \app\common\model\RedisTops::insert($data);
  1546. // 清理数据
  1547. $res && $redis->zRemRangeByRank($key, 0, -1);
  1548. }
  1549. }
  1550. }
  1551. }
  1552. }
  1553. }