当检测到 webview 数量限制错误时自动切换为 redirectTo 模式

This commit is contained in:
赵毅 2025-09-15 14:56:30 +08:00
parent bfcf74aaf0
commit 35cab6537a

View File

@ -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 })
}
/**