❤ 项目配置 permission.ts
逻辑:
1、导入进度条、路由、element-plus组件
2、定义白名单
3、
import NProgress from "../progress"; //导入进度条
import router from '../../router' //导入路由
import {ElMessage } from 'element-plus' //Elmentui的提示信息
// 白名单,
const whiteList =['/home', '/','/login','/applylist','/rules','/about','/main','/admin','/spefevue']; //定义白名单
// 路由守卫
router.beforeEach((to, from, next) => {
console.log(to, from, next,'路由加载中!');
NProgress.start(); //开启进度条
if (to.matched.length ===0) { //如果未匹配到路由
from.name ? next({ name:from.name }) : next('/'); //如果上级也未匹配到路由则跳转登录页面,如果上级能匹配到则转上级路由
} else{
//如果未匹配到路由--检测白名单
if (whiteList.indexOf(to.path) !== -1) {
// 在免登录白名单,直接进入
console.log('白名单进入!');
next()
// window.location.href = 'http://wzfg.jrdsj.com';
// window.location.href = 'http://www.wuzhengai.com';
}else{
console.log('不在白名单--验证token');
//token验证--获取 token
const token = localStorage.getItem('Authorization');
//token 不存在
if (token === null || token === '') {
ElMessage.error('您还没有登录,请先登录');
next('/home');
} else {
next();
}
// console.log('重定向!');
// next('/');//不满足要求重定向
}
}
});
router.afterEach(() => {
console.log('路由加载完成!');
NProgress.done(); //完成进度条
});
export {permission}