User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * User 用来实现用户体系建立和管理
  7. *
  8. * \~english
  9. * The `User` is used to realize the establishment and management of user system
  10. */
  11. final class User
  12. {
  13. /**
  14. * @ignore
  15. * @var Auth $auth 授权对象
  16. */
  17. private $auth;
  18. /// @cond
  19. public function __construct($auth)
  20. {
  21. $this->auth = $auth;
  22. }
  23. /// @endcond
  24. /**
  25. * \~chinese
  26. * \brief
  27. * 注册单个用户 | 批量注册用户
  28. *
  29. * @param array $users 要注册的用户信息,注册单个用户时传入一维数组,批量注册用户时传入二维数组。
  30. * - `username` String 类型,用户名,长度不可超过 64 个字节长度。支持以下字符集:
  31. * - 26 个小写英文字母 a-z;
  32. * - 26 个大写英文字母 A-Z;
  33. * - 10 个数字 0-9;
  34. * - “_”, “-”, “.”。
  35. * <pre><b style="color: red">注意:不区分大小写。同一个 app 下,用户名唯一。</b></pre>
  36. * - `password` String 类型,登录密码,长度不可超过 64 个字符长度。
  37. * - `nickname` String 类型,昵称(可选),仅用在客户端推送通知栏显示的昵称,并不是用户个人信息的昵称,开发者可自定义该内容。长度不可超过 100 个字符。支持以下字符集:
  38. * - 26 个小写英文字母 a-z;
  39. * - 26 个大写英文字母 A-Z;
  40. * - 10 个数字 0-9;
  41. * - 中文;
  42. * - 特殊字符。
  43. * @return array 注册的用户信息或者错误
  44. *
  45. * \~english
  46. * \brief
  47. * Register individual users | Batch registered users
  48. *
  49. * @param array $users For the user information to be registered, a one-dimensional array is passed in when registering a single user, and a two-dimensional array is passed in when registering users in batch.
  50. * - `username` String type, user name, length cannot exceed 64 bytes. The following character sets are supported:
  51. * - 26 lowercase English letters a-z;
  52. * - 26 uppercase English letters A-Z;
  53. * - 10 digits 0-9;
  54. * - "_", "-" ".".
  55. * <pre><b style="color: red">Note: case insensitive. Under the same app, the user name is unique.</b></pre>
  56. * - `password` String type, login password, the length cannot exceed 64 characters.
  57. * - `nickname` String type, nickname (optional). It is only used for the nickname displayed in the client push notification bar, not the nickname of the user's personal information. The developer can customize this content. The length cannot exceed 100 characters. The following character sets are supported:
  58. * - 26 lowercase English letters a-z;
  59. * - 26 uppercase English letters A-Z;
  60. * - 10 digits 0-9;
  61. * - Chinese;
  62. * - Special characters;
  63. * @return array Registered user information or error
  64. */
  65. public function create($users)
  66. {
  67. // 一维数组标识
  68. $usersOneFlag = false;
  69. if (count($users) == count($users, 1)) {
  70. // 一维数组
  71. $usersOneFlag = true;
  72. $this->authUser($users);
  73. } else {
  74. // 多维数组
  75. foreach ($users as $user) {
  76. $this->authUser($user);
  77. }
  78. }
  79. $uri = $this->auth->getBaseUri() . '/users';
  80. $resp = Http::post($uri, $users, $this->auth->headers());
  81. if (!$resp->ok()) {
  82. return \Easemob\error($resp);
  83. }
  84. $data = $resp->data();
  85. return $usersOneFlag ? $data['entities'][0] : $data['entities'];
  86. }
  87. /**
  88. * \~chinese
  89. * \brief
  90. * 获取单个用户的详细信息
  91. *
  92. * @param string $username 用户名
  93. * @return array 用户信息或者错误
  94. *
  95. * \~english
  96. * \brief
  97. * Get the details of a single user
  98. *
  99. * @param string $username User name
  100. * @return array User information or error
  101. */
  102. public function get($username)
  103. {
  104. if (!trim($username)) {
  105. \Easemob\exception('Please enter your username');
  106. }
  107. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  108. $resp = Http::get($uri, $this->auth->headers());
  109. if (!$resp->ok()) {
  110. return \Easemob\error($resp);
  111. }
  112. $data = $resp->data();
  113. return $data['entities'][0];
  114. }
  115. /**
  116. * \~chinese
  117. * \brief
  118. * 批量获取用户
  119. *
  120. * @param int $limit 获取用户的数量。默认值 10,最大值 100。超过 100 按照 100 返回。
  121. * @param string $cursor 游标,用于分页显示用户列表。第一次发起批量查询用户请求时无需设置 cursor,请求成功后会获得第一页用户列表。从响应 body 中获取 cursor,并在下一次请求 中传入该 cursor,直到响应 body 中不再有 cursor 字段,则表示已查询到 app 中所有用户。
  122. * @param boolean $activated 用户是否激活。true:已激活;false:封禁,封禁需要通过解禁接口进行解禁,才能正常登录。
  123. * @return array 分页用户信息或者错误
  124. *
  125. * \~english
  126. * \brief Get users in batch
  127. *
  128. * @param int $limit Gets the number of users. The default value is 10 and the maximum value is 100. If it exceeds 100, it will be returned as 100.
  129. * @param string $cursor Cursor, used to display the list of users in pages. When initiating a batch query user request for the first time, there is no need to set cursor. After the request is successful, the user list on the first page will be obtained. If there is no response from cursor to the next user in the URL field of cursor, it indicates that there is no response from cursor to the next user in the URL field of cursor.
  130. * @param boolean $activated Whether the user is activated. True: activated; False: blocking. The blocking needs to be lifted through the unblocking interface to log in normally.
  131. * @return array Paging user information or error
  132. */
  133. public function listUsers($limit = 10, $cursor = '', $activated = true)
  134. {
  135. $limit = (int)$limit <= 0 ? 10 : (int)$limit;
  136. $limit = $limit > 100 ? 100 : $limit;
  137. $activated = (boolean)$activated;
  138. $uri = $this->auth->getBaseUri() . '/users';
  139. $uri .= '?limit='.$limit;
  140. $uri .= $cursor ? '&cursor='.$cursor : '';
  141. $uri .= '&activated='.($activated ? 1 : 0);
  142. $resp = Http::get($uri, $this->auth->headers());
  143. if (!$resp->ok()) {
  144. return \Easemob\error($resp);
  145. }
  146. $data = $resp->data();
  147. return array(
  148. 'data' => $data['entities'],
  149. 'cursor' => isset($data['cursor']) && $data['cursor'] ? $data['cursor'] : '',
  150. );
  151. }
  152. /**
  153. * \~chinese
  154. * \brief
  155. * 删除单个用户
  156. *
  157. * \details
  158. * 删除一个用户,如果此用户是群组或者聊天室的群主,系统会同时删除这些群组和聊天室。请在操作时进行确认。
  159. *
  160. * @param string $username 用户名
  161. * @return boolean|array 成功或失败或者错误
  162. *
  163. * \~english
  164. * \brief
  165. * Delete single user
  166. *
  167. * \details
  168. * Delete a user. If the user is the group owner of a group or chat room, the system will delete these groups and chat rooms at the same time. Please confirm during operation.
  169. *
  170. * @param string $username User name
  171. * @return boolean|array Success or failure or error
  172. */
  173. public function delete($username)
  174. {
  175. if (!trim($username)) {
  176. \Easemob\exception('Please enter your username');
  177. }
  178. $uri = $this->auth->getBaseUri() . '/users/' . $username;
  179. $resp = Http::delete($uri, null, $this->auth->headers());
  180. if (!$resp->ok()) {
  181. return \Easemob\error($resp);
  182. }
  183. $data = $resp->data();
  184. return isset($data['entities'][0]) && $data['entities'][0]['username'] === $username ? true : false;
  185. }
  186. /**
  187. * \~chinese
  188. * \brief
  189. * 批量删除用户
  190. *
  191. * \details
  192. * 删除某个 APP 下指定数量的用户账号。
  193. *
  194. * @param int $limit 要删除的用户数量,建议这个数值在 100-500 之间,不要过大。需要注意的是,这里只是批量的一次性删除掉 N 个用户,具体删除哪些并没有指定,可以在返回值中查看到哪些用户被删除掉了。如果 $limit 的值小于等于 0,值会按 1 处理
  195. * @return array 被删除的用户信息或者错误
  196. *
  197. * \~english
  198. * \brief
  199. * Batch delete user
  200. *
  201. * \details
  202. * Delete a specified number of user accounts under an app.
  203. *
  204. * @param int $limit The number of users to be deleted is recommended to be between 100-500, not too large. It should be noted that only n users are deleted in batches at one time. The specific deleted users are not specified. You can see which users have been deleted in the return value. If the value of $limit is less than or equal to 0, the value will be treated as 1
  205. * @return array Deleted user information or error
  206. */
  207. public function batchDelete($limit = 0)
  208. {
  209. $limit = (int)$limit <= 0 ? 1 : (int)$limit;
  210. $uri = $this->auth->getBaseUri() . '/users?limit='.$limit;
  211. $resp = Http::delete($uri, null, $this->auth->headers());
  212. if (!$resp->ok()) {
  213. return \Easemob\error($resp);
  214. }
  215. $data = $resp->data();
  216. return $data['entities'];
  217. }
  218. /// @cond
  219. public function deleteAll()
  220. {
  221. return $this->batchDelete(0);
  222. }
  223. /// @endcond
  224. /**
  225. * \~chinese
  226. * \brief
  227. * 修改用户密码
  228. *
  229. * \details
  230. * 可以修改用户的登录密码,不需要提供原密码。
  231. *
  232. * @param string $username 用户名
  233. * @param string $newpassword 新密码
  234. * @return boolean|array 成功或者错误
  235. *
  236. * \~english
  237. * \brief
  238. * Modify user password
  239. *
  240. * \details
  241. * You can change the user's login password without providing the original password.
  242. *
  243. * @param string $username User name
  244. * @param string $newpassword New password
  245. * @return boolean|array Success or error
  246. */
  247. public function updateUserPassword($username, $newpassword)
  248. {
  249. if (!trim($username) || !trim($newpassword)) {
  250. \Easemob\exception('Please enter your username and password');
  251. }
  252. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/password';
  253. $body = compact('newpassword');
  254. $resp = Http::put($uri, $body, $this->auth->headers());
  255. if (!$resp->ok()) {
  256. return \Easemob\error($resp);
  257. }
  258. return true;
  259. }
  260. /**
  261. * \~chinese
  262. * \brief
  263. * 获取用户在线状态
  264. *
  265. * @param string $username 要获取在线状态的用户名
  266. * @return boolean|array 是否在线(true:在线,false:离线)或者错误
  267. *
  268. * \~english
  269. * \brief
  270. * Get user online status
  271. *
  272. * @param string $username User name
  273. * @return boolean|array Online status (true: online, false: offline) or error
  274. */
  275. public function isUserOnline($username)
  276. {
  277. if (!is_string($username) || !trim($username)) {
  278. \Easemob\exception('Please enter your username');
  279. }
  280. $result = $this->status($username);
  281. return isset($result[$username]) && $result[$username] === 'online' ? true : false;
  282. }
  283. /**
  284. * \~chinese
  285. * \brief
  286. * 批量获取用户在线状态
  287. *
  288. * \details
  289. * 批量查看用户的在线状态,最大同时查看100个用户。
  290. *
  291. * @param array $usernames 要获取在线状态的用户名数组,最多不能超过100个
  292. * @return array 用户在线状态数组(数组键为用户名,数组值为用户对应的在线状态,true:在线,false:离线)或者错误
  293. *
  294. * \~english
  295. * \brief
  296. * Get online status of users in batch
  297. *
  298. * \details
  299. * View the online status of users in batches, with a maximum of 100 users at the same time.
  300. *
  301. * @param array $usernames User name array, no more than 100
  302. * @return array User online status array (array key is user name, array value is user's corresponding online status, true: online, false: offline) or error
  303. */
  304. public function isUsersOnline($usernames)
  305. {
  306. if (!is_array($usernames) || empty($usernames)) {
  307. \Easemob\exception('Please enter user name array');
  308. }
  309. $result = $this->status($usernames);
  310. $data = array();
  311. foreach ($result as $user => $status) {
  312. $data[$user] = $status === 'online' ? true : false;
  313. }
  314. return $data;
  315. }
  316. /**
  317. * \~chinese
  318. * \brief
  319. * 强制下线
  320. *
  321. * \details
  322. * 强制用户即把用户状态改为离线,用户需要重新登录才能正常使用。
  323. *
  324. * @param string $username 要强制下线用户的用户名
  325. * @return boolean|array 成功或者错误
  326. *
  327. * \~english
  328. * \brief
  329. * Force user offline
  330. *
  331. * \details
  332. * Force the user to change the user status to offline, and the user needs to log in again to use it normally.
  333. *
  334. * @param string $username User name
  335. * @return boolean|array Success or error
  336. */
  337. public function forceLogoutAllDevices($username)
  338. {
  339. if (!trim($username)) {
  340. \Easemob\exception('Please enter your username');
  341. }
  342. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/disconnect';
  343. $resp = Http::get($uri, $this->auth->headers());
  344. if (!$resp->ok()) {
  345. return \Easemob\error($resp);
  346. }
  347. $data = $resp->data();
  348. return $data['data']['result'];
  349. }
  350. /// @cond
  351. public function forceLogoutOneDevice($username, $resource)
  352. {
  353. }
  354. /// @endcond
  355. /**
  356. * @ignore 获取用户在线状态 | 批量获取用户在线状态
  357. * @param string|array $username 要获取在线状态的用户名,string:获取用户在线状态;array:批量获取用户在线状态
  358. * @return boolean|array 是否在线(true:在线,false:离线)或者用户在线状态数组(数组键为用户名,数组值为用户对应的在线状态,true:在线,false:离线)或者错误
  359. */
  360. private function status($username)
  361. {
  362. if (is_array($username)) {
  363. // 批量获取用户在线状态
  364. $uri = $this->auth->getBaseUri() . '/users/batch/status';
  365. $body = array('usernames' => $username);
  366. $resp = Http::post($uri, $body, $this->auth->headers());
  367. } else {
  368. // 获取用户在线状态
  369. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/status';
  370. $resp = Http::get($uri, $this->auth->headers());
  371. }
  372. if (!$resp->ok()) {
  373. return \Easemob\error($resp);
  374. }
  375. $data = $resp->data();
  376. if (is_array($username)) {
  377. $result = array();
  378. foreach ($data['data'] as $val) {
  379. foreach ($val as $key => $item) {
  380. $result[$key] = $item;
  381. }
  382. }
  383. return $result;
  384. }
  385. return $data['data'];
  386. }
  387. /**
  388. * @ignore 验证用户信息
  389. * @param array $user 用户信息
  390. */
  391. private function authUser($user)
  392. {
  393. if (!isset($user['username']) || !trim($user['username']) || !isset($user['password']) || !trim($user['password'])) {
  394. return \Easemob\exception('Please enter your username and password');
  395. }
  396. }
  397. }