From 878778f8f9b7121d54a8e899b3a452bc77c482d0 Mon Sep 17 00:00:00 2001 From: kk <875203880@qq.com> Date: Sat, 11 Jul 2026 17:52:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3code=E8=A2=AB=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E4=BD=BF=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/auth/loading.vue | 48 +++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/pages/auth/loading.vue b/src/pages/auth/loading.vue index 966b5da..199285d 100644 --- a/src/pages/auth/loading.vue +++ b/src/pages/auth/loading.vue @@ -72,6 +72,9 @@ export default { * 发起 OAuth 授权(静默授权,只获取 openid) */ async startAuth() { + // 清除上次使用的 code 标记 + uni.removeStorageSync('__used_auth_code'); + const platform = detectPlatform(); const appNo = getAppNo(platform); @@ -140,6 +143,20 @@ export default { return; } + // 防止 code 重复提交:先检查再标记 + const usedCode = uni.getStorageSync('__used_auth_code'); + if (usedCode === code) { + console.warn('[loading] code 已使用过,跳过'); + return; + } + uni.setStorageSync('__used_auth_code', code); + + // 立即清除 URL 中的 code 参数,避免刷新重复处理 + const url = new URL(window.location.href); + url.searchParams.delete('code'); + url.searchParams.delete('state'); + window.history.replaceState({}, '', url.toString()); + try { this.statusText = '正在验证身份...'; @@ -165,12 +182,6 @@ export default { return; } - // 清除 URL 中的 code 参数,避免刷新重复处理 - const url = new URL(window.location.href); - url.searchParams.delete('code'); - url.searchParams.delete('state'); - window.history.replaceState({}, '', url.toString()); - // 存储 openid uni.setStorageSync('openid', openid); @@ -219,6 +230,9 @@ export default { * 发起非静默授权获取用户信息(snsapi_userinfo,弹窗确认) */ async startUserInfoAuth() { + // 清除上次使用的 code 标记 + uni.removeStorageSync('__used_auth_code'); + const platform = detectPlatform(); const appNo = getAppNo(platform); @@ -288,16 +302,24 @@ export default { return; } + // 防止 code 重复提交 + const usedCode = uni.getStorageSync('__used_auth_code'); + if (usedCode === code) { + console.warn('[loading] code 已使用过,跳过'); + return; + } + uni.setStorageSync('__used_auth_code', code); + + // 立即清除 URL 中的 code/action 参数,避免刷新重复处理 + const url = new URL(window.location.href); + url.searchParams.delete('code'); + url.searchParams.delete('state'); + url.searchParams.delete('action'); + window.history.replaceState({}, '', url.toString()); + try { this.statusText = '正在获取用户信息...'; - // 清除 URL 中的 code/action 参数,避免刷新重复处理 - const url = new URL(window.location.href); - url.searchParams.delete('code'); - url.searchParams.delete('state'); - url.searchParams.delete('action'); - window.history.replaceState({}, '', url.toString()); - // 用 code 调用 POST /api/v1/user/info 获取用户信息(必须用 POST,GET 是另一个端点) console.log('[loading] 请求 POST user/info, app_no:', appNo, 'code:', code); const res = await gatewayPost('/api/v1/user/info', {