loading.vue 的回调处理统一从 localStorage 读取
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 41s

This commit is contained in:
kk
2026-07-11 12:12:53 +08:00
parent 925ebe9bb9
commit 9ecd6d963b
3 changed files with 35 additions and 8 deletions
+7
View File
@@ -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;
+22 -8
View File
@@ -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 });
},
+6
View File
@@ -325,6 +325,12 @@ export default {
}
try {
// 存入 localStorageloading 页面复用时 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