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], }); router.beforeEach(async (to, from, next) => { loadingBar.start(); const permissionStore = usePermissionStore(); //判断是不是访问登录页 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;