Party.php 101 KB

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