TencentImAccountService.php 6.4 KB

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