Party.php 79 KB

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