WechatService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 cacheAuthState(string $state): void
  88. {
  89. try {
  90. $states = Cache::get('wechat_auth_states', []);
  91. $states[$state] = true;
  92. Cache::put('wechat_auth_states', $states, now()->addMinutes(30));
  93. } catch (\Exception $e) {
  94. Log::error('缓存微信授权state失败', [
  95. 'state' => $state,
  96. 'error' => $e->getMessage()
  97. ]);
  98. throw new BusinessException('系统错误,请稍后重试');
  99. }
  100. }
  101. /**
  102. * 验证state
  103. *
  104. * @throws BusinessException
  105. */
  106. protected function validateState(string $state): void
  107. {
  108. try {
  109. $states = Cache::get('wechat_auth_states', []);
  110. if (!isset($states[$state])) {
  111. Log::warning('微信授权state无效', ['state' => $state]);
  112. throw new BusinessException('授权已过期,请重新授权');
  113. }
  114. // 移除已使用的state
  115. unset($states[$state]);
  116. Cache::put('wechat_auth_states', $states, now()->addMinutes(30));
  117. } catch (BusinessException $e) {
  118. throw $e;
  119. } catch (\Exception $e) {
  120. Log::error('验证微信授权state失败', [
  121. 'state' => $state,
  122. 'error' => $e->getMessage()
  123. ]);
  124. throw new BusinessException('系统错误,请稍后重试');
  125. }
  126. }
  127. /**
  128. * 生成随机state
  129. */
  130. protected function generateState(): string
  131. {
  132. return md5(uniqid((string)mt_rand(), true));
  133. }
  134. /**
  135. * 格式化用户信息
  136. *
  137. * @return array{
  138. * nickname: string,
  139. * avatar: string,
  140. * gender: string|null,
  141. * country: string|null,
  142. * province: string|null,
  143. * city: string|null
  144. * }
  145. */
  146. protected function formatUserInfo(UserInterface $user): array
  147. {
  148. return [
  149. 'nickname' => $user->getName(),
  150. 'avatar' => $user->getAvatar(),
  151. 'gender' => $this->formatGender($user->getRaw()['sex'] ?? null),
  152. 'country' => $user->getRaw()['country'] ?? null,
  153. 'province' => $user->getRaw()['province'] ?? null,
  154. 'city' => $user->getRaw()['city'] ?? null,
  155. ];
  156. }
  157. /**
  158. * 格式化性别信息
  159. *
  160. * @param int|null $gender 微信返回的性别值:1为男性,2为女性,0为未知
  161. * @return string|null male/female/null
  162. */
  163. protected function formatGender(?int $gender): ?string
  164. {
  165. return match ($gender) {
  166. 1 => 'male',
  167. 2 => 'female',
  168. default => null,
  169. };
  170. }
  171. /**
  172. * 记录错误日志
  173. */
  174. protected function logError(string $message, \Exception $e, array $context = []): void
  175. {
  176. Log::error($message, array_merge($context, [
  177. 'error' => $e->getMessage(),
  178. 'trace' => $e->getTraceAsString(),
  179. ]));
  180. }
  181. protected function getAuthStateKey(string $state): string
  182. {
  183. return config('wechat.auth.cache_prefix') . $state;
  184. }
  185. /**
  186. * 获取JSAPI配置
  187. *
  188. * @param string $url 当前网页的URL,不包含#及其后面部分
  189. * @return array JSAPI配置信息
  190. *
  191. * @throws BusinessException
  192. */
  193. public function getJsConfig(string $url): array
  194. {
  195. try {
  196. $apis = [
  197. 'updateAppMessageShareData',
  198. 'updateTimelineShareData',
  199. 'chooseImage',
  200. 'previewImage',
  201. 'uploadImage',
  202. 'downloadImage',
  203. 'getLocation',
  204. 'openLocation',
  205. 'scanQRCode'
  206. ];
  207. return $this->app->getUtils()->buildJsSdkConfig($url, $apis);
  208. } catch (\Throwable $e) {
  209. \Log::error('获取JSAPI配置失败', [
  210. 'url' => $url,
  211. 'error' => $e->getMessage()
  212. ]);
  213. throw new BusinessException('获取JSAPI配置失败,请稍后重试');
  214. }
  215. }
  216. }