import { createRouter, createWebHistory } from 'vue-router'; import { getToken } from "@/utils/auth"; const whiteList = ["/login"]; const routes = [ { path: "/", name: "Index", component: () => import("@/views/index.vue"), }, { path: "/login", name: "Login", component: () => import("@/views/login.vue"), }, { path: "/traceOrderManage", name: "TraceOrderManage", component: () => import("@/views/traceOrderManage/layout.vue"), redirect: { name: "TraceOrderManageIndex" }, children: [ { path: "", name: "TraceOrderManageIndex", component: () => import("@/views/traceOrderManage/index.vue"), }, { path: "infeed", name: "Infeed", component: () => import("@/views/traceOrderManage/infeed/layout.vue"), redirect: { name: "PrimaryMaterial" }, children: [ { path: "primaryMaterial", name: "PrimaryMaterial", component: () => import("@/views/traceOrderManage/infeed/primaryMaterial/index.vue"), }, { path: "rawMaterial", name: "RawMaterial", component: () => import("@/views/traceOrderManage/infeed/rawMaterial/index.vue"), }, { path: "mask", name: "Mask", component: () => import("@/views/traceOrderManage/infeed/mask/index.vue"), }, { path: "equipment", name: "Equipment", component: () => import("@/views/traceOrderManage/infeed/equipment/index.vue"), }, ], }, { path: "outfeed", name: "Outfeed", component: () => import("@/views/traceOrderManage/outfeed/layout.vue"), redirect: { name: "JobReport" }, children: [ { path: "jobReport", name: "JobReport", component: () => import("@/views/traceOrderManage/outfeed/jobReport/index.vue"), }, { path: "parameterConfiguration", name: "ParameterConfiguration", component: () => import( "@/views/traceOrderManage/outfeed/parameterConfiguration/index.vue" ), }, { path: "processGuidance", name: "ProcessGuidance", component: () => import("@/views/traceOrderManage/outfeed/processGuidance/index.vue"), }, ], }, ], }, ]; const router = createRouter({ history: createWebHistory(), routes }); router.beforeEach(async (to, from, next) => { const token = getToken(); // 已登录,访问 login → 跳首页 if (token && to.path === "/login") { return next("/"); } // 白名单放行 if (whiteList.includes(to.path)) { return next(); } // 未登录,访问受保护路由 if (!token) { return next({ path: "/login", query: { redirect: to.fullPath }, }); } // 已登录,正常访问 next(); }); export default router;