import { createRouter, createWebHistory } from "vue-router"; import NProgress from "nprogress"; import "nprogress/nprogress.css"; import DefaultLayout from "@/layouts/DefaultLayout.vue"; import { useAuthStore } from "@/stores/auth"; import { usePermissionStore } from "@/stores/permissions"; NProgress.configure({ showSpinner: false }); const router = createRouter({ history: createWebHistory(), routes: [ { path: "/login", name: "login", component: () => import("@/views/login/index.vue"), }, { path: "/", component: DefaultLayout, redirect: "/dashboard", meta: { requiresAuth: true }, children: [ { path: "/dashboard", name: "dashboard", component: () => import("@/views/dashboard/index.vue"), meta: { title: "工作台" }, }, // Base { path: "/base/othercompany", name: "base-othercompany", component: () => import("@/views/base/othercompany/index.vue"), meta: { title: "合作伙伴", moduleId: "v_b_othercompany" }, }, { path: "/base/contact", name: "base-contact", component: () => import("@/views/base/contact/index.vue"), meta: { title: "联系人", moduleId: "v_b_contact" }, }, { path: "/base/otherdata", name: "base-otherdata", component: () => import("@/views/base/otherdata/index.vue"), meta: { title: "其他数据", moduleId: "v_b_otherdata" }, }, // System { path: "/system/module-management", name: "system-module-management", component: () => import("@/views/system/module-management/index.vue"), meta: { title: "模块管理", moduleId: "m_metadata" }, }, { path: "/system/i18n-type", name: "system-i18n-type", component: () => import("@/views/system/i18n-type/index.vue"), meta: { title: "多语言类型", moduleId: "m_i18n" }, }, { path: "/system/login-log", name: "system-login-log", component: () => import("@/views/system/login-log/index.vue"), meta: { title: "登录日志", moduleId: "d_login_log" }, }, { path: "/system/user", name: "system-user", component: () => import("@/views/system/user/index.vue"), meta: { title: "用户管理", moduleId: "b_user" }, }, ], }, { path: "/:pathMatch(.*)*", redirect: "/dashboard", }, ], }); // Auth + permission guard router.beforeEach(async (to) => { NProgress.start(); const authStore = useAuthStore(); if (to.path === "/login") { if (authStore.isAuthenticated) return "/dashboard"; return; } if (to.matched.some((r) => r.meta.requiresAuth)) { if (!authStore.isAuthenticated) { return { path: "/login", query: { redirect: to.fullPath } }; } // Permission check const moduleId = (to.meta as { moduleId?: string }).moduleId; if (moduleId) { const permissionStore = usePermissionStore(); await permissionStore.load(); if (!permissionStore.canAccess(moduleId)) { return "/dashboard"; } } } }); router.afterEach(() => { NProgress.done(); }); export default router;