Party.php 93 KB

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