Ver código fonte

聊天室自定义属性,完善方法

lizhen_gitee 1 ano atrás
pai
commit
0e0ca02aa0

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

@@ -130,7 +130,7 @@ class Party extends Common
 
             //[环信]创建聊天室
             $easemob = new Easemob();
-            $easemob_room_id = $easemob->room_create($party_name,$party_name,$this->auth->id);
+            $easemob_room_id = $easemob->room_createRoom($party_name,$party_name,$this->auth->id);
             if(empty($easemob_room_id)){
                 $this->error('创建房间失败');
             }

+ 73 - 1
application/api/controller/Test.php

@@ -25,11 +25,83 @@ class Test extends Api
     //创建聊天室
     public function createroom(){
         $easemob = new Easemob();
-        $room_id = $easemob->room_create('第一个聊天室','聊天室简介','2');
+        $room_id = $easemob->room_create('聊天室名称','聊天室简介',2);
         dump($room_id);
 
     }
 
+    //获取聊天室详情
+    public function getroominfo(){
+        $easemob = new Easemob();
+
+        $room_id = 230909536174085;
+        $rs = $easemob->room_getRoom($room_id);
+        dump($rs);
+    }
+
+    //修改聊天室
+    public function updateroominfo(){
+        $easemob = new Easemob();
+
+        $room_id = 230909536174085;
+        $rs = $easemob->room_updateRoom($room_id,'聊天室名称','聊天室简介');
+        dump($rs);
+    }
+
+    //强制设置聊天室属性
+    public function setmetadata(){
+        $easemob = new Easemob();
+
+        $room_id = 230909536174085;
+        $owner_id = 4;
+        $matedata = [
+                //限量10个
+                'seat11'  => 666,
+                'seat22'  => 666,
+                'seat33'  => 666,
+                'seat44'  => 666,
+                'seat55'  => 666,
+                'seat66'  => 666,
+                'seat77'  => 666,
+                'seat88'  => 666,
+                'seat99'  => 666,
+                'seat100' => 666,
+        ];
+        $rs = $easemob->room_setRoomCustomAttributeForced($room_id,$owner_id,$matedata);
+        dump($rs);
+    }
+
+    //获取聊天室属性
+    public function getmetadata(){
+        $easemob = new Easemob();
+        $room_id = 230909536174085;
+
+        $key = 'seat2';
+        $key2 = 'seat22';
+
+        //可以一个,可以多个,不能有重复
+        $keys = [$key,$key2];
+        //空数组,则全返回
+        $keys = [];
+
+        $rs = $easemob->room_getRoomCustomAttribute($room_id,$keys);
+        dump($rs);
+        dump($rs[$key]);
+        dump($rs[$key2]);
+    }
+
+    //强制删除聊天室属性
+    public function deletemetadata(){
+        $easemob = new Easemob();
+        $room_id = 230909536174085;
+        $owner_id = 4;
+        $keys = ['sate4'];
+
+
+        $rs = $easemob->room_deleteRoomCustomAttributeForced($room_id,$owner_id,$keys);
+        dump($rs);
+    }
+
     //////////////////////////////
 
 }

+ 90 - 1
application/common/library/Easemob.php

@@ -24,6 +24,8 @@ class Easemob {
         $this->user = new User($this->auth);
     }
 
+    /////////////////////////////////管理用户/////////////////////////////
+
     //注册用户
     /*
         array(6) {
@@ -57,6 +59,7 @@ class Easemob {
 
     //获取 app 中的聊天室
     //获取用户加入的聊天室
+
     //查询聊天室详情
     public function room_getRoom($roomId){
         $rs = $this->room->getRoom($roomId);
@@ -67,22 +70,108 @@ class Easemob {
     /*
         string(15) "230821166383116"
      */
