mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/learning-system-portal.git
synced 2025-12-09 02:46:44 +08:00
70 lines
2.2 KiB
JavaScript
70 lines
2.2 KiB
JavaScript
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()
|
||
})
|