Party.php 83 KB

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