Files
ruoyi-plus-soybean/src/router/permission.ts
2021-09-14 01:31:29 +08:00

36 lines
957 B
TypeScript

import type { Router } from 'vue-router';
import { useTitle } from '@vueuse/core';
import { getToken, getLoginRedirectUrl } from '@/utils';
import { RouteNameMap, whitelistRoutes } from './routes';
/**
* 路由守卫函数
* @param router - 路由实例
*/
export default function createRouterGuide(router: Router) {
router.beforeEach((to, from, next) => {
window.$loadingBar?.start();
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(to => {
// 设置document title
useTitle(to.meta.title as string);
// 结束 loadingBar
window.$loadingBar?.finish();
});
}