当检测到 webview 数量限制错误时自动切换为 redirectTo 模式
This commit is contained in:
parent
bfcf74aaf0
commit
35cab6537a
@ -8,33 +8,51 @@ export const picUrl = 'https://test.hshuishang.com'; // 图片地址前缀
|
||||
* @param {String} path - 跳转的目标页面路径
|
||||
* @param {Object} options - 配置选项对象
|
||||
* @param {Boolean} options.isLogin - 是否需要校验登录态,默认为 true
|
||||
* @param {Boolean} options.forceReplace - 是否强制替换当前页面,解决webview数量限制问题,默认为 false
|
||||
*/
|
||||
export const NavgateTo = (path, options = {}) => {
|
||||
// 首页不需要登录验证
|
||||
// 首页不需要登录验证
|
||||
if (path === '/pages/index/index') {
|
||||
uni.navigateTo({ url: path });
|
||||
return;
|
||||
}
|
||||
const { isLogin = true } = options;
|
||||
const { isLogin = true, forceReplace = false } = options;
|
||||
const ctoken = uni.getStorageSync('ctoken');
|
||||
if (isLogin) {
|
||||
if (!ctoken) {
|
||||
uni.redirectTo({ url: '/pages/login/login' })
|
||||
return
|
||||
} else {
|
||||
if (path == '1') {
|
||||
uni.navigateBack({
|
||||
delta: 1
|
||||
})
|
||||
return
|
||||
}
|
||||
uni.navigateTo({
|
||||
url: path
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 登录校验
|
||||
if (isLogin && !ctoken) {
|
||||
uni.redirectTo({ url: '/pages/login/login' })
|
||||
return
|
||||
}
|
||||
|
||||
// 返回上一页
|
||||
if (path == '1') {
|
||||
uni.navigateBack({
|
||||
delta: 1
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 选择合适的跳转方式
|
||||
const navigateOptions = {
|
||||
url: path,
|
||||
fail: (err) => {
|
||||
// 处理webview数量限制错误
|
||||
if (err.errMsg && err.errMsg.includes('webview count limit')) {
|
||||
console.warn('检测到webview数量限制,自动切换为redirectTo模式');
|
||||
uni.redirectTo({ url: path });
|
||||
} else {
|
||||
console.error('页面跳转失败:', err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 如果强制替换,使用redirectTo
|
||||
if (forceReplace) {
|
||||
uni.redirectTo(navigateOptions);
|
||||
} else {
|
||||
uni.navigateTo(navigateOptions);
|
||||
}
|
||||
uni.navigateTo({ url: path })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user