根目录,新建一个permission.js
import { getToken } from '@/utils/auth'
// 登录页面
const loginPage = "/pages/index"
// 页面白名单
const whiteList = [
'/pages/login', '/pages/register', '/pages/index'
]
// 检查地址白名单
function checkWhite(url) {
const path = url.split('?')[0]
return whiteList.indexOf(path) !== -1
}
// 页面跳转验证拦截器
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
uni.addInterceptor(item, {
invoke(to) {
if (getToken()) {
if (to.url === loginPage) {
uni.reLaunch({ url: "/" })
}
return true
} else {
if (checkWhite(to.url)) {
return true
}
uni.showModal({
title: "系统提示",
content: "未登录,请先登录",
confirmText:"去登录",
success: function (res) {
if (res.confirm) {
uni.reLaunch({ url: '/pages/login' })
}else if(res.cancel){
uni.reLaunch({ url: '/pages/index' })
}
},
})
return false
}
},
fail(err) {
console.log(err)
}
})
})
uniapp配置路由拦截器
最新推荐文章于 2024-07-09 13:40:19 发布