Group.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * Group 用来管理群组
  7. *
  8. * \~english
  9. * The `Group` is used to manage group
  10. */
  11. final class Group
  12. {
  13. /**
  14. * @ignore
  15. * @var Auth $auth 授权对象
  16. */
  17. private $auth;
  18. /**
  19. * @ignore
  20. * @var array $modifiedAllowedField 修改群组信息时,请求体中允许的属性,5.6 版本前不能声明类常量数组,改为静态变量代替
  21. */
  22. private static $modifiedAllowedField = array(
  23. 'groupname',
  24. 'description',
  25. 'maxusers',
  26. 'membersonly',
  27. 'allowinvites',
  28. 'custom',
  29. );
  30. /// @cond
  31. public function __construct($auth)
  32. {
  33. $this->auth = $auth;
  34. }
  35. /// @endcond
  36. /**
  37. * \~chinese
  38. * \brief
  39. * 获取 App 中所有的群组(可分页)
  40. *
  41. * @param int $limit 一次获取的群组数量,默认获取 10 条。
  42. * @param string $cursor 分页使用,传入游标后便从游标起始的地方进行查询,类似于数据库 limit 1,5 中 1 的作用,可以理解为页码。
  43. * @return array 群组列表信息或者错误
  44. *
  45. * \~english
  46. * \brief
  47. * Get all groups in the app (pagable)
  48. *
  49. * @param int $limit Number of groups obtained at one time, get 10 by default.
  50. * @param string $cursor Paging is used. After the cursor is passed in, it will be queried from the beginning of the cursor, which is similar to the function of 1 in database limit 1,5. It can be understood as page number.
  51. * @return array Group list information or error
  52. */
  53. public function listGroups($limit = 10, $cursor = '')
  54. {
  55. $limit = (int)$limit < 0 ? 1 : (int)$limit;
  56. $uri = $this->auth->getBaseUri() . '/chatgroups';
  57. $uri .= $limit ? '?limit=' . $limit : '';
  58. $uri .= ($limit && $cursor) ? '&cursor='.$cursor : '';
  59. $resp = Http::get($uri, $this->auth->headers());
  60. if (!$resp->ok()) {
  61. return \Easemob\error($resp);
  62. }
  63. $data = $resp->data();
  64. return array(
  65. 'cursor' => isset($data['cursor']) ? $data['cursor'] : '',
  66. 'data' => $data['data'],
  67. );
  68. }
  69. /**
  70. * \~chinese
  71. * \brief
  72. * 获取 App 中所有的群组
  73. *
  74. * @return array 群组列表信息或者错误
  75. *
  76. * \~english
  77. * \brief
  78. * Get all groups in the app
  79. *
  80. * @return array Group list information or error
  81. */
  82. public function listAllGroups()
  83. {
  84. $data = $this->listGroups(0);
  85. return $data['data'];
  86. }
  87. /**
  88. * \~chinese
  89. * \brief
  90. * 分页获取单个用户加入的所有群组
  91. *
  92. * @param string $username 用户名
  93. * @param int $pageSize 每页获取的群组数量。该参数仅适用于分页获取方法。默认取 10 条。
  94. * @param int $pageNum 当前页码。该参数仅适用于分页获取方法。
  95. * @return array 群组信息或者错误
  96. *
  97. * \~english
  98. * \brief
  99. * Paged access to all groups joined by a single user
  100. *
  101. * @param string $username User name
  102. * @param int $pageSize Number of groups obtained per page. This parameter is only applicable to the paging get method. 10 by default.
  103. * @param int $pageNum Current page number. This parameter is only applicable to the paging get method.
  104. * @return array Group information or error
  105. */
  106. public function listGroupsUserJoined($username, $pageSize = 10, $pageNum = 1)
  107. {
  108. if (!trim($username)) {
  109. \Easemob\exception('Please pass the user name');
  110. }
  111. $pageSize = (int)$pageSize > 0 ? (int)$pageSize : 0;
  112. $pageNum = (int)$pageNum > 0 ? (int)$pageNum : 1;
  113. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/joined_chatgroups';
  114. $uri .= $pageSize ? ('?pagesize='.$pageSize.'&pagenum='.$pageNum) : '';
  115. $resp = Http::get($uri, $this->auth->headers());
  116. if (!$resp->ok()) {
  117. return \Easemob\error($resp);
  118. }
  119. $data = $resp->data();
  120. return $data['data'];
  121. }
  122. /**
  123. * \~chinese
  124. * brief
  125. * 获取单个用户加入的所有群组
  126. *
  127. * @param string $username 用户名
  128. * @return array 群组信息或者错误
  129. *
  130. * \~english
  131. * brief
  132. * Get all groups joined by a single user
  133. *
  134. * @param string $username User name
  135. * @return array Group information or error
  136. */
  137. public function listAllGroupsUserJoined($username)
  138. {
  139. return $this->listGroupsUserJoined($username, 0);
  140. }
  141. /**
  142. * \~chinese
  143. * \brief
  144. * 获取群组详情
  145. *
  146. * \details
  147. * 可以获取一个或多个群组的详情。当获取多个群组的详情时,返回所有存在的群组的详情;对于不存在的群组,返回 “group id doesn’t exist”。
  148. *
  149. * @param string $groupId 群组 ID,可以以逗号分割,同时传递多个群组 ID
  150. * @return array 群组信息或者错误
  151. *
  152. * \~english
  153. * \brief
  154. * Get group details
  155. *
  156. * \details
  157. * You can get the details of one or more groups. When the details of multiple groups are obtained, the details of all existing groups are returned; For group ID 'exist', return 'EST'.
  158. *
  159. * @param string $groupId Group ID, which can be separated by commas, and multiple group IDs can be passed at the same time
  160. * @return array Group information or error
  161. */
  162. public function getGroup($groupId)
  163. {
  164. if (!trim($groupId)) {
  165. \Easemob\exception('Please pass the group ID');
  166. }
  167. $uri = $this->auth->getBaseUri() . '/chatgroups/'. $groupId;
  168. $resp = Http::get($uri, $this->auth->headers());
  169. if (!$resp->ok()) {
  170. return \Easemob\error($resp);
  171. }
  172. $data = $resp->data();
  173. return count($data['data']) > 1 ? $data['data'] : $data['data'][0];
  174. }
  175. /**
  176. * \~chinese
  177. * \brief
  178. * 创建公开群
  179. *
  180. * \details
  181. * 创建一个公开的群组,并设置群主、群组名称、群组描述、群成员、群成员最大人数(包括群主)、加入群是否需要批准、群组扩展信息。
  182. *
  183. * @param string $owner 群组管理员的用户名
  184. * @param string $groupname 群组名称,最大长度为 128 字符。
  185. * @param string $desc 群组描述,最大长度为 512 字符。
  186. * @param array $members 群组成员,此属性为非必需,但是如果加此项,数组元素至少一个,不能超过 100 个。(注:群主 user1 不需要写入到 members 里面)
  187. * @param int $maxusers 群组最大成员数(包括群主),值为数值类型,默认值 200,具体上限请参考 <a href="https://console.easemob.com/user/login" target="_blank">环信即时通讯云控制台</a>。
  188. * @param boolean $members_only 用户申请入群是否需要群主或者群管理员审批,默认是 false。true:是;false:否。
  189. * @param string $custom 群组扩展信息,例如可以给群组添加业务相关的标记,不要超过 1024 字符。
  190. * @return string|array 群组 id 或者错误
  191. *
  192. * \~english
  193. * \brief
  194. * Create public group
  195. *
  196. * \details
  197. * Create a public group, and set the group owner, group name, group description, group members, the maximum number of group members (including group owners), whether to join the group requires approval, and group extension information.
  198. *
  199. * @param string $owner User name of the group administrator
  200. * @param string $groupname Group name, with a maximum length of 128 characters.
  201. * @param string $desc Group description, with a maximum length of 512 characters.
  202. * @param array $members This attribute is not required for group members, but if this item is added, there must be at least one array element, not more than 100. (Note: group leader user1 does not need to be written into members)
  203. * @param int $maxusers The maximum number of group members (including group owners), the value is numerical type, the default value is 200, please refer to the specific upper limit <a href="https://console.easemob.com/user/login" target="_blank">easemob console</a>。
  204. * @param boolean $members_only Whether the user's application to join the group needs the approval of the group owner or group administrator. The default is false. True: Yes; False: No.
  205. * @param string $custom Group extension information, for example, you can add business-related tags to the group, which should not exceed 1024 characters.
  206. * @return string|array Group ID or error
  207. */
  208. public function createPublicGroup($owner, $groupname, $desc, $members = array(), $maxusers = 200, $members_only = true, $custom = '')
  209. {
  210. // 公开群
  211. $public = true;
  212. // 公开群(public 为 true),则不允许群成员邀请别人加入此群
  213. $allowinvites = false;
  214. $data = compact('owner', 'groupname', 'desc', 'public', 'maxusers', 'allowinvites', 'members_only', 'members', 'custom');
  215. return $this->create($data);
  216. }
  217. /**
  218. * \~chinese
  219. * \brief
  220. * 创建私有群
  221. *
  222. * \details
  223. * 创建一个私有的群组,并设置群主、群组名称、群组描述、群成员、群成员最大人数(包括群主)、是否允许群成员邀请别人加入此群、群组扩展信息。
  224. *
  225. * @param string $owner 群组管理员的用户名
  226. * @param string $groupname 群组名称,最大长度为 128 字符。
  227. * @param string $desc 群组描述,最大长度为 512 字符。
  228. * @param array $members 群组成员,此属性为非必需,但是如果加此项,数组元素至少一个,不能超过 100 个。(注:群主 user1 不需要写入到 members 里面)
  229. * @param int $maxusers 群组最大成员数(包括群主),值为数值类型,默认值 200,具体上限请参考 <a href="https://console.easemob.com/user/login" target="_blank">环信即时通讯云控制台</a>。
  230. * @param boolean $allowinvites 是否允许群成员邀请别人加入此群:true:允许群成员邀请人加入此群;false:只有群主或者管理员才可以往群里加人。
  231. * @param string $custom 群组扩展信息,例如可以给群组添加业务相关的标记,不要超过 1024 字符。
  232. * @return string|array 群组 id 或者错误
  233. *
  234. * \~english
  235. * \brief
  236. * Create private group
  237. *
  238. * \details
  239. * Create a private group, and set the group owner, group name, group description, group members, maximum number of group members (including group owner), whether to allow group members to invite others to join the group, and group extension information.
  240. *
  241. * @param string $owner User name of the group administrator
  242. * @param string $groupname Group name, with a maximum length of 128 characters.
  243. * @param string $desc Group description, with a maximum length of 512 characters.
  244. * @param array $members This attribute is not required for group members, but if this item is added, there must be at least one array element, not more than 100. (Note: group leader user1 does not need to be written into members)
  245. * @param int $maxusers The maximum number of group members (including group owners), the value is numerical type, the default value is 200, please refer to the specific upper limit <a href="https://console.easemob.com/user/login" target="_blank">easemob console</a>。
  246. * @param boolean $allowinvites Allow group members to invite others to join the group: true: allow group members to invite people to join the group; False: only the group leader or administrator can add people to the group.
  247. * @param string $custom Group extension information, for example, you can add business-related tags to the group, which should not exceed 1024 characters.
  248. * @return string|array Group ID or error
  249. */
  250. public function createPrivateGroup($owner, $groupname, $desc, $members = array(), $maxusers = 200, $allowinvites = false, $custom = '')
  251. {
  252. // 私有群
  253. $public = false;
  254. // 如果允许了群成员邀请用户进群(allowinvites 为 true),那么就不需要群主或群管理员审批了
  255. $members_only = $allowinvites ? false : true;
  256. $data = compact('owner', 'groupname', 'desc', 'public', 'maxusers', 'allowinvites', 'members_only', 'members', 'custom');
  257. return $this->create($data);
  258. }
  259. /**
  260. * \~chinese
  261. * \brief
  262. * 修改群组信息
  263. *
  264. * \details
  265. * 修改成功的数据行会返回 true,失败为 false。请求 body 只接收 groupname、description、maxusers、membersonly、allowinvites、custom 六个属性,传不存在的字段,或者不能修改的字段会抛异常。
  266. *
  267. * @param array $data 群组信息
  268. * - `groupname` string 类型,群组名称,修改时值不能包含斜杠(“/”),最大长度为 128 字符。
  269. * - `description` string 类型,群组描述,修改时值不能包含斜杠(“/”),最大长度为 512 字符。
  270. * - `maxusers` int 类型,群组成员最大数(包括群主),值为数值类型。
  271. * - `membersonly` string 类型,加入群组是否需要群主或者群管理员审批:true:是;false:否。
  272. * - `allowinvites` string 类型,是否允许群成员邀请别人加入此群:true:允许群成员邀请人加入此群;false:只有群主才可以往群里加人。
  273. * - `custom` string 类型,群组扩展信息,例如可以给群组添加业务相关的标记,不要超过 1,024 字符。
  274. * @return boolean|array 成功或者错误
  275. *
  276. * \~english
  277. * \brief
  278. * Modify group information
  279. *
  280. * \details
  281. * The modified data row will return true, and the failure will be false. The request body only receives six attributes: groupname, description, maxusers, members only, allowinvites and custom. If it passes non-existent fields or fields that cannot be modified, exceptions will be thrown.
  282. *
  283. * @param array $data Group information
  284. * - `groupname` string type. Group name. When modified, the value cannot contain slash ("/"), and the maximum length is 128 characters.
  285. * - `description` string type. Group description. When modifying, the value cannot contain slash ("/"), and the maximum length is 512 characters.
  286. * - `maxusers` int type. The maximum number of group members (including group owners) is the numerical value type.
  287. * - `membersonly` string type. Whether to join the group requires the approval of the group owner or group administrator: true: Yes; False: No.
  288. * - `allowinvites` string type. Allow group members to invite others to join the group: true: allow group members to invite people to join the group; False: only the group leader can add people to the group.
  289. * - `custom` string type. Group extension information, for example, you can add business-related tags to the group, which should not exceed 1024 characters.
  290. * @return boolean|array Success or error
  291. */
  292. public function updateGroup($data)
  293. {
  294. $data['group_id'] = isset($data['group_id']) ? trim($data['group_id']) : '';
  295. $groupId = $data['group_id'];
  296. if (!$groupId) {
  297. \Easemob\exception('Please pass the group ID');
  298. }
  299. unset($data['group_id']);
  300. foreach ($data as $field => $value) {
  301. if (!in_array($field, self::$modifiedAllowedField)) {
  302. \Easemob\exception('Only groupname, description, maxusers, members only, allowinvites and custom are allowed at most');
  303. }
  304. }
  305. if (isset($data['groupname'])) {
  306. $data['groupname'] = trim($data['groupname']);
  307. if (!$data['groupname']) {
  308. \Easemob\exception('Group name cannot be empty');
  309. } elseif (preg_match('/\//', $data['groupname'])) {
  310. \Easemob\exception('The group name cannot contain a diagonal bar ("/)');
  311. } elseif (mb_strlen($data['groupname']) > 128) {
  312. \Easemob\exception('The maximum length of the group name is 128 characters');
  313. }
  314. }
  315. if (isset($data['description'])) {
  316. $data['description'] = trim($data['description']);
  317. if (!$data['description']) {
  318. \Easemob\exception('Group description cannot be empty');
  319. } elseif (preg_match('/\//', $data['description'])) {
  320. \Easemob\exception('The group description cannot contain diagonal bars ("/")');
  321. } elseif (mb_strlen($data['description']) > 512) {
  322. \Easemob\exception('The maximum length of the group description is 512 characters');
  323. }
  324. }
  325. // 获取原始群组信息
  326. $info = $this->getGroup($groupId);
  327. if (isset($data['maxusers'])) {
  328. $data['maxusers'] = (int)$data['maxusers'];
  329. }
  330. // 如果是公开群(public为true),则不允许群成员邀请别人加入此群
  331. if ($info['public'] && isset($data['allowinvites'])) {
  332. $data['allowinvites'] = false;
  333. }
  334. if (isset($data['membersonly'])) {
  335. // 如果允许了群成员邀请用户进群(allowinvites为true),那么就不需要群主或群管理员审批了
  336. $data['membersonly'] = $info['allowinvites'] ? false : (boolean)$data['membersonly'];
  337. }
  338. if (isset($data['allowinvites'])) {
  339. $data['allowinvites'] = (boolean)$data['allowinvites'];
  340. if (isset($data['membersonly'])) {
  341. // 如果允许了群成员邀请用户进群(allowinvites为true),那么就不需要群主或群管理员审批了
  342. $data['membersonly'] = $data['allowinvites'] ? false : (boolean)$data['membersonly'];
  343. }
  344. }
  345. if (isset($data['custom'])) {
  346. $data['custom'] = trim($data['custom']);
  347. if ($data['custom'] && mb_strlen($data['custom']) > 1024) {
  348. \Easemob\exception('The maximum length of group extension information is 1024 characters');
  349. }
  350. }
  351. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId;
  352. $resp = Http::put($uri, $data, $this->auth->headers());
  353. if (!$resp->ok()) {
  354. return \Easemob\error($resp);
  355. }
  356. return true;
  357. }
  358. /**
  359. * \~chinese
  360. * \brief
  361. * 删除群组
  362. *
  363. * @param string $groupId 群组 id
  364. * @return boolean|array 成功或者错误
  365. *
  366. * \~english
  367. * \brief
  368. * Delete Group
  369. *
  370. * @param string $groupId Group ID
  371. * @return boolean|array Success or error
  372. */
  373. public function destroyGroup($groupId)
  374. {
  375. if (!trim($groupId)) {
  376. \Easemob\exception('Please pass the group ID');
  377. }
  378. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId;
  379. $resp = Http::delete($uri, null, $this->auth->headers());
  380. if (!$resp->ok()) {
  381. return \Easemob\error($resp);
  382. }
  383. return true;
  384. }
  385. /**
  386. * \~chinese
  387. * \brief
  388. * 获取群组公告
  389. *
  390. * \details
  391. * 获取指定群组 ID 的群组公告。
  392. *
  393. * @param string $groupId 群组 id
  394. * @return array 公告信息或者错误
  395. *
  396. * \~english
  397. * \brief
  398. * Get group announcements
  399. *
  400. * \details
  401. * Gets the group announcement of the specified group ID.
  402. *
  403. * @param string $groupId Group ID
  404. * @return array Announcement information or error
  405. */
  406. public function getGroupAnnouncement($groupId)
  407. {
  408. if (!trim($groupId)) {
  409. \Easemob\exception('Please enter group ID');
  410. }
  411. $uri = $this->auth->getBaseUri() . '/chatgroups/'. $groupId . '/announcement';
  412. $resp = Http::get($uri, $this->auth->headers());
  413. if (!$resp->ok()) {
  414. return \Easemob\error($resp);
  415. }
  416. $data = $resp->data();
  417. return $data['data'];
  418. }
  419. /**
  420. * \~chinese
  421. * \brief
  422. * 修改群组公告
  423. *
  424. * \details
  425. * 修改指定群组 ID 的群组公告,注意群组公告的内容不能超过 512 个字符。
  426. *
  427. * @param string $groupId 群组 ID
  428. * @param string $announcement 群组公告内容
  429. * @return boolean|array 成功或者错误
  430. *
  431. * \~english
  432. * \brief
  433. * Modify group announcement
  434. *
  435. * \details
  436. * Modify the group announcement of the specified group ID. note that the content of the group announcement cannot exceed 512 characters.
  437. *
  438. * @param string $groupId Group ID
  439. * @param string $announcement Group announcement content
  440. * @return boolean|array Success or error
  441. */
  442. public function updateGroupAnnouncement($groupId, $announcement)
  443. {
  444. if (!trim($groupId) || !trim($announcement)) {
  445. \Easemob\exception('Please enter the group ID and announcement content');
  446. }
  447. if (mb_strlen($announcement) > 512) {
  448. \Easemob\exception('The content of the group announcement cannot exceed 512 characters');
  449. }
  450. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/announcement';
  451. $body = compact('announcement');
  452. $resp = Http::post($uri, $body, $this->auth->headers());
  453. if (!$resp->ok()) {
  454. return \Easemob\error($resp);
  455. }
  456. $data = $resp->data();
  457. return $data['data']['result'];
  458. }
  459. /**
  460. * \~chinese
  461. * \brief
  462. * 获取群组共享文件
  463. *
  464. * \details
  465. * 分页获取指定群组 ID 的群组共享文件,之后可以根据 response 中返回的 file_id,file_id 是群组共享文件的唯一标识,调用 {@link #downloadGroupShareFile($fileName, $groupId, $fileId)} 下载文件,或调用 {@link #deleteGroupShareFile($groupId, $fileId)} 删除文件。
  466. *
  467. * @param string $groupId 群组 ID
  468. * @param int $pageSize 每页获取的群组数量。该参数仅适用于分页获取方法。默认取 10 条。
  469. * @param int $pageNum 当前页码。该参数仅适用于分页获取方法。
  470. * @return array 群组文件信息或者错误
  471. *
  472. * \~english
  473. * \brief
  474. * Get group shared files
  475. *
  476. * \details
  477. * Get the group shared file of the specified group ID by paging, and then according to the file returned in the response_ id,file_ ID is the unique identification of the group shared file, call {@link #downloadGroupShareFile($fileName, $groupId, $fileId)} download files, or call {@link #deleteGroupShareFile($groupId, $fileId)} delete file.
  478. *
  479. * @param string $groupId Group ID
  480. * @param int $pageSize Number of groups obtained per page. This parameter is only applicable to the paging get method. 10 by default.
  481. * @param int $pageNum Current page number. This parameter is only applicable to the paging get method.
  482. * @return array Group file information or error
  483. */
  484. public function getGroupShareFiles($groupId, $pageSize = 10, $pageNum = 1)
  485. {
  486. if (!trim($groupId)) {
  487. \Easemob\exception('Please enter the group ID');
  488. }
  489. $pageSize = (int)$pageSize > 0 ? (int)$pageSize : 0;
  490. $pageNum = (int)$pageNum > 0 ? (int)$pageNum : 1;
  491. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/share_files';
  492. $uri .= $pageSize ? ('?pagesize='.$pageSize.'&pagenum='.$pageNum) : '';
  493. $resp = Http::get($uri, $this->auth->headers());
  494. if (!$resp->ok()) {
  495. return \Easemob\error($resp);
  496. }
  497. $data = $resp->data();
  498. return $data['data'];
  499. }
  500. /**
  501. * \~chinese
  502. * \brief
  503. * 上传群组共享文件
  504. *
  505. * \details
  506. * 上传指定群组 ID 的群组共享文件。注意上传的文件大小不能超过 10 MB。
  507. *
  508. * @param string $groupId 群组 ID
  509. * @param string $fileName 上传的文件路径
  510. * @return array 上传的文件信息或者错误
  511. *
  512. * \~english
  513. * \brief
  514. * Upload group shared files
  515. *
  516. * \details
  517. * Upload the group shared file with the specified group ID. Note that the uploaded file size cannot exceed 10 Mb.
  518. *
  519. * @param string $groupId Group ID
  520. * @param string $fileName Uploaded file path
  521. * @return array Uploaded file information or error
  522. */
  523. public function uploadGroupShareFile($groupId, $fileName)
  524. {
  525. if (!trim($groupId)) {
  526. \Easemob\exception('Please enter the group ID');
  527. }
  528. if (!trim($fileName)) {
  529. \Easemob\exception('Please pass in the attachment name');
  530. }
  531. $file = fopen($fileName, 'rb');
  532. if ($file === false) {
  533. \Easemob\exception('The attachment cannot be read');
  534. }
  535. $stat = fstat($file);
  536. $size = $stat['size'];
  537. $data = fread($file, $size);
  538. fclose($file);
  539. $mimeType = mime_content_type($fileName) ? mime_content_type($fileName) : null;
  540. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/share_files';
  541. $resp = Http::multipartPost($uri, $fileName, $data, $mimeType, $this->auth->headers());
  542. if (!$resp->ok()) {
  543. return \Easemob\error($resp);
  544. }
  545. $data = $resp->data();
  546. return $data['data'];
  547. }
  548. /**
  549. * \~chinese
  550. * \brief
  551. * 下载群组共享文件
  552. *
  553. * \details
  554. * 根据指定的群组 ID 与 file_id 下载群组共享文件,file_id 通过 {@link #getGroupShareFiles($groupId, $pageSize = 10, $pageNum = 1)} 获取。
  555. *
  556. * @param string $fileName 要下载的文件路径
  557. * @param string $groupId 群组 ID
  558. * @param string $fileId 群组共享文件 id
  559. * @return int|array 文件大小或者错误
  560. *
  561. * \~english
  562. * \brief
  563. * Download group shared files
  564. *
  565. * \details
  566. * According to the specified group ID and file_id download group shared file, file_id is obtained through {@link #getGroupShareFiles($groupId, $pageSize = 10, $pageNum = 1)}.
  567. *
  568. * @param string $fileName File path to download
  569. * @param string $groupId Group ID
  570. * @param string $fileId file_id
  571. * @return int|array File size or error
  572. */
  573. public function downloadGroupShareFile($fileName, $groupId, $fileId)
  574. {
  575. if (!trim($fileName) || !trim($fileId) || !trim($fileId)) {
  576. \Easemob\exception('Please enter the file path, group ID and group shared file ID to download');
  577. }
  578. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/share_files/' . $fileId;
  579. $resp = Http::get($uri, $this->auth->headers());
  580. if (!$resp->ok()) {
  581. return \Easemob\error($resp);
  582. }
  583. $dir = dirname($fileName);
  584. if (!file_exists($dir)) {
  585. mkdir($dir, 0755, true);
  586. }
  587. return file_put_contents($fileName, $resp->body);
  588. }
  589. /**
  590. * \~chinese
  591. * \brief
  592. * 删除群组共享文件
  593. *
  594. * \details
  595. * 根据指定的群组 ID 与 file_id 删除群组共享文件,file_id 通过 {@link #getGroupShareFiles($groupId, $pageSize = 10, $pageNum = 1)} 获取。
  596. *
  597. * @param string $groupId 群组 ID
  598. * @param string $fileId 群组共享文件 id
  599. * @return boolean|array 成功或者错误
  600. *
  601. * \~english
  602. * \brief
  603. * Delete group shared files
  604. *
  605. * \details
  606. * According to the specified group ID and file_id delete group shared file, file_id is obtained through {@link #getGroupShareFiles($groupId, $pageSize = 10, $pageNum = 1)}。
  607. *
  608. * @param string $groupId Group ID
  609. * @param string $fileId file_id
  610. * @return boolean|array Success or error
  611. */
  612. public function deleteGroupShareFile($groupId, $fileId)
  613. {
  614. if (!trim($groupId) || !trim($fileId)) {
  615. \Easemob\exception('Please pass in the group ID and the group shared file ID');
  616. }
  617. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/share_files/' . $fileId;
  618. $resp = Http::delete($uri, null, $this->auth->headers());
  619. if (!$resp->ok()) {
  620. return \Easemob\error($resp);
  621. }
  622. $data = $resp->data();
  623. return $data['data']['result'];
  624. }
  625. /**
  626. * \~chinese
  627. * \brief
  628. * 分页获取群组成员
  629. *
  630. * @param string $groupId 群组 ID
  631. * @param int $pageSize 每页成员数量,默认值为 10,最大为 100。
  632. * @param int $pageNum 当前页码。默认值为 1。
  633. * @return array 群组成员信息或者错误
  634. *
  635. * \~english
  636. * \brief
  637. * Paging get group members
  638. *
  639. * @param string $groupId Group ID
  640. * @param int $pageSize Number of members per page. The default value is 10 and the maximum value is 100.
  641. * @param int $pageNum Current page number. The default value is 1.
  642. * @return array Group member information or error
  643. */
  644. public function listGroupMembers($groupId, $pageSize = 10, $pageNum = 1)
  645. {
  646. if (!trim($groupId)) {
  647. \Easemob\exception('Please enter the group ID');
  648. }
  649. $pageSize = (int)$pageSize > 0 ? (int)$pageSize : 0;
  650. $pageNum = (int)$pageNum > 0 ? (int)$pageNum : 1;
  651. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/users';
  652. $uri .= $pageSize ? ('?pagesize='.$pageSize.'&pagenum='.$pageNum) : '';
  653. $resp = Http::get($uri, $this->auth->headers());
  654. if (!$resp->ok()) {
  655. return \Easemob\error($resp);
  656. }
  657. $data = $resp->data();
  658. return $data['data'];
  659. }
  660. /**
  661. * \~chinese
  662. * \brief
  663. * 获取群组所有成员
  664. *
  665. * @param string $groupId 群组 ID
  666. * @return array 群组成员信息或者错误
  667. *
  668. * \~english
  669. * \brief
  670. * Get all members of the group
  671. *
  672. * @param string $groupId Group ID
  673. * @return array Group member information or error
  674. */
  675. public function listAllGroupMembers($groupId)
  676. {
  677. return $this->listGroupMembers($groupId, 0);
  678. }
  679. /**
  680. * \~chinese
  681. * \brief
  682. * 添加单个群组成员
  683. *
  684. * \details
  685. * 一次给群添加一个成员,不能重复添加同一个成员。如果用户已经是群成员,将添加失败,并返回错误。
  686. *
  687. * @param string $groupId 群组 ID
  688. * @param string $username 环信用户 ID
  689. * @return boolean|array 成功或者错误
  690. *
  691. * \~english
  692. * \brief
  693. * Add individual group members
  694. *
  695. * \details
  696. * Add one member to the group at a time. You cannot add the same member repeatedly. If the user is already a member of the group, the addition fails with an error.
  697. *
  698. * @param string $groupId Group ID
  699. * @param string $username User name
  700. * @return boolean|array Success or error
  701. */
  702. public function addGroupMember($groupId, $username)
  703. {
  704. return $this->addUsers($groupId, $username);
  705. }
  706. /**
  707. * \~chinese
  708. * \brief
  709. * 批量添加群组成员
  710. *
  711. * \details
  712. * 为群组添加多个成员,一次最多可以添加 60 位成员。如果所有用户均已是群成员,将添加失败,并返回错误。
  713. *
  714. * @param string $groupId 群组 ID
  715. * @param array $usernames 环信用户 ID 数组
  716. * @return boolean|array 成功或者错误
  717. *
  718. * \~english
  719. * \brief
  720. * Batch add group members
  721. *
  722. * \details
  723. * Add multiple members to the group. You can add up to 60 members at a time. If all users are already members of the group, the addition fails with an error.
  724. *
  725. * @param string $groupId Group ID
  726. * @param array $usernames User name
  727. * @return boolean|array Success or error
  728. */
  729. public function addGroupMembers($groupId, $usernames)
  730. {
  731. return $this->addUsers($groupId, $usernames);
  732. }
  733. /**
  734. * \~chinese
  735. * \brief
  736. * 移除单个群组成员
  737. *
  738. * \details
  739. * 从群中移除某个成员。如果被移除用户不是群成员,将移除失败,并返回错误。
  740. *
  741. * @param string $groupId 群组 ID
  742. * @param string $username 环信用户 ID
  743. * @return boolean|array 成功或者错误
  744. *
  745. * \~english
  746. * \brief
  747. * Remove individual group members
  748. *
  749. * \details
  750. * Remove a member from the group. If the removed user is not a member of the group, the removal fails with an error.
  751. *
  752. * @param string $groupId Group ID
  753. * @param string $username User name
  754. * @return boolean|array Success or error
  755. */
  756. public function removeGroupMember($groupId, $username)
  757. {
  758. return $this->removeUsers($groupId, $username);
  759. }
  760. /**
  761. * \~chinese
  762. * \brief
  763. * 批量移除群组成员
  764. *
  765. * \details
  766. * 移除群成员,用户名之间用英文逗号分隔。建议一次最多移除 60 个群成员。如果所有被移除用户均不是群成员,将移除失败,并返回错误。
  767. *
  768. * @param string $groupId 群组 ID
  769. * @param array $username 环信用户 ID 数组
  770. * @return boolean|array 成功或者错误
  771. *
  772. * \~english
  773. * \brief
  774. * Batch remove group members
  775. *
  776. * \details
  777. * Remove group members and separate user names with English commas. It is recommended to remove up to 60 group members at a time. If all the removed users are not members of the group, the removal fails with an error.
  778. *
  779. * @param string $groupId Group ID
  780. * @param array $username User name array
  781. * @return boolean|array Success or error
  782. */
  783. public function removeGroupMembers($groupId, $usernames)
  784. {
  785. return $this->removeUsers($groupId, $usernames);
  786. }
  787. /**
  788. * \~chinese
  789. * \brief
  790. * 获取群管理员列表
  791. *
  792. * @param string $groupId 群组 ID
  793. * @return array 群管理员信息或者错误
  794. *
  795. * \~english
  796. * \brief
  797. * Get the list of group administrators
  798. *
  799. * @param string $groupId Group ID
  800. * @return array Group administrator information or error
  801. */
  802. public function listGroupAdmins($groupId)
  803. {
  804. if (!trim($groupId)) {
  805. \Easemob\exception('Please enter the group ID');
  806. }
  807. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/admin';
  808. $resp = Http::get($uri, $this->auth->headers());
  809. if (!$resp->ok()) {
  810. return \Easemob\error($resp);
  811. }
  812. $data = $resp->data();
  813. return $data['data'];
  814. }
  815. /**
  816. * \~chinese
  817. * \brief
  818. * 添加群管理员
  819. *
  820. * \details
  821. * 将一个群成员角色权限提升为群管理员。
  822. *
  823. * @param string $groupId 群组 ID
  824. * @param string $newadmin 添加的新管理员用户 ID
  825. * @return boolean|array 成功或者错误
  826. *
  827. * \~english
  828. * \brief
  829. * Add group administrator
  830. *
  831. * \details
  832. * Promote the role permission of a group member to group administrator.
  833. *
  834. * @param string $groupId Group ID
  835. * @param string $newadmin User name
  836. * @return boolean|array Success or error
  837. */
  838. public function addGroupAdmin($groupId, $newadmin)
  839. {
  840. if (!trim($groupId) || !trim($newadmin)) {
  841. \Easemob\exception('Please pass in the group ID and the new administrator user ID to be added');
  842. }
  843. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/admin';
  844. $body = compact('newadmin');
  845. $resp = Http::post($uri, $body, $this->auth->headers());
  846. if (!$resp->ok()) {
  847. return \Easemob\error($resp);
  848. }
  849. return true;
  850. }
  851. /**
  852. * \~chinese
  853. * \brief
  854. * 移除群管理员
  855. *
  856. * \details
  857. * 将用户的角色从群管理员降为群普通成员。
  858. *
  859. * @param string $groupId 群组 ID
  860. * @param string $oldadmin 移除的管理员用户 ID
  861. * @return boolean|array 成功或者错误
  862. *
  863. * \~english
  864. * \brief
  865. * Remove group administrator
  866. *
  867. * \details
  868. * Reduce the user's role from group administrator to ordinary member of the group.
  869. *
  870. * @param string $groupId Group ID
  871. * @param string $oldadmin User name
  872. * @return boolean|array Success or error
  873. */
  874. public function removeGroupAdmin($groupId, $oldadmin)
  875. {
  876. if (!trim($groupId) || !trim($oldadmin)) {
  877. \Easemob\exception('Please pass in the group ID and the administrator user ID to be removed');
  878. }
  879. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/admin/' . $oldadmin;
  880. $resp = Http::delete($uri, null, $this->auth->headers());
  881. if (!$resp->ok()) {
  882. return \Easemob\error($resp);
  883. }
  884. $data = $resp->data();
  885. return $data['data']['result'] === 'success' ? true : false;
  886. }
  887. /**
  888. * \~chinese
  889. * \brief
  890. * 转让群组
  891. *
  892. * \details
  893. * 修改群主为同一群组中的其他成员。
  894. *
  895. * @param string $groupId 群组 ID
  896. * @param string $newowner 群组的新管理员用户 ID
  897. * @return boolean|array 成功或者错误
  898. *
  899. * \~english
  900. * \brief
  901. * Transfer group
  902. *
  903. * \details
  904. * Modify the group owner to other members in the same group.
  905. *
  906. * @param string $groupId Group ID
  907. * @param string $newowner User name
  908. * @return boolean|array Success or error
  909. */
  910. public function updateGroupOwner($groupId, $newowner)
  911. {
  912. if (!trim($groupId) || !trim($newowner)) {
  913. \Easemob\exception('Please pass in the group ID and the new administrator user ID of the group');
  914. }
  915. $uri = $this->auth->getBaseUri(). '/chatgroups/' . $groupId;
  916. $body = compact('newowner');
  917. $resp = Http::put($uri, $body, $this->auth->headers());
  918. if (!$resp->ok()) {
  919. return \Easemob\error($resp);
  920. }
  921. return true;
  922. }
  923. /**
  924. * @ignore 创建群组
  925. * @param array $data 群组信息
  926. * @return string|array 群组 id 或者错误
  927. */
  928. private function create($data)
  929. {
  930. $data['groupname'] = trim($data['groupname']);
  931. $data['desc'] = trim($data['desc']);
  932. $data['owner'] = trim($data['owner']);
  933. if (!isset($data['groupname']) || !$data['groupname']) {
  934. \Easemob\exception('Please pass the group name');
  935. }
  936. if (!isset($data['desc']) || !$data['desc']) {
  937. \Easemob\exception('Please pass the group description');
  938. }
  939. if (!isset($data['owner']) || !$data['owner']) {
  940. \Easemob\exception('Please pass the administrator of the group');
  941. }
  942. if (isset($data['members'])) {
  943. if (!is_array($data['members']) || empty($data['members'])) {
  944. \Easemob\exception('Please pass in group members. Group members must be arrays');
  945. } elseif (count($data['members']) > 100) {
  946. \Easemob\exception('Group members cannot exceed 100');
  947. } elseif (in_array($data['owner'], $data['members'])) {
  948. \Easemob\exception('The group leader does not need to be included in the group members');
  949. }
  950. }
  951. $data['public'] = (boolean)$data['public'];
  952. $data['maxusers'] = isset($data['maxusers']) && (int)$data['maxusers'] ? (int)$data['maxusers'] : 200;
  953. // 如果是公开群(public 为 true),则不允许群成员邀请别人加入此群
  954. $data['allowinvites'] = (boolean)$data['allowinvites'];
  955. // 如果允许了群成员邀请用户进群(allowinvites为true),那么就不需要群主或群管理员审批了
  956. $data['members_only'] = (boolean)$data['members_only'];
  957. $data['custom'] = trim($data['custom']);
  958. $uri = $this->auth->getBaseUri() . '/chatgroups';
  959. $resp = Http::post($uri, $data, $this->auth->headers());
  960. if (!$resp->ok()) {
  961. return \Easemob\error($resp);
  962. }
  963. $data = $resp->data();
  964. return isset($data['data']['groupid']) ? $data['data']['groupid'] : $data['data'];
  965. }
  966. /**
  967. * @ignore (批量)添加群组成员
  968. * @param string $groupId 群组 ID
  969. * @param string|array $usernames 环信用户 ID,string: 添加单个群组成员;array: 批量添加群组成员
  970. * @return boolean|array 成功或者错误
  971. */
  972. private function addUsers($groupId, $usernames)
  973. {
  974. if (!trim($groupId)) {
  975. \Easemob\exception('Please pass the group ID');
  976. }
  977. if ((is_array($usernames) && empty($usernames)) || (is_string($usernames) && !trim($usernames))) {
  978. \Easemob\exception('Please pass the user name');
  979. }
  980. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/users';
  981. $uri .= is_array($usernames) ? '' : ('/' . $usernames);
  982. $body = is_array($usernames) ? compact('usernames') : null;
  983. $resp = Http::post($uri, $body, $this->auth->headers());
  984. if (!$resp->ok()) {
  985. return \Easemob\error($resp);
  986. }
  987. return true;
  988. }
  989. /**
  990. * @ignore(批量)移除群组成员
  991. * @param string $groupId 群组 ID
  992. * @param string|array $usernames 环信用户 ID,string: 移除单个群组成员;array: 批量移除群组成员
  993. * @return boolean|array 成功或者错误
  994. */
  995. private function removeUsers($groupId, $usernames)
  996. {
  997. if (!trim($groupId)) {
  998. \Easemob\exception('Please pass the group ID');
  999. }
  1000. if ((is_array($usernames) && empty($usernames)) || (is_string($usernames) && !trim($usernames))) {
  1001. \Easemob\exception('Please pass the user name');
  1002. }
  1003. $uri = $this->auth->getBaseUri() . '/chatgroups/' . $groupId . '/users/';
  1004. $uri .= is_array($usernames) ? implode(',', $usernames) : $usernames;
  1005. $resp = Http::delete($uri, null, $this->auth->headers());
  1006. if (!$resp->ok()) {
  1007. return \Easemob\error($resp);
  1008. }
  1009. return true;
  1010. }
  1011. }