Party.php 78 KB

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