canteen_system/src/router/index.ts

76 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-04-28 14:13:49 +08:00
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";
2025-04-25 09:17:25 +08:00
const router = createRouter({
2025-04-28 14:13:49 +08:00
history: createWebHistory(import.meta.env.BASE_URL),
routes: [...constantRoutes, ...errorRouter],
2025-04-25 09:17:25 +08:00
});
2025-04-28 14:14:15 +08:00
<<<<<<< HEAD
2025-04-28 14:13:49 +08:00
router.beforeEach(async (to, from, next) => {
loadingBar.start();
const permissionStore = usePermissionStore();
//判断是不是访问登录页
2025-04-28 14:14:15 +08:00
=======
2025-04-25 09:17:25 +08:00
/**
*
*/
router.beforeEach((to: any, from: any, next: any) => {
//动态设置标题
2025-04-25 17:40:30 +08:00
const title: string = "智慧食堂系统";
2025-04-25 09:17:25 +08:00
document.title = to.meta.name ? `${to.meta.name} - ${title}` : title;
//todo 鉴权操作...
2025-04-28 14:14:15 +08:00
>>>>>>> 8716e35d55875b7e8377cae748005c95a6fe60c2
2025-04-25 09:17:25 +08:00
const userStore = useUserStore();
2025-04-28 14:13:49 +08:00
// 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 });
2025-04-25 09:17:25 +08:00
// }
//放行
return next();
});
/**
*
*/
2025-04-28 14:13:49 +08:00
router.onError((error) => {
console.error("路由错误", error.message);
loadingBar.finish();
2025-04-25 09:17:25 +08:00
});
/**
*
* */
2025-04-28 14:13:49 +08:00
router.afterEach((to) => {
//动态设置标题
document.title = (to.meta.title as string) ?? "";
loadingBar.finish();
2025-04-25 09:17:25 +08:00
});
export default router;