Files
learning-system-portal/src/security.js
2022-05-29 18:56:34 +08:00

70 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/token'
import { routers } from "@/data/pages"
import watermark from './utils/warterMark.js'
NProgress.configure({ showSpinner: false })
const whiteList = ['/login','/logout','/loading','/pc/loading','/500','/auth-redirect','/forget','/reset/password']
router.beforeEach((to, from, next) => {
watermark.set("");
NProgress.start();
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入
next()
}else{
if(getToken()){
if(to.path === '/login'){
next({ path: '/pc/index' })
NProgress.done();
} else {
//后续这里需要增加一定的控制
if (!store.getters.init) {
// 判断当前控制台是否已拉取完数据
store.dispatch('InitData').then(res => {
//加载信息资源归属,系统分类信息
store.dispatch('resOwner/loadResOwners');
store.dispatch('sysType/loadSysTypes');
store.commit('app/SET_INITDATA',true);
//routers数据先使用固定的以后在初始化接口中返回
let myRouters=routers();
store.dispatch('GenerateRoutes',{routers:myRouters}).then(accessRoutes=>{
router.addRoutes(accessRoutes) // 动态添加可访问路由表
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
});
}).catch(err => {
console.log(err);
store.commit('app/SET_INITDATA',false);
//如果初始化错误,就不再执行了,不然会一直循环下去
next({ path: '/500' })
//NProgress.done();
})
} else {
next();
}
}
//next();
}else{
//next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
location.href=process.env.VUE_APP_LOGIN_URL;
NProgress.done()
}
}
})
router.afterEach(() => {
NProgress.done()
})