refactor(projects): 完善路由配置

This commit is contained in:
Soybean
2021-09-14 01:31:29 +08:00
parent 1dcdcdc485
commit ab77d58e46
24 changed files with 441 additions and 301 deletions

View File

@ -1,4 +1,7 @@
import type { Router } from 'vue-router';
import { useTitle } from '@vueuse/core';
import { getToken, getLoginRedirectUrl } from '@/utils';
import { RouteNameMap, whitelistRoutes } from './routes';
/**
* 路由守卫函数
@ -7,9 +10,26 @@ import type { Router } from 'vue-router';
export default function createRouterGuide(router: Router) {
router.beforeEach((to, from, next) => {
window.$loadingBar?.start();
next();
const token = getToken();
if (whitelistRoutes.includes(to.name as string)) {
if (to.name === RouteNameMap.get('login') && token) {
next('/');
return;
}
next();
return;
}
if (token) {
next();
} else {
const redirectUrl = getLoginRedirectUrl();
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
}
});
router.afterEach(() => {
router.afterEach(to => {
// 设置document title
useTitle(to.meta.title as string);
// 结束 loadingBar
window.$loadingBar?.finish();
});
}