From 45147426e04607fbba7e2e9424495f6f106c7ae5 Mon Sep 17 00:00:00 2001 From: kk <875203880@qq.com> Date: Sat, 11 Jul 2026 11:01:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=BF=85=E8=A6=81=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E6=8B=BC=E6=8E=A5=E5=9C=A8redirect=5Furi=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 10 ++++++---- src/pages/auth/loading.vue | 40 +++++++++++++++++++++----------------- src/pages/index/index.vue | 12 ++++++------ 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/App.vue b/src/App.vue index cbcb5a7..0061fb1 100644 --- a/src/App.vue +++ b/src/App.vue @@ -30,7 +30,7 @@ const urlParams = new URLSearchParams(window.location.search); deviceId = urlParams.get('device_id') || ''; - // 兜底:从路径解析(如 /pages/index/index/N1154) + // 兜底:从路径解析(如 /N1154) if (!deviceId) { const path = window.location.pathname; const segments = path.split('/').filter(Boolean); @@ -46,14 +46,16 @@ const code = urlParams.get('code'); if (code) { - // 有 code → 进入授权 Loading 页处理,保留 code/action/device_id 参数 + // 有 code → 进入授权 Loading 页处理 + // redirect_uri 已携带 device_id 和 action,回调时直接从 URL 取 const action = urlParams.get('action') || ''; + const callbackDeviceId = urlParams.get('device_id') || deviceId || ''; let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code); if (action) { loadingUrl += '&action=' + action; } - if (deviceId) { - loadingUrl += '&device_id=' + deviceId; + if (callbackDeviceId) { + loadingUrl += '&device_id=' + callbackDeviceId; } uni.redirectTo({ url: loadingUrl }); return; diff --git a/src/pages/auth/loading.vue b/src/pages/auth/loading.vue index 5a53ef4..41118ed 100644 --- a/src/pages/auth/loading.vue +++ b/src/pages/auth/loading.vue @@ -29,22 +29,25 @@ export default { return; } - // 优先从页面参数获取 code(App.vue 跳转时带过来) - // 兜底从 URL search 参数获取(微信直接回调时) + // 从 URL 参数获取 code(App.vue 跳转时带过来) let code = options.code || ''; if (!code) { const urlParams = new URLSearchParams(window.location.search); code = urlParams.get('code') || ''; - // 同步获取 action(可能在 URL 参数中) - if (!this.action) { - this.action = urlParams.get('action') || ''; - } - // 同步获取 device_id - if (!this.deviceId) { - this.deviceId = urlParams.get('device_id') || ''; - } } + // 兜底:从 localStorage 恢复(redirect_uri 参数为主,localStorage 为备用) + if (!this.action) { + this.action = localStorage.getItem('__auth_action') || ''; + } + if (!this.deviceId) { + this.deviceId = localStorage.getItem('__auth_device_id') || ''; + } + // 清理 localStorage(一次性使用,避免残留影响下次流程) + localStorage.removeItem('__auth_action'); + localStorage.removeItem('__auth_device_id'); + console.log('[loading] onLoad - action:', this.action, 'deviceId:', this.deviceId, 'code:', code ? 'yes' : 'no'); + if (this.action === 'userinfo') { // 获取用户信息流程 if (code) { @@ -84,10 +87,13 @@ export default { try { this.statusText = '正在获取授权...'; - // 回调地址必须是 HTTPS,使用配置文件中的地址 - const redirectUri = config.authRedirectUri || ( + // 回调地址:基础地址 + device_id 参数(回调时保留) + let redirectUri = config.authRedirectUri || ( window.location.origin.replace(/^http:/, 'https:') + window.location.pathname ); + if (this.deviceId) { + redirectUri += (redirectUri.includes('?') ? '&' : '?') + 'device_id=' + encodeURIComponent(this.deviceId); + } // 使用 snsapi_base 静默授权,不弹窗 const res = await gatewayGet('/api/v1/user/auth/url', { @@ -206,16 +212,14 @@ export default { try { this.statusText = '正在获取用户信息...'; - // 回调地址:当前 loading 页,带 action=userinfo 和 device_id - const baseUri = config.authRedirectUri || ( + // 回调地址:基础地址 + action + device_id 参数(回调时保留) + let redirectUri = config.authRedirectUri || ( window.location.origin.replace(/^http:/, 'https:') + window.location.pathname ); - const redirectUriObj = new URL(baseUri); - redirectUriObj.searchParams.set('action', 'userinfo'); + redirectUri += (redirectUri.includes('?') ? '&' : '?') + 'action=userinfo'; if (this.deviceId) { - redirectUriObj.searchParams.set('device_id', this.deviceId); + redirectUri += '&device_id=' + encodeURIComponent(this.deviceId); } - const redirectUri = redirectUriObj.toString(); const res = await gatewayGet('/api/v1/user/auth/url', { app_no: appNo, diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 6e3c3da..d8435a2 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -325,14 +325,14 @@ export default { } try { - // 回调地址:loading 页处理后跳回首页(带 action 和 device_id) - const baseUri = config.authRedirectUri || window.location.origin + '/pages/auth/loading'; - const redirectUriObj = new URL(baseUri); - redirectUriObj.searchParams.set('action', 'userinfo'); + // 回调地址:基础地址 + action + device_id 参数(回调时保留) + let redirectUri = config.authRedirectUri || ( + window.location.origin.replace(/^http:/, 'https:') + window.location.pathname + ); + redirectUri += (redirectUri.includes('?') ? '&' : '?') + 'action=userinfo'; if (this.deviceId) { - redirectUriObj.searchParams.set('device_id', this.deviceId); + redirectUri += '&device_id=' + encodeURIComponent(this.deviceId); } - const redirectUri = redirectUriObj.toString(); // 获取非静默授权链接(会弹窗确认) const res = await gatewayGet('/api/v1/user/auth/url', {