WechatService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Exceptions\BusinessException;
  4. use EasyWeChat\OfficialAccount\Application;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Log;
  7. use Overtrue\Socialite\Contracts\ProviderInterface;
  8. use Overtrue\Socialite\Contracts\UserInterface;
  9. class WechatService
  10. {
  11. protected Application $app;
  12. protected AccountService $accountService;
  13. protected ProviderInterface $socialite;
  14. public function __construct(AccountService $accountService)
  15. {
  16. $this->app = new Application(config('wechat.official_account.default'));
  17. $this->accountService = $accountService;
  18. $this->socialite = $this->app->getOAuth();
  19. }
  20. /**
  21. * 获取微信授权URL
  22. *
  23. * @param string $redirectUrl 授权后重定向地址
  24. * @param string $scope 授权范围 snsapi_base或snsapi_userinfo
  25. * @return array{auth_url: string, state: string}
  26. *
  27. * @throws BusinessException
  28. */
  29. public function getAuthUrl(string $redirectUrl, ?string $scope = null): array
  30. {
  31. $scope = $scope ?? config('wechat.auth.default_scope');
  32. try {
  33. $state = $this->generateState();
  34. $this->cacheAuthState($state);
  35. $url = $this->socialite
  36. ->scopes([$scope])
  37. ->withState($state)
  38. ->redirect($redirectUrl);
  39. return [
  40. 'auth_url' => $url,
  41. 'state' => $state,
  42. ];
  43. } catch (\Exception $e) {
  44. $this->logError('生成微信授权URL失败', $e);
  45. throw new BusinessException('生成授权链接失败,请稍后重试');
  46. }
  47. }
  48. /**
  49. * 处理微信授权回调
  50. *
  51. * @param string $code 授权码
  52. * @param string $state 状态码
  53. * @param string|null $inviteCode 邀请码
  54. * @return array{token: string, user: array}
  55. *
  56. * @throws BusinessException
  57. */
  58. public function handleAuthCallback(string $code, string $state, ?string $inviteCode = null): array
  59. {
  60. try {
  61. // 验证state
  62. $this->validateState($state);
  63. // 获取用户信息
  64. $user = $this->socialite->userFromCode($code);
  65. // 整理用户信息
  66. $userInfo = $this->formatUserInfo($user);
  67. // 添加邀请人信息
  68. if ($inviteCode) {
  69. $userInfo['invite_code'] = $inviteCode;
  70. }
  71. // 执行登录
  72. return $this->accountService->wxLogin($user->getId(), $userInfo);
  73. } catch (BusinessException $e) {
  74. throw $e;
  75. } catch (\Exception $e) {
  76. $this->logError('微信授权回调处理失败', $e, [
  77. 'code' => $code,
  78. 'state' => $state,
  79. 'invite_code' => $inviteCode,
  80. ]);
  81. throw new BusinessException('微信授权失败,请稍后重试');
  82. }
  83. }
  84. /**
  85. * 生成随机state
  86. */
  87. protected function generateState(): string
  88. {
  89. return md5(uniqid(microtime(true), true));
  90. }
  91. /**
  92. * 缓存授权state
  93. */
  94. protected function cacheAuthState(string $state): void
  95. {
  96. Cache::put(
  97. $this->getAuthStateKey($state),
  98. true,
  99. config('wechat.auth.cache_ttl')
  100. );
  101. }
  102. /**
  103. * 验证state
  104. *
  105. * @throws BusinessException
  106. */
  107. protected function validateState(string $state): void
  108. {
  109. if (! Cache::pull($this->getAuthStateKey($state))) {
  110. throw new BusinessException('无效的授权请求');
  111. }
  112. }
  113. /**
  114. * 格式化用户信息
  115. *
  116. * @return array{
  117. * nickname: string,
  118. * avatar: string,
  119. * gender: string|null,
  120. * country: string|null,
  121. * province: string|null,
  122. * city: string|null
  123. * }
  124. */
  125. protected function formatUserInfo(UserInterface $user): array
  126. {
  127. return [
  128. 'nickname' => $user->getName(),
  129. 'avatar' => $user->getAvatar(),
  130. 'gender' => $this->formatGender($user->getRaw()['sex'] ?? null),
  131. 'country' => $user->getRaw()['country'] ?? null,
  132. 'province' => $user->getRaw()['province'] ?? null,
  133. 'city' => $user->getRaw()['city'] ?? null,
  134. ];
  135. }
  136. /**
  137. * 格式化性别信息
  138. *
  139. * @param int|null $gender 微信返回的性别值:1为男性,2为女性,0为未知
  140. * @return string|null male/female/null
  141. */
  142. protected function formatGender(?int $gender): ?string
  143. {
  144. return match ($gender) {
  145. 1 => 'male',
  146. 2 => 'female',
  147. default => null,
  148. };
  149. }
  150. /**
  151. * 记录错误日志
  152. */
  153. protected function logError(string $message, \Exception $e, array $context = []): void
  154. {
  155. Log::error($message, array_merge($context, [
  156. 'error' => $e->getMessage(),
  157. 'trace' => $e->getTraceAsString(),
  158. ]));
  159. }
  160. protected function getAuthStateKey(string $state): string
  161. {
  162. return config('wechat.auth.cache_prefix').$state;
  163. }
  164. }