Party.php 102 KB

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