123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use Redis;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use app\common\service\RoomService;
- use app\common\library\Easemob;
- require_once EXTEND_PATH . 'shengwang/src/RtcTokenBuilder2.php';
- require_once EXTEND_PATH . 'shengwang/src/RtmTokenBuilder2.php';
- /**
- *
- * 派对信息接口
- */
- class Party extends Api
- {
- protected $noNeedLogin = ['test_party_attr','updatePartyInfo','getPatyType','shengwang_token','addUserPositionToParty', 'clearMoney','clearCharm','getPartyRankList','getDefaultBackground','getPartGifList','getPartHeadgifList','isNotalk','getMusicList','updateTops','handleParty'];
- protected $noNeedRight = ['*'];
- protected $redis;
- public function __construct()
- {
- $this->roomTypeArr = [1=>"party",2=>"live"];
- //redis
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
- $this->redis = $redis;
- parent::__construct();
- }
- /**
- * 创建/进入派对
- */
- public function createParty() {
- if(!$this->apiLimit()){
- $this->error('访问频繁,请稍后重试');
- }
- $party_logo = input('party_logo');
- $party_name = input('party_name');
- $seatnum = input('seatnum');
- $is_public = input('is_public',1); //是否公开:1=是,0=否
- //$room_type = input('room_type',2); // 房间类型:1=公会房,2=K歌房
- $room_type = 2; // 房间类型:1=公会房,2=K歌房
- $partyModel = new \app\common\model\Party();
- $user_id = $this->auth->id;
- // 开直播时,判断当前用户是否为主播
- $anchorInfo = [];
- if($room_type == 2) {
- /*$anchorInfo = \app\common\model\UserAnchor::where(["user_id"=>$user_id])->find();
- $anchorInfo || $this->error("您还未申请主播!");
- $anchorInfo->status == 0 && $this->error("您的主播申请信息还未受理,请耐心等待!");
- $anchorInfo->status == 2 && $this->error("您的主播申请未通过,请重新提交!");*/
- }
- // 1。实名认证
- $userAuthInfo = \app\common\model\UserAuth::where(["user_id"=>$user_id])->find();
- if($userAuthInfo) {
- if($userAuthInfo->status == 0) {
- $this->error("您的实名认证还在这审核中...,请耐心等待!");
- } elseif($userAuthInfo->status == 2) {
- $this->error("您的实名认证审核未通过,请重新审核!");
- }
- } else {
- $this->error("请先申请实名认证!");
- }
- if($room_type == 1) {
- // 2。开厅申请
- $authInfo = model('Guild')->where(["user_id"=>$user_id])->order('id desc')->find();
- if($authInfo) {
- if($authInfo->status == 0) {
- $this->error("您的公会申请还在这审核中...,请耐心等待!");
- } elseif($authInfo->status == -1) {
- $this->error("您的公会申请已解散,请重新申请!");
- }
- } else {
- $this->error("请先申请公会!");
- }
- }
- // 判断派对是否存在
- $where = [];
- $where["user_id"] = $user_id;
- $where["room_type"] = $room_type;
- $sqlPartyInfo = $partyModel->where($where)->find();
- if(!$sqlPartyInfo) { // sql中不存在派对信息
- $party_type = 8; // 派对类型(情感互动,心动点单 等)
- $party_notice = "请编辑房间公告"; // 派对公告
- $party_notice_detail = "请编辑房间公告内容!"; // 派对公告详情
- $party_ids = $partyModel->column("party_id");
- // 创建派对ID (临时ID四位,派对数不超过8999)
- $party_id = $this->auth->getUinqueId(5,$party_ids);
- if($party_id > 99999) {
- $this->error("房间超限,请联系客服");
- }
- if ($room_type == 1) {//公会房间获取家族信息
- $guildWhere['user_id'] = $user_id;
- $guildWhere['status'] = 1;
- $guild = model('Guild')->where($guildWhere)->order('id desc')->find();
- if (!empty($guild)) {
- $party_name = $guild['name'].'的房间';
- $party_logo = $guild['image'];
- $party_notice = $guild['desc'];
- $party_notice_detail = $guild['notice'];
- }
- $party_type = 7;
- }
- //创建房间,初始化环信
- $easemob_room_id = $this->easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type);
- //写入派对
- Db::startTrans();
- $data = [];
- $data["user_id"] = $this->auth->id;
- $data["room_type"] = $room_type;
- $data["party_id"] = $party_id;
- $data["party_hot"] = 0;
- $data["party_name"] = $party_name;
- $data["party_logo"] = $party_logo;
- $data["party_type"] = $party_type;
- $data["party_notice"] = $party_notice;
- $data["party_notice_detail"] = $party_notice_detail;
- $data["is_online"] = 1;
- $data["status"] = 1;//直接正常状态
- $data["is_recommend"] = 0;
- $data["createtime"] = time();
- $data["easemob_room_id"] = $easemob_room_id;
- if($room_type == 1){
- $data["platRate"] = config('site.party_party_platrate');
- $data["guilderRate"] = config('site.party_party_guilderrate');
- }else{
- $data["platRate"] = config('site.party_live_platrate');
- $data["guilderRate"] = config('site.party_live_guilderrate');
- }
- $id = $partyModel->insertGetId($data);
- if(!$id) {
- Db::rollback();
- $this->error("房间创建失败,请稍后重试!");
- }
- $data["id"] = $id;
- if($room_type == 2) {
- \app\common\model\User::update(["is_live"=>1],["id"=>$user_id]);
- }
- $partyInfo = $partyModel->where(["id"=>$id])->find();
- $partyInfo["is_new"] = 1;
- // 冠名
- /*$userInfo = [];
- if($partyInfo["naming"]) {
- $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$partyInfo["naming"]])->find();
- }*/
- // 头像
- /*if($partyInfo["user_id"] > 0 && $room_type == 2) {
- $partyInfo["avatar"] = \app\common\model\User::where(["id"=>$partyInfo["user_id"]])->value("avatar");
- }*/
- $partyInfo["avatar"] = $party_logo;
- // 派对类型
- $partyTypeName = "普通房";
- if($partyInfo["party_type"]) {
- $partyTypeName = \app\common\model\PartyType::where(["id"=>$partyInfo["party_type"]])->value("name");
- }
- $partyInfo["naming"] = [];
- $partyInfo["type_name"] = $partyTypeName;
- // 加入缓存排序,rediskey:派对热度
- $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
- // 加入缓存,rediskey:派对详情
- $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($partyInfo));
- if($room_type != 2) {
- //更新家族 party_id
- $guildWhere['user_id'] = $user_id;
- $guildWhere['status'] = 1;
- $guild = model('Guild')->where($guildWhere)->order('id desc')->find();
- if (!empty($guild)) {
- $rs_guild = model('Guild')->where($guildWhere)->update(['party_id'=>$partyInfo['id']]);
- if($rs_guild === false){
- Db::rollback();
- $this->error('创建房间失败');
- }
- }
- }
- Db::commit();
- } else {
- $partyInfo = $sqlPartyInfo;
- //初始化房间
- if($partyInfo['status'] == 3){
- $update = [
- 'status' => 1,
- 'party_hot' => 0,
- 'party_logo' => $party_logo,
- 'party_name' => $party_name,
- 'seatnum' => $seatnum,
- 'is_public' => $is_public,
- 'party_notice' => '请编辑房间公告',
- 'party_notice_detail' => '请编辑房间公告内容',
- 'is_recommend' => 0,
- 'is_close' => 0,
- 'party_pass' => '',
- 'is_screen' => 0,
- 'on_model' => 1,
- 'background' => '',
- ];
- if($room_type == 1){
- $update["platRate"] = config('site.party_party_platrate');
- $update["guilderRate"] = config('site.party_party_guilderrate');
- }else{
- $update["platRate"] = config('site.party_live_platrate');
- $update["guilderRate"] = config('site.party_live_guilderrate');
- }
- $matedata = [
- 'seatnum'=> $seatnum,//麦位数量
- //'waitsing_list' => json_encode([]),//已点歌曲列表
- 'party_name' => $party_name,//房名字
- 'party_logo' => $party_logo,//logo
- 'is_public' => $is_public,//是否公开
- 'room_type' => $room_type,//房间类型
- 'online_user_num' => 0,//当前房间在线的人
- //'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
- 'background' => '', //房间背景
- 'party_notice' => $update['party_notice'],
- 'party_notice_detail' => $update['party_notice_detail'],
- ];
- }else{
- $update = [
- 'status' => 1,
- 'party_logo' => $party_logo,
- 'party_name' => $party_name,
- 'is_public' => $is_public,
- ];
- $matedata = [
- 'party_name' => $party_name,//房名字
- 'party_logo' => $party_logo,//logo
- 'is_public' => $is_public,//是否公开
- ];
- }
- //没有房间号的给创建一个
- if(empty($partyInfo['easemob_room_id'])){
- $easemob_room_id = $this->easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type);
- $update['easemob_room_id'] = $easemob_room_id;
- }else{
- $easemob_room_id = $partyInfo['easemob_room_id'];
- }
- Db::name('party')->where('id',$partyInfo['id'])->update($update);
- $partyInfo = $partyModel->where(["id"=>$partyInfo['id']])->find();
- $partyInfo["is_new"] = 0;
- // 加入缓存,rediskey:派对详情
- $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo['id'],json_encode($partyInfo));
- //[环信]初始化房间,初始化自定义属性
- $easemob = new Easemob();
- $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$matedata);
- }
- //声网三个token
- $partyInfo['shengwang_token'] = $this->shengwang_token_private($easemob_room_id);
- //帮房主走一次joinparty
- $this->creater_joinParty($partyInfo['id'],$easemob_room_id,$partyInfo['user_id']);
- $this->success("获取成功!",$partyInfo);
- }
-
- private function easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type){
- //[环信]创建聊天室
- $easemob = new Easemob();
- $easemob_room_id = $easemob->room_createRoom($party_name,$party_name,$this->auth->id);
- if(empty($easemob_room_id)){
- $this->error('创建房间失败');
- }
- //[环信]初始化房间,初始化自定义属性
- $matedata = [
- 'seatnum'=> $seatnum,//麦位数量
- 'waitsing_list' => json_encode([]),//已点歌曲列表
- 'party_name' => $party_name,//房名字
- 'party_logo' => $party_logo,//logo
- 'is_public' => $is_public,//是否公开
- 'room_type' => $room_type,//房间类型
- 'online_user_num' => 0,//当前房间在线的人
- 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
- 'background' => '', //房间背景
- //'party_notice' => '请编辑房间公告',
- //'party_notice_detail' => '请编辑房间公告内容',
- ];
- $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$matedata);
- //[环信]初始化N个麦位
- $seatdata = [];
- for($i=0;$i<$seatnum;$i++){
- $seat = [
- 'charm' => 0, //红心,魅力值
- 'isMaster' => false, // 是否是房主
- 'headUrl' => '', // 头像
- 'userNo' => '', // 座位上用户no
- 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
- 'name' => '', // 座位上用户昵称
- 'seatIndex' => -1, // 座位编号
- 'chorusSongCode' => '', // 是否合唱
- 'isAudioMuted' => 1, // 是否静音
- 'isVideoMuted' => 0, // 是否开启视频
- 'checked' => false, // 用于送礼物选择用户
- 'isUsed' => true, // 用于送礼物选择用户
- 'gender' => 1, //性别
- ];
- //创建完房间就进入,所以房主直接在麦位0
- /*if($i == 0){
- $seat['isMaster'] = true;
- $seat['headUrl'] = localpath_to_netpath($this->auth->avatar);
- $seat['userNo'] = $this->auth->id;
- $seat['rtcUid'] = $this->auth->id;
- $seat['name'] = $this->auth->nickname;
- $seat['seatIndex'] = $i;
- }*/
- $seatdata['seat'.$i] = json_encode($seat);
- }
- $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$seatdata);
- return $easemob_room_id;
- }
- //声网3个token
- public function shengwang_token(){
- $party_id = input('party_id'); // 派对ID
- $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
- if(!$partyInfo) {
- $this->error(__('派对不存在!'));
- }
- $rs = $this->shengwang_token_private($partyInfo['easemob_room_id']);
- $this->success(1,$rs);
- }
- //声网3个token
- private function shengwang_token_private($easemob_room_id){
- $config = config('shengwang');
- $appId = $config['app_id'];
- $appCertificate = $config['app_zhengshu'];;
- $channelName = $easemob_room_id;
- $uid = 0; //任何人都可进入
- $uidStr = '0';//任何人都可进入
- $tokenExpirationInSeconds = 86400;
- $privilegeExpirationInSeconds = 86400;
- //rtc token
- $rtc_token = \RtcTokenBuilder2::buildTokenWithUid($appId, $appCertificate, $channelName, $uid, \RtcTokenBuilder2::ROLE_PUBLISHER, $tokenExpirationInSeconds, $privilegeExpirationInSeconds);
- //chorus token
- $chorus_token = \RtcTokenBuilder2::buildTokenWithUid($appId, $appCertificate, $channelName.'_ex', $uid, \RtcTokenBuilder2::ROLE_PUBLISHER, $tokenExpirationInSeconds, $privilegeExpirationInSeconds);
- //rtm token
- $user = '0';
- $expireTimeInSeconds = 86400;
- $rtm_token = \RtmTokenBuilder2::buildToken($appId, $appCertificate, $user, $expireTimeInSeconds);
- $result = [
- 'rtc_token' => $rtc_token,
- 'chorus_token' => $chorus_token,
- 'rtm_token' => $rtm_token,
- ];
- //dump($result);
- return $result;
- }
- /**
- * 解散房间
- */
- public function closeParty() {
- $party_id = input('party_id'); // 派对ID
- $room_type = input('room_type',1); // 房间类型:1=派对,2=直播
- if (!$party_id || !in_array($room_type,[1,2])) {
- $this->error(__('Invalid parameters'));
- }
- $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
- if(!$partyInfo) {
- $this->error(__('派对不存在!'));
- }
- $room_type = $partyInfo['room_type'];
- if($partyInfo['user_id'] != $this->auth->id){
- $this->error('房主才能解散');
- }
- //房间从首页列表消失
- $update = [
- 'status' => 3,
- ];
- Db::name('party')->where('id',$party_id)->update($update);
- // 存redis 房间信息
- $redispartyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
- if($redispartyInfo) {
- $redispartyInfo = json_decode($redispartyInfo,true);
- $redispartyInfo = array_replace($redispartyInfo,$update);
- $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($redispartyInfo));
- }
- //用户魅力榜,清空
- $this->redis->zRemRangeByRank("hourCharm_".$party_id, 0,-1);
- //用户贡献榜,清空
- $this->redis->zRemRangeByRank("hourWealth_".$party_id, 0,-1);
- //贡献榜前三,设置为空
- $this->redis->hSet("user_jewel_top3",$party_id,json_encode([]));
- //排麦列表,清空
- $this->redis->hSet("party_lineup",$party_id,serialize([]));
- //清空房间
- $user_ids = $this->redis->hGetAll("online_".$party_id);
- if(!empty($user_ids)){
- foreach($user_ids as $key => $userId){
- $this->redis->HDel("online_" . $party_id, $userId);
- $this->redis->zRem("party_user_".$party_id,$userId); //新加的
- $this->redis->hDel("livingUser", $userId);
- }
- }
- //全部下麦
- $key = "party_seat_".$party_id;
- $seats = $this->redis->hGetAll($key);
- foreach($seats as $seat_num => $seatuserid){
- $this->redis->HDel($key, $seat_num);
- }
- //环信操作
- $easemob_room_id = $partyInfo['easemob_room_id'];
- if(!empty($easemob_room_id)){
- ///初始化房间信息,重置自定义属性
- $matedata = [
- 'waitsing_list' => json_encode([]), //清空房间待唱列表
- 'online_user_num' => 0, //当前房间在线的人
- 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
- ];
- $easemob = new Easemob();
- $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
- //重置麦位
- $seatnum = $partyInfo['seatnum'];
- $seatdata = [];
- for($i=0;$i<$seatnum;$i++){
- $seat = [
- 'charm' => 0, //红心,魅力值
- 'isMaster' => false, // 是否是房主
- 'headUrl' => '', // 头像
- 'userNo' => '', // 座位上用户no
- 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
- 'name' => '', // 座位上用户昵称
- 'seatIndex' => -1, // 座位编号
- 'chorusSongCode' => '', // 是否合唱
- 'isAudioMuted' => 1, // 是否静音
- 'isVideoMuted' => 0, // 是否开启视频
- 'checked' => false, // 用于送礼物选择用户
- 'isUsed' => true, // 用于送礼物选择用户
- 'gender' => 1, //性别
- ];
- $seatdata['seat'.$i] = json_encode($seat);
- }
- $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$seatdata);
- //清空房间人,聊天室
- //分页获取聊天室成员列表
- $usernames = $easemob->room_listRoomMembers($easemob_room_id,0);
- //批量移除聊天室成员
- $user_ids = [];
- if(!empty($usernames)){
- foreach($usernames as $username){
- foreach($username as $key => $val)
- {
- $user_ids[] = $val;
- }
- }
- }
- if(!empty($user_ids)){
- $easemob->room_removeRoomMembers($easemob_room_id,$user_ids);
- }
- }
- $this->success();
- }
- /**
- * 获取派对列表排序
- */
- public function getPartyList() {
- $order = input('orderby','new');
- $page = input('page',1);
- $listrow = input('listrow',10);
- $this->reset_all_party();
- //
- $orderby = 'party.party_hot desc, party.id desc';
- $where = [];
- if($order == 'recommend'){
- $where['party.is_recommend'] = 1;
- }
- if($order == 'follow'){
- $follow_party_ids = Db::name('party_follow')->where('user_id',$this->auth->id)->column('party_id');
- $where['party.id'] = ['IN',$follow_party_ids];
- }
- $where['party.status'] = 1; //正常
- //$where['party.is_online'] = 1; //在线
- $where['party.is_close'] = 0; //未关闭
- $where['party.is_public'] = 1; //是否公开:1=是,0=否
- $field = 'party.id,party.user_id,party.room_type,party.party_id,party.party_name,party.party_hot,
- party.party_logo,party.party_pass,party.party_type,party.easemob_room_id,type.name as type_name';
- $list = Db::name('party')->alias('party')
- ->join('party_type type','party.party_type = type.id','LEFT')
- ->field($field)->where($where)->order($orderby)->autopage()->select();
- $list = list_domain_image($list,['party_logo']);
- $resdata = [];
- if (!empty($list)) {
- $roomService = new RoomService();
- foreach($list as $k => $v) {
- $mod = isset($v["party_type"]) ? intval($v["party_type"])%5 : 1;
- //房间用户列表
- $userDataResult = $roomService->getPartyUserList(['party_id'=>$v['id']]);
- $userDataRes = isset($userDataResult['data']) ? $userDataResult['data'] : [];
- $partyUserList = [
- 'user_list' => isset($userDataRes['member_list']) ? $userDataRes['member_list'] : [],
- 'online_num' => isset($userDataRes['online_num']) ? $userDataRes['online_num'] : 0,
- ];
- //排序显示用
- $sortnum = ($page-1) * $listrow + $k + 1;
- $resdata[] = [
- "id" => $v["id"],
- "user_id" => $v["user_id"],
- "room_type" => $v['room_type'],
- "party_id" => $v["party_id"],
- "r_id" => $v["party_id"],
- "party_name" => $v["party_name"],
- "party_hot" => $v['party_hot'],
- "party_logo" => $v["party_logo"],
- "party_pass" => $v["party_pass"] ? $v["party_pass"]:"",
- "party_type" => $v['type_name'],
- "party_type_color" => $mod == 0?5:$mod,
- "easemob_room_id" => $v['easemob_room_id'],
- "party_user" => $partyUserList,
- "sortnum" => $sortnum,
- ];
- }
- }
- $this->success("获取成功!",$resdata);
- }
- //把所有房间的信息,都放到redis里去
- private function reset_all_party(){
- //rediskey:派对热度
- $redisPartyRankList = $this->redis->zRevRange("partyRank",0,-1,true);
- //rediskey:派对热度
- $redisLiveRankList = $this->redis->zRevRange("liveRank",0,-1,true);
- $roomService = new RoomService();
- if(!$redisPartyRankList) {//party
- $roomParams = ['room_type' => 1];
- $roomPartyRes = $roomService->roomRedis($roomParams);
- $redisPartyRankList = $roomPartyRes['data'];
- }
- if(!$redisLiveRankList) {//live
- $roomParams = ['room_type' => 2];
- $roomLiveRes = $roomService->roomRedis($roomParams);
- $redisLiveRankList = $roomLiveRes['data'];
- }
- }
- /**
- * 获取派对用户排序,(日榜,周榜,月榜) X (魅力榜,财富榜)
- */
- /*public function getPartyUserRank_old() {
- $room_type = input('room_type',1); // 房间类型
- $party_id = input("party_id");// 派对ID
- if (!$party_id) $this->error(__('Invalid parameters'));
- $res = [];
- // 获取今天
- $day = date("Ymd");
- // 获取本周第一天
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
- // 获取本月第一天
- $monthday = date("Ym01");
- $userModel = new \app\common\model\User();
- // 获取条数
- $num = 50;
- // 获取50条魅力排行日记录
- $getday = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$day."d",0,$num-1,true);
- $res['getRankListDay'] = $userModel->rankList($getday);
- // 获取50条财富排行日记录
- $today = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true);
- $res['toRankListDay'] = $userModel->rankList($today);
- // 获取50条魅力排行周记录
- $getweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$weekday."w",0,$num-1,true);
- $res['getRankListWeek'] = $userModel->rankList($getweek);
- // 获取50条财富排行周记录
- $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$weekday."w",0,$num-1,true);
- $res['toRankListWeek'] = $userModel->rankList($toweek);
- // 获取50条魅力排行月记录
- $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$monthday."m",0,$num-1,true);
- $res['getRankListMonth'] = $userModel->rankList($toweek);
- // 获取50条财富排行月记录
- $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$monthday."m",0,$num-1,true);
- $res['toRankListMonth'] = $userModel->rankList($toweek);
- return $this->success("获取成功!",$res);
- }*/
- //获取派对用户排序(魅力榜,财富榜),有数值的在上,0的在下
- public function getPartyUserRank() {
- $party_id = input("party_id");// 派对ID
- if (!$party_id) $this->error(__('Invalid parameters'));
- $userModel = new \app\common\model\User();
- // 获取所有魅力排行
- $getday = $this->redis->zRevRange("hourCharm_".$party_id,0,-1,true);
- $res['getRankListDay'] = $userModel->rankList($getday);
- // 获取所有财富排行
- $today = $this->redis->zRevRange("hourWealth_".$party_id,0,-1,true);
- $res['toRankListDay'] = $userModel->rankList($today);
- return $this->success("获取成功!",$res);
- }
- /**
- * 派对热度更新(已废弃)
- */
- /*public function changeUserPartyhot() {
- $party_id = input('party_id',0,"intval"); // 派对ID
- $room_type = input('room_type',1); // 房间类型
- $party_hot = input('party_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值)
- if (!$party_id || !$party_hot) {
- $this->error(__('Invalid parameters'));
- }
- // 更新热度
- $this->redis->zIncrBy($this->roomTypeArr[$room_type]."Rank", $party_hot, $party_id);
- return $this->success("更新成功!");
- }*/
- /**
- * 更新主持人和麦位前四位至首页房间排行
- */
- public function addUserPositionToParty() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $user_id = input('user_id',0,"intval"); // 用户ID
- $upOrdown = input('upordown'); // 上下麦:1=上麦-1=下麦
- $room_type = input('room_type',1); // 房间类型
- $avatar = input('avatar'); // 用户头像 (upOrdown参数为-1时可以不传)
- $position = input('position'); // 麦位置:0=支持人,1=1号麦,2=2号麦,3=3号麦,4=4号麦
- if (!$party_id || !$user_id || !in_array($position,[0,1,2,3,4,5,6,7,8]) || !in_array($upOrdown,[1,-1])) {
- $this->error(__('Invalid parameters'));
- }
- if($upOrdown == 1) {
- // 先完成所有麦上记录
- \app\common\model\UserOnsiteTime::update(["status"=>2],["user_id"=>$user_id]);
- // 保存上麦记录
- $data = [];
- $data["user_id"] = $user_id;
- $data["party_id"] = $party_id;
- $data["onsite_time"] = time();
- \app\common\model\UserOnsiteTime::insert($data);
- }
- if($upOrdown == -1) {
- // 更新下麦时间
- $update = [];
- $update["offsite_time"] = time();
- $update["status"] = 2;
- \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
- }
- if(in_array($position,[5,6,7,8])) return $this->success("设置成功!");
- $redisData = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
- if($redisData) {
- $partyInfo = json_decode($redisData,true);
- if($upOrdown == 1) {
- // 删掉已有头像,防止重复
- $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
- if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $avatar) unset($partyInfo["party_user"][$k]);
- $partyInfo["party_user"][$position] = $avatar;
- } else {
- if(isset($partyInfo["party_user"])) unset($partyInfo["party_user"][$position]);
- }
- // sort($partyInfo["party_user"]);
- $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
- }
- return $this->success("设置成功!");
- }
- /**
- * 获取派对在线人数列表
- */
- public function getOnlieList() {
- $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $res = $this->redis->hGetAll("online_".$party_id);
- $user_ids = [];$userList = [];
- if($res) $user_ids = array_values($res);
- // 获取用户列表信息
- if($user_ids){
- $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")
- ->join('user_config_charm charm','user.charm_level = charm.level','LEFT')
- ->join('user_config_wealth wealth','user.wealth_level = wealth.level','LEFT')
- ->where(["user.id"=>["in",$user_ids],'user.is_stealth'=>0])
- ->autopage()->select();
- $userList = list_domain_image($userList,['avatar','charm_image','wealth_image']);
- }
- $this->success("获取成功!",$userList);
- }
- /** 连续进房提示
- * @param $user_id
- * @param $party_id
- * @return int
- */
- private function getUserJoinPartyDays($user_id, $party_id)
- {
- $days = 1;
- $joinPartyDays = $this->redis->hGet("joinParty_days",$user_id."-".$party_id);
- if ($joinPartyDays){
- $joinPartyDaysArr = explode('-',$joinPartyDays);
- $oldTime = (int)$joinPartyDaysArr[0];
- $todayStartTime = strtotime(date('Y-m-d'));//今天0点
- $yesterdayStartTime = strtotime(date("Y-m-d",strtotime("-1 day")));//昨天开始时间戳
- $todayEndTime = $todayStartTime+24 * 60 * 60-1;//今天结束时间戳
- //昨天登录过,+1
- if ($yesterdayStartTime <= $oldTime && $oldTime < $todayStartTime){
- $days = (int)$joinPartyDaysArr[1];
- $days++;
- $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-'. $days);
- }elseif($todayStartTime <= $oldTime && $oldTime <= $todayEndTime){
- //今天登录过 days
- $days = (int)$joinPartyDaysArr[1];
- } else {
- $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
- }
- }else{
- $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
- }
- return $days;
- }
- /**
- * 加入派对
- */
- public function joinParty() {
- $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
- $room_type = input("room_type",1);//
- $party_pass = input("party_pass");//
- if (!$party_id || !in_array($room_type,[1,2])) {
- $this->error(__('Invalid parameters'));
- }
- $user_id = $this->auth->id;
- // 判断派对密码
- $partyInfo = Db::name('party')->where('id',$party_id)->find();
- if($partyInfo) {
- if($partyInfo["status"] != 1) $this->error("该房间已被解散!");
- // if($partyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
- //if($partyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
- $partyManage = $this->redis->hGet("party_manage_".$party_id,$user_id."-1");
- if(isset($partyInfo["party_pass"]) && $partyInfo["party_pass"] && $partyInfo["user_id"] != $user_id && empty($partyManage)) {
- if(($party_pass != $partyInfo["party_pass"] || strlen($party_pass) != 4) && $this->auth->is_manager == 0) {
- $this->error("派对密码不正确!");
- }
- }
- // 公会管理员和公会长激活派对厅
- /* $guildInfo = \app\common\model\Guild::where(['user_id' => $partyInfo['user_id'], 'status' => 1])->find();
- if ($guildInfo) {
- // 获取公会管理员
- $guildMemberInfo = \app\common\model\GuildMember::where(["user_id" => $user_id, "guild_id" => $guildInfo['id'], 'status' => 1])->find();
- if ($guildMemberInfo && $guildMemberInfo['role'] == 0 && $partyInfo['is_online'] == 0) {
- //$this->error("当前直播已结束!");//这种情况理论上不存在
- }
- }*/
- // // 如果是房主自己进入房间,则更新用户 为在线状态
- // if ($partyInfo["user_id"] == $user_id) {
- // if ($room_type == 2)
- // \app\common\model\User::update(["is_live" => 1], ["id" => $partyInfo["user_id"]]);
- // }
- } else {
- $this->error("派对信息获取失败!");
- }
- // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
- $lsetList = [];
- for($i=1;$i<=4;$i++) {
- $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i);
- $hgetlist = unserialize($hgetlist);
- if($hgetlist){
- $restime = time() - intval($hgetlist["createtime"]);
- }else{
- $restime = 0;
- }
- // 房管
- if($hgetlist && $i == 1) {
- $lsetList["manage_restime"] = "";
- //重新设置成管理员
- $this->setGroupAdmin($partyInfo['easemob_room_id'],$user_id,1);
- }
- // 禁言
- if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) {
- $lsetList["manage_notalk"] = $restime;
- }
- // 拉黑,管理员不能被拉黑
- if($hgetlist && $i == 3 && $restime < $hgetlist["time"] && $this->auth->is_manager == 0) {
- $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
- $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
- break;
- }
- // 踢出,管理员不能被踢出
- if($hgetlist && $i == 4 && $restime < $hgetlist["time"] && $this->auth->is_manager == 0) {
- $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
- $this->error(__('您已被该房间踢出,解除时间:'.$restime));
- break;
- }
- }
- //下移
- $this->redis->zAdd("party_user_".$party_id,$user_id,$user_id);
- $this->redis->hSet("online_".$party_id,$user_id,$user_id); //新增的
- // 判断当前用户是否存在在直播间通过切换过来的
- $livingUserPartyId = $this->redis->hGet("livingUser", $user_id);
- if ($livingUserPartyId) {
- if($livingUserPartyId != $party_id){
- // 扣除房间在线人数
- $this->redis->HDel("online_" . $livingUserPartyId, $user_id);
- //首页用户头像
- $this->redis->zRem("party_user_".$livingUserPartyId, $user_id);
- // 扣除在线用户在房间情况
- $this->redis->hDel("livingUser", $user_id);
- }
- }
- // 记录在线用户在房间情况
- $this->redis->hSet("livingUser", $user_id, $party_id);
- //下移
- // 获取用户魅力值。大概率没用了
- // 获取用户排行榜前三名头像。大概率没用了
-
- // 获取是否被当前用户关注
- $lsetList["is_follow"] = 0;
- $where = [];
- $where["user_id"] = $user_id;
- $where["party_id"] = $party_id;
- if(Db::name('party_follow')->where($where)->find()) {
- $lsetList["is_follow"] = 1;
- }
- if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
- // $partyInfo["type_name"] = $partyInfo["type_name"];
- } else {
- $partyInfo["type_name"] = "普通房";
- }
- $partyInfo["party_hot"] = changeW($partyInfo["party_hot"]);
- $partyInfo['is_new'] = $partyInfo["status"] == 0 ? 1 : 0;
- $partyInfo['join_days'] = $this->getUserJoinPartyDays($user_id,$party_id);
- $lsetList["partyInfo"] = $partyInfo;
- //声网三个token
- $lsetList['shengwang_token'] = $this->shengwang_token_private($partyInfo['easemob_room_id']);
- //[环信] 更新聊天室自定义属性 在线用户人数
- $easemob = new Easemob();
- $matedata = [
- 'online_user_num' => count($this->redis->hGetAll("online_".$party_id)),
- ];
- $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
- return $this->success("加入成功!",$lsetList);
- }
- private function creater_joinParty($party_id,$easemob_room_id,$party_user_id) {
- $user_id = $this->auth->id;
- // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
- $lsetList = [];
- for($i=1;$i<=4;$i++) {
- $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i);
- $hgetlist = unserialize($hgetlist);
- // 房管
- if($hgetlist && $i == 1) {
- $lsetList["manage_restime"] = "";
- //重新设置成管理员
- $this->setGroupAdmin($easemob_room_id,$user_id,1);
- }
- }
- //下移
- $this->redis->zAdd("party_user_".$party_id,$user_id,$user_id);
- $this->redis->hSet("online_".$party_id,$user_id,$user_id); //新增的
- // 判断当前用户是否存在在直播间通过切换过来的
- $livingUserPartyId = $this->redis->hGet("livingUser", $user_id);
- if ($livingUserPartyId) {
- if($livingUserPartyId != $party_id){
- // 扣除房间在线人数
- $this->redis->HDel("online_" . $livingUserPartyId, $user_id);
- //首页用户头像
- $this->redis->zRem("party_user_".$livingUserPartyId, $user_id);
- // 扣除在线用户在房间情况
- $this->redis->hDel("livingUser", $user_id);
- }
- }
- // 记录在线用户在房间情况
- $this->redis->hSet("livingUser", $user_id, $party_id);
- //下移
- //[环信] 更新聊天室自定义属性 在线用户人数
- $easemob = new Easemob();
- $matedata = [
- 'online_user_num' => count($this->redis->hGetAll("online_".$party_id)),
- ];
- $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_user_id,$matedata);
- return true;
- }
- /**
- * 判断是否被禁言
- */
- public function isNotalk() {
- $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
- $user_id = input('user_id',0,"intval"); // 用户ID 逻辑ID
- if (!$user_id || !$party_id) {
- $this->error(__('Invalid parameters'));
- }
- // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
- $lsetList = [];
- $lsetList["is_notalk"] = 0;
- $lsetList["notalk_time"] = 0;
- $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-2");
- if(!$hgetlist){
- return $this->success("获取成功!",$lsetList);
- }
- $hgetlist = unserialize($hgetlist);
- $restime = time() - intval($hgetlist["createtime"]);
- // 禁言
- if($hgetlist && $restime < $hgetlist["time"]) {
- $lsetList["is_notalk"] = 1;
- $lsetList["notalk_time"] = $restime;//已经禁了N秒
- }
- return $this->success("获取成功!",$lsetList);
- }
- /**
- * 退出派对
- */
- public function outParty() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $room_type = input("room_type",1);//
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $userId = $this->auth->id;
- $this->redis->zRem("party_user_".$party_id,$userId);
- // 处理online_party_id
- $this->redis->HDel("online_" . $party_id, $this->auth->id);
- // 扣除在线用户在房间情况
- $this->redis->hDel("livingUser", $this->auth->id);
- //下麦
- $key = "party_seat_".$party_id;
- $seats = $this->redis->hGetAll($key);
- foreach($seats as $seat_num => $seatuserid){
- if($seatuserid == $userId){
- $this->redis->HDel($key, $seat_num);
- }
- }
- // 更新房间在线状态
- $partyInfo = $this->redis->get($this->roomTypeArr[$room_type].'_'.$party_id);
- if ($partyInfo) {
- $partyInfo = json_decode($partyInfo, true);
- $memCount = count($this->redis->hGetAll("online_" . $party_id));
- if ($memCount <= 0) {
- $partyInfo["is_online"] = 0;
- $this->redis->set($this->roomTypeArr[$room_type].'_'.$party_id,json_encode($partyInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
- }
- //[环信] 更新聊天室自定义属性 在线用户人数
- $easemob = new Easemob();
- $matedata = [
- 'online_user_num' => count($this->redis->hGetAll("online_".$party_id)),
- ];
- $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
- //清除这个人点的歌,前台自己处理了
- /*$easemob_room_id = $partyInfo['easemob_room_id'];
- $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
- $singlist = json_decode($singlist['waitsing_list'],true);
- if(!empty($singlist)){
- foreach($singlist as $key => $sing){
- if($sing['userNo'] == $userId){
- unset($singlist[$key]);
- }
- }
- //重新排序
- $singlist = $this->waitsing_array_column_sort($singlist);
- //重新设置
- $matedata = [
- 'waitsing_list' => json_encode($singlist),
- ];
- $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$matedata);
- }*/
- }
- $this->success("退出成功!",[]);
- }
- /**
- * 派对内搜索用户
- */
- public function searchUserParty() {
- $u_id = input('u_id',0,"intval"); // 用户u_id
- $party_id = input('party_id',0,"intval"); // 直播间ID
- // $userids = $this->redis->zRange("party_user_".$party_id,0,-1,true);
- // $useridArr = [];
- // if($userids)foreach($userids as $k => $v) $useridArr[$v] = $k; // 因为array_slip 不能交换数字啊
- // $userid = isset($useridArr[$u_id])?$useridArr[$u_id]:0;
- $userModel = new \app\common\model\User();
- $where = [];
- $where["u_id"] = $u_id;
- $userInfo = $userModel->field("id,u_id,avatar,nickname,level,gender")->where($where)->select();
- return $this->success("查询成功!",$userInfo);
- }
- /**
- * 设置冠名
- */
- public function setNaming() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $user_id = input('user_id',0,"intval"); // 用户ID
- $type = input('type',1); // 1:设置 0:取消设置
- if (!$party_id || !$user_id || ($type != 1 && $type != 0)) {
- $this->error(__('Invalid parameters'));
- }
- $partyModel = new \app\common\model\Party();
- $data = [];
- $where = [];
- $where["id"] = $party_id;
- $partyInfo = $partyModel->where(["id"=>$party_id])->find();
- $getredisPartyInfo = $this->redis->get($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id);
- $redisPartyInfo = json_decode($getredisPartyInfo,true);
- if($type == 1) {
- $data["naming"] = $user_id;
- // 冠名
- $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$user_id])->find($user_id);
- $redisPartyInfo["naming"] = $userInfo;
- } else {
- $data["naming"] = 0;
- $redisPartyInfo["naming"] = [];
- }
- $this->redis->set($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id,json_encode($redisPartyInfo));
- $res = $partyModel->update($data,$where);
- if($res !== false) {
- $this->success("操作成功!");
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 派对关注
- */
- public function followparty() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $user_id = $this->auth->id;
- // 添加记录
- $data = [];
- $data["user_id"] = $user_id;
- $data["party_id"] = $party_id;
- if(Db::name('party_follow')->where($data)->find()) {
- $res = Db::name('party_follow')->where($data)->delete();
- $this->success("已取消关注");
- }
- $data["createtime"] = time();
- $res = Db::name('party_follow')->insert($data);
- $this->success("已关注");
- }
- /**
- * 获取派对信息
- */
- public function getPartyInfo() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $user_id = $this->auth->id;
- $partyModel = new \app\common\model\Party();
- $userModel = new \app\common\model\User();
- // 获取主体信息
- $where = [];
- $where["a.id"] = $party_id;
- $partyInfo = $partyModel->alias("a")
- ->field("a.id,a.user_id,a.party_id,a.party_name,a.party_hot,a.party_logo,a.party_notice,a.party_notice_detail,a.on_model,a.background,a.is_public,
- rt.id as party_type,rt.name as type_name")
- ->join("hx_party_type rt","rt.id = a.party_type", "left")
- ->where($where)
- ->find();
- if($partyInfo) {
- $mod = isset($partyInfo["party_type"])?intval($partyInfo["party_type"])%5:1;
- $partyInfo["party_type_color"] = $mod == 0?5:$mod;
- if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
- // $partyInfo["type_name"] = $partyInfo["type_name"];
- } else {
- $partyInfo["type_name"] = "普通房";
- }
- // 获取是否被当前用户关注
- $partyInfo["is_follow"] = 0;
- $where = [];
- $where["user_id"] = $user_id;
- $where["party_id"] = $party_id;
- if(Db::name('party_follow')->where($where)->find()) {
- $partyInfo["is_follow"] = 1;
- }
- // 获取房主信息
- $where = [];
- $where["id"] = $partyInfo["user_id"];
- $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
- // 获取技能信息
- $partyInfo["userInfo"] = $userInfo;
- $partyInfo["party_hot"] = changeW($partyInfo["party_hot"]);
- }
- $this->success("获取成功!",$partyInfo);
- }
- /**
- * 派对设置
- */
- public function setParty() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $room_type = input('room_type',1); // 房间类型:1=公会,2=个人k歌
-
- if (!$party_id || !$room_type) {
- $this->error(__('Invalid parameters'));
- }
- // 派对名称
- // 派对logo/封面
- // 派对密码
- // 使用场景
- // 是否关闭公屏:1=是,0=否
- // 上麦模式:1=自由模式,2=麦序模式
- // 派对背景
- // 是否公开:1=是,0=否
- $field_array = ['party_name','party_logo','party_pass'/*,'party_type'*/,'is_screen','on_model','background','is_public','party_notice','party_notice_detail'];
- $data = [];
- foreach($field_array as $key => $field){
- //前端传不了post,改了
- if(!request()->has($field,'post')){
- continue;
- }
- /*if(!input('?'.$field)){
- continue;
- }*/
- $newone = input($field);
- $data[$field] = $newone;
- }
- if(isset($data['party_pass'])){
- if(empty($data['party_pass'])){
- $data['party_pass'] = '';
- }
- if(!empty($data['party_pass']) && strlen($data['party_pass']) != 4) {
- $this->error("房间密码必须为四位!");
- }
- }
- $partyModel = new \app\common\model\Party();
- $where = [];
- $where["id"] = $party_id;
- $res = $partyModel->update($data,$where);
- if($res) {
- // 获取派对类型
- //if($party_type) $data["type_name"] = \app\common\model\PartyType::where(["id"=>$party_type])->value("name");
- // 存redis 房间信息
- $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
- if($partyInfo) {
- $partyInfo = json_decode($partyInfo,true);
- $partyInfo = array_replace($partyInfo,$data);
- $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
- }
- //[环信]初始化房间,初始化自定义属性
- $matedata = [];
- if(isset($data['party_name'])){
- $matedata['party_name'] = $data['party_name'];
- }
- if(isset($data['party_logo'])){
- $matedata['party_logo'] = $data['party_logo'];
- }
- if(isset($data['is_public'])){
- $matedata['is_public'] = $data['is_public'];
- }
- if(isset($data['background'])){
- $matedata['background'] = $data['background'];
- }
- if(isset($data['party_notice'])){
- $matedata['party_notice'] = $data['party_notice'];
- }
- if(isset($data['party_notice_detail'])){
- $matedata['party_notice_detail'] = $data['party_notice_detail'];
- }
- if(!empty($matedata)){
- $easemob = new Easemob();
- $rs = $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
- }
- $this->success("房间设置成功!",$data);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 获取派对背景
- */
- public function getDefaultBackground() {
- $room_type = input('room_type',1); // 房间类型
- if (!in_array($room_type,[1,2])) {
- $this->error(__('Invalid parameters'));
- }
- $this->success("获取成功!",\app\common\model\PartyBackground::where(["room_type"=>$room_type])->select());
- }
- //环信聊天室设置管理员,type=1设置,type=0移除
- private function setGroupAdmin($easemob_room_id,$user_id,$type = 1)
- {
- $easemob = new Easemob();
- if($type == 1){
- $easemob->room_promoteRoomAdmin($easemob_room_id,$user_id);
- }else{
- $easemob->room_demoteRoomAdmin($easemob_room_id,$user_id);
- }
- }
- /**
- * 派对管理设置
- */
- public function partyManageSet() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $user_id = input('user_id',0,"intval"); // 用户ID
- $item = input('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
- $time = input('time'); // 限制时间(单位:秒):0=永久
- if (!$party_id || !$user_id || !in_array($item,[1,2,3,4])) {
- $this->error(__('Invalid parameters'));
- }
- //2=禁言,3=拉黑,4=踢出 ,都要有时间
- if($item>1 && $time<=0) {
- $this->error(__('时间设置有误'));
- }
- //不能对房主恶意操作
- $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
- if ($partyinfo['user_id'] == $user_id && in_array($item, [2, 3, 4])) {
- $this->error("您当前无权限操作!");
- }
- //只有房主才能加管理员
- if($item == 1 && $this->auth->id != $partyinfo['user_id']){
- $this->error('只有房主才能设置管理员');
- }
- // 获取用户信息
- $userInfo = \app\common\model\User::field("avatar,nickname,gender")->where(["id"=>$user_id])->find();
- if(!$userInfo) $this->error("用户信息获取失败!");
- $data = [];
- $data["user_id"] = $user_id;
- $data["avatar"] = $userInfo->avatar;
- $data["nickname"] = $userInfo->nickname;
- $data["gender"] = $userInfo->gender;
- $data["item"] = $item;
- $data["time"] = $time;
- $data["createtime"] = time();
- $res = $this->redis->hSet("party_manage_".$party_id,$user_id."-".$item,serialize($data));
- if ($item==1) {
- $this->setGroupAdmin($partyinfo['easemob_room_id'],$user_id,1);
- }
- if($res !== false) {
- $this->success("设置成功!",$data);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 派对管理设置列表
- * 仅用来给前端展示,禁言的鉴权在isNotalk接口,拉黑与提出的鉴权在joinparty接口
- */
- public function partyManageSetList() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $item = input('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
- if (!$party_id || !in_array($item,[1,2,3,4])) {
- $this->error(__('Invalid parameters'));
- }
- $time = time();
- $hget=$this->redis->hGetAll("party_manage_".$party_id);
- $list=array();
- foreach($hget as $key=>$val) {
- if(substr($key,-2) == "-".$item){
- array_push($list,unserialize($val));
- }
- }
- if($list) {
- foreach($list as $k => $v) if(bcadd($v["createtime"],$v["time"]) <= $time && $v["item"] > 1) unset($list[$k]);
- if($list) $list = array_values($list);
- }
- $this->success("获取成功!",$list);
- }
- /**
- * 派对管理设置移除
- */
- public function partyManageSetDel() {
- $id = input('user_id',0,"intval"); // userid
- $party_id = input('party_id',0,"intval"); // 房间ID
- $item = input('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
- if (!$id || !in_array($item,[1,2,3,4])) {
- $this->error(__('Invalid parameters'));
- }
- $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
- //只有房主才能加管理员
- if($item == 1 && $this->auth->id != $partyinfo['user_id']){
- $this->error('只有房主才能免除管理员');
- }
- $res = $this->redis->hDel("party_manage_".$party_id,$id."-".$item);
- if ($item==1) {
- $this->setGroupAdmin($partyinfo['easemob_room_id'],$id,0);
- }
- if($res !== false) {
- $this->success("移除成功!",$res);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 更新派对公告
- */
- public function savePartyNotice() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $room_type = input('room_type',1); // 房间类型:1=派对2=直播
- $party_notice = input('party_notice'); // 公告标题
- $party_notice_detail = input('party_notice_detail'); // 公告内容
- if (!$party_id || !$party_notice || !$party_notice_detail) {
- $this->error(__('Invalid parameters'));
- }
- $partyModel = new \app\common\model\Party();
- $where = [];
- $where["id"] = $party_id;
- $data = [];
- $data["party_notice"] = $party_notice;
- $data["party_notice_detail"] = $party_notice_detail;
- $res = $partyModel->update($data,$where);
- if($res) {
- // 存redis 房间信息
- $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
- if($partyInfo) {
- $partyInfo = json_decode($partyInfo,true);
- $partyInfo = array_replace($partyInfo,$data);
- $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
- }
- $this->success("更新成功!",$res);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 开始排麦
- */
- public function addLineUp() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $mai_id = input('mai_id',"1"); // 麦位置
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $userInfo = $this->auth->getUserinfo();
- $userid = $userInfo['id'];
- // 获取 房间信息
- $partyUser = \app\common\model\Party::where(['id'=>$party_id])->value("user_id");
- if(!$partyUser) $this->error(__('房间信息未找到!'));
- // $is_home = $partyUser == $userInfo->id?1:0;
- $data = unserialize($this->redis->hGet("party_lineup",$party_id));
- $data[$userid]["user_id"] = $userInfo['id'];
- $data[$userid]["avatar"] = $userInfo['avatar'];
- $data[$userid]["mai_id"] = $mai_id;
- $data[$userid]["nickname"] = $userInfo['nickname'];
- // $data["is_home"] = $is_home;
- // $data[$userid]["level"] = $userInfo['level'];
- $data[$userid]["gender"] = $userInfo['gender'];
- $res = false;
- $data && $res = $this->redis->hSet("party_lineup",$party_id,serialize($data));
- $data = array_values($data);
- if($res !== false) {
- $this->success("设置成功!",$data);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- /**
- * 取消排麦
- */
- public function cancelLineUp() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- $user_id = input('user_id',0); // 直播间ID
- $is_empty = input('is_empty',0,"intval"); // 是否清空排麦列表 1=清空,0=不清空
- $is_baomai = input('is_baomai',0,'intval'); //是否抱麦
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $data=unserialize($this->redis->hGet("party_lineup",$party_id));
- $baomai_user = [];
- if($data && $is_empty != 1) {
- foreach($data as $k => $v) {
- if($v["user_id"] == $user_id) {
- $baomai_user = $v;
- unset($data[$k]);
- break;
- }
- }
- }
- // unset($data[$userid]);
- if($is_empty == 1) $data = [];
- $this->redis->hSet("party_lineup",$party_id,serialize($data));
- //抱麦
- if($is_empty != 1 && $is_baomai == 1 && !empty($baomai_user)){
- $this->baomai($party_id,$user_id,$baomai_user);
- }
- //
- $datas = [];
- is_array($data) && $datas=array_values($data);
- if($datas) {
- $this->success("移除成功!",$datas);
- } else {
- $this->success("移除成功!",[]);
- }
- }
- private function baomai($party_id,$user_id,$baomai_user){
- $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
- if(empty($partyinfo)){
- return false;
- }
- //获取所有麦位,假设8个
- $easemob = new Easemob();
- $seatlist = $easemob->room_getRoomCustomAttribute($partyinfo['easemob_room_id'],['seat0','seat1','seat2','seat3','seat4','seat5','seat6','seat7']);
- $newseat = [
- 'charm' => 0, //红心,魅力值
- 'isMaster' => false, // 是否是房主
- 'headUrl' => $baomai_user['avatar'], // 头像
- 'userNo' => $baomai_user['user_id'], // 座位上用户no
- 'rtcUid' => $baomai_user['user_id'], // 座位上用户id,与rtc的userId一致
- 'name' => $baomai_user['nickname'], // 座位上用户昵称
- 'seatIndex' => -1, // 座位编号
- 'chorusSongCode' => '', // 是否合唱
- 'isAudioMuted' => 1, // 是否静音
- 'isVideoMuted' => 0, // 是否开启视频
- 'checked' => false, // 用于送礼物选择用户
- 'isUsed' => true, // 用于送礼物选择用户
- 'gender' => $baomai_user['gender'], //性别
- ];
- if(empty($seatlist)){
- $newseat['seatIndex'] = 0;
- $matedata['seat0'] = $newseat;
- }else{
- //第一遍轮询麦位,尝试找到这个人,如果找到在任何一个麦位,立刻return
- foreach($seatlist as $k => $seat){
- $seat = json_decode($seat,true);
- //找到当前用户所在的麦位
- if(isset($seat['rtcUid']) && !empty($seat['rtcUid']) && $seat['rtcUid'] == $user_id){
- return true;
- }
- }
- //第二遍轮询麦位,找一个空麦位,放进去
- $matedata = [];
- foreach($seatlist as $k => $seat){
- $seat = json_decode($seat,true);
- //找到当前用户所在的麦位
- if(isset($seat['rtcUid']) && empty($seat['rtcUid'])){
- //dump(substr($k,-1));
- $newseat['seatIndex'] = substr($k,-1);
- //初始化这个麦位
- $matedata = [
- $k => json_encode($newseat),
- ];
- }
- }
- }
- //重置房间在线人数
- if(!empty($matedata)){
- $easemob->room_setRoomCustomAttributeForced($partyinfo['easemob_room_id'],$partyinfo['user_id'],$matedata);
- }
- return true;
- }
- /**
- * 排麦列表
- */
- public function lineUpList() {
- $party_id = input('party_id',0,"intval"); // 直播间ID
- if (!$party_id) {
- $this->error(__('Invalid parameters'));
- }
- $data = $this->redis->hGet("party_lineup",$party_id);
- $dataArr = unserialize($data);
- $datas = [];
- is_array($dataArr) && $datas=array_values($dataArr);
- if($datas) {
- $this->success("获取成功!",$datas);
- } else {
- $this->success("获取成功!",[]);
- }
- }
- /**
- * 获取派对类型
- */
- public function getPatyType() {
- $room_type = input("room_type",1);//
- if (!in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
- $partytypeModel = new \app\common\model\PartyType();
- $partytypeList = $partytypeModel->where(["room_type"=>$room_type])->select();
- $this->success("获取成功!",$partytypeList);
- }
- /**
- * 判断派对是否设置过密码
- */
- public function getPatyIspass() {
- $party_id = input("party_id");// 派对ID
- $room_type = input("room_type",1);//
- $is_miniprogram = input("is_miniprogram",0);//
- if (!$party_id) $this->error(__('Invalid parameters'));
- $user_id = $this->auth->id;
- // 存redis 房间信息
- $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
- if($partyInfo) {
- $partyInfo = json_decode($partyInfo,true);
- } else {
- $partyModel = new \app\common\model\Party();
- $partyInfo = $partyModel->field("id,user_id,party_pass")->where(["id"=>$party_id])->find();
- if(!$partyInfo) {
- $this->error(__('房间信息获取失败!'));
- }
- }
- $data = [];
- $data["status"] = 0;
- $data["is_pass"] = $partyInfo["party_pass"]?1:0;
- $partyManage = $this->redis->hGet("party_manage_".$party_id,$user_id."-1");
- if($partyInfo["user_id"] == $user_id || $this->auth->is_manager == 1 || !empty($partyManage)){ $data["is_pass"] = 0;}
- if($is_miniprogram == 1) { // 小程序单独处理
- // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
- $lsetList = [];
- for($i=1;$i<=4;$i++) {
- $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i);
- $hgetlist = unserialize($hgetlist);
- $restime = time() - intval($hgetlist["createtime"]);
- // 房管
- if($hgetlist && $i == 1) $lsetList["manage_restime"] = "";
- // 禁言
- if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime;
- // 拉黑
- if($hgetlist && $i == 3 && $restime < $hgetlist["time"]) {
- $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
- $lsetList["manage_black"] = $restime;
- $lsetList["tips"] = '您已被该房间拉黑,解除时间:'.$restime;
- // $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
- break;
- }
- // 踢出
- if($hgetlist && $i == 4 && $restime < $hgetlist["time"]) {
- $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
- $lsetList["manage_takeout"] = $restime;
- $lsetList["tips"] = '您已被该房间踢出,解除时间:'.$restime;
- // $this->error(__('您已被该房间踢出,解除时间:'.$restime));
- break;
- }
- }
- if($lsetList) {
- $data["status"] = 1;
- $data["hgetlist"] = $lsetList;
- }
- }
- $this->success("获取成功!",$data);
- }
- /**
- * 获取房间gif表情分类
- */
- public function getPartyGifType()
- {
- $list = \app\common\model\PartyGifType::field("id,name,image")->order("weigh", "asc")->select();
- $this->success("获取成功!", $list);
- }
- /**
- * 获取房间gif表情列表
- */
- public function getPartGifList() {
- $this->success("获取成功!",\app\common\model\PartyGif::field("id,gif_image")->order("weight","asc")->select());
- }
- /**
- * 获取房间头像gif表情列表
- */
- public function getPartHeadgifList() {
- $this->success("获取成功!",\app\common\model\PartyHeadgif::field("id,name,gif_image")->order("weight","asc")->select());
- }
- /**
- * 全麦/单独赠送礼物
- */
- public function giveGiftToYou() {
- // 是否背包赠送: 1=是,0=否
- $is_back = input("is_back",0);
- // 接口防并发
- if($is_back){
- if (!$this->apiLimit(50, 1000)) {
- //1000毫秒50次
- $this->error('休息一下吧');
- }
- }else{
- if (!$this->apiLimit(10, 1000)) {
- //1000毫秒10次
- $this->error('休息一下吧');
- }
- }
- //接收参数
- $user_ids = input("user_id");// 赠送对象用户id
- $seat_ids = input("seat_id");// 赠送对象麦位id
- $gift_id = input("gift_id",0,'intval');// 礼物ID。如果是背包赠送,则是gift_back表的id,这样不合理,但是已经不好改了
- $party_id = input("party_id",0,'intval');// 派对ID
- $room_type = input('room_type',1,'intval'); // 房间类型
- $number = input("number",1,'intval');// 赠送数量
- if (!$user_ids || $seat_ids == '' || !in_array($is_back,[0,1]) || !$gift_id || !$number || !in_array($room_type,[1,2]))
- {
- $this->error(__('Invalid parameters'));
- }
- if(!$party_id){
- $this->error('只有房间内能送礼物');
- }
- //处理参数
- $user_id_arr = explode(",",$user_ids);
- $seat_id_arr = explode(",",$seat_ids);
- $userCount = count($user_id_arr);
- $seatCount = count($seat_id_arr);
- if($userCount != $seatCount){
- $this->error();
- }
- $userauthid = $this->auth->id;
- $jewel_to_money = config('jewel_to_money'); //金币收益到海钻
- // 不可以赠送给自己
- if(in_array($userauthid,$user_id_arr)) $this->error("不可以赠送给自己!");
- $backGiftId = 0; //背包礼物表的 gift_id
- Db::startTrans();
- if($is_back == 1) {
- // 获取背包礼物信息
- $giftbackInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
- if(!$giftbackInfo){
- Db::rollback();
- $this->error("背包礼物获取失败");
- }
- $backGiftId = $giftbackInfo['gift_id'];
- // 随机获取一个礼物
- $allCount = $number*$userCount;
- $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')->lock(true)->select();
- if(count($giftbackList) < $allCount)
- {
- Db::rollback();
- $this->error("背包数量不足");
- }
- $giftInfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
- $giftInfo['is_baobi'] = 0;
- $giftValue = $giftInfo["value"] * $number;
- $giftCountValue = $giftValue * $userCount;
- } else {
- // 获取礼物信息
- $giftInfo = Db::name('gift')->field('id,value,name,image,special as gif_image,is_baobi')->where('id',$gift_id)->find();
- if (!$giftInfo) {
- Db::rollback();
- $this->error("请选择礼物!");
- }
- $giftValue = $giftInfo["value"] * $number;
- // 判断当前用户余额,这里不需要锁表,钱包操作会锁
- $giftCountValue = $giftValue * $userCount;
- $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
- if($user_jewel < $giftCountValue){
- Db::rollback();
- $this->error("您的金币余额不足!");
- }
- }
- //爆币基础要折扣,所以多两个变量
- $giftValue_rate = $giftValue;
- $giftCountValue_rate = $giftCountValue;
- //进行抽点设置
- $partyInfo = null;
- $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();
- if(empty($partyInfo)){
- Db::rollback();
- $this->error("不存在的派对!");
- }
- // 获取系统配置信息
- $platRate = $partyInfo->platRate; // 平台抽成百分比
- $guilderRate = $partyInfo->guilderRate; // 房主抽成百分比,包括公会房主,普通房房主
- //爆币礼物单独比例
- if($is_back != 1){
- if(isset($giftInfo['is_baobi']) && $giftInfo['is_baobi'] == 1){
- $platRate = config('site.baobi_gift_platrate');
- $guilderRate = config('site.baobi_gift_guilderrate');
- //基数折扣
- $baserate = config('site.baobi_gift_baserate') ?: 40;
- $giftValue_rate = bcdiv(bcmul($giftValue_rate,$baserate),100);
- $giftCountValue_rate = bcdiv(bcmul($giftCountValue_rate,$baserate),100);
- }
- }
- //getValue削减
-
- //
- $returnData = [];
- try {
- $i = 0;
- //用户大循环
- foreach($user_id_arr as $user_id_key => $user_id) {
- if($is_back == 1) {
- $giftback_ids = array_column($giftbackList,'id');
- if($user_id_key == 0){
- //第一个人就全给扣掉
- $res1 = Db::name('gift_back')->where('id','IN',$giftback_ids)->delete();
- if(!$res1){
- Db::rollback();
- $this->error('背包送礼物失败');
- }
- }else{
- //其他人的时候就不用扣了
- $res1 = true;
- }
- } else {
- $res1 = true;
- // 扣除当前用户金币余额
- $rs_wallet = model('wallet')->lockChangeAccountRemain($userauthid, $giftValue, '-', 0, "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "金币!", 3,'jewel');
- if($rs_wallet['status'] == false){
- Db::rollback();
- $this->error($rs_wallet['msg']);
- }
- }
- // 添加礼物赠送记录表
- $data = [];
- $data["user_id"] = $userauthid;
- $data["user_to_id"] = $user_id;
- $data["seat_num"] = $seat_id_arr[$user_id_key];
- $data["party_id"] = $party_id;
- //update hx_gift_user_party set is_baobi = 1 where gift_id in (294,295,297,299,309,310,311,312);
- $data["is_baobi"] = 0;
- if($is_back != 1){
- if(isset($giftInfo['is_baobi']) && $giftInfo['is_baobi'] == 1){
- $data["is_baobi"] = 1;
- }
- }
- $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id;
- $data["gift_give_type"] = $is_back ? 1 : 2;
- $data["gift_name"] = $giftInfo["name"];
- $data["gift_gif_image"] = $giftInfo["image"];
- $data["number"] = $number;
- $data["price"] = $giftInfo["value"];
- $data["value"] = $giftValue_rate;
- //每个礼物都要计算平台抽成和房主抽成
- $data['platvalue'] = bcmul($platRate/100 ,$data["value"],2);//平台抽成
- $data['guildervalue'] = bcmul($guilderRate/100 ,$data["value"],2);// 房主抽成
- $data['guildermoney'] = bcdiv($data['guildervalue'],$jewel_to_money,2);
- $data['getvalue'] = bcsub(bcsub($data["value"] ,$data['platvalue'],2),$data['guildervalue'],2);//减去抽成剩余价值
- $data['task_status'] = 0; //环信任务
- $data['new_task_status'] = 0; //加钱任务
- $data["createtime"] = time();
- $res5 = Db::name('gift_user_party')->insertGetId($data);
- if(!$res5){
- Db::rollback();
- $this->error('送礼物失败');
- }
- // 添加获赠用户余额
- /*if($data['getvalue'] > 0){
- $getMoney = bcdiv($data['getvalue'],$jewel_to_money,2);
- if($getMoney > 0){
- $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$this->auth->nickname}送你{$giftInfo['name']}x{$number}",101,'money');
- if($rs_wallet['status'] == false){
- Db::rollback();
- $this->error($rs_wallet['msg']);
- }
- }
- }*/
- // 增加房主抽成
- /*if ($partyInfo) {
- $guilderMoney = $data['guildermoney'];
- if($guilderMoney > 0){
- $touserInfo = Db::name('user')->field('nickname')->where('id',$user_id)->find();
- $rs_wallet = model('wallet')->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$this->auth->nickname}送礼物{$giftInfo['name']}x{$number}给{$touserInfo['nickname']},房间礼物抽成",102,'money');
- if($rs_wallet['status'] == false){
- Db::rollback();
- $this->error($rs_wallet['msg']);
- }
- }
- }*/
- if ($res1 && $res5) {
- $i++;
- if($party_id > 0) {
- if($is_back != 1){
- if(isset($giftInfo['is_baobi']) && $giftInfo['is_baobi'] == 1){
- // tcp 更新用户魅力值
- $this->redis->zIncrBy("hourCharm_".$party_id, $giftValue_rate, $user_id);
- // tcp 更新用户贡献值
- $this->redis->zIncrBy("hourWealth_".$party_id, $giftValue_rate, $userauthid);
- }
- }
- }
- // +message
- if(isset($giftInfo['is_baobi']) && $giftInfo['is_baobi'] == 0){
- \app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number." 价值 ".$giftValue_rate ." 金币");
- }
- }
- }
- //爆币,背包不爆币
- if($is_back != 1){
- if(isset($giftInfo['is_baobi']) && $giftInfo['is_baobi'] == 1){
- $res_baobi = $this->baobi($this->auth->id,$data["gift_id"],$giftCountValue,$giftInfo["value"]);
- if($res_baobi === false){
- Db::rollback();
- $this->error('爆币失败');
- }elseif($res_baobi === true){
- $returnData['baobi_total'] = 0;
- $returnData['baobi_beilv'] = 0;
- }else{
- //爆币了
- $returnData['baobi_total'] = $res_baobi['baobi_total'];
- $returnData['baobi_beilv'] = $res_baobi['baobi_beilv'];
- }
- // tcp 更新房间热度
- $this->updatePartyHot($partyInfo, $giftCountValue_rate,$partyInfo['room_type']);
- }
- }
- //增加送礼用户的财富等级
- $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$giftCountValue);
- // tcp 获取房间用户周前三名
- $this->getPartyUserTop($partyInfo, $room_type);
- if($i == $userCount) {
- $returnData["image"] = localpath_to_netpath($giftInfo["image"]);
- $returnData["gif_image"] = localpath_to_netpath($giftInfo["gif_image"]);
- $returnData['wealth_info'] = $res_wealth;
- // 增加抽点记录
- Db::commit();
- $this->success("赠送成功!",$returnData);
- } else {
- Db::rollback();
- $this->error("赠送失败!");
- }
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
- /**
- * 更新派对信息(热度等)
- */
- private function updatePartyHot($partyInfo,$hotValue,$room_type) {
- if(!$partyInfo) return $hotValue;
- $party_hot = $partyInfo->party_hot > 0 ? $partyInfo->party_hot:0;
- $party_hot_value = $party_hot + $hotValue;
- $party_hot_value = $party_hot_value > 0 ? $party_hot_value : 0;
- if($party_hot_value != $party_hot) {
- // 保存数据
- $partyInfo->party_hot = $party_hot_value;
- $partyInfo->save();
- // 更新redis 加入缓存排序
- $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
- // 更新redis 加入缓存
- $redPartyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]);
- if($redPartyInfo) {
- $redPartyInfo = json_decode($redPartyInfo,true);
- $redPartyInfo["party_hot"] = $party_hot_value;
- $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($redPartyInfo));
- }
- }
- return $party_hot_value;
- }
- //gift_id必须是爆币礼物才可以
- public function baobi($user_id,$gift_id,$pay_total,$giftInfo_value){
- //奖项参数
- $conf_arr = Db::name('gift_baobi_config')->where('gift_id',$gift_id)->select();
- if(empty($conf_arr)){
- return true;
- }
- //用户今天的爆币情况,与消费情况
- $today_start = strtotime(date('Y-m-d')) - (86400 * 3);
- $today_end = time();
- $map = [
- //'user_id' => $user_id,
- // 'createtime' => ['BETWEEN',[$today_start,$today_end]],
- //因为有了自动删除计划任务,这里不在需要时间限制,gift_id加索引提升速度
- 'gift_id' => $gift_id,
- ];
- $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();
- //select IFNULL(sum(pay_total),0) as today_pay_total,IFNULL(sum(baobi_total),0) as today_baobi_total from hx_gift_baobi_log where user_id = 1001 and createtime BETWEEN 1703088000 and 1703174399
- //dump($today_data);
- $baobi_zhisun_bili = Db::name('gift')->where('id',$gift_id)->value('baobi_zhisun_bili');
- if($today_data['today_baobi_total'] >= bcdiv(bcmul($today_data['today_pay_total'],$baobi_zhisun_bili,2),100,2)){
- //爆币比消费还高了,不用抽奖了,记录个日志,返回。这里的日志仅用来记录今日消费 sum(pay_total)
- $data = [];
- $data['user_id'] = $user_id;
- $data['gift_id'] = $gift_id;
- $data['baobi_id'] = 0;
- $data['beilv'] = 0;
- $data['rate'] = 0;
- $data['pay_total'] = $pay_total;
- $data['baobi_total'] = 0;
- $data['createtime'] = time();
- $log_id = Db::name('gift_baobi_log')->insertGetId($data);
- if(!$log_id){
- return false;
- }
- return true;
- }
- //概率新数组
- $gailv = [];
- $bei = 100; //小数变整数
- foreach ($conf_arr as $key=>$value)
- {
- $gailv[$value['id']] = $value['rate']*$bei;
- }
- //dump($gailv);
- //奖项新数组
- $conf_column = [];
- foreach ($conf_arr as $key=>$value)
- {
- $conf_column[$value['id']] = $value;
- }
- //dump($conf_column);
- //抽
- $rid = $this->getRand($gailv); //根据概率获取奖项id
- //dump($rid);
- //返回中奖结果
- $result = $conf_column[$rid];
- //爆币金额
- $baobi_total = bcmul($giftInfo_value,$result['beilv'],0);
- //写入爆币记录,不论有没有钱。全都写入其实是为了方便查日志,比在money_log里找范围更小,而且可删除
- $data = [];
- $data['user_id'] = $user_id;
- $data['gift_id'] = $gift_id;
- $data['baobi_id'] = $result['id'];
- $data['beilv'] = $result['beilv'];
- $data['rate'] = $result['rate'];
- $data['pay_total'] = $pay_total;
- $data['baobi_total'] = $baobi_total;
- $data['createtime'] = time();
- $log_id = Db::name('gift_baobi_log')->insertGetId($data);
- if(!$log_id){
- return false;
- }
- //直接给用户钱
- if($baobi_total > 0){
- $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$baobi_total,'+',0,'送礼物爆币'.$result['beilv'].'倍',21,'jewel');
- if($rs_wallet['status'] === false){
- return false;
- }
- //返回爆币金额
- return [
- 'baobi_total' => $baobi_total,
- 'baobi_beilv' => $result['beilv'],
- ];
- }
- //默认成功
- return true;
- }
- //概率获得算法
- private function getRand($proArr) {
- //概率数组的总概率精度
- $proSum = array_sum($proArr);
- $key = rand(1, $proSum);
- // echo $key;
- $result = 0;
- $now = 0;
- foreach ($proArr as $k=>$v)
- {
- $now = $now + $v;
- if($key<=$now)
- {
- $result = $k;
- break;
- }
- }
- unset ($proArr);
- return $result;
- }
- //某房间内礼物列表
- public function getPartyGiftLog(){
- $party_id = input('party_id',0);
- $list = Db::name('gift_user_party')->alias('log')
- ->field('log.gift_name,log.gift_gif_image,log.number,log.value,log.guildermoney,log.createtime,user.nickname as give_nickname,b.nickname as get_nickname')
- ->join('user','log.user_id = user.id','LEFT')
- ->join('user b','log.user_to_id = b.id','LEFT')
- ->where('log.party_id',$party_id)->order('log.id desc')->autopage()->select();
- $list = list_domain_image($list,['gift_gif_image']);
- foreach($list as $key => &$val){
- $val['createtime'] = get_last_time($val['createtime']);
- }
- $this->success('success',$list);
- }
- /**
- * 用户赠送礼物后房间内用户排行,财富榜前三名
- */
- private function getPartyUserTop($party_info,$room_type) {
- // 获取当天零点
- $userModel = new \app\common\model\User();
- // 获取条数
- $num = 3;
- // 获取3条财富排行记录
- $getweek = $this->redis->zRevRange("hourWealth_".$party_info['id'],0,$num-1,true);
- $userList = $userModel->rankList($getweek);
- $avatarArr = [];
- if($userList) {
- foreach($userList as $k => $v) {
- $v["jewel"] > 0 && $avatarArr[] = $v;
- }
- // 加入缓存做备份
- $this->redis->hSet("user_jewel_top3",$party_info['id'],json_encode($avatarArr));
- }
- return $avatarArr;
- //[环信]更新财富榜前3。放到异步了
- }
- /**
- * 单个房间魅力值清零
- */
- public function partyClearCharm() {
- $party_id = input("party_id");// 派对ID
- $userId = input("user_id", 0);// 用户ID
- if($party_id <= 0) {
- $this->error("请输入派对ID");
- }
- if ($userId) {
- $this->redis->zRem("hourCharm_" . $party_id, $userId);
- } else {
- $this->redis->del("hourCharm_" . $party_id);
- }
- $this->success("操作成功!");
- }
- //获取某房间内某个用户的魅力值
- //上麦时候初始化小红心用的
- public function getPartyUserCharm(){
- $party_id = input("party_id");// 派对ID
- $userId = input("user_id", 0);// 用户ID
- $seat_num = input('seat_num',0);//麦位数字0-7
- $key = "party_seat_".$party_id;
- $seat_userid = $this->redis->hGet($key,$seat_num);
- if($seat_userid){
- $this->error('麦上有人了');
- }
- //轮询下麦
- $key = "party_seat_".$party_id;
- $seats = $this->redis->hGetAll($key);
- foreach($seats as $seatnum => $seatuserid){
- if($seatuserid == $userId){
- $this->redis->HDel($key, $seatnum);
- }
- }
- //上麦
- $this->redis->hSet($key,$seat_num,$userId);
- //获取魅力值
- $users = $this->redis->zRevRange("hourCharm_".$party_id,0,-1,true);
- $charm = 0;
- if($users) {
- foreach($users as $k => $v){
- if($userId == $k){
- $charm = changeW($v);
- }
- }
- }
- $this->success(1,$charm);
- }
- //下麦
- public function seat_down(){
- $party_id = input("party_id");// 派对ID
- $userId = input("user_id", 0);// 用户ID
- /*$seat_num = input('seat_num',0);//麦位数字0-7
- $key = "party_seat_".$party_id;
- $this->redis->HDel($key, $seat_num);*/
- //轮询下麦
- $key = "party_seat_".$party_id;
- $seats = $this->redis->hGetAll($key);
- foreach($seats as $seat_num => $seatuserid){
- if($seatuserid == $userId){
- $this->redis->HDel($key, $seat_num);
- }
- }
- $this->success(1);
- }
- /**
- * 获取音乐列表
- */
- public function getMusicList() {
- $this->success("获取成功!",\app\common\model\Music::select());
- }
- //派对举报
- public function report_party() {
- $type = input('type');// 类型描述
- $content = input('content');// 内容
- $images = input('images');// 图片
- $party_id = input('party_id');//
- if (!$type || !$content || !$images || !$party_id) {
- $this->error("请完成举报内容!");
- }
- $data = [];
- $data["user_id"] = $this->auth->id;
- $data["party_id"] = $party_id;
- $data["type"] = $type;
- $data["content"] = $content;
- $data["images"] = $images;
- $data["createtime"] = time();
- $res = Db::name('party_report')->insertGetId($data);
- if ($res) {
- $this->success("举报内容提交成功!");
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
- //=============待唱歌曲操作=================
- //待唱,列表
- public function waitsing_list(){
- $party_id = input('party_id');
- $party_info = Db::name('party')->where('id',$party_id)->find();
- if(empty($party_info)){
- $this->error();
- }
- $easemob_room_id = $party_info['easemob_room_id'];
- $easemob = new Easemob();
- $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
- if(empty($singlist)){
- //默认为空
- $singlist['waitsing_list'] = '[]';
- }
- $singlist = json_decode($singlist['waitsing_list'],true);
- //重新排序
- $singlist = $this->waitsing_array_column_sort($singlist);
- $this->success(1,$singlist);
- }
- //待唱,增加
- public function waitsing_add(){
- //查找派对
- $party_id = input('party_id');
- $party_info = Db::name('party')->where('id',$party_id)->find();
- if(empty($party_info)){
- $this->error();
- }
- $easemob_room_id = $party_info['easemob_room_id'];
- //接收新歌
- $sing_json = input('sing_json','','htmlspecialchars_decode');
- $sing_json = json_decode($sing_json,true);
- $sing_json['sing_no'] = createUniqueNo('s');//附加新参数:歌曲编号
- $sing_json['sort'] = time();//附加新参数:排序
- //获取已有列表
- $easemob = new Easemob();
- $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
- if(empty($singlist)){
- //默认为空
- $singlist['waitsing_list'] = '[]';
- }
- $singlist = json_decode($singlist['waitsing_list'],true);
- //加入新歌
- $singlist[] = $sing_json;
- //重新排序
- $singlist = $this->waitsing_array_column_sort($singlist);
- //重新设置
- $matedata = [
- 'waitsing_list' => json_encode($singlist),
- ];
- $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
- //返回最新歌曲列表
- $this->success(1,$singlist);
- }
- //根据歌曲sort字段,正序排列
- private function waitsing_array_column_sort($data){
- if(empty($data)){
- return $data;
- }
- //dump($data);
- $sort = array_column($data,'sort');
- $sing_no = array_column($data,'sing_no');
- $a = array_multisort($sort,SORT_ASC,$sing_no,$data);
- //dump($data);exit;
- return $data;
- }
- //待唱,删除
- public function waitsing_delete(){
- //查找派对
- $party_id = input('party_id');
- $party_info = Db::name('party')->where('id',$party_id)->find();
- if(empty($party_info)){
- $this->error();
- }
- $easemob_room_id = $party_info['easemob_room_id'];
- //需要删除的编号
- $sing_no = input('sing_no','');
- //获取已有列表
- $easemob = new Easemob();
- $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
- if(empty($singlist)){
- //默认为空
- $singlist['waitsing_list'] = '[]';
- }
- $singlist = json_decode($singlist['waitsing_list'],true);
- //轮询,找到,删除
- foreach($singlist as $key => $sing){
- if($sing['sing_no'] == $sing_no){
- unset($singlist[$key]);
- }
- }
- //重新排序
- $singlist = $this->waitsing_array_column_sort($singlist);
- //重新设置
- $matedata = [
- 'waitsing_list' => json_encode($singlist),
- ];
- $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
- //返回最新歌曲列表
- $this->success(1,$singlist);
- }
- //待唱,置顶
- public function waitsing_top(){
- //查找派对
- $party_id = input('party_id');
- $party_info = Db::name('party')->where('id',$party_id)->find();
- if(empty($party_info)){
- $this->error();
- }
- $easemob_room_id = $party_info['easemob_room_id'];
- //需要置顶的编号
- $sing_no = input('sing_no','');
- //获取已有列表
- $easemob = new Easemob();
- $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
- if(empty($singlist)){
- //默认为空
- $singlist['waitsing_list'] = '[]';
- }
- $singlist = json_decode($singlist['waitsing_list'],true);
- //轮询,找到,改排序
- if(!empty($singlist)){
- //拿到所有排序字段,排列,取第0个
- $sort = array_column($singlist,'sort');
- ksort($sort);
- foreach($singlist as $key => &$sing){
- if($sing['sing_no'] == $sing_no){
- $sing['sort'] = $sort[0] - 1;
- }
- }
- }
- //重新排序
- $singlist = $this->waitsing_array_column_sort($singlist);
- //重新设置
- $matedata = [
- 'waitsing_list' => json_encode($singlist),
- ];
- $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
- //返回最新歌曲列表
- $this->success(1,$singlist);
- }
- //轮询所有房间,挨个初始化自定义属性
- public function room_task(){
- $ids = Db::name('party')->select();
- $easemob = new Easemob();
- foreach($ids as $key => $party){
- //[环信]初始化房间
- $matedata = [
- 'seatnum'=> $party['seatnum'],//麦位数量
- 'waitsing_list' => json_encode([]),//已点歌曲列表
- 'party_name' => $party['party_name'],//房名字
- 'party_logo' => localpath_to_netpath($party['party_logo']),//logo
- 'is_public' => $party['is_public'],//是否公开
- 'room_type' => $party['room_type'],//房间类型
- 'online_user_num' => 0,//当前房间在线的人
- 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
- 'background' => '',//背景图
- ];
- $rs = $easemob->room_setRoomCustomAttributeForced($party['easemob_room_id'],$party['user_id'],$matedata);
- //[环信]初始化N个麦位
- $seatdata = [];
- for($i=0;$i<$party['seatnum'];$i++){
- $seat = [
- 'charm' => 0, //红心,魅力值
- 'isMaster' => false, // 是否是房主
- 'headUrl' => '', // 头像
- 'userNo' => '', // 座位上用户no
- 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
- 'name' => '', // 座位上用户昵称
- 'seatIndex' => -1, // 座位编号
- 'chorusSongCode' => '', // 是否合唱
- 'isAudioMuted' => 1, // 是否静音
- 'isVideoMuted' => 0, // 是否开启视频
- 'checked' => false, // 用于送礼物选择用户
- 'isUsed' => true, // 用于送礼物选择用户
- 'gender' => 1, //性别
- ];
- //创建完房间就进入,所以房主直接在麦位1
- $owner = Db::name('user')->where('id',$party['user_id'])->find();
- if($i == 0){
- $seat['isMaster'] = true;
- $seat['headUrl'] = localpath_to_netpath($owner['avatar']);
- $seat['userNo'] = $owner['id'];
- $seat['rtcUid'] = $owner['id'];
- $seat['name'] = $owner['nickname'];
- $seat['seatIndex']= $i;
- }
- $seatdata['seat'.$i] = json_encode($seat);
- }
- $rs = $easemob->room_setRoomCustomAttributeForced($party['easemob_room_id'],$party['user_id'],$seatdata);
- }
- }
- //显示聊天室所有自定义属性,自用工具
- public function test_party_attr(){
- $party_id = input('party_id',0);
- $party_info = Db::name('party')->where('id',$party_id)->find();
- $easemob = new Easemob();
- $field = [
- 'seatnum',
- 'waitsing_list',
- 'party_name',
- 'party_logo',
- 'is_public',
- 'room_type',
- 'online_user_num',
- 'wealth_top3_userlist',
- 'background',
- 'party_notice',
- 'party_notice_detail',
- ];
- for($i=0;$i<$party_info['seatnum'];$i++){
- $field[] = 'seat'.$i;
- }
- $all_attr = $easemob->room_getRoomCustomAttribute($party_info['easemob_room_id'],$field);
- dump($all_attr);
- echo '<hr>';
- foreach($all_attr as $key => $val){
- echo $key;
- echo '<br>';
- $val = json_decode($val,true);
- dump($val);
- echo '<hr>';
- }
- //
- /*$matedata = [
- 'background' => '1.jpg',
- ];
- $rs = $easemob->room_setRoomCustomAttributeForced($party_info['easemob_room_id'],$party_info['user_id'],$matedata);
- dump($rs);*/
- }
- //============================定时任务==========================//
- /**
- * redis清理排行榜 并数据库备份
- */
- /*public function updateTops() {
- // 前一天日期
- $yestaday = date("Ymd",bcsub(time(),86400));
- // 上个周一
- $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
- // 上个月一号
- $monty = bcsub(date("m"),1);
- if($monty < 10) $monty = "0".$monty;
- $premonth = date("Y").$monty."01";
- // 获取所有派对ID
- $party_ids = \app\common\model\Party::where(["status"=>1])->column("id");
- // 获取数据
- $time = time();
- if($party_ids) foreach($party_ids as $k => $v) {
- $keys = [
- "party_jewel_get_".$v.":".$yestaday."d",
- "party_jewel_to_".$v.":".$yestaday."d",
- "party_jewel_get_".$v.":".$preweek."w",
- "party_jewel_to_".$v.":".$preweek."w",
- "party_jewel_get_".$v.":".$premonth."m",
- "party_jewel_to_".$v.":".$premonth."m",
- ];
- foreach($keys as $key) {
- // 备份数据
- $redisData = $this->redis->zRevRange($key,0,-1,true);
- if($redisData) {
- $keyInfo = \app\common\model\RedisTops::where(["key"=>$key])->find();
- $data = ["party_id"=>$v,"key"=>$key,"value"=>json_encode($redisData),"createtime" => $time];
- if(!$keyInfo) {
- $res = \app\common\model\RedisTops::insert($data);
- // 清理数据
- $res && $this->redis->zRemRangeByRank($key,0,-1);
- }
- }
- }
- }
- }*/
- public function handleParty() {
- return false;
- $partyList = \app\common\model\Party::where('is_online',1)->select();
- // $this->redis->del("online_" . 3);die;
- // $this->redis->hSet("online_" . 3, 4, 4);die;
- foreach ($partyList as $party){
- $userId = $party['user_id'];
- $roomId = $party['id'];
- $this->redis->HDel("online_" . $roomId, $userId);
- $this->redis->zRem("party_user_".$roomId, $userId);
- // 扣除在线用户在房间情况
- $this->redis->hDel("livingUser",$userId);
- // // 更新房间在线状态
- $partyInfo = $this->redis->get("party_" . $roomId);
- if ($partyInfo) {
- $partyInfo = json_decode($partyInfo, true);
- $memCount = count($this->redis->hGetAll("online_" . $roomId));
- if ($memCount <= 0) {
- dump($roomId.'--1---');
- $partyInfo["is_online"] = 0;
- $this->redis->set("party_" . $roomId, json_encode($partyInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
- }
- }
- $liveInfo = $this->redis->get("live_" . $roomId);
- if ($liveInfo) {
- $liveInfo = json_decode($liveInfo, true);
- $memCount = count($this->redis->hGetAll("online_" . $roomId));
- if ($memCount <= 0) {
- dump($roomId.'---2--');
- $liveInfo["is_online"] = 0;
- $this->redis->set("live_" . $roomId, json_encode($liveInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
- }else{
- dump($roomId.'---3--');
- $this->redis->del("online_" . $roomId);
- $liveInfo["is_online"] = 0;
- $this->redis->set("live_" . $roomId, json_encode($liveInfo));
- \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
- }
- }
- }
- $this->success("获取成功!");
- }
- }
|