优化页面栈
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 {object} options.query - 携带参数
|
||||||
* @param {function} options.onBack - 页面返回时的回调(目标页调用 uni.navigateBack 时传递数据)
|
* @param {function} options.onBack - 页面返回时的回调(目标页调用 uni.navigateBack 时传递数据)
|
||||||
*/
|
*/
|
||||||
export const navTo = function(url, {
|
export const navTo = function(url, {
|
||||||
needLogin = false,
|
needLogin = false,
|
||||||
query = {},
|
query = {},
|
||||||
onBack = null
|
onBack = null
|
||||||
} = {}) {
|
} = {}) {
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
|
|
||||||
if (needLogin && !userStore.hasLogin) {
|
if (needLogin && !userStore.hasLogin) {
|
||||||
uni.navigateTo({
|
const pages = getCurrentPages();
|
||||||
url: '/pages/complete-info/complete-info'
|
if (pages.length >= 10) {
|
||||||
});
|
uni.redirectTo({
|
||||||
return;
|
url: '/pages/complete-info/complete-info',
|
||||||
}
|
fail: (err) => {
|
||||||
|
console.error('页面跳转失败:', err);
|
||||||
const queryStr = Object.entries(query)
|
}
|
||||||
.map(([key, val]) => `${key}=${encodeURIComponent(val)}`)
|
});
|
||||||
.join('&');
|
} else {
|
||||||
const finalUrl = queryStr ? `${url}?${queryStr}` : url;
|
uni.navigateTo({
|
||||||
|
url: '/pages/complete-info/complete-info',
|
||||||
if (onBack) {
|
fail: (err) => {
|
||||||
const pages = getCurrentPages();
|
console.error('页面跳转失败:', err);
|
||||||
const currentPage = pages[pages.length - 1];
|
uni.redirectTo({
|
||||||
currentPage.__onBackCallback__ = onBack;
|
url: '/pages/complete-info/complete-info',
|
||||||
}
|
fail: (err2) => {
|
||||||
|
console.error('redirectTo也失败:', err2);
|
||||||
uni.navigateTo({
|
}
|
||||||
url: finalUrl
|
});
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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({
|
export const navBack = function({
|
||||||
|
|||||||
@@ -72,9 +72,32 @@ export function navigateToLoginPage(options = {}) {
|
|||||||
? `?${new URLSearchParams(params).toString()}`
|
? `?${new URLSearchParams(params).toString()}`
|
||||||
: '';
|
: '';
|
||||||
|
|
||||||
uni.navigateTo({
|
const finalUrl = `${loginPage}${queryString}`;
|
||||||
url: `${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)
|
resolve(resData.data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 处理业务错误
|
// 处理业务错误
|
||||||
if (resData.data?.code === 401 || resData.data?.code === 402) {
|
if (resData.data?.code === 401 || resData.data?.code === 402) {
|
||||||
uni.navigateTo({
|
const pages = getCurrentPages();
|
||||||
url:'/packageB/login?flag=nw'
|
if (pages.length >= 10) {
|
||||||
})
|
// 页面栈已满,使用redirectTo替代
|
||||||
// navTo('/packageB/login?flag=nw');
|
uni.redirectTo({
|
||||||
useUserStore().logOut()
|
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 || '请求出现异常,请联系工作人员'
|
const errorMsg = msg || '请求出现异常,请联系工作人员'
|
||||||
|
|||||||
Reference in New Issue
Block a user