TencentImAccountService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\TencentImAccountException;
  4. use App\Models\User;
  5. use App\Traits\TencentIm;
  6. use Illuminate\Support\Arr;
  7. class TencentImAccountService
  8. {
  9. use TencentIm;
  10. public const IM_IDENTIFIER_PREFIX = 'IM_USER_';
  11. public const TENCENT_REST_APIS = [
  12. 'accountImport' => 'v4/im_open_login_svc/account_import', // 导入单个帐号
  13. 'multiAccountImport' => 'v4/im_open_login_svc/multiaccount_import', // 导入多个帐号
  14. 'accountDelete' => 'v4/im_open_login_svc/account_delete', // 删除帐号
  15. 'accountCheck' => 'v4/im_open_login_svc/account_check', // 查询帐号是否已导入IM
  16. 'kick' => 'v4/im_open_login_svc/kick', // 失效帐号登录态
  17. 'queryState' => 'v4/openim/querystate', // 查询帐号在线状态
  18. ];
  19. /**
  20. * 导入单个账号.
  21. *
  22. * @return string
  23. *
  24. * @throws TencentImAccountException
  25. * @throws \App\Exceptions\TencentImException
  26. * @throws \GuzzleHttp\Exception\GuzzleException
  27. */
  28. public function accountImport(User $user)
  29. {
  30. $this->restApiName = self::TENCENT_REST_APIS['accountImport'];
  31. $baseApiHost = $this->getTencentImRestApiBaseHost();
  32. $params = [
  33. 'Identifier' => self::IM_IDENTIFIER_PREFIX . $user->id,
  34. // 'Nick' => $user->nickname ?? null,
  35. // 'FaceUrl' => valid_url($user->avatar)
  36. ];
  37. if ($user->nickname) {
  38. $params = Arr::add($params, 'Nick', $user->nickName);
  39. }
  40. if ($user->avatar) {
  41. $params = Arr::add($params, 'FaceUrl', $user->avatar);
  42. }
  43. $apiResult = $this->requestApi($baseApiHost, $params);
  44. self::verifyApiResult($apiResult);
  45. return self::IM_IDENTIFIER_PREFIX . $user->id;
  46. }
  47. /**
  48. * 导入多个账号.
  49. *
  50. * @return array|string[]
  51. *
  52. * @throws TencentImAccountException
  53. * @throws \App\Exceptions\TencentImException
  54. * @throws \GuzzleHttp\Exception\GuzzleException
  55. */
  56. public function multiAccountImport(array $accounts)
  57. {
  58. $this->restApiName = self::TENCENT_REST_APIS['multiAccountImport'];
  59. $baseApiHost = $this->getTencentImRestApiBaseHost();
  60. $accounts = array_unique(array_filter($accounts));
  61. $this->verifyRestApiMaxItem($accounts);
  62. $accounts = array_map(function ($value) {
  63. return self::IM_IDENTIFIER_PREFIX . $value;
  64. }, $accounts);
  65. $params = [
  66. 'Accounts' => $accounts,
  67. ];
  68. $apiResult = $this->requestApi($baseApiHost, $params);
  69. self::verifyApiResult($apiResult);
  70. return $accounts;
  71. }
  72. /**
  73. * 删除账号.
  74. *
  75. * @return bool
  76. *
  77. * @throws TencentImAccountException
  78. * @throws \App\Exceptions\TencentImException
  79. * @throws \GuzzleHttp\Exception\GuzzleException
  80. */
  81. public function accountDelete(array $accounts)
  82. {
  83. $this->restApiName = self::TENCENT_REST_APIS['accountDelete'];
  84. $baseApiHost = $this->getTencentImRestApiBaseHost();
  85. $accounts = array_unique(array_filter($accounts));
  86. $this->verifyRestApiMaxItem($accounts);
  87. $accounts = array_map(function ($value) {
  88. $UserID = $value;
  89. return compact('UserID');
  90. }, $accounts);
  91. $params = [
  92. 'DeleteItem' => $accounts,
  93. ];
  94. $apiResult = $this->requestApi($baseApiHost, $params);
  95. self::verifyApiResult($apiResult);
  96. return true;
  97. }
  98. /**
  99. * 查询帐号是否已导入IM.
  100. *
  101. * @throws TencentImAccountException
  102. * @throws \App\Exceptions\TencentImException
  103. * @throws \GuzzleHttp\Exception\GuzzleException
  104. */
  105. public function accountCheck(array $accounts)
  106. {
  107. $this->restApiName = self::TENCENT_REST_APIS['accountCheck'];
  108. $baseApiHost = $this->getTencentImRestApiBaseHost();
  109. $accounts = array_unique(array_filter($accounts));
  110. $this->verifyRestApiMaxItem($accounts);
  111. $accounts = array_map(function ($value) {
  112. $UserID = $value;
  113. return compact('UserID');
  114. }, $accounts);
  115. $params = [
  116. 'CheckItem' => $accounts,
  117. ];
  118. $apiResult = $this->requestApi($baseApiHost, $params);
  119. self::verifyApiResult($apiResult);
  120. return $apiResult['ResultItem'];
  121. }
  122. /**
  123. * 强制下线
  124. *
  125. * @return bool
  126. *
  127. * @throws TencentImAccountException
  128. * @throws \App\Exceptions\TencentImException
  129. * @throws \GuzzleHttp\Exception\GuzzleException
  130. */
  131. public function kick(string $identifier)
  132. {
  133. $this->restApiName = self::TENCENT_REST_APIS['kick'];
  134. $baseApiHost = $this->getTencentImRestApiBaseHost();
  135. $params = ['Identifier' => $identifier];
  136. $apiResult = $this->requestApi($baseApiHost, $params);
  137. self::verifyApiResult($apiResult);
  138. return true;
  139. }
  140. /**
  141. * 获取用户当前的登录状态
  142. *
  143. * @throws TencentImAccountException
  144. * @throws \App\Exceptions\TencentImException
  145. * @throws \GuzzleHttp\Exception\GuzzleException
  146. */
  147. public function queryState(array $accounts, bool $isDetail = false)
  148. {
  149. $this->restApiName = self::TENCENT_REST_APIS['queryState'];
  150. $baseApiHost = $this->getTencentImRestApiBaseHost();
  151. $accounts = array_unique(array_filter($accounts));
  152. $this->verifyRestApiMaxItem($accounts);
  153. $params = [
  154. 'To_Account' => $accounts,
  155. ];
  156. $apiResult = $this->requestApi($baseApiHost, $params);
  157. self::verifyApiResult($apiResult);
  158. return $apiResult['QueryResult'];
  159. }
  160. /**
  161. * 验证接口返回.
  162. *
  163. * @return bool
  164. *
  165. * @throws TencentImAccountException
  166. */
  167. public static function verifyApiResult($apiResult)
  168. {
  169. if (!is_array($apiResult)) {
  170. throw new TencentImAccountException('IM 请求失败');
  171. }
  172. if (count(array_diff(['ActionStatus', 'ErrorCode', 'ErrorInfo'], array_keys($apiResult))) > 0) {
  173. throw new TencentImAccountException('IM 接口返回异常');
  174. }
  175. if (('OK' != $apiResult['ActionStatus']) || (0 != $apiResult['ErrorCode'])) {
  176. throw new TencentImAccountException('操作失败: ' . $apiResult['ErrorInfo']);
  177. }
  178. return true;
  179. }
  180. }