-    public function room_create($name,$info,$user_id){
+    public function room_createRoom($name,$info,$user_id){
         $user_id = '' . $user_id . '';
         $room_id = $this->room->createRoom($name,$info,$user_id,[$user_id]);
         return $room_id;
     }
 
     //修改聊天室信息
+    /*
+        bool(true)
+     */
+    public function room_updateRoom($room_id,$name,$description,$maxusers = false){
+        $data = [
+            'room_id' => $room_id,
+            'name'    => $name,
+            'description'=>$description,
+        ];
+        if($maxusers !== false){
+            $data['maxusers'] = $maxusers;
+        }
+        $rs = $this->room->updateRoom($data);
+        return $rs;
+    }
 
     //删除聊天室
     //获取聊天室公告
     //修改聊天室公告
     //设置聊天室自定义属性
+
     //获取聊天室自定义属性
+    /*
+    array(14) {
+        ["seat4"] => string(3) "555"
+        ["seat3"] => string(3) "555"
+        ["seat2"] => string(3) "555"
+        ["seat1"] => string(3) "555"
+        ["seat8"] => string(3) "555"
+        ["sate1"] => string(1) "5"
+        ["seat7"] => string(3) "555"
+        ["sate2"] => string(1) "6"
+        ["seat6"] => string(3) "555"
+        ["seat5"] => string(3) "555"
+        ["sate3"] => string(1) "7"
+        ["seat9"] => string(3) "555"
+        ["sate4"] => string(1) "8"
+        ["seat10"] => string(3) "555"
+    }
+    */
+    public function room_getRoomCustomAttribute($room_id,$keys){
+        $rs = $this->room->getRoomCustomAttribute($room_id,$keys);
+        return $rs;
+    }
     //删除聊天室自定义属性
+
     //强制设置聊天室自定义属性
+    //初次设置新增,或修改都可用,一次请求的数组大小为10,可多次设置,共100个
+    /*
+        array(2) {
+          ["successKeys"] => array(10) {
+            [0] => string(5) "seat1"
+            [1] => string(5) "seat2"
+            [2] => string(5) "seat3"
+            [3] => string(5) "seat4"
+            [4] => string(5) "seat5"
+            [5] => string(5) "seat6"
+            [6] => string(5) "seat7"
+            [7] => string(5) "seat8"
+            [8] => string(5) "seat9"
+            [9] => string(6) "seat10"
+          }
+          ["errorKeys"] => array(0) {
+          }
+        }
+     */
+    public function room_setRoomCustomAttributeForced($room_id,$owner_id,$matedata){
+        $owner_id = ''.$owner_id.'';
+        $rs = $this->room->setRoomCustomAttributeForced($room_id,$owner_id,$matedata);
+        return $rs;
+    }
+
     //强制删除聊天室自定义属性
+    /*
+        array(2) {
+            ["successKeys"] => array(1) {
+                [0] => string(5) "sate1"  //被删除的keys
+            }
+            ["errorKeys"] => array(0) {
+            }
+        }
+        //再次删除同一个
+        array(2) {
+            ["successKeys"] => array(0) {
+            }
+            ["errorKeys"] => array(1) {
+                ["sate1"] => string(33) "properties key sate1 is not exist"
+            }
+        }
+    */
+    public function room_deleteRoomCustomAttributeForced($room_id,$owner_id,$keys){
+        $owner_id = ''.$owner_id.'';
+        $rs = $this->room->deleteRoomCustomAttributeForced($room_id,$owner_id,$keys);
+        return $rs;
+    }
 
     ///////////////管理聊天室成员//////////////////
 

+ 63 - 0
vendor/maniac/easemob-php/src/Room.php

@@ -414,6 +414,69 @@ final class Room
         return $data['data']['result'];
     }
 
+    //设置聊天室自定义属性
+
+    //获取聊天室自定义属性
+    /*
+        array(2) {
+            ["seat2"] => string(3) "555"
+            ["seat22"] => string(3) "555"
+        }
+    */
+    public function getRoomCustomAttribute($roomId,$keys){
+        if (!trim($roomId)) {
+            \Easemob\exception('Please pass the chat room ID');
+        }
+
+        $uri = $this->auth->getBaseUri() . '/metadata/chatroom/' . $roomId;
+        $body = compact('keys');
+        $resp = Http::post($uri, $body, $this->auth->headers());
+        if (!$resp->ok()) {
+            return \Easemob\error($resp);
+        }
+        $data = $resp->data();
+        return $data['data'];
+    }
+
+    //删除聊天室自定义属性
+
+    //强制设置聊天室自定义属性
+    public function setRoomCustomAttributeForced($roomId,$owner_uid, $metaData)
+    {
+        if (!trim($roomId)) {
+            \Easemob\exception('Please pass the chat room ID');
+        }
+
+        $uri = $this->auth->getBaseUri() . '/metadata/chatroom/' . $roomId . '/user/' . $owner_uid . '/forced';
+        $body = compact('metaData');
+        $resp = Http::put($uri, $body, $this->auth->headers());
+        //dump($resp);
+        if (!$resp->ok()) {
+            return \Easemob\error($resp);
+        }
+        $data = $resp->data();
+        return $data['data'];
+        return $data['data']['successKeys'];
+    }
+
+    //强制删除聊天室自定义属性
+    public function deleteRoomCustomAttributeForced($roomId,$owner_uid,$keys)
+    {
+        if (!trim($roomId)) {
+            \Easemob\exception('Please pass the chat room ID');
+        }
+
+        $uri = $this->auth->getBaseUri() . '/metadata/chatroom/' . $roomId . '/user/' . $owner_uid . '/forced';
+        $body = compact('keys');
+        $resp = Http::delete($uri, $body, $this->auth->headers());
+        if (!$resp->ok()) {
+            return \Easemob\error($resp);
+        }
+        $data = $resp->data();
+        return $data['data'];
+        return $data['data']['successKeys'];
+    }
+
     /**
      * \~chinese
      * \brief