Party.php 102 KB

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