canteen_system/src/router/index.ts

76 lines
2.3 KiB
TypeScript

import { createRouter, createWebHistory } from "vue-router";
import { constantRoutes, errorRouter } from "./constantRoutes";
import { usePermissionStore } from "@/stores/permission";
import { isEmpty } from "lodash-es";
import { initDynamicRouter } from "./dynamicRouters";
import { useUserStore } from "@/stores/user";
import { LOGIN_ROUTER, ROUTER_WHITE_LIST } from "@/config/constant";
import { loadingBar, message } from "@/utils";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [...constantRoutes, ...errorRouter],
});
<<<<<<< HEAD
router.beforeEach(async (to, from, next) => {
loadingBar.start();
const permissionStore = usePermissionStore();
//判断是不是访问登录页
=======
/**
* 前置路由守卫
*/
router.beforeEach((to: any, from: any, next: any) => {
//动态设置标题
const title: string = "智慧食堂系统";
document.title = to.meta.name ? `${to.meta.name} - ${title}` : title;
//todo 鉴权操作...
>>>>>>> 8716e35d55875b7e8377cae748005c95a6fe60c2
const userStore = useUserStore();
// if (
// to.path.toLocaleLowerCase() === LOGIN_ROUTER.path &&
// userStore.tokenInfo?.tokenValue
//
// ) {
// //如果已登录 且访问login页面 直接返回当前页面
// message.warning("当前已登录,请先退出账号");
// return next(from.fullPath);
// }
// //判断访问路径是不是白名单d
// if (ROUTER_WHITE_LIST.includes(to.path)) {
// return next();
// }
// //不在白名单内需要查看是否携带token 没有token需要返回登录页进行登录
// if (!userStore.tokenInfo?.tokenValue) {
// message.warning("当未找到token,请重新登陆!");
// return next(LOGIN_ROUTER.path);
// }
//
// // 6.如果没有菜单列表,就重新请求菜单列表并添加动态路由
// if (isEmpty(permissionStore.authMenuList)) {
// await initDynamicRouter();
// return next({ ...to, replace: true });
// }
//放行
return next();
});
/**
* 路由跳转错误
*/
router.onError((error) => {
console.error("路由错误", error.message);
loadingBar.finish();
});
/**
* 后置路由守卫
* */
router.afterEach((to) => {
//动态设置标题
document.title = (to.meta.title as string) ?? "";
loadingBar.finish();
});
export default router;