Party.php 93 KB

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