Party.php 94 KB

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