TencentImAccountService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 = 'ORANGEPAI_USER_';
  11. // const IM_IDENTIFIER_PREFIX = 'IM_USER_';
  12. const TENCENT_REST_APIS = [
  13. 'accountImport' => 'v4/im_open_login_svc/account_import', //导入单个帐号
  14. 'multiAccountImport' => 'v4/im_open_login_svc/multiaccount_import', //导入多个帐号
  15. 'accountDelete' => 'v4/im_open_login_svc/account_delete', //删除帐号
  16. 'accountCheck' => 'v4/im_open_login_svc/account_check', //查询帐号是否已导入IM
  17. 'kick' => 'v4/im_open_login_svc/kick', //失效帐号登录态
  18. 'queryState' => 'v4/openim/querystate', //查询帐号在线状态
  19. ];
  20. /**
  21. * 导入单个账号
  22. * @param User $user
  23. * @return string
  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. * @param array $accounts
  50. * @return array|string[]
  51. * @throws TencentImAccountException
  52. * @throws \App\Exceptions\TencentImException
  53. * @throws \GuzzleHttp\Exception\GuzzleException
  54. */
  55. public function multiAccountImport(array $accounts)
  56. {
  57. $this->restApiName = self::TENCENT_REST_APIS['multiAccountImport'];
  58. $baseApiHost = $this->getTencentImRestApiBaseHost();
  59. $accounts = array_unique(array_filter($accounts));
  60. $this->verifyRestApiMaxItem($accounts);
  61. $accounts = array_map(function ($value) {
  62. return self::IM_IDENTIFIER_PREFIX . $value;
  63. }, $accounts);
  64. $params = [
  65. 'Accounts' => $accounts,
  66. ];
  67. $apiResult = $this->requestApi($baseApiHost, $params);
  68. self::verifyApiResult($apiResult);
  69. return $accounts;
  70. }
  71. /**
  72. * 删除账号
  73. * @param array $accounts
  74. * @return bool
  75. * @throws TencentImAccountException
  76. * @throws \App\Exceptions\TencentImException
  77. * @throws \GuzzleHttp\Exception\GuzzleException
  78. */
  79. public function accountDelete(array $accounts)
  80. {
  81. $this->restApiName = self::TENCENT_REST_APIS['accountDelete'];
  82. $baseApiHost = $this->getTencentImRestApiBaseHost();
  83. $accounts = array_unique(array_filter($accounts));
  84. $this->verifyRestApiMaxItem($accounts);
  85. $accounts = array_map(function ($value) {
  86. $UserID = $value;
  87. return compact('UserID');
  88. }, $accounts);
  89. $params = [
  90. 'DeleteItem' => $accounts
  91. ];
  92. $apiResult = $this->requestApi($baseApiHost, $params);
  93. self::verifyApiResult($apiResult);
  94. return true;
  95. }
  96. /**
  97. * 查询帐号是否已导入IM
  98. * @param array $accounts
  99. * @return mixed
  100. * @throws TencentImAccountException
  101. * @throws \App\Exceptions\TencentImException
  102. * @throws \GuzzleHttp\Exception\GuzzleException
  103. */
  104. public function accountCheck(array $accounts)
  105. {
  106. $this->restApiName = self::TENCENT_REST_APIS['accountCheck'];
  107. $baseApiHost = $this->getTencentImRestApiBaseHost();
  108. $accounts = array_unique(array_filter($accounts));
  109. $this->verifyRestApiMaxItem($accounts);
  110. $accounts = array_map(function ($value) {
  111. $UserID = $value;
  112. return compact('UserID');
  113. }, $accounts);
  114. $params = [
  115. 'CheckItem' => $accounts
  116. ];
  117. $apiResult = $this->requestApi($baseApiHost, $params);
  118. self::verifyApiResult($apiResult);
  119. return $apiResult['ResultItem'];
  120. }
  121. /**
  122. * 强制下线
  123. * @param string $identifier
  124. * @return bool
  125. * @throws TencentImAccountException
  126. * @throws \App\Exceptions\TencentImException
  127. * @throws \GuzzleHttp\Exception\GuzzleException
  128. */
  129. public function kick(string $identifier)
  130. {
  131. $this->restApiName = self::TENCENT_REST_APIS['kick'];
  132. $baseApiHost = $this->getTencentImRestApiBaseHost();
  133. $params = ['Identifier' => $identifier];
  134. $apiResult = $this->requestApi($baseApiHost, $params);
  135. self::verifyApiResult($apiResult);
  136. return true;
  137. }
  138. /**
  139. * 获取用户当前的登录状态
  140. * @param array $accounts
  141. * @param bool $isDetail
  142. * @return mixed
  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. * @param $apiResult
  163. * @return bool
  164. * @throws TencentImAccountException
  165. */
  166. static public function verifyApiResult($apiResult)
  167. {
  168. if (!is_array($apiResult)) {
  169. throw new TencentImAccountException('IM 请求失败');
  170. }
  171. if (count(array_diff(['ActionStatus', 'ErrorCode', 'ErrorInfo'], array_keys($apiResult))) > 0) {
  172. throw new TencentImAccountException('IM 接口返回异常');
  173. }
  174. if (($apiResult['ActionStatus'] != 'OK') || ($apiResult['ErrorCode'] != 0)) {
  175. throw new TencentImAccountException('操作失败: ' . $apiResult['ErrorInfo']);
  176. }
  177. return true;
  178. }
  179. }