Party.php 93 KB

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