|
@@ -85,7 +85,6 @@ class WechatService
|
|
|
|
|
|
// 执行登录
|
|
|
return $this->accountService->wxLogin($user->getId(), $userInfo);
|
|
|
-
|
|
|
} catch (BusinessException $e) {
|
|
|
throw $e;
|
|
|
} catch (\Exception $e) {
|
|
@@ -98,24 +97,22 @@ class WechatService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 生成随机state
|
|
|
- */
|
|
|
- protected function generateState(): string
|
|
|
- {
|
|
|
- return md5(uniqid(microtime(true), true));
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 缓存授权state
|
|
|
*/
|
|
|
protected function cacheAuthState(string $state): void
|
|
|
{
|
|
|
- Cache::put(
|
|
|
- $this->getAuthStateKey($state),
|
|
|
- true,
|
|
|
- config('wechat.auth.cache_ttl')
|
|
|
- );
|
|
|
+ try {
|
|
|
+ $states = Cache::get('wechat_auth_states', []);
|
|
|
+ $states[$state] = true;
|
|
|
+ Cache::put('wechat_auth_states', $states, now()->addMinutes(30));
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('缓存微信授权state失败', [
|
|
|
+ 'state' => $state,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ throw new BusinessException('系统错误,请稍后重试');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -125,11 +122,36 @@ class WechatService
|
|
|
*/
|
|
|
protected function validateState(string $state): void
|
|
|
{
|
|
|
- if (! Cache::pull($this->getAuthStateKey($state))) {
|
|
|
- throw new BusinessException('无效的授权请求');
|
|
|
+ try {
|
|
|
+ $states = Cache::get('wechat_auth_states', []);
|
|
|
+
|
|
|
+ if (!isset($states[$state])) {
|
|
|
+ Log::warning('微信授权state无效', ['state' => $state]);
|
|
|
+ throw new BusinessException('授权已过期,请重新授权');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除已使用的state
|
|
|
+ unset($states[$state]);
|
|
|
+ Cache::put('wechat_auth_states', $states, now()->addMinutes(30));
|
|
|
+ } catch (BusinessException $e) {
|
|
|
+ throw $e;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('验证微信授权state失败', [
|
|
|
+ 'state' => $state,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ throw new BusinessException('系统错误,请稍后重试');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 生成随机state
|
|
|
+ */
|
|
|
+ protected function generateState(): string
|
|
|
+ {
|
|
|
+ return md5(uniqid((string)mt_rand(), true));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 格式化用户信息
|
|
|
*
|
|
@@ -182,7 +204,7 @@ class WechatService
|
|
|
|
|
|
protected function getAuthStateKey(string $state): string
|
|
|
{
|
|
|
- return config('wechat.auth.cache_prefix').$state;
|
|
|
+ return config('wechat.auth.cache_prefix') . $state;
|
|
|
}
|
|
|
|
|
|
/**
|