Party.php 101 KB

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