TencentImAccountService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. 'portrait_set' => 'v4/profile/portrait_set', //修改资料
  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. ];
  35. if ($user->name) {
  36. $params = Arr::add($params, 'Nick', $user->name);
  37. }
  38. if ($user->avatar) {
  39. $params = Arr::add($params, 'FaceUrl', $user->avatar);
  40. }
  41. $apiResult = $this->requestApi($baseApiHost, $params);
  42. self::verifyApiResult($apiResult);
  43. return self::IM_IDENTIFIER_PREFIX . $user->id;
  44. }
  45. /**
  46. * 导入多个账号
  47. * @param array $accounts
  48. * @return array|string[]
  49. * @throws TencentImAccountException
  50. * @throws \App\Exceptions\TencentImException
  51. * @throws \GuzzleHttp\Exception\GuzzleException
  52. */
  53. public function multiAccountImport(array $accounts)
  54. {
  55. $this->restApiName = self::TENCENT_REST_APIS['multiAccountImport'];
  56. $baseApiHost = $this->getTencentImRestApiBaseHost();
  57. $accounts = array_unique(array_filter($accounts));
  58. $this->verifyRestApiMaxItem($accounts);
  59. $accounts = array_map(function ($value) {
  60. return self::IM_IDENTIFIER_PREFIX . $value;
  61. }, $accounts);
  62. $params = [
  63. 'Accounts' => $accounts,
  64. ];
  65. $apiResult = $this->requestApi($baseApiHost, $params);
  66. self::verifyApiResult($apiResult);
  67. return $accounts;
  68. }
  69. /**
  70. * 删除账号
  71. * @param array $accounts
  72. * @return bool
  73. * @throws TencentImAccountException
  74. * @throws \App\Exceptions\TencentImException
  75. * @throws \GuzzleHttp\Exception\GuzzleException
  76. */
  77. public function accountDelete(array $accounts)
  78. {
  79. $this->restApiName = self::TENCENT_REST_APIS['accountDelete'];
  80. $baseApiHost = $this->getTencentImRestApiBaseHost();
  81. $accounts = array_unique(array_filter($accounts));
  82. $this->verifyRestApiMaxItem($accounts);
  83. $accounts = array_map(function ($value) {
  84. $UserID = $value;
  85. return compact('UserID');
  86. }, $accounts);
  87. $params = [
  88. 'DeleteItem' => $accounts
  89. ];
  90. $apiResult = $this->requestApi($baseApiHost, $params);
  91. self::verifyApiResult($apiResult);
  92. return true;
  93. }
  94. /**
  95. * 查询帐号是否已导入IM
  96. * @param array $accounts
  97. * @return mixed
  98. * @throws TencentImAccountException
  99. * @throws \App\Exceptions\TencentImException
  100. * @throws \GuzzleHttp\Exception\GuzzleException
  101. */
  102. public function accountCheck(array $accounts)
  103. {
  104. $this->restApiName = self::TENCENT_REST_APIS['accountCheck'];
  105. $baseApiHost = $this->getTencentImRestApiBaseHost();
  106. $member_list = array();
  107. foreach ($accounts as $k=>$v){
  108. $acc_arr = array();
  109. if(!empty($v['tencent_im_user_id'])){
  110. $acc_arr['UserID'] = $v['tencent_im_user_id'];
  111. $member_list []= $acc_arr;
  112. }
  113. }
  114. $params = [
  115. 'CheckItem' => $member_list
  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. /**
  180. * 修改资料
  181. */
  182. public function changeInfo($data){
  183. $this->restApiName = self::TENCENT_REST_APIS['portrait_set'];
  184. $baseApiHost = $this->getTencentImRestApiBaseHost();
  185. $arrs = array();
  186. if(!empty($data['name'])){
  187. $arr['Tag'] = 'Tag_Profile_IM_Nick';
  188. $arr['Value'] = $data['name'];
  189. $arrs []=$arr;
  190. }
  191. if(!empty($data['avatar'])){
  192. $arr['Tag'] = 'Tag_Profile_IM_Image';
  193. $arr['Value'] = $data['avatar'];
  194. $arrs []=$arr;
  195. }
  196. $params = [
  197. 'From_Account'=>$data['tencent_im_user_id'],
  198. 'ProfileItem' => $arrs
  199. ];
  200. $apiResult = $this->requestApi($baseApiHost, $params);
  201. self::verifyApiResult($apiResult);
  202. return true;
  203. }
  204. }