Party.php 77 KB

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