├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── App ├── Easemob.php ├── EasemobError.php ├── EasemobGroups.php ├── EasemobMessages.php ├── EasemobRooms.php ├── ErrorMessage.php └── Http.php ├── EasemobServiceProvider.php ├── Facades └── Easemob.php └── config └── easemob.php /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea 3 | .DS_Store 4 | composer.lock -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 link1st 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # laravel-easemob 2 | 环信即时通讯laravel包开发,用于环信用户、群、聊天室等功能 3 | 4 | ## 安装 5 | 加载包 6 | 7 | `"link1st/laravel-easemob": "dev-master"` 8 | 9 | 或 10 | 11 | `composer require link1st/laravel-easemob` 12 | 13 | 在配置文件中添加 **config/app.php** 14 | 15 | ```php 16 | 'providers' => [ 17 | /** 18 | * 添加供应商 19 | */ 20 | link1st\Easemob\EasemobServiceProvider::class, 21 | ], 22 | 'aliases' => [ 23 | /** 24 | * 添加别名 25 | */ 26 | 'Easemob' => link1st\Easemob\Facades\Easemob::class, 27 | ], 28 | ``` 29 | 30 | 生成配置文件 31 | 32 | `php artisan vendor:publish` 33 | 34 | 设置环信的参数 **config/easemob.php** 35 | 36 | 37 | ## 使用 38 | - - - 39 | ### 获取token 40 | `\Easemob::getToken();` 41 | 42 | - - - 43 | ### 开放注册用户 44 | `$user = \Easemob::publicRegistration('xiaoming1');` 45 | 46 | ### 授权注册 同一个用户只能注册一次 47 | `$user = \Easemob::authorizationRegistration('xiaoming1');` 48 | 49 | ### 批量注册 50 | ```php 51 | $users = [ 52 | ['username'=>'xiaoming2','password'=>1], 53 | ['username'=>'xiaoming3','password'=>1], 54 | ]; 55 | $user = \Easemob::authorizationRegistrations($users); 56 | ``` 57 | 58 | - - - 59 | ### 获取用户 60 | `$user = \Easemob::getUser('xiaoming1');` 61 | 62 | ### 获取app所有的用户 63 | ```php 64 | $user = \Easemob::getUserAll(100,'LTgzNDAxMjM3OTprcFJFRUpzdUVlYWh5V1UwQjNSbldR'); 65 | ``` 66 | 67 | ### 删除用户 68 | `$user = \Easemob::delUser('xiaoming1');` 69 | 70 | ### 修改用户密码 71 | `$user = \Easemob::editUserPassword('xiaoming2',111);` 72 | 73 | ### 修改昵称 74 | `$user = \Easemob::editUserNickName('xiaoming2',11);` 75 | 76 | ### 强制用户下线 77 | `$user = \Easemob::disconnect('xiaoming2');` 78 | 79 | ### 添加好友 80 | `$user = \Easemob::addFriend('xiaoming2','xiaoming3');` 81 | 82 | ### 删除用户 83 | `$user = \Easemob::delFriend('xiaoming2','xiaoming3');` 84 | 85 | ### 显示用户好友 86 | `$user = \Easemob::showFriends('xiaoming2');` 87 | 88 | - - - 89 | ### 上传文件 90 | `\Easemob::uploadFile($file_path);` 91 | 92 | ### 下载文件 93 | `\Easemob::downloadFile($uuid, $share_secret);` 94 | 95 | - - - 96 | ### 发送文本消息 97 | `\Easemob::sendMessageText($users, $target_type = 'users', $message = "", $send_user = 'admin', $ext = []);` 98 | 99 | ### 发送图片消息 100 | `\Easemob::sendMessageImg($users, $target_type = 'users', $uuid, $share_secret, $file_name, $width = 480, $height = 720, $send_user = 'admin');` 101 | 102 | ### 发送语音消息 103 | `\Easemob::sendMessageAudio($users, $target_type = 'users', $uuid, $share_secret, $file_name, $length = 10, $send_user = 'admin');` 104 | 105 | ### 发送视频消息 106 | `\Easemob::sendMessageVideo($users, $target_type = 'users', $video_uuid, $video_share_secret, $video_file_name, $length = 10, $video_length = 58103, $img_uuid, $img_share_secret, $send_user = 'admin');` 107 | 108 | ### 消息透传 109 | `\Easemob::sendMessagePNS($users, $target_type = 'users', $action = "", $send_user = 'admin');` 110 | 111 | - - - 112 | ### 获取群信息 113 | `\Easemob::groups($group_ids);` 114 | 115 | ### 新建群 116 | `\Easemob::groupCreate($group_name, $group_description = '描述', $owner_user, $members_users = [], $is_public = true, $max_user = 200, $is_approval = true)` 117 | 118 | ### 修改群信息 119 | `\Easemob::groupEdit($group_id, $group_name = "", $group_description = "", $max_user = 0)` 120 | 121 | ### 删除群 122 | `\Easemob::groupDel($group_id)` 123 | 124 | ### 获取所有群成员 125 | `\Easemob::groupUsers($group_id)` 126 | 127 | ### 添加群成员 128 | `\Easemob::groupAddUsers($group_id, $users)` 129 | 130 | ### 删除群成员 131 | `\Easemob::groupDelUsers($group_id, $users)` 132 | 133 | ### 获取用户所以参加的群 134 | `\Easemob::userToGroups($user)` 135 | 136 | ### 群转让 137 | `\Easemob::groupTransfer($group_id, $new_owner_user)` 138 | 139 | - - - 140 | ### 查看聊天室详情 141 | `\Easemob::room($room_id)` 142 | 143 | ### 创建聊天室 144 | `\Easemob::roomCreate($room_name, $owner_name, $room_description = "描述", $max_user = 200, $member_users = [])` 145 | 146 | ### 删除聊天室 147 | `\Easemob::roomDel($room_id)` 148 | 149 | ### 修改聊天室信息 150 | `\Easemob::roomEdit($room_id, $room_name = "", $room_description = "", $max_user = 0)` 151 | 152 | ### 获取用户参加的聊天室 153 | `\Easemob::userToRooms($user)` 154 | 155 | ### 聊天室添加成员 156 | `\Easemob::roomAddUsers($room_id, $users)` 157 | 158 | 159 | ### 聊天室删除成员 160 | `\Easemob::roomDelUsers($room_id, $users)` 161 | 162 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "link1st/laravel-easemob", 3 | "description": "环信即时通讯laravel包开发,用于环信用户、群、聊天室等功能", 4 | "keywords": [ 5 | "link1st", 6 | "laravel", 7 | "easemob" 8 | ], 9 | "license": "MIT", 10 | "minimum-stability": "stable", 11 | "type": "library", 12 | "require": { 13 | "php": ">=5.6.4", 14 | "ext-curl": "*", 15 | "ext-json": "*" 16 | }, 17 | "authors": [ 18 | { 19 | "name": "link1st", 20 | "email": "link12stl@gmail.com" 21 | } 22 | ], 23 | "autoload": { 24 | "psr-4": { 25 | "link1st\\Easemob\\": "src/" 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/App/Easemob.php: -------------------------------------------------------------------------------- 1 | domain_name = Config::get('easemob.domain_name'); 48 | $this->org_name = Config::get('easemob.org_name'); 49 | $this->app_name = Config::get('easemob.app_name'); 50 | $this->client_id = Config::get('easemob.client_id'); 51 | $this->client_secret = Config::get('easemob.client_secret'); 52 | $this->token_cache_time = Config::get('easemob.token_cache_time'); 53 | $this->url = sprintf('%s/%s/%s/', $this->domain_name, $this->org_name, $this->app_name); 54 | } 55 | 56 | /*********************** 注册 **********************************/ 57 | 58 | /** 59 | * 开放注册用户 60 | * 61 | * @param $name [用户名] 62 | * @param string $password [密码] 63 | * @param string $nick_name [昵称] 64 | * 65 | * @return mixed 66 | * @throws EasemobError 67 | */ 68 | public function publicRegistration($name, $password = '', $nick_name = "") 69 | { 70 | $url = $this->url.'users'; 71 | $option = [ 72 | 'username' => $name, 73 | 'password' => $password, 74 | 'nickname' => $nick_name, 75 | ]; 76 | 77 | return Http::postCurl($url, $option, 0); 78 | } 79 | 80 | 81 | /** 82 | * 授权注册用户 83 | * 84 | * @param $name [用户名] 85 | * @param string $password [密码] 86 | * 87 | * @return mixed 88 | * @throws EasemobError 89 | */ 90 | public function authorizationRegistration($name, $password = '123456') 91 | { 92 | $url = $this->url.'users'; 93 | $option = [ 94 | 'username' => $name, 95 | 'password' => $password, 96 | ]; 97 | $access_token = $this->getToken(); 98 | $header [] = 'Authorization: Bearer '.$access_token; 99 | 100 | return Http::postCurl($url, $option, $header); 101 | } 102 | 103 | 104 | /** 105 | * 授权注册用户——批量 106 | * 密码不为空 107 | * 108 | * @param array $users [用户名 包含 username,password的数组] 109 | * 110 | * @return mixed 111 | * @throws EasemobError 112 | */ 113 | public function authorizationRegistrations($users) 114 | { 115 | $url = $this->url.'users'; 116 | $option = $users; 117 | $access_token = $this->getToken(); 118 | $header [] = 'Authorization: Bearer '.$access_token; 119 | 120 | return Http::postCurl($url, $option, $header); 121 | } 122 | 123 | /*********************** 用户操作 **********************************/ 124 | 125 | /** 126 | * 获取单个用户 127 | * 128 | * @param $user_name 129 | * 130 | * @return mixed 131 | * @throws EasemobError 132 | */ 133 | public function getUser($user_name) 134 | { 135 | $url = $this->url.'users/'.$user_name; 136 | $option = []; 137 | $access_token = $this->getToken(); 138 | $header [] = 'Authorization: Bearer '.$access_token; 139 | 140 | return Http::postCurl($url, $option, $header, 'GET'); 141 | } 142 | 143 | 144 | /** 145 | * 获取所有用户 146 | * 147 | * @param int $limit [显示条数] 148 | * @param string $cursor [光标,在此之后的数据] 149 | * 150 | * @return mixed 151 | * @throws EasemobError 152 | */ 153 | public function getUserAll($limit = 10, $cursor = '') 154 | { 155 | $url = $this->url.'users'; 156 | $option = [ 157 | 'limit' => $limit, 158 | 'cursor' => $cursor 159 | ]; 160 | $access_token = $this->getToken(); 161 | $header [] = 'Authorization: Bearer '.$access_token; 162 | 163 | return Http::postCurl($url, $option, $header, 'GET'); 164 | } 165 | 166 | 167 | /** 168 | * 删除用户 169 | * 删除一个用户会删除以该用户为群主的所有群组和聊天室 170 | * 171 | * @param $user_name 172 | * 173 | * @return mixed 174 | * @throws EasemobError 175 | */ 176 | public function delUser($user_name) 177 | { 178 | $url = $this->url.'users/'.$user_name; 179 | $option = []; 180 | $access_token = $this->getToken(); 181 | $header [] = 'Authorization: Bearer '.$access_token; 182 | 183 | return Http::postCurl($url, $option, $header, 'DELETE'); 184 | } 185 | 186 | 187 | /** 188 | * 修改密码 189 | * 190 | * @param $user_name 191 | * @param $new_password [新密码] 192 | * 193 | * @return mixed 194 | * @throws EasemobError 195 | */ 196 | public function editUserPassword($user_name, $new_password) 197 | { 198 | $url = $this->url.'users/'.$user_name.'/password'; 199 | $option = [ 200 | 'newpassword' => $new_password 201 | ]; 202 | $access_token = $this->getToken(); 203 | $header [] = 'Authorization: Bearer '.$access_token; 204 | 205 | return Http::postCurl($url, $option, $header, 'PUT'); 206 | } 207 | 208 | 209 | /** 210 | * 修改用户昵称 211 | * 只能在后台看到,前端无法看见这个昵称 212 | * 213 | * @param $user_name 214 | * @param $nickname 215 | * 216 | * @return mixed 217 | * @throws EasemobError 218 | */ 219 | public function editUserNickName($user_name, $nickname) 220 | { 221 | $url = $this->url.'users/'.$user_name; 222 | $option = [ 223 | 'nickname' => $nickname 224 | ]; 225 | $access_token = $this->getToken(); 226 | $header [] = 'Authorization: Bearer '.$access_token; 227 | 228 | return Http::postCurl($url, $option, $header, 'PUT'); 229 | } 230 | 231 | 232 | /** 233 | * 强制用户下线 234 | * 235 | * @param $user_name 236 | * 237 | * @return mixed 238 | * @throws EasemobError 239 | */ 240 | public function disconnect($user_name) 241 | { 242 | $url = $this->url.'users/'.$user_name.'/disconnect'; 243 | $option = []; 244 | $access_token = $this->getToken(); 245 | $header [] = 'Authorization: Bearer '.$access_token; 246 | 247 | return Http::postCurl($url, $option, $header, 'GET'); 248 | } 249 | 250 | 251 | /*********************** 好友操作 **********************************/ 252 | 253 | /** 254 | * 给用户添加好友 255 | * 256 | * @param $owner_username [主人] 257 | * @param $friend_username [朋友] 258 | * 259 | * @return mixed 260 | * @throws EasemobError 261 | */ 262 | public function addFriend($owner_username, $friend_username) 263 | { 264 | $url = $this->url.'users/'.$owner_username.'/contacts/users/'.$friend_username; 265 | $option = []; 266 | $access_token = $this->getToken(); 267 | $header [] = 'Authorization: Bearer '.$access_token; 268 | 269 | return Http::postCurl($url, $option, $header, 'POST'); 270 | } 271 | 272 | 273 | /** 274 | * 给用户删除好友 275 | * 276 | * @param $owner_username [主人] 277 | * @param $friend_username [朋友] 278 | * 279 | * @return mixed 280 | * @throws EasemobError 281 | */ 282 | public function delFriend($owner_username, $friend_username) 283 | { 284 | $url = $this->url.'users/'.$owner_username.'/contacts/users/'.$friend_username; 285 | $option = []; 286 | $access_token = $this->getToken(); 287 | $header [] = 'Authorization: Bearer '.$access_token; 288 | 289 | return Http::postCurl($url, $option, $header, 'DELETE'); 290 | } 291 | 292 | 293 | /** 294 | * 查看用户所以好友 295 | * 296 | * @param $user_name 297 | * 298 | * @return mixed 299 | * @throws EasemobError 300 | */ 301 | public function showFriends($user_name) 302 | { 303 | $url = $this->url.'users/'.$user_name.'/contacts/users/'; 304 | $option = []; 305 | $access_token = $this->getToken(); 306 | $header [] = 'Authorization: Bearer '.$access_token; 307 | 308 | return Http::postCurl($url, $option, $header, 'GET'); 309 | } 310 | 311 | /*********************** 文件上传下载 **********************************/ 312 | 313 | /** 314 | * 上传文件 315 | * 316 | * @param $file_path 317 | * 318 | * @return mixed 319 | * @throws EasemobError 320 | */ 321 | public function uploadFile($file_path) 322 | { 323 | if ( ! is_file($file_path)) { 324 | throw new EasemobError('文件不存在', 404); 325 | } 326 | $url = $this->url.'chatfiles'; 327 | 328 | $curl_file = curl_file_create($file_path); 329 | $option = [ 330 | 'file' => $curl_file, 331 | ]; 332 | $access_token = $this->getToken(); 333 | $header [] = 'Authorization: Bearer '.$access_token; 334 | 335 | return Http::postCurl($url, $option, $header, 'POST'); 336 | } 337 | 338 | 339 | /** 340 | * 下载文件 341 | * 342 | * @param $uuid [uuid] 343 | * @param $share_secret [秘钥] 344 | * 345 | * @return mixed 346 | * @throws EasemobError 347 | */ 348 | public function downloadFile($uuid, $share_secret) 349 | { 350 | $url = $this->url.'chatfiles/'.$uuid; 351 | 352 | $option = []; 353 | $access_token = $this->getToken(); 354 | $header [] = 'Authorization: Bearer '.$access_token; 355 | $header [] = 'share-secret: '.$share_secret; 356 | 357 | return Http::postCurl($url, $option, $header, 'GET', 10, false); 358 | } 359 | 360 | 361 | /*********************** token操作 **********************************/ 362 | 363 | /** 364 | * 返回token 365 | * 366 | * @return mixed 367 | * @throws EasemobError 368 | */ 369 | public function getToken() 370 | { 371 | if (Cache::has(self::CACHE_NAME)) { 372 | return Cache::get(self::CACHE_NAME); 373 | } else { 374 | $url = $this->url."token"; 375 | $option = [ 376 | 'grant_type' => 'client_credentials', 377 | 'client_id' => $this->client_id, 378 | 'client_secret' => $this->client_secret, 379 | ]; 380 | $return = Http::postCurl($url, $option); 381 | Cache::put(self::CACHE_NAME, $return['access_token'], (int) ($return['expires_in'] / 60)); 382 | 383 | return $return['access_token']; 384 | 385 | } 386 | } 387 | 388 | /** 389 | * 字符串替换 390 | * 391 | * @param $string 392 | * 393 | * @return mixed 394 | */ 395 | protected static function stringReplace($string) 396 | { 397 | $string = str_replace('\\', '', $string); 398 | $string = str_replace(' ', '+', $string); 399 | 400 | return $string; 401 | } 402 | 403 | } 404 | -------------------------------------------------------------------------------- /src/App/EasemobError.php: -------------------------------------------------------------------------------- 1 | url.'chatgroups/'.implode(',', $group_ids); 25 | $option = []; 26 | $access_token = $this->getToken(); 27 | $header [] = 'Authorization: Bearer '.$access_token; 28 | 29 | return Http::postCurl($url, $option, $header, 'GET'); 30 | } 31 | 32 | 33 | /** 34 | * 创建群 35 | * 36 | * @param string $group_name [群名称] 37 | * @param string $group_description [群描述] 38 | * @param string $owner_user [群主] 39 | * @param array $members_users [成员] 40 | * @param bool $is_public [是否为公开群] 41 | * @param int $max_user [最大人数] 42 | * @param bool $is_approval [加群是否要批准] 43 | * 44 | * @return mixed 45 | * @throws EasemobError 46 | */ 47 | public function groupCreate($group_name, $group_description = '描述', $owner_user, $members_users = [], $is_public = true, $max_user = 200, $is_approval = true) 48 | { 49 | $url = $this->url.'chatgroups'; 50 | $option = [ 51 | "groupname" => self::stringReplace($group_name), 52 | "desc" => self::stringReplace($group_description), 53 | "owner" => $owner_user, 54 | "public" => $is_public, 55 | "maxusers" => $max_user, 56 | "approval" => $is_approval, 57 | ]; 58 | if ( ! empty($members_users)) { 59 | $option['members'] = $members_users; 60 | } 61 | $access_token = $this->getToken(); 62 | $header [] = 'Authorization: Bearer '.$access_token; 63 | 64 | return Http::postCurl($url, $option, $header, 'POST'); 65 | } 66 | 67 | 68 | /** 69 | * 修改群信息 70 | * 71 | * @param string $group_id 72 | * @param string $group_name 73 | * @param string $group_description 74 | * @param int $max_user 75 | * 76 | * @return mixed 77 | * @throws EasemobError 78 | */ 79 | public function groupEdit($group_id, $group_name = "", $group_description = "", $max_user = 0) 80 | { 81 | $url = $this->url.'chatgroups/'.$group_id; 82 | $option = [ 83 | "groupname" => self::stringReplace($group_name), 84 | "description" => self::stringReplace($group_description), 85 | "maxusers" => $max_user, 86 | ]; 87 | $option = array_filter($option); 88 | if (empty($option)) { 89 | throw new EasemobError('提交修改的参数,不修改提交空!'); 90 | } 91 | 92 | $access_token = $this->getToken(); 93 | $header [] = 'Authorization: Bearer '.$access_token; 94 | 95 | return Http::postCurl($url, $option, $header, 'PUT'); 96 | } 97 | 98 | 99 | /** 100 | * 删除群 101 | * 102 | * @param string $group_id 103 | * 104 | * @return mixed 105 | * @throws EasemobError 106 | */ 107 | public function groupDel($group_id) 108 | { 109 | $url = $this->url.'chatgroups/'.$group_id; 110 | $option = []; 111 | $access_token = $this->getToken(); 112 | $header [] = 'Authorization: Bearer '.$access_token; 113 | 114 | return Http::postCurl($url, $option, $header, 'DELETE'); 115 | } 116 | 117 | 118 | /** 119 | * 获取所有的群成员 120 | * 121 | * @param $group_id 122 | * 123 | * @return mixed 124 | * @throws EasemobError 125 | */ 126 | public function groupUsers($group_id) 127 | { 128 | $url = $this->url.'chatgroups/'.$group_id.'/users'; 129 | $option = []; 130 | $access_token = $this->getToken(); 131 | $header [] = 'Authorization: Bearer '.$access_token; 132 | 133 | return Http::postCurl($url, $option, $header, 'GET'); 134 | } 135 | 136 | 137 | /** 138 | * 添加群成员——批量 139 | * 140 | * @param string $group_id 141 | * @param array $users [用户名称 数组] 142 | * 143 | * @return mixed 144 | * @throws EasemobError 145 | */ 146 | public function groupAddUsers($group_id, $users) 147 | { 148 | if (count($users) >= 60 || count($users) < 1) { 149 | throw new EasemobError('一次最多可以添加60位成员,最少为1个'); 150 | } 151 | 152 | $url = $this->url.'chatgroups/'.$group_id.'/users'; 153 | $option = [ 154 | 'usernames' => $users 155 | ]; 156 | $access_token = $this->getToken(); 157 | $header [] = 'Authorization: Bearer '.$access_token; 158 | 159 | return Http::postCurl($url, $option, $header, 'POST'); 160 | } 161 | 162 | 163 | /** 164 | * 删除群成员——批量 165 | * 群主删除 必须先转让群 166 | * 167 | * @param string $group_id 168 | * @param array $users [用户名称 数组] 169 | * 170 | * @return mixed 171 | * @throws EasemobError 172 | */ 173 | public function groupDelUsers($group_id, $users) 174 | { 175 | if (empty($users) || ! is_array($users)) { 176 | throw new EasemobError('删除的用户不存在,或者提交参数不为数组!'); 177 | } 178 | 179 | $url = $this->url.'chatgroups/'.$group_id.'/users/'.implode(',', $users); 180 | $option = []; 181 | $access_token = $this->getToken(); 182 | $header [] = 'Authorization: Bearer '.$access_token; 183 | 184 | return Http::postCurl($url, $option, $header, 'DELETE'); 185 | } 186 | 187 | 188 | /** 189 | * 获取用户所有参加的群 190 | * 191 | * @param $user 192 | * 193 | * @return mixed 194 | * @throws EasemobError 195 | */ 196 | public function userToGroups($user) 197 | { 198 | $url = $this->url.'users/'.$user.'/joined_chatgroups'; 199 | $option = []; 200 | $access_token = $this->getToken(); 201 | $header [] = 'Authorization: Bearer '.$access_token; 202 | 203 | return Http::postCurl($url, $option, $header, 'GET'); 204 | } 205 | 206 | 207 | /** 208 | * 群转让 209 | * 210 | * @param $group_id [群Id] 211 | * @param $new_owner_user [新的群主] 212 | * 213 | * @return mixed 214 | * @throws EasemobError 215 | */ 216 | public function groupTransfer($group_id, $new_owner_user) 217 | { 218 | $url = $this->url.'chatgroups/'.$group_id; 219 | $option = [ 220 | 'newowner' => $new_owner_user 221 | ]; 222 | $access_token = $this->getToken(); 223 | $header [] = 'Authorization: Bearer '.$access_token; 224 | 225 | return Http::postCurl($url, $option, $header, 'PUT'); 226 | } 227 | 228 | } -------------------------------------------------------------------------------- /src/App/EasemobMessages.php: -------------------------------------------------------------------------------- 1 | target_array)) { 28 | throw new EasemobError('target_type 参数错误!'); 29 | } 30 | 31 | $url = $this->url.'messages'; 32 | $option = [ 33 | 'target_type' => $target_type, 34 | 'target' => $users, 35 | 'msg' => [ 36 | 'type' => 'txt', 37 | 'msg' => $message 38 | ], 39 | 'from' => $send_user 40 | ]; 41 | 42 | // 是否有消息扩展 43 | if ( ! empty($ext)) { 44 | $option['ext'] = $ext; 45 | } 46 | 47 | $access_token = $this->getToken(); 48 | $header [] = 'Authorization: Bearer '.$access_token; 49 | 50 | return Http::postCurl($url, $option, $header, 'POST'); 51 | } 52 | 53 | 54 | /** 55 | * 发送图片消息 56 | * 57 | * @param array $users [接收的对象数组] 58 | * @param string $target_type [类型] 59 | * @param string $uuid [文件的uuid] 60 | * @param string $share_secret [文件的秘钥 上传后生产] 61 | * @param string $file_name [指定文件名] 62 | * @param int $width [宽] 63 | * @param int $height [高] 64 | * @param string $send_user 65 | * 66 | * @return mixed 67 | * @throws EasemobError 68 | */ 69 | public function sendMessageImg($users, $target_type = 'users', $uuid, $share_secret, $file_name, $width = 480, $height = 720, $send_user = 'admin') 70 | { 71 | if ( ! in_array($target_type, $this->target_array)) { 72 | throw new EasemobError('target_type 参数错误!'); 73 | } 74 | 75 | $url = $this->url.'messages'; 76 | $option = [ 77 | 'target_type' => $target_type, 78 | 'target' => $users, 79 | 'msg' => [ 80 | 'type' => 'img', 81 | 'url' => $this->url.'chatfiles/'.$uuid, 82 | 'filename' => $file_name, 83 | 'secret' => $share_secret, 84 | 'size' => [ 85 | 'width' => $width, 86 | 'height' => $height 87 | ] 88 | ], 89 | 'from' => $send_user 90 | ]; 91 | $access_token = $this->getToken(); 92 | $header [] = 'Authorization: Bearer '.$access_token; 93 | 94 | return Http::postCurl($url, $option, $header, 'POST'); 95 | } 96 | 97 | 98 | /** 99 | * 发送语音消息 100 | * 101 | * @param array $users [接收的对象数组] 102 | * @param string $target_type [类型] 103 | * @param string $uuid [文件的uuid] 104 | * @param string $share_secret [文件的秘钥 上传后生产] 105 | * @param string $file_name [指定文件名] 106 | * @param int $length [长度] 107 | * @param string $send_user 108 | * 109 | * @return mixed 110 | * @throws EasemobError 111 | */ 112 | public function sendMessageAudio($users, $target_type = 'users', $uuid, $share_secret, $file_name, $length = 10, $send_user = 'admin') 113 | { 114 | if ( ! in_array($target_type, $this->target_array)) { 115 | throw new EasemobError('target_type 参数错误!'); 116 | } 117 | 118 | $url = $this->url.'messages'; 119 | $option = [ 120 | 'target_type' => $target_type, 121 | 'target' => $users, 122 | 'msg' => [ 123 | 'type' => 'audio', 124 | 'url' => $this->url.'chatfiles/'.$uuid, 125 | 'filename' => $file_name, 126 | 'secret' => $share_secret, 127 | 'length' => $length 128 | ], 129 | 'from' => $send_user 130 | ]; 131 | $access_token = $this->getToken(); 132 | $header [] = 'Authorization: Bearer '.$access_token; 133 | 134 | return Http::postCurl($url, $option, $header, 'POST'); 135 | } 136 | 137 | /** 138 | * 发送视频消息 139 | * 140 | * @param array $users 141 | * @param string $target_type [类型] 142 | * @param string $uuid [文件的uuid] 143 | * @param string $share_secret [文件的秘钥 上传后生产] 144 | * @param string $file_name [指定文件名] 145 | * @param int $length [长度] 146 | * @param string $send_user 147 | * 148 | * @return mixed 149 | * @throws EasemobError 150 | */ 151 | /** 152 | * 发送视频消息 153 | * 154 | * @param array $users [接收的对象数组] 155 | * @param string $target_type [类型] 156 | * @param $video_uuid [视频uuid] 157 | * @param $video_share_secret [视频秘钥] 158 | * @param $video_file_name [下载的时候视频名称] 159 | * @param int $length [长度] 160 | * @param int $video_length [视频大小] 161 | * @param $img_uuid [缩略图] 162 | * @param $img_share_secret [图片秘钥] 163 | * @param string $send_user [发送者] 164 | * 165 | * @return mixed 166 | * @throws EasemobError 167 | */ 168 | public function sendMessageVideo($users, $target_type = 'users', $video_uuid, $video_share_secret, $video_file_name, $length = 10, $video_length = 58103, $img_uuid, $img_share_secret, $send_user = 'admin') 169 | { 170 | if ( ! in_array($target_type, $this->target_array)) { 171 | throw new EasemobError('target_type 参数错误!'); 172 | } 173 | 174 | $url = $this->url.'messages'; 175 | $option = [ 176 | 'target_type' => $target_type, 177 | 'target' => $users, 178 | 'msg' => [ 179 | 'type' => 'video', 180 | 'url' => $this->url.'chatfiles/'.$video_uuid, 181 | 'filename' => $video_file_name, 182 | 'thumb_secret' => $video_share_secret, 183 | 'length' => $length, 184 | 'file_length' => $video_length, 185 | 'thumb' => $this->url.'chatfiles/'.$img_uuid, 186 | 'secret' => $img_share_secret 187 | ], 188 | 'from' => $send_user 189 | ]; 190 | $access_token = $this->getToken(); 191 | $header [] = 'Authorization: Bearer '.$access_token; 192 | 193 | return Http::postCurl($url, $option, $header, 'POST'); 194 | } 195 | 196 | 197 | /** 198 | * 消息透传 199 | * 200 | * @param array $users [接收的对象数组] 201 | * @param string $target_type [类型] 202 | * @param string $action [内容] 203 | * @param string $send_user [消息发送者] 204 | * 205 | * @return mixed 206 | * @throws EasemobError 207 | */ 208 | public function sendMessagePNS($users, $target_type = 'users', $action = "", $send_user = 'admin') 209 | { 210 | if ( ! in_array($target_type, $this->target_array)) { 211 | throw new EasemobError('target_type 参数错误!'); 212 | } 213 | 214 | $url = $this->url.'messages'; 215 | $option = [ 216 | 'target_type' => $target_type, 217 | 'target' => $users, 218 | 'msg' => [ 219 | 'type' => 'cmd', 220 | 'action' => $action 221 | ], 222 | 'from' => $send_user 223 | ]; 224 | $access_token = $this->getToken(); 225 | $header [] = 'Authorization: Bearer '.$access_token; 226 | 227 | return Http::postCurl($url, $option, $header, 'POST'); 228 | } 229 | 230 | /** 231 | * 获取用户离线消息数 232 | * 233 | * @param $user_name 234 | * 235 | * @return mixed 236 | * @throws EasemobError 237 | */ 238 | public function offlineMsgCount($user_name) 239 | { 240 | $url = $this->url.'users/'.$user_name.'/offline_msg_count'; 241 | $option = []; 242 | $access_token = $this->getToken(); 243 | $header [] = 'Authorization: Bearer '.$access_token; 244 | 245 | return Http::postCurl($url, $option, $header, 'GET'); 246 | } 247 | } -------------------------------------------------------------------------------- /src/App/EasemobRooms.php: -------------------------------------------------------------------------------- 1 | url.'chatrooms/'.$room_id; 24 | $option = []; 25 | $access_token = $this->getToken(); 26 | $header [] = 'Authorization: Bearer '.$access_token; 27 | 28 | return Http::postCurl($url, $option, $header, 'GET'); 29 | } 30 | 31 | 32 | /** 33 | * 创建聊天室 34 | * 35 | * @param $room_name 36 | * @param $owner_name 37 | * @param string $room_description 38 | * @param int $max_user 39 | * @param array $member_users 40 | * 41 | * @return mixed 42 | * @throws EasemobError 43 | */ 44 | public function roomCreate($room_name, $owner_name, $room_description = "描述", $max_user = 200, $member_users = []) 45 | { 46 | $url = $this->url.'chatrooms'; 47 | $option = [ 48 | 'name' => $room_name, 49 | 'description' => $room_description, 50 | 'maxusers' => $max_user, 51 | 'owner' => $owner_name, 52 | ]; 53 | if ( ! empty($member_users)) { 54 | $option['members'] = $member_users; 55 | } 56 | $access_token = $this->getToken(); 57 | $header [] = 'Authorization: Bearer '.$access_token; 58 | 59 | return Http::postCurl($url, $option, $header, 'POST'); 60 | } 61 | 62 | 63 | /** 64 | * 删除聊天室 65 | * 66 | * @param $room_id 67 | * 68 | * @return mixed 69 | * @throws EasemobError 70 | */ 71 | public function roomDel($room_id) 72 | { 73 | $url = $this->url.'chatrooms/'.$room_id; 74 | $option = []; 75 | $access_token = $this->getToken(); 76 | $header [] = 'Authorization: Bearer '.$access_token; 77 | 78 | return Http::postCurl($url, $option, $header, 'DELETE'); 79 | } 80 | 81 | 82 | /** 83 | * 修改聊天室信息 84 | * 85 | * @param $room_id 86 | * @param string $room_name 87 | * @param string $room_description 88 | * @param int $max_user 89 | * 90 | * @return mixed 91 | * @throws EasemobError 92 | */ 93 | public function roomEdit($room_id, $room_name = "", $room_description = "", $max_user = 0) 94 | { 95 | $url = $this->url.'chatgroups/'.$room_id; 96 | $option = [ 97 | "name" => self::stringReplace($room_name), 98 | "description" => self::stringReplace($room_description), 99 | "maxusers" => $max_user, 100 | ]; 101 | $option = array_filter($option); 102 | if (empty($option)) { 103 | throw new EasemobError('提交修改的参数,不修改提交空!'); 104 | } 105 | 106 | $access_token = $this->getToken(); 107 | $header [] = 'Authorization: Bearer '.$access_token; 108 | 109 | return Http::postCurl($url, $option, $header, 'PUT'); 110 | } 111 | 112 | 113 | /** 114 | * 获取用户所有参加的聊天室 115 | * 116 | * @param $user 117 | * 118 | * @return mixed 119 | * @throws EasemobError 120 | */ 121 | public function userToRooms($user) 122 | { 123 | $url = $this->url.'users/'.$user.'/joined_chatrooms'; 124 | $option = []; 125 | $access_token = $this->getToken(); 126 | $header [] = 'Authorization: Bearer '.$access_token; 127 | 128 | return Http::postCurl($url, $option, $header, 'GET'); 129 | } 130 | 131 | 132 | /** 133 | * 聊天室添加成员——批量 134 | * 135 | * @param string $room_id 136 | * @param array $users 137 | * 138 | * @return mixed 139 | * @throws EasemobError 140 | */ 141 | public function roomAddUsers($room_id, $users) 142 | { 143 | $url = $this->url.'chatrooms/'.$room_id.'/users'; 144 | $option = [ 145 | 'usernames' => $users 146 | ]; 147 | $access_token = $this->getToken(); 148 | $header [] = 'Authorization: Bearer '.$access_token; 149 | 150 | return Http::postCurl($url, $option, $header, 'POST'); 151 | } 152 | 153 | 154 | /** 155 | * 聊天室删除成员——批量 156 | * 157 | * @param string $room_id 158 | * @param array $users 159 | * 160 | * @return mixed 161 | * @throws EasemobError 162 | */ 163 | public function roomDelUsers($room_id, $users) 164 | { 165 | $url = $this->url.'chatrooms/'.$room_id.'/users/'.implode(',', $users); 166 | $option = []; 167 | $access_token = $this->getToken(); 168 | $header [] = 'Authorization: Bearer '.$access_token; 169 | 170 | return Http::postCurl($url, $option, $header, 'DELETE'); 171 | } 172 | } -------------------------------------------------------------------------------- /src/App/ErrorMessage.php: -------------------------------------------------------------------------------- 1 | publishes([ 31 | __DIR__.'/config/easemob.php' => config_path('easemob.php'), 32 | ]); 33 | 34 | } 35 | 36 | 37 | /** 38 | * 默认包位置 39 | * 40 | * Register the service provider. 41 | * 42 | * @return void 43 | */ 44 | public function register() 45 | { 46 | // 将给定配置文件合现配置文件接合 47 | $this->mergeConfigFrom( 48 | __DIR__.'/config/easemob.php', 'easemob' 49 | ); 50 | 51 | // 容器绑定 52 | $this->app->bind('Easemob', function () { 53 | return new Easemob(); 54 | }); 55 | } 56 | 57 | 58 | /** 59 | * Get the services provided by the provider. 60 | * 61 | * @return array 62 | */ 63 | public function provides() 64 | { 65 | return []; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/Facades/Easemob.php: -------------------------------------------------------------------------------- 1 | env('DOMAIN_NAME', 'https://a1.easemob.com'), 14 | 15 | /* 16 | |-------------------------------------------------------------------------- 17 | | 企业的唯一标识 18 | |-------------------------------------------------------------------------- 19 | | 20 | | 开发者在环信开发者管理后台注册账号时填写的企业 ID 21 | | AppKey 后,会得到一个 xxxx#xxxx 格式的字符串,org_name 对应#前面部分 22 | */ 23 | 24 | 'org_name' => env('ORG_NAME', ''), 25 | 26 | /* 27 | |-------------------------------------------------------------------------- 28 | | “APP”唯一标识 29 | |-------------------------------------------------------------------------- 30 | | 31 | | 开发者在环信开发者管理后台创建应用时填写的“应用名称” 32 | | AppKey 后,会得到一个 xxxx#xxxx 格式的字符串,app_name 对应#后面部分 33 | */ 34 | 35 | 'app_name' => env('APP_NAME', ''), 36 | 37 | /* 38 | |-------------------------------------------------------------------------- 39 | | 客户ID 40 | |-------------------------------------------------------------------------- 41 | | 42 | */ 43 | 44 | 'client_id' => env('CLIENT_ID', ''), 45 | 46 | /* 47 | |-------------------------------------------------------------------------- 48 | | 客户秘钥 49 | |-------------------------------------------------------------------------- 50 | | 51 | */ 52 | 53 | 'client_secret' => env('CLIENT_SECRET', ''), 54 | 55 | ]; 56 | --------------------------------------------------------------------------------