Party.php 81 KB

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