TencentImGroupService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\TencentImException;
  4. use App\Exceptions\TencentImGroupException;
  5. use App\Models\TencentImGroup;
  6. use App\Traits\TencentIm;
  7. use Carbon\Carbon;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. class TencentImGroupService
  11. {
  12. use TencentIm;
  13. const TENCENT_REST_APIS = [
  14. 'createGroup' => 'v4/group_open_http_svc/create_group', //创建群组 支持同时创建多个
  15. 'getGroupList' => 'v4/group_open_http_svc/get_appid_group_list', //获取app中所有群组
  16. ];
  17. const IM_GROUP_RAW_TYPE_PUBLIC = 'Public'; //陌生人社交群
  18. const IM_GROUP_RAW_TYPE_Private = 'Private'; //即 Work,好友工作群
  19. const IM_GROUP_RAW_TYPE_ChatRoom = 'ChatRoom'; //即 Meeting,会议群
  20. const IM_GROUP_RAW_TYPE_AVChatRoom = 'AVChatRoom'; //直播群
  21. const IM_GROUP_RAW_TYPE_TIPS = [
  22. self::IM_GROUP_RAW_TYPE_PUBLIC => '陌生人交友群',
  23. self::IM_GROUP_RAW_TYPE_Private => '好友工作群',
  24. self::IM_GROUP_RAW_TYPE_ChatRoom => '会议群',
  25. self::IM_GROUP_RAW_TYPE_AVChatRoom => '直播群',
  26. ];
  27. /**
  28. * 获取所有 APP 中的群组
  29. * @param int $limit
  30. * @param int $next
  31. * @return \Psr\Http\Message\ResponseInterface
  32. * @throws \App\Exceptions\TencentImAccountException
  33. * @throws \App\Exceptions\TencentImException
  34. * @throws \GuzzleHttp\Exception\GuzzleException
  35. */
  36. public function getGroupList(int $limit = 10000, int $next = 0)
  37. {
  38. $this->restApiName = self::TENCENT_REST_APIS['getGroupList'];
  39. $baseApiHost = $this->getTencentImRestApiBaseHost();
  40. $params = [
  41. 'Limit' => $limit,
  42. 'Next' => $next
  43. ];
  44. $apiResult = $this->requestApi($baseApiHost, $params);
  45. self::verifyApiResult($apiResult);
  46. return $apiResult;
  47. }
  48. /**
  49. * 验证类型是否存在
  50. * @param string $groupType
  51. * @return bool
  52. * @throws TencentImGroupException
  53. */
  54. static public function verifyImGroupType(string $groupType)
  55. {
  56. if (!array_key_exists($groupType, TencentImGroup::IM_GROUP_RAW_TYPE_TIPS)) {
  57. throw new TencentImGroupException('群类型不存在');
  58. }
  59. return true;
  60. }
  61. /**
  62. * 验证当日建群上限,并返回当前可以添加的数量
  63. * @return int
  64. * @throws TencentImGroupException
  65. */
  66. static public function verifyToDayCreateCount()
  67. {
  68. if (TencentImGroup::getToDayCreateCount() >= TencentImGroup::IM_GROUP_CREATE_DAY_MAX) {
  69. throw new TencentImGroupException('单日新建群组上限为: ' . TencentImGroup::IM_GROUP_CREATE_DAY_MAX);
  70. }
  71. return (TencentImGroup::IM_GROUP_CREATE_DAY_MAX - TencentImGroup::getToDayCreateCount());
  72. }
  73. /**
  74. * 验证当月建群上限,并返回可以添加的数量
  75. * @return int
  76. * @throws TencentImGroupException
  77. */
  78. static public function verifyMonthCreateCount()
  79. {
  80. if (TencentImGroup::getMonthCreateCount() >= TencentImGroup::IM_GROUP_CREATE_MONTH_MAX) {
  81. throw new TencentImGroupException('单月新建群组上限为: ' . TencentImGroup::IM_GROUP_CREATE_MONTH_MAX);
  82. }
  83. return (TencentImGroup::IM_GROUP_CREATE_MONTH_MAX - TencentImGroup::getMonthCreateCount());
  84. }
  85. /**
  86. * 验证是否可以添加群
  87. * @param int $groupCount
  88. * @throws TencentImGroupException
  89. */
  90. static public function verifyIsCreateGroup(int $groupCount)
  91. {
  92. //验证单次最低
  93. if ($groupCount < TencentImGroup::IM_GROUP_ONCE_CREATE_MIN) {
  94. throw new TencentImGroupException('单次添加最低' . TencentImGroup::IM_GROUP_ONCE_CREATE_MIN . ' 个群');
  95. }
  96. //验证单次最多
  97. if ($groupCount > TencentImGroup::IM_GROUP_ONCE_CREATE_MAX) {
  98. throw new TencentImGroupException('单次添加最多' . TencentImGroup::IM_GROUP_ONCE_CREATE_MAX . ' 个群');
  99. }
  100. //验证当天添加群数量
  101. $canToDayCount = self::verifyToDayCreateCount();
  102. //验证当月添加上限
  103. $canMonthCount = self::verifyMonthCreateCount();
  104. if ($groupCount > $canToDayCount) {
  105. throw new TencentImGroupException('当前创建数量超过日上限,当日还可创建 ' . $canToDayCount . '个');
  106. }
  107. if ($groupCount > $canMonthCount) {
  108. throw new TencentImGroupException('当前创建数量超过月上限,当月还可创建 ' . $canMonthCount . '个');
  109. }
  110. return true;
  111. }
  112. /**
  113. *
  114. * @param int $groupCount
  115. * @param string $groupType
  116. * @throws TencentImGroupException
  117. * @throws \App\Exceptions\TencentImException
  118. */
  119. public function createGroups(int $groupCount, string $groupType = TencentImGroup::DEFAULT_IM_RAW_GROUP_TYPE)
  120. {
  121. try {
  122. //验证群类型是否存在
  123. self::verifyImGroupType($groupType);
  124. //是否可以创建群
  125. self::verifyIsCreateGroup($groupCount);
  126. DB::beginTransaction();
  127. $this->restApiName = self::TENCENT_REST_APIS['createGroup'];
  128. $baseApiHost = $this->getTencentImRestApiBaseHost();
  129. //TODO 做单点登录,避免并发
  130. $groupStartIndex = TencentImGroup::getGroupIndex();
  131. $groupEndIndex = $groupStartIndex + $groupCount;
  132. for ($i = $groupStartIndex; $i < $groupEndIndex; $i++) {
  133. $item = [
  134. //TODO 新建群默认群主为APP管理员
  135. 'Owner_Account' => config('im.identifier'),
  136. 'Type' => TencentImGroup::IM_GROUP_RAW_TYPE_ChatRoom,
  137. 'Name' => (string)$i,
  138. ];
  139. $apiResult = $this->requestApi($baseApiHost, $item);
  140. self::verifyApiResult($apiResult);
  141. $item['GroupId'] = $apiResult['GroupId'];
  142. $item['groupIndex'] = $i;
  143. $params[] = $item;
  144. }
  145. unset($item);
  146. if (count($params) != $groupCount) {
  147. throw new TencentImGroupException('添加群组失败!');
  148. }
  149. $params = array_map(function ($value) {
  150. return [
  151. 'group_name' => $value['Name'],
  152. 'im_group_raw_id' => $value['GroupId'] ?? $value['groupIndex'],
  153. 'group_owner_user_id' => null,
  154. 'im_group_owner_raw_user_id' => $value['Owner_Account'],
  155. 'group_type' => null,
  156. 'im_group_raw_type' => $value['Type'],
  157. 'is_use' => TencentImGroup::UN_USE,
  158. 'bind_system_im_group_id' => null,
  159. 'group_index' => $value['groupIndex'] ?? 0,
  160. 'created_at' => Carbon::now(),
  161. 'updated_at' => Carbon::now(),
  162. ];
  163. }, $params);
  164. if (!TencentImGroup::insert($params)) {
  165. Log::log(date('Y-m-d H:i:s', time()) . '创建IM群组,写入系统记录失败', $params);
  166. throw new TencentImGroupException('创建IM群写入系统记录失败,请联系管理员检查');
  167. }
  168. DB::commit();
  169. return true;
  170. } catch (TencentImGroupException $e) {
  171. DB::rollBack();
  172. return ['error' => $e->getMessage()];
  173. } catch (TencentImException $e) {
  174. DB::rollBack();
  175. dd($e);
  176. return ['error' => $e->getMessage()];
  177. } catch (\Exception $e) {
  178. DB::rollBack();
  179. dd($e);
  180. return ['error' => $e->getMessage()];
  181. }
  182. }
  183. public function createGroup(array $createData)
  184. {
  185. $this->restApiName = self::TENCENT_REST_APIS['createGroup'];
  186. $baseApiHost = $this->getTencentImRestApiBaseHost();
  187. //TODO 做单点登录,避免并发
  188. $params = [
  189. //TODO 新建群默认群主为APP管理员
  190. 'Owner_Account' => config('im.identifier'),
  191. 'Type' => TencentImGroup::IM_GROUP_RAW_TYPE_ChatRoom,
  192. 'Name' => TencentImGroup::getGroupIndex(),
  193. ];
  194. $apiResult = $this->requestApi($baseApiHost, $params);
  195. dd($apiResult);
  196. }
  197. /**
  198. * 构建添加群数组
  199. * @param int $groupCount
  200. * @return mixed
  201. */
  202. private function generateCreateGroupData(int $groupCount)
  203. {
  204. $groupStartIndex = TencentImGroup::getGroupIndex();
  205. $groupEndIndex = $groupStartIndex + $groupCount;
  206. for ($i = $groupStartIndex; $i < $groupEndIndex; $i++) {
  207. $item = [
  208. //TODO 新建群默认群主为APP管理员
  209. 'Owner_Account' => config('im.identifier'),
  210. 'Type' => TencentImGroup::IM_GROUP_RAW_TYPE_ChatRoom,
  211. 'Name' => $i,
  212. ];
  213. $this->createGroup($item);
  214. $params[] = $item;
  215. }
  216. unset($item);
  217. return $params;
  218. }
  219. }