Party.php 83 KB

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