TencentImAccountService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_TEST_';
  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. dd($apiResult);
  45. self::verifyApiResult($apiResult);
  46. return self::IM_IDENTIFIER_PREFIX . $user->id;
  47. }
  48. /**
  49. * 导入多个账号
  50. * @param array $accounts
  51. * @return array|string[]
  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. * @param array $accounts
  75. * @return bool
  76. * @throws TencentImAccountException
  77. * @throws \App\Exceptions\TencentImException
  78. * @throws \GuzzleHttp\Exception\GuzzleException
  79. */
  80. public function accountDelete(array $accounts)
  81. {
  82. $this->restApiName = self::TENCENT_REST_APIS['accountDelete'];
  83. $baseApiHost = $this->getTencentImRestApiBaseHost();
  84. $accounts = array_unique(array_filter($accounts));
  85. $this->verifyRestApiMaxItem($accounts);
  86. $accounts = array_map(function ($value) {
  87. $UserID = $value;
  88. return compact('UserID');
  89. }, $accounts);
  90. $params = [
  91. 'DeleteItem' => $accounts
  92. ];
  93. $apiResult = $this->requestApi($baseApiHost, $params);
  94. self::verifyApiResult($apiResult);
  95. return true;
  96. }
  97. /**
  98. * 查询帐号是否已导入IM
  99. * @param array $accounts
  100. * @return mixed
  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. * @param string $identifier
  125. * @return bool
  126. * @throws TencentImAccountException
  127. * @throws \App\Exceptions\TencentImException
  128. * @throws \GuzzleHttp\Exception\GuzzleException
  129. */
  130. public function kick(string $identifier)
  131. {
  132. $this->restApiName = self::TENCENT_REST_APIS['kick'];
  133. $baseApiHost = $this->getTencentImRestApiBaseHost();
  134. $params = ['Identifier' => $identifier];
  135. $apiResult = $this->requestApi($baseApiHost, $params);
  136. self::verifyApiResult($apiResult);
  137. return true;
  138. }
  139. /**
  140. * 获取用户当前的登录状态
  141. * @param array $accounts
  142. * @param bool $isDetail
  143. * @return mixed
  144. * @throws TencentImAccountException
  145. * @throws \App\Exceptions\TencentImException
  146. * @throws \GuzzleHttp\Exception\GuzzleException
  147. */
  148. public function queryState(array $accounts, bool $isDetail = false)
  149. {
  150. $this->restApiName = self::TENCENT_REST_APIS['queryState'];
  151. $baseApiHost = $this->getTencentImRestApiBaseHost();
  152. $accounts = array_unique(array_filter($accounts));
  153. $this->verifyRestApiMaxItem($accounts);
  154. $params = [
  155. 'To_Account' => $accounts
  156. ];
  157. $apiResult = $this->requestApi($baseApiHost, $params);
  158. self::verifyApiResult($apiResult);
  159. return $apiResult['QueryResult'];
  160. }
  161. /**
  162. * 验证接口返回
  163. * @param $apiResult
  164. * @return bool
  165. * @throws TencentImAccountException
  166. */
  167. static public 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 (($apiResult['ActionStatus'] != 'OK') || ($apiResult['ErrorCode'] != 0)) {
  176. throw new TencentImAccountException('操作失败: ' . $apiResult['ErrorInfo']);
  177. }
  178. return true;
  179. }
  180. }