Party.php 94 KB

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