Skip to content

Commit 7129f8b

Browse files
author
link
committed
用户体系接口的完成
1 parent 9f522e4 commit 7129f8b

File tree

5 files changed

+275
-39
lines changed

5 files changed

+275
-39
lines changed

src/EasemobServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use Illuminate\Support\ServiceProvider;
44

5+
use link1st\Easemob\App\Easemob;
6+
57
class EasemobServiceProvider extends ServiceProvider {
68

79
/**

src/app/Easemob.php

Lines changed: 232 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace link1st\Easemob;
2+
namespace link1st\Easemob\App;
33

44
use Config;
55
use Cache;
@@ -28,6 +28,9 @@ class Easemob
2828
// token缓存时间
2929
public $token_cache_time = null;
3030

31+
// url地址
32+
public $url = null;
33+
3134

3235
public function __construct()
3336
{
@@ -37,53 +40,253 @@ public function __construct()
3740
$this->client_id = Config::get('easemob.client_id');
3841
$this->client_secret = Config::get('easemob.client_secret');
3942
$this->token_cache_time = Config::get('easemob.token_cache_time');
43+
$this->url = sprintf('%s/%s/%s/', $this->domain_name, $this->org_name, $this->app_name);
4044
}
4145

46+
/*********************** 注册 **********************************/
4247

4348
/**
44-
* 获取配置项
45-
* @return int
49+
* 开放注册用户
50+
*
51+
* @param $name [用户名]
52+
* @param string $password [密码]
53+
* @param string $nick_name [昵称]
54+
*
55+
* @return mixed
4656
*/
47-
function get_config()
57+
public function publicRegistration($name, $password = '', $nick_name = "")
4858
{
49-
return Config::get('easemob.EASEMOB_DOMAIN', "");
59+
$url = $this->url.'users';
60+
$option = [
61+
'username' => $name,
62+
'password' => $password,
63+
'nickname' => $nick_name,
64+
];
65+
66+
return Http::postCurl($url, $option, 0);
5067
}
5168

5269

5370
/**
54-
* 默认
71+
* 授权注册用户
72+
*
73+
* @param $name [用户名]
74+
* @param string $password [密码]
75+
* @param string $nick_name [昵称]
76+
*
5577
* @return mixed
5678
*/
57-
public function index()
79+
public function authorizationRegistration($name, $password = '123456')
5880
{
59-
return 'index';
81+
$url = $this->url.'users';
82+
$option = [
83+
'username' => $name,
84+
'password' => $password,
85+
];
86+
$access_token = $this->getToken();
87+
$header [] = 'Authorization: Bearer '.$access_token;
88+
89+
return Http::postCurl($url, $option, $header);
6090
}
6191

6292

63-
public function getToken()
93+
/**
94+
* 授权注册用户——批量
95+
* 密码不为空
96+
*
97+
* @param array $users [用户名 包含 username,password的数组]
98+
*
99+
* @return mixed
100+
*/
101+
public function authorizationRegistrations($users)
64102
{
65-
$url = $this->domain_name."/easemob-demo/chatdemoui/token";
66-
$option = [
67-
'grant_type' => 'client_credentials',
68-
'client_id' => $this->client_id,
69-
'client_secret' => $this->client_secret,
103+
$url = $this->url.'users';
104+
$option = $users;
105+
$access_token = $this->getToken();
106+
$header [] = 'Authorization: Bearer '.$access_token;
107+
108+
return Http::postCurl($url, $option, $header);
109+
}
110+
111+
/*********************** 用户操作 **********************************/
112+
113+
/**
114+
* 获取单个用户
115+
*
116+
* @param $user_name
117+
*
118+
* @return mixed
119+
*/
120+
public function getUser($user_name)
121+
{
122+
$url = $this->url.'users/'.$user_name;
123+
$option = [];
124+
$access_token = $this->getToken();
125+
$header [] = 'Authorization: Bearer '.$access_token;
126+
127+
return Http::postCurl($url, $option, $header, 'GET');
128+
}
129+
130+
131+
/**
132+
* 获取所有用户
133+
*
134+
* @param int $limit [显示条数]
135+
* @param string $cursor [光标,在此之后的数据]
136+
*
137+
* @return mixed
138+
*/
139+
public function getUserAll($limit = 10, $cursor = '')
140+
{
141+
$url = $this->url.'users';
142+
$option = [
143+
'limit' => $limit,
144+
'cursor' => $cursor
145+
];
146+
$access_token = $this->getToken();
147+
$header [] = 'Authorization: Bearer '.$access_token;
148+
149+
return Http::postCurl($url, $option, $header, 'GET');
150+
}
151+
152+
153+
/**
154+
* 删除用户
155+
* 删除一个用户会删除以该用户为群主的所有群组和聊天室
156+
*
157+
* @param $user_name
158+
*
159+
* @return mixed
160+
*/
161+
public function delUser($user_name)
162+
{
163+
$url = $this->url.'users/'.$user_name;
164+
$option = [];
165+
$access_token = $this->getToken();
166+
$header [] = 'Authorization: Bearer '.$access_token;
167+
168+
return Http::postCurl($url, $option, $header, 'DELETE');
169+
}
170+
171+
172+
/**
173+
* 修改密码
174+
*
175+
* @param $user_name
176+
* @param $new_password [新密码]
177+
*
178+
* @return mixed
179+
*/
180+
public function editUserPassword($user_name, $new_password)
181+
{
182+
$url = $this->url.'users/'.$user_name.'/password';
183+
$option = [
184+
'newpassword' => $password
185+
];
186+
$access_token = $this->getToken();
187+
$header [] = 'Authorization: Bearer '.$access_token;
188+
189+
return Http::postCurl($url, $option, $header, 'PUT');
190+
}
191+
192+
193+
/**
194+
* 修改用户昵称
195+
* 只能在后台看到,前端无法看见这个昵称
196+
*
197+
* @param $user_name
198+
* @param $nickname
199+
*
200+
* @return mixed
201+
*/
202+
public function editUserNickName($user_name, $nickname)
203+
{
204+
$url = $this->url.'users/'.$user_name;
205+
$option = [
206+
'nickname' => $nickname
70207
];
71-
$return = Http::postCurl($url,$option);
72-
dump($return);
73-
return $return->access_token;
74-
//// 获取token
75-
//$token = Cache::remember(self::CACHE_NAME, $this->token_cache_time, function () {
76-
// $url = $this->domain_name."/easemob-demo/chatdemoui/token";
77-
// $option = [
78-
// 'grant_type' => 'client_credentials',
79-
// 'client_id' => $this->client_id,
80-
// 'client_secret' => $this->client_secret,
81-
// ];
82-
// $return = Http::postCurl($url,$option);
83-
// dump($return);
84-
// return $return->access_token;
85-
//});
208+
$access_token = $this->getToken();
209+
$header [] = 'Authorization: Bearer '.$access_token;
210+
211+
return Http::postCurl($url, $option, $header, 'PUT');
212+
}
213+
214+
215+
/*********************** 好友操作 **********************************/
216+
217+
/**
218+
* 给用户添加好友
219+
* @param $owner_username [主人]
220+
* @param $friend_username [朋友]
221+
*
222+
* @return mixed
223+
*/
224+
public function addFriend($owner_username, $friend_username)
225+
{
226+
$url = $this->url.'users/'.$owner_username.'/contacts/users/'.$friend_username;
227+
$option = [];
228+
$access_token = $this->getToken();
229+
$header [] = 'Authorization: Bearer '.$access_token;
230+
231+
return Http::postCurl($url, $option, $header, 'POST');
232+
}
233+
234+
/**
235+
* 给用户删除好友
236+
* @param $owner_username [主人]
237+
* @param $friend_username [朋友]
238+
*
239+
* @return mixed
240+
*/
241+
public function delFriend($owner_username, $friend_username)
242+
{
243+
$url = $this->url.'users/'.$owner_username.'/contacts/users/'.$friend_username;
244+
$option = [];
245+
$access_token = $this->getToken();
246+
$header [] = 'Authorization: Bearer '.$access_token;
247+
248+
return Http::postCurl($url, $option, $header, 'DELETE');
249+
}
250+
251+
/**
252+
* 查看用户所以好友
253+
* @param $user_name
254+
*
255+
* @return mixed
256+
*/
257+
public function showFriends($user_name){
258+
$url = $this->url.'users/'.$user_name.'/contacts/users/';
259+
$option = [];
260+
$access_token = $this->getToken();
261+
$header [] = 'Authorization: Bearer '.$access_token;
262+
return Http::postCurl($url, $option, $header, 'GET');
263+
}
264+
265+
266+
/*********************** token操作 **********************************/
267+
268+
/**
269+
* 返回token
270+
*
271+
* @return mixed
272+
*/
273+
public function getToken()
274+
{
275+
if (Cache::has(self::CACHE_NAME)) {
276+
return Cache::get(self::CACHE_NAME);
277+
} else {
278+
$url = $this->url."token";
279+
$option = [
280+
'grant_type' => 'client_credentials',
281+
'client_id' => $this->client_id,
282+
'client_secret' => $this->client_secret,
283+
];
284+
$return = Http::postCurl($url, $option);
285+
Cache::put(self::CACHE_NAME, $return['access_token'], (int) ($return['expires_in'] / 60));
286+
287+
return $return['access_token'];
86288

289+
}
87290
}
88291

89292
}

src/app/EasemobError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Date: 2016/12/6
66
* Time: 15:43
77
*/
8-
namespace link1st\Easemob;
8+
namespace link1st\Easemob\App;
99

1010
class EasemobError extends \Exception
1111
{

src/app/ErrorMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
2+
namespace link1st\Easemob\App;
33
/**
44
* Created by PhpStorm.
55
* User: link

src/app/Http.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Date: 2016/12/6
66
* Time: 14:56
77
*/
8-
namespace link1st\Easemob;
8+
namespace link1st\Easemob\App;
99

1010
use Cache;
1111

@@ -16,18 +16,33 @@
1616
*/
1717
class Http
1818
{
19+
20+
/**
21+
* http请求
22+
*
23+
* @param $url
24+
* @param $option
25+
* @param int $header
26+
* @param string $type
27+
* @param int $setopt
28+
*
29+
* @return mixed
30+
* @throws EasemobError
31+
*/
1932
public static function postCurl($url, $option, $header = 0, $type = 'POST',$setopt = 10) {
2033
$curl = curl_init (); // 启动一个CURL会话
34+
if (! empty ( $option )) {
35+
if($type == "GET" ){
36+
$url .= '?'.http_build_query($option);
37+
}else{
38+
$options = json_encode ( $option );
39+
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $options ); // Post提交的数据包
40+
}
41+
}
2142
curl_setopt ( $curl, CURLOPT_URL, $url ); // 要访问的地址
2243
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE ); // 对认证证书来源的检查
2344
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE ); // 从证书中检查SSL加密算法是否存在
2445
curl_setopt ( $curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
25-
if (! empty ( $option )) {
26-
$options = json_encode ( $option );
27-
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $options ); // Post提交的数据包
28-
}else{
29-
$options = "";
30-
}
3146
curl_setopt ( $curl, CURLOPT_TIMEOUT, $setopt ); // 设置超时限制防止死循环
3247
if(empty($header)){
3348
$header = array();
@@ -43,6 +58,22 @@ public static function postCurl($url, $option, $header = 0, $type = 'POST',$seto
4358
Cache::pull(Easemob::CACHE_NAME);
4459
}
4560

46-
return $result;
61+
if($status !== 200){
62+
$return_array = json_decode($result,true);
63+
if($return_array){
64+
$error_message = '请求错误:' ;
65+
if(isset($return_array['error']))
66+
$error_message .= $return_array['error'];
67+
if(isset($return_array['error_description']))
68+
$error_message .= ' '.$return_array['error_description'];
69+
}else{
70+
$error_message = '请求错误!' ;
71+
}
72+
throw new EasemobError($error_message,$status);
73+
}
74+
75+
$result_array = json_decode($result,true);
76+
77+
return $result_array;
4778
}
4879
}

0 commit comments

Comments
 (0)