Party.php 78 KB

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