Parcourir la source

后台解散房间,接口解散房间优化

lizhen_gitee il y a 1 an
Parent
commit
13d7f2d82d
2 fichiers modifiés avec 131 ajouts et 16 suppressions
  1. 121 15
      application/admin/controller/party/Party.php
  2. 10 1
      application/api/controller/Party.php

+ 121 - 15
application/admin/controller/party/Party.php

@@ -20,6 +20,8 @@ class Party extends Backend
      */
     protected $model = null;
 
+    protected $redis;
+
     public function _initialize()
     {
         parent::_initialize();
@@ -33,6 +35,18 @@ class Party extends Backend
         $this->view->assign("isScreenList", $this->model->getIsScreenList());
         $this->view->assign("onModelList", $this->model->getOnModelList());
         $this->view->assign("isPublicList", $this->model->getIsPublicList());
+
+        //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;
     }
 
     public function import()
@@ -117,27 +131,17 @@ class Party extends Backend
                     $party_id = $row->id;
                     if($party_id > 0) {
                         // 存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']);
-                        }
-                        $partyInfo = $redis->get($roomTypeArr[$room_type]."_".$party_id);
+                        $partyInfo = $this->redis->get($roomTypeArr[$room_type]."_".$party_id);
                         if($partyInfo) {
                             $partyInfo = json_decode($partyInfo,true);
                             $partyInfo = array_replace($partyInfo,$params);
-                            $redis->set($roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
+                            $this->redis->set($roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
                         }
+
+
                     }
 
-                    /*if($params["is_close"] == 1) {
-                        // 强制关闭需要退出正在房间的用户
-                        $this->outMemberFromRoom($row->easemob_room_id);
-                    }*/
+
 
                     //是否采用模型验证
                     if ($this->modelValidate) {
@@ -147,6 +151,12 @@ class Party extends Backend
                     }
                     $result = $row->allowField(true)->save($params);
                     Db::commit();
+
+                    if($params["status"] == 3) {
+                        //解散房间
+                        $this->closeParty($party_id,$room_type);
+                    }
+
                 } catch (ValidateException $e) {
                     Db::rollback();
                     $this->error($e->getMessage());
@@ -169,6 +179,102 @@ class Party extends Backend
         return $this->view->fetch();
     }
 
+    /**
+     * 解散房间
+     */
+    public function closeParty($party_id,$room_type) {
+
+
+        $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
+        if(!$partyInfo) {
+            $this->error(__('派对不存在!'));
+        }
+
+
+        //房间从首页列表消失
+
+
+        //用户魅力榜,清空
+        $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);
+            }
+        }
+
+        //环信操作
+        $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);
+            }
+        }
+
+        return true;
+    }
 
     /**
      * 踢出房间内所有用户

+ 10 - 1
application/api/controller/Party.php

@@ -380,6 +380,7 @@ class Party extends Api
         if(!$partyInfo) {
             $this->error(__('派对不存在!'));
         }
+        $room_type = $partyInfo['room_type'];
 
         if($partyInfo['user_id'] != $this->auth->id){
             $this->error('房主才能解散');
@@ -391,6 +392,14 @@ class Party extends Api
         ];
         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);
 
@@ -450,7 +459,7 @@ class Party extends Api
 
                 $seatdata['seat'.$i] = json_encode($seat);
             }
-            $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$seatdata);
+            $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$seatdata);
 
             //清空房间人,聊天室
             //分页获取聊天室成员列表