Party.php 98 KB

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