canteen_system/src/router/index.ts

65 lines
2.0 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:13:49 +08:00
router.beforeEach(async (to, from, next) => {
loadingBar.start();
const permissionStore = usePermissionStore();
//判断是不是访问登录页
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;