11<?php
2- namespace link1st \Easemob ;
2+ namespace link1st \Easemob \ App ;
33
44use Config ;
55use 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}
0 commit comments