Party.php 101 KB

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