import { createRouter, createWebHistory } from "vue-router"; import { useAuthStore, ROLE_ADMIN, ROLE_AGENT,ROLE_OEM } from "../stores/auth"; const placeholder = () => import("../views/PlaceholderView.vue"); const mainChildren = [ { path: "", name: "home", component: () => import("../views/HomeView.vue"), }, { path: "avatar", name: "avatar", component: () => import("../views/AvatarView.vue"), meta: { title: "形象" }, }, { path: "material", name: "material", component: () => import("../views/MaterialView.vue"), meta: { title: "素材" }, }, { path: "voice", name: "voice", component: () => import("../views/VoiceView.vue"), meta: { title: "声音" }, }, // { // path: "compose", // name: "compose", // component: placeholder, // meta: { title: "合成" }, // }, // { // path: "extract", // name: "extract", // component: placeholder, // meta: { title: "提取" }, // }, // { // path: "design", // name: "design", // component: placeholder, // meta: { title: "设计" }, // }, // { // path: "model", // name: "model", // component: placeholder, // meta: { title: "模型" }, // }, // { // path: "help", // name: "help", // component: placeholder, // meta: { title: "帮助" }, // }, { path: "local-config", name: "local-config", component: () => import("../views/LocalConfigView.vue"), meta: { title: "本地配置" }, }, { path: "card-activate", name: "card-activate", component: () => import("../views/CardActivateView.vue"), meta: { title: "卡密激活" }, }, { path: "admin", component: () => import("../views/AdminView.vue"), meta: { title: "管理", requiresRole: ROLE_ADMIN }, redirect: { name: "admin-users" }, children: [ { path: "", name: "admin-overview", component: () => import("../views/admin/AdminOverviewView.vue"), meta: { title: "概览" }, }, { path: "users", name: "admin-users", component: () => import("../views/admin/AdminUsersView.vue"), meta: { title: "用户管理" }, }, { path: "desktop-config", name: "admin-desktop-config", component: () => import("../views/admin/AdminDesktopConfigView.vue"), meta: { title: "桌面配置" }, }, { path: "card-keys", name: "admin-card-keys", component: () => import("../views/admin/AdminCardKeysView.vue"), meta: { title: "卡密管理" }, }, ], }, { path: "oem", component: () => import("../views/OemView.vue"), meta: { title: "管理", requiresRole: ROLE_OEM}, redirect: { name: "oem-users" }, children: [ { path: "", name: "oem-overview", component: () => import("../views/oem/OemOverviewView.vue"), meta: { title: "概览" }, }, { path: "users", name: "oem-users", component: () => import("../views/oem/OemUsersView.vue"), meta: { title: "用户管理" }, }, { path: "desktop-config", name: "oem-desktop-config", component: () => import("../views/oem/OemDesktopConfigView.vue"), meta: { title: "桌面配置" }, }, { path: "card-keys", name: "oem-card-keys", component: () => import("../views/oem/OemCardKeysView.vue"), meta: { title: "卡密管理" }, }, ], }, { path: "agent", name: "agent", component: () => import("../views/AgentView.vue"), meta: { title: "代理", requiresRole: ROLE_AGENT }, children: [ { path: "users", name: "agent-users", component: () => import("../views/agent/AgentUsersView.vue"), meta: { title: "用户管理" }, }, { path: "card-keys", name: "agent-card-keys", component: () => import("../views/agent/AgentCardKeysView.vue"), meta: { title: "卡密管理" }, }, ], }, ]; if (import.meta.env.DEV) { mainChildren.push({ path: "debug", name: "debug", component: () => import("../views/DebugView.vue"), meta: { title: "调试", devOnly: true }, }); } const routes = [ { path: "/", component: () => import("../layouts/MainLayout.vue"), meta: { requiresAuth: true }, children: mainChildren, }, { path: "/login", name: "login", component: () => import("../views/LoginView.vue"), }, { path: "/register", name: "register", component: () => import("../views/RegisterView.vue"), }, { path: "/:pathMatch(.*)*", redirect: "/login", }, ]; const router = createRouter({ history: createWebHistory(), routes, }); router.beforeEach((to) => { const auth = useAuthStore(); const requiresAuth = to.matched.some((record) => record.meta.requiresAuth); if (requiresAuth && !auth.isAuthenticated) { return { name: "login" }; } if ((to.name === "login" || to.name === "register") && auth.isAuthenticated) { return { name: "home" }; } const requiredRole = to.matched .map((record) => record.meta.requiresRole) .find((role) => role != null); console.log('requiredRole',requiredRole); console.log('auth.roleId',auth.roleId); if (requiredRole != null && auth.roleId !== requiredRole) { return { name: "home" }; } }); export default router;