uniapph5页面使用浏览器自己的按钮返回会出现各种问题,比如onShow不执行,底部加载不执行,所以需要把跳转页面的方法重写,下面的代码加上了鉴权
import {
getToken
} from '@/utils/auth'
// 登录页面
const loginPage = "/pages/login"
// 页面白名单
const whiteList = [
'/','/pages/login', '/pages/register', '/pages/common/webview/index'
]
// 检查地址白名单
function checkWhite(url) {
const path = url.split('?')[0]
return whiteList.indexOf(path) !== -1
}
// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
// 处理通过浏览器按钮返回,触底加载,onShow不执行的bug 跳转页面用redirectTo,跳转时将页面路径记载到历史记录中
list.forEach(item => {
uni.addInterceptor(item, {
invoke(to) {
if (getToken()) {
let locationUrl = window.location.href.split("#")[1]
// console.log('pathname:', location.href.split(location.host)[1])
// if (pageList.includes(locationUrl)) {
// history.pushState({}, '', location.href.split(location.host)[1]);
// }else if(location.href.split(location.host)[1],includes('/pages/login')||){
// history.pushState({}, '', location.href.split(location.host)[1]);
// }
history.pushState({}, '', location.href.split(location.host)[1]);
if (to.url === loginPage) {
uni.reLaunch({
url: "/"
})
}
return true
} else {
if (checkWhite(to.url)) {
return true
}
uni.reLaunch({
url: loginPage
})
return false
}
},
fail(err) {
console.log(err)
}
})
})
uni.addInterceptor('navigateBack', {
invoke(to) {
history.go(-1)
return false
},
fail(err) {
console.log(err)
}
})
上面的js放到main.js中引入