Party.php 101 KB

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