diff --git a/src/App.vue b/src/App.vue index 94d09b5..98a2f99 100644 --- a/src/App.vue +++ b/src/App.vue @@ -60,6 +60,13 @@ } catch (_) { // state 解析失败,忽略 } + // 存入 localStorage,确保 loading 页面复用时也能获取(redirectTo 可能不触发 onLoad) + if (action) { + localStorage.setItem('__auth_action', action); + } + if (callbackDeviceId) { + localStorage.setItem('__auth_device_id', callbackDeviceId); + } let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code); if (action) { loadingUrl += '&action=' + action; diff --git a/src/pages/auth/loading.vue b/src/pages/auth/loading.vue index 34cbb73..ba3d7b5 100644 --- a/src/pages/auth/loading.vue +++ b/src/pages/auth/loading.vue @@ -36,16 +36,13 @@ export default { code = urlParams.get('code') || ''; } - // 兜底:从 localStorage 恢复(redirect_uri 参数为主,localStorage 为备用) + // 兜底:从 localStorage 恢复(页面复用时 onLoad 不触发,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') { @@ -87,6 +84,11 @@ export default { try { this.statusText = '正在获取授权...'; + // 存入 localStorage(页面复用时 onLoad 不会重新触发,需要从 localStorage 读取) + if (this.deviceId) { + localStorage.setItem('__auth_device_id', this.deviceId); + } + // 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数 const redirectUri = config.authRedirectUri || ( window.location.origin.replace(/^http:/, 'https:') + window.location.pathname @@ -221,6 +223,12 @@ export default { try { this.statusText = '正在获取用户信息...'; + // 存入 localStorage(页面复用时 onLoad 不会重新触发,需要从 localStorage 读取) + localStorage.setItem('__auth_action', 'userinfo'); + if (this.deviceId) { + localStorage.setItem('__auth_device_id', this.deviceId); + } + // 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数 const redirectUri = config.authRedirectUri || ( window.location.origin.replace(/^http:/, 'https:') + window.location.pathname @@ -317,16 +325,22 @@ export default { * 跳回首页并标记为"我的"tab */ goToHomeWithAction() { - const url = this.deviceId - ? `/pages/index/index?device_id=${this.deviceId}&action=mine` + // 兼容页面复用:优先 this.deviceId,其次 localStorage + const deviceId = this.deviceId || localStorage.getItem('__auth_device_id') || ''; + const url = deviceId + ? `/pages/index/index?device_id=${deviceId}&action=mine` : '/pages/index/index?action=mine'; + localStorage.removeItem('__auth_device_id'); + localStorage.removeItem('__auth_action'); uni.redirectTo({ url }); }, goToHome() { - const url = this.deviceId - ? `/pages/index/index?device_id=${this.deviceId}` + const deviceId = this.deviceId || localStorage.getItem('__auth_device_id') || ''; + const url = deviceId + ? `/pages/index/index?device_id=${deviceId}` : '/pages/index/index'; + localStorage.removeItem('__auth_device_id'); uni.redirectTo({ url }); }, diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 10fa4b1..bbdeb40 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -325,6 +325,12 @@ export default { } try { + // 存入 localStorage(loading 页面复用时 onLoad 不会重新触发,需要从 localStorage 读取) + localStorage.setItem('__auth_action', 'userinfo'); + if (this.deviceId) { + localStorage.setItem('__auth_device_id', this.deviceId); + } + // 回调地址:必须与微信公众号后台注册地址一致,不能带自定义参数 const redirectUri = config.authRedirectUri || ( window.location.origin.replace(/^http:/, 'https:') + window.location.pathname