WechatService.php 7.2 KB

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