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

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

View File

@ -8,6 +8,7 @@ export const picUrl = 'https://test.hshuishang.com'; // 图片地址前缀
* @param {String} path - 跳转的目标页面路径 * @param {String} path - 跳转的目标页面路径
* @param {Object} options - 配置选项对象 * @param {Object} options - 配置选项对象
* @param {Boolean} options.isLogin - 是否需要校验登录态默认为 true * @param {Boolean} options.isLogin - 是否需要校验登录态默认为 true
* @param {Boolean} options.forceReplace - 是否强制替换当前页面解决webview数量限制问题默认为 false
*/ */
export const NavgateTo = (path, options = {}) => { export const NavgateTo = (path, options = {}) => {
// 首页不需要登录验证 // 首页不需要登录验证
@ -15,26 +16,43 @@ export const NavgateTo = (path, options = {}) => {
uni.navigateTo({ url: path }); uni.navigateTo({ url: path });
return; return;
} }
const { isLogin = true } = options; const { isLogin = true, forceReplace = false } = options;
const ctoken = uni.getStorageSync('ctoken'); const ctoken = uni.getStorageSync('ctoken');
if (isLogin) {
if (!ctoken) { // 登录校验
if (isLogin && !ctoken) {
uni.redirectTo({ url: '/pages/login/login' }) uni.redirectTo({ url: '/pages/login/login' })
return return
} else { }
// 返回上一页
if (path == '1') { if (path == '1') {
uni.navigateBack({ uni.navigateBack({
delta: 1 delta: 1
}) })
return return
} }
uni.navigateTo({
url: path // 选择合适的跳转方式
}); const navigateOptions = {
return; 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);
} }
} }
uni.navigateTo({ url: path }) };
// 如果强制替换使用redirectTo
if (forceReplace) {
uni.redirectTo(navigateOptions);
} else {
uni.navigateTo(navigateOptions);
}
} }
/** /**