Party.php 75 KB

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