You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

60 lines
2.5 KiB

import router from './index'
import Setting from '@/setting'
import util from '@/libs/util'
import store from '@/store'
const managerPath = ['/stat/list', '/screen', '/user/list', '/role/list'] // 管理员才能访问的页面路径
router.beforeEach((to, from, next) => {
document.title = Setting.titleSuffix
const user = store.state.user
JSON.stringify(user) == '{}' && util.local.clear()
const role = util.local.get(Setting.tokenKey)
if (!role && to.path !== '/login' && to.path !== '/index/list') {
next('/index/list')
} else if(role && to.path == '/login') {
next('/index')
} else {
const mg = from.query.mg
const schoolId = from.query.schoolId
const routes = store.state.auth.routes
const toPath = to.path
// mg是判断是否是管理员的参数,base64解码后如果等于true则为管理员,否则为普通用户。
// 如果是管理员则需要从获取的路由权限里判断当前访问页面是否有权限访问,如果没权限访问则跳转到403
if((mg && atob(decodeURI(mg)) === 'true') || (to.query.mg && atob(decodeURI(to.query.mg)) === 'true')){
if(routes.length){
if(routes.find(n => n.name == toPath) || toPath == '/setting/person' || toPath == '/setting/download' || toPath == '/index/list'){
if (toPath === '/index/list') {
next()
} else {
if(!to.query.mg){
next({
path: toPath,
query: {
mg,
schoolId
}
})
}else{
next()
}
}
}else{
// 如果没有权限访问,并且开启了动态路由,并且访问的页面不是403,则跳到403
if(to.path !== '/403' && Setting.dynamicRoute){
next('/403')
}else{
next()
}
}
}else{
next()
}
}else{// 如果是普通用户,则无法访问上面定义的常量里的3个页面路径
if(managerPath.includes(toPath) && Setting.dynamicRoute){
next('/403')
}else{
next()
}
}
}
})