优化页面栈
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({
|
||||
|
||||
@@ -72,9 +72,32 @@ export function navigateToLoginPage(options = {}) {
|
||||
? `?${new URLSearchParams(params).toString()}`
|
||||
: '';
|
||||
|
||||
uni.navigateTo({
|
||||
url: `${loginPage}${queryString}`
|
||||
});
|
||||
const finalUrl = `${loginPage}${queryString}`;
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -215,14 +215,34 @@ export function myRequest(url, data = {}, method = 'GET', port = 9100, headers =
|
||||
resolve(resData.data)
|
||||
return
|
||||
}
|
||||
// 处理业务错误
|
||||
if (resData.data?.code === 401 || resData.data?.code === 402) {
|
||||
uni.navigateTo({
|
||||
url:'/packageB/login?flag=nw'
|
||||
})
|
||||
// navTo('/packageB/login?flag=nw');
|
||||
useUserStore().logOut()
|
||||
|
||||
// 处理业务错误
|
||||
if (resData.data?.code === 401 || resData.data?.code === 402) {
|
||||
const pages = getCurrentPages();
|
||||
if (pages.length >= 10) {
|
||||
// 页面栈已满,使用redirectTo替代
|
||||
uni.redirectTo({
|
||||
url:'/packageB/login?flag=nw',
|
||||
fail: (err) => {
|
||||
console.error('页面跳转失败:', err);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
uni.navigateTo({
|
||||
url:'/packageB/login?flag=nw',
|
||||
fail: (err) => {
|
||||
console.error('页面跳转失败:', err);
|
||||
// 失败后尝试redirectTo
|
||||
uni.redirectTo({
|
||||
url:'/packageB/login?flag=nw',
|
||||
fail: (err2) => {
|
||||
console.error('redirectTo也失败:', err2);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
useUserStore().logOut()
|
||||
|
||||
}
|
||||
// 显示具体的错误信息
|
||||
const errorMsg = msg || '请求出现异常,请联系工作人员'
|
||||
|
||||
Reference in New Issue
Block a user