优化页面栈
This commit is contained in:
@@ -61,34 +61,74 @@ export const urls ='http://222.80.110.161:11111/images/train/'
|
||||
* @param {object} options.query - 携带参数
|
||||
* @param {function} options.onBack - 页面返回时的回调(目标页调用 uni.navigateBack 时传递数据)
|
||||
*/
|
||||
export const navTo = function(url, {
|
||||
needLogin = false,
|
||||
query = {},
|
||||
onBack = null
|
||||
} = {}) {
|
||||
const userStore = useUserStore();
|
||||
|
||||
if (needLogin && !userStore.hasLogin) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/complete-info/complete-info'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const queryStr = Object.entries(query)
|
||||
.map(([key, val]) => `${key}=${encodeURIComponent(val)}`)
|
||||
.join('&');
|
||||
const finalUrl = queryStr ? `${url}?${queryStr}` : url;
|
||||
|
||||
if (onBack) {
|
||||
const pages = getCurrentPages();
|
||||
const currentPage = pages[pages.length - 1];
|
||||
currentPage.__onBackCallback__ = onBack;
|
||||
}
|
||||
|
||||
uni.navigateTo({
|
||||
url: finalUrl
|
||||
});
|
||||
export const navTo = function(url, {
|
||||
needLogin = false,
|
||||
query = {},
|
||||
onBack = null
|
||||
} = {}) {
|
||||
const userStore = useUserStore();
|
||||
|
||||
if (needLogin && !userStore.hasLogin) {
|
||||
const pages = getCurrentPages();
|
||||
if (pages.length >= 10) {
|
||||
uni.redirectTo({
|
||||
url: '/pages/complete-info/complete-info',
|
||||
fail: (err) => {
|
||||
console.error('页面跳转失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
uni.navigateTo({
|
||||
url: '/pages/complete-info/complete-info',
|
||||
fail: (err) => {
|
||||
console.error('页面跳转失败:', err);
|
||||
uni.redirectTo({
|
||||
url: '/pages/complete-info/complete-info',
|
||||
fail: (err2) => {
|
||||
console.error('redirectTo也失败:', err2);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const queryStr = Object.entries(query)
|
||||
.map(([key, val]) => `${key}=${encodeURIComponent(val)}`)
|
||||
.join('&');
|
||||
const finalUrl = queryStr ? `${url}?${queryStr}` : url;
|
||||
|
||||
if (onBack) {
|
||||
const pages = getCurrentPages();
|
||||
const currentPage = pages[pages.length - 1];
|
||||
currentPage.__onBackCallback__ = onBack;
|
||||
}
|
||||
|
||||
const pages = getCurrentPages();
|
||||
if (pages.length >= 10) {
|
||||
// 页面栈已满,使用redirectTo替代
|
||||
uni.redirectTo({
|
||||
url: finalUrl,
|
||||
fail: (err) => {
|
||||
console.error('页面跳转失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
uni.navigateTo({
|
||||
url: finalUrl,
|
||||
fail: (err) => {
|
||||
console.error('页面跳转失败:', err);
|
||||
// 失败后尝试redirectTo
|
||||
uni.redirectTo({
|
||||
url: finalUrl,
|
||||
fail: (err2) => {
|
||||
console.error('redirectTo也失败:', err2);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const navBack = function({
|
||||
|
||||
Reference in New Issue
Block a user