Files
learning-system-portal/src/security.js
2024-07-11 21:32:47 +08:00

101 lines
3.8 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'
import testUser from './utils/testUsers.js'
import xpage from '@/utils/xpage'
NProgress.configure({ showSpinner: false })
const whiteList = ['/login','/logout','/loading','/pc/loading','/500','/auth-redirect','/forget','/reset/password']
router.beforeEach((to, from, next) => {
watermark.set("");
//动态计算文件的路径
let configPath=process.env.VUE_APP_FILE_RELATIVE_PATH;
if(configPath.startsWith('http')){
xpage.constants.fileBaseUrl=configPath;
}else{
xpage.constants.fileBaseUrl=window.location.protocol+'//'+window.location.host+configPath;
}
NProgress.start();
// console.log('whiteList::',whiteList,to.path)
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入
next()
}else{
if(getToken()){
if(to.path === '/login'){
// 如果是外部用户,把配置的路由跳转到个人中心
if(store.getters.userInfo.role === 2){
next({ path: process.env.VUE_APP_PUBLIC_PATH+'/uc/study/courses' })
}else{
next({ path: process.env.VUE_APP_PUBLIC_PATH+'/index' })
}
NProgress.done();
} else {
//console.log('store.getters.userInfo:',store.getters.userInfo.role)
// 如果是外部用户,把配置的路由跳转到个人中心
if(store.getters.userInfo.role === 2){
if(to.path === '/index' || to.path === '/course' || to.path === '/case' || to.path === '/article' ) location.href = '/pc/uc/study/task'
}
//后续这里需要增加一定的控制
if (!store.getters.init) {
sessionStorage.removeItem(xpage.constants.localCaseFiltersKey);
sessionStorage.removeItem(xpage.constants.localCourseFiltersKey);
// 判断当前控制台是否已拉取完数据
store.dispatch('InitData').then(res => {
//加载白名单信息
//testUser.init();
//加载信息资源归属,系统分类信息
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 {
to.meta.keepAlive = true
next();
}
}
//next();
}else{
//next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
//设置之前的路径
//store.commit('portal/SetBackUrl',location.href);
//console.log(location.href,'location.href');
//let urlPre=window.location.protocol+'//'+window.location.host;
//let backUrl=location.href.substring(urlPre.length); encodeURIComponent()
location.href=process.env.VUE_APP_LOGIN_URL+"?returnUrl="+encodeURIComponent(location.href);
NProgress.done()
}
}
})
router.afterEach(() => {
NProgress.done()
})