Party.php 83 KB

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