步骤一:新建routerIntercept.js文件
步骤二:routerIntercept文件中写入:(根据自己需要修改whiteList白名单中的页面路径和自己的逻辑处理)
import Vue from 'vue'
// 白名单
const whiteList = [
'/pages/public/login',
'/pages/public/privacyAgreement',
]
export default async function() {
const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
list.forEach(item => {
uni.addInterceptor(item, {
invoke(e) {
const url = e.url.split('?')[0]
let pass
if (whiteList) {
console.log('白名单内');
// 白名单之内
pass = whiteList.some((item) => {
if (typeof(item) === 'object' && item.pattern) {
return item.pattern.test(url)
}
return url === item
})
}
// 不是白名单并且没有登录
if (!pass && !uni.getStorageSync('accessToken')) {
console.l