Party.php 83 KB

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