From e1f71fb22029e84dc03b67ce0d113a62fba013a8 Mon Sep 17 00:00:00 2001 From: kk <875203880@qq.com> Date: Mon, 20 Jul 2026 17:45:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96state=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 29 ++++--------------------- src/pages/auth/loading.vue | 44 +++++++------------------------------- 2 files changed, 12 insertions(+), 61 deletions(-) diff --git a/src/App.vue b/src/App.vue index b657e11..20abd9f 100644 --- a/src/App.vue +++ b/src/App.vue @@ -47,32 +47,11 @@ if (code) { // 有 code → 进入授权 Loading 页处理 - // 从 state 中解析 device_id 和 action(OAuth 标准保证 state 原样回传) - let action = ''; - let callbackDeviceId = deviceId || ''; - try { - const stateStr = urlParams.get('state'); - if (stateStr) { - const stateObj = JSON.parse(decodeURIComponent(stateStr)); - action = stateObj.action || action; - callbackDeviceId = stateObj.device_id || callbackDeviceId; - } - } catch (_) { - // state 解析失败,忽略 - } - // 存入 localStorage,确保 loading 页面复用时也能获取(redirectTo 可能不触发 onLoad) - if (action) { - localStorage.setItem('__auth_action', action); - } - if (callbackDeviceId) { - localStorage.setItem('__auth_device_id', callbackDeviceId); - } + // state 为 "base" 或 "userinfo",直接透传 + const state = urlParams.get('state') || ''; let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code); - if (action) { - loadingUrl += '&action=' + action; - } - if (callbackDeviceId) { - loadingUrl += '&device_id=' + callbackDeviceId; + if (state) { + loadingUrl += '&state=' + state; } uni.redirectTo({ url: loadingUrl }); return; diff --git a/src/pages/auth/loading.vue b/src/pages/auth/loading.vue index 9dded8b..bada88d 100644 --- a/src/pages/auth/loading.vue +++ b/src/pages/auth/loading.vue @@ -38,28 +38,15 @@ export default { code = urlParams.get('code') || urlParams.get('auth_code') || ''; } - // 解析 state 参数(OAuth 标准保证 state 原样回传,包含 action 和 device_id) - if (!this.action || !this.deviceId) { - const stateRaw = options.state || ''; - if (stateRaw) { - try { - const stateObj = JSON.parse(decodeURIComponent(stateRaw)); - if (!this.action && stateObj.action) { - this.action = stateObj.action; - } - if (!this.deviceId && stateObj.device_id) { - this.deviceId = stateObj.device_id; - } - } catch (_) { - // state 解析失败,继续用 localStorage 兜底 - } + // 解析 state 参数(OAuth 原样回传,"base" 或 "userinfo") + if (!this.action) { + const state = options.state || ''; + if (state === 'userinfo') { + this.action = 'userinfo'; } } // 兜底:从 localStorage 恢复(页面复用时 onLoad 不触发,localStorage 由跳转函数清理) - if (!this.action) { - this.action = localStorage.getItem('__auth_action') || ''; - } if (!this.deviceId) { this.deviceId = localStorage.getItem('__auth_device_id') || ''; } @@ -133,17 +120,11 @@ export default { // 静默授权:微信用 snsapi_base,支付宝用 auth_base const scopes = platform === 'alipay' ? 'auth_base' : 'snsapi_base'; - // 构建 state(包含 device_id,回调时原样回传) - const stateObj = {}; - if (this.deviceId) { - stateObj.device_id = this.deviceId; - } - const res = await gatewayGet('/api/v1/user/auth/url', { app_no: appNo, scopes: scopes, redirect_uri: redirectUri, - state: Object.keys(stateObj).length > 0 ? JSON.stringify(stateObj) : '', + state: 'base', }); const data = res.data || {}; @@ -298,7 +279,6 @@ export default { this.statusText = '正在获取用户信息...'; // 存入 localStorage(页面复用时 onLoad 不会重新触发,需要从 localStorage 读取) - localStorage.setItem('__auth_action', 'userinfo'); if (this.deviceId) { localStorage.setItem('__auth_device_id', this.deviceId); } @@ -311,17 +291,11 @@ export default { // 用户信息授权:微信用 snsapi_userinfo,支付宝用 auth_user const scopes = platform === 'alipay' ? 'auth_user' : 'snsapi_userinfo'; - // 构建 state(包含 action 和 device_id,回调时原样回传) - const stateObj = { action: 'userinfo' }; - if (this.deviceId) { - stateObj.device_id = this.deviceId; - } - const res = await gatewayGet('/api/v1/user/auth/url', { app_no: appNo, scopes: scopes, redirect_uri: redirectUri, - state: JSON.stringify(stateObj), + state: 'userinfo', }); const data = res.data || {}; @@ -367,12 +341,11 @@ export default { } uni.setStorageSync('__used_auth_code', code); - // 立即清除 URL 中的 code/action 参数,避免刷新重复处理 + // 立即清除 URL 中的 code/state 参数,避免刷新重复处理 const url = new URL(window.location.href); url.searchParams.delete('code'); url.searchParams.delete('auth_code'); url.searchParams.delete('state'); - url.searchParams.delete('action'); window.history.replaceState({}, '', url.toString()); try { @@ -422,7 +395,6 @@ export default { ? `/pages/index/index?device_id=${deviceId}&action=mine` : '/pages/index/index?action=mine'; localStorage.removeItem('__auth_device_id'); - localStorage.removeItem('__auth_action'); uni.removeStorageSync('__auth_callback_done'); uni.redirectTo({ url }); },