1、在src/utils中新建permission.js文件
// 白名单
const whiteList = ['/pages/admin/bindUser', '/pages/admin/login']
export default async function() {
const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
// 给uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
list.forEach(item => {
uni.addInterceptor(item, {
invoke(e) {
// 获取要跳转的页面路径
const url = e.url.split('?')[0]
// 判断当前窗口是白名单,如果是则不重定向路由
var pass = false
if (whiteList) {
pass = whiteList.indexOf(url) != -1
}
// 不是白名单并且没有token
if (!pass && !uni.getStorageSync('token')) {
uni.showToast({
title: '请先登录',
icon: 'none'
})
uni.navigateTo({
url: "/pages/admin/login"
})
return false
}
return e
},
fail(err) { // 失败回调拦截
console.log(err)
}
})
})
}
2、在App.vue中引入并执行
import routingIntercept from '@/utils/permission.js'
onLaunch: function (option) {
routingIntercept()
}