refactor(member pages): unify onShow login redirect logic

1. extract common onShow login check to dedicated method redirectTologinOnShowLifecycle
2. add fallback callback for login failure to navigate back on wechat mini program
3. update all page login redirect calls to use the new unified method
4. add debug logs for redirect and auth flow tracing
This commit is contained in:
2026-06-27 18:25:53 +08:00
parent 0b760e41a3
commit 2e535b9d93
19 changed files with 69 additions and 36 deletions

View File

@@ -8,9 +8,11 @@ export default {
/**
* 获取用户登录凭证code
*/
getCode(callback) {
getCode(callback, fallbackFunc) {
console.log(`-----> auth.js getCode`)
// 微信小程序
// #ifdef MP-WEIXIN
console.log(`微信小程序登录`)
uni.login({
provider: 'weixin',
timeout: 3000,
@@ -29,12 +31,15 @@ export default {
.unionid;
typeof callback == 'function' && callback(this.authInfo);
} else {
fallbackFunc && fallbackFunc(res);
this.$util.showToast({
title: res.message ? res.message : '小程序配置错误'
});
}
}
})
} else {
fallbackFunc && fallbackFunc(res);
}
},
fail: (res) => {
@@ -46,6 +51,7 @@ export default {
});
}
// #endif
fallbackFunc && fallbackFunc(res);
}
})
// #endif
@@ -66,6 +72,7 @@ export default {
.user_id;
typeof callback == 'function' && callback(this.authInfo);
} else {
fallbackFunc && fallbackFunc(res);
this.$util.showToast({
title: res.message ? res.message : '小程序配置错误'
});
@@ -94,6 +101,9 @@ export default {
if (res.code >= 0) {
location.href = res.data;
}
},
fail: err => {
fallbackFunc && fallbackFunc(err);
}
});
}

View File

@@ -257,20 +257,41 @@ export default {
},
/**
* 重新定向到登录授权
* 重新定向到登录授权, 不区分生命周期
* @param {String} backUrl
* @param {Object} params
* @param {Object} params 附加参数
* @param {Function} effectFnc 副作用函数, 登录授权失败的处理方式
*/
redirectTologin(backUrl = '', params = {}) {
redirectTologin(backUrl = '', params = {}, effectFnc = ()=>{}) {
console.log(`------> redirectTologin`, backUrl);
setTimeout(() => {
this.$nextTick(() => {
let mode = 'navigateTo';
// #ifdef H5
mode = 'redirectTo';
// #ifdef H5 || MP-HARMONY
mode = 'redirectTo'; // H5必须使用redirectTo, 不然点击返回箭头无法返回。
// #endif
this.$refs?.login?.open(backUrl, mode);
this.$refs?.login?.open(backUrl, mode, effectFnc);
});
}, 0);
},
/**
* 重新定向到登录授权, 当页面显示的时候, 失败后,
* 原因:onLoad、onReady 只执行一次;
* 而且,有的页面不需要这个功能
* @param {String} backUrl
*/
redirectTologinOnShowLifecycle(backUrl) {
console.log(`------> redirectTologinOnShowLifecycle`, backUrl);
// #ifdef H5 || MP-HARMONY
this.redirectTologin(backUrl);
// #endif
// #ifdef MP-WEIXIN
this.redirectTologin(backUrl, {}, () => {
uni.navigateBack({delta: 1 });
})
// #endif
}
},
filters: {

View File

@@ -970,6 +970,7 @@ export default {
* @param {Object} link
*/
diyRedirectTo(link) {
console.log(`-----> diyRedirectTo`, link);
//if (link == null || Object.keys(link).length == 1) return;
if (link.wap_url) {
link.wap_url = decodeEntity(link.wap_url);
@@ -1085,7 +1086,7 @@ export default {
}
}
} else if (link.wap_url) {
} else if (link.wap_url) { // 微页面/内部页面
this.redirectTo(link.wap_url, {...link});
}
},

View File

@@ -253,16 +253,11 @@ export default {
},
// 打开弹窗
open(url = '', mode = 'navigateTo') {
open(url = '', mode = 'navigateTo', callback = ()=>{}) {
console.log(`------> ns-login.open`, url);
if (url) this.url = url;
// #ifdef MP-WEIXIN
this.getCode(authData => {
this.authLogin(authData, 'authOnlyLogin');
});
// #endif
// #ifdef H5
// #ifdef H5 || MP-HARMONY
if (this.$util.isWeChatMiniProgram()) {
// 微信小程序环境,使用微信授权登录
let authData = uni.getStorageSync('authInfo');
@@ -275,20 +270,26 @@ export default {
}
} else if (this.$util.isWeiXin()) {
// 微信浏览器环境,显示登录页面
this.toLogin(mode);
this.toLoginH5Page(mode);
} else {
// 其他浏览器环境,显示登录页面
this.toLogin(mode);
this.toLoginH5Page(mode);
}
return;
// #endif
// #ifndef MP-WEIXIN || H5
this.toLogin(mode);
// #ifdef MP-WEIXIN
this.getCode(authData => {
this.authLogin(authData, 'authOnlyLogin');
}, (err) => {
callback && callback(err);
});
// #endif
},
// 跳转去登录页
toLogin(mode = 'navigateTo') {
toLoginH5Page(mode = 'navigateTo') {
if (this.url) this.$util.redirectTo(this.$util.LOGIN_PAGE_URL, {
back: encodeURIComponent(this.url)
}, mode);
@@ -631,7 +632,7 @@ export default {
if (this.registerConfig.third_party == 1 && this.registerConfig.bind_mobile == 1) {
this.openCompleteInfoPop();
} else if (this.registerConfig.third_party == 0) {
this.toLogin();
this.toLoginH5Page();
} else {
this.openCompleteInfoPop();
}

View File

@@ -84,7 +84,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/account');
this.redirectTologinOnShowLifecycle('/pages_tool/member/account');
return;
}
this.getTransferType();

View File

@@ -129,7 +129,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/address');
this.redirectTologinOnShowLifecycle('/pages_tool/member/address');
return;
}

View File

@@ -87,7 +87,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/balance');
this.redirectTologinOnShowLifecycle('/pages_tool/member/balance');
return;
}
this.getWithdrawConfig();

View File

@@ -95,7 +95,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/balance_detail');
this.redirectTologinOnShowLifecycle('/pages_tool/member/balance_detail');
return;
}
},

