94 lines
1.8 KiB
TypeScript
94 lines
1.8 KiB
TypeScript
import NProgress from 'nprogress';
|
|
import 'nprogress/nprogress.css';
|
|
|
|
import './styles/nprogress/index.css';
|
|
|
|
import router from './router';
|
|
import { useLoginStore, useMenuStore } from '~/store';
|
|
import { ElMessageBox } from 'element-plus';
|
|
|
|
/**
|
|
* 检查是否登陆
|
|
*/
|
|
const checkLogin = async () => {
|
|
const login = useLoginStore();
|
|
const token = login.token;
|
|
try {
|
|
if (!token) {
|
|
throw new Error('请登陆');
|
|
}
|
|
const check = !(await login.checkLoginIsExp())
|
|
if (!check) {
|
|
throw new Error("登录超时");
|
|
}
|
|
return check;
|
|
} catch (error) {
|
|
login.logout();
|
|
return false;
|
|
}
|
|
};
|
|
/**
|
|
* 检查路由
|
|
*/
|
|
const checkRouter = (path: string) => {
|
|
const menu = useMenuStore()
|
|
const validate = menu.routers.includes(path);
|
|
if (!validate) {
|
|
// 开发环境
|
|
if (import.meta.env.DEV) {
|
|
ElMessageBox.alert(
|
|
`[ ${path} ] 路由未添加到菜单中 请添加后访问`,
|
|
'提示',
|
|
{
|
|
confirmButtonText: '去添加',
|
|
showClose: false
|
|
}
|
|
).then(() => {
|
|
router.push('/system/menu');
|
|
});
|
|
} else {
|
|
router.push('/exception/403');
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* 路由守护
|
|
*/
|
|
export function guardian() {
|
|
router.beforeEach(async ({ path, meta }) => {
|
|
const {
|
|
// 路由检查
|
|
ignoreRouterCheck = false,
|
|
// 登陆检查
|
|
ignoreLoginCheck = false
|
|
} = meta;
|
|
// 登录验证
|
|
if (!ignoreLoginCheck) {
|
|
await checkLogin()
|
|
}
|
|
// 路由检查
|
|
if (!ignoreRouterCheck) {
|
|
const check = checkRouter(path);
|
|
if (!check) {
|
|
return false;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 顶部进度条
|
|
*/
|
|
export function topProgressBar() {
|
|
router.beforeEach((to, from, next) => {
|
|
NProgress.start();
|
|
next();
|
|
});
|
|
router.afterEach(() => {
|
|
NProgress.done();
|
|
});
|
|
}
|