View File

@@ -80,7 +80,7 @@
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/bind_mobile');
this.redirectTologinOnShowLifecycle('/pages_tool/member/bind_mobile');
return;
}
},

View File

@@ -140,7 +140,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/card');
this.redirectTologinOnShowLifecycle('/pages_tool/member/card');
return;
}

View File

@@ -296,7 +296,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/card_buy');
this.redirectTologinOnShowLifecycle('/pages_tool/member/card_buy');
return;
}

View File

@@ -84,7 +84,7 @@ export default {
onLoad() {},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/card_log');
this.redirectTologinOnShowLifecycle('/pages_tool/member/card_log');
return;
}
this.loadStat();

View File

@@ -104,7 +104,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/coupon');
this.redirectTologinOnShowLifecycle('/pages_tool/member/coupon');
return;
}

View File

@@ -248,7 +248,7 @@
onLoad(option) {},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/invite_friends');
this.redirectTologinOnShowLifecycle('/pages_tool/member/invite_friends');
return;
}

View File

@@ -219,7 +219,7 @@
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/level');
this.redirectTologinOnShowLifecycle('/pages_tool/member/level');
return;
}

View File

@@ -90,7 +90,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/point');
this.redirectTologinOnShowLifecycle('/pages_tool/member/point');
return;
}

View File

@@ -88,7 +88,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/info');
this.redirectTologinOnShowLifecycle('/pages_tool/member/info');
return;
}

View File

@@ -45,7 +45,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/member/signin');
this.redirectTologinOnShowLifecycle('/pages_tool/member/signin');
return;
}

View File

@@ -49,7 +49,7 @@ export default {
},
onShow() {
if (!this.storeToken) {
this.redirectTologin('/pages_tool/recharge/order_list');
this.redirectTologinOnShowLifecycle('/pages_tool/recharge/order_list');
return;
}