This commit is contained in:
fengchuanhn@gmail.com
2026-05-17 12:54:04 +08:00
parent e6c8c18be0
commit f1fce871bb
28 changed files with 2566 additions and 235 deletions

View File

@@ -1,112 +1,317 @@
import { createRouter, createWebHistory } from "vue-router";
import { useAuthStore } from "../stores/auth";
const placeholder = () => import("../views/PlaceholderView.vue");
const mainChildren = [
{
path: "",
name: "home",
component: () => import("../views/HomeView.vue"),
},
{
path: "avatar",
name: "avatar",
component: placeholder,
meta: { title: "形象" },
},
{
path: "material",
name: "material",
component: placeholder,
meta: { title: "素材" },
},
{
path: "voice",
name: "voice",
component: placeholder,
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",
import { createRouter, createWebHistory } from "vue-router";
import { useAuthStore, ROLE_ADMIN, ROLE_AGENT } from "../stores/auth";
const placeholder = () => import("../views/PlaceholderView.vue");
const mainChildren = [
{
path: "",
name: "home",
component: () => import("../views/HomeView.vue"),
},
{
path: "avatar",
name: "avatar",
component: placeholder,
meta: { title: "形象" },
},
{
path: "material",
name: "material",
component: placeholder,
meta: { title: "素材" },
},
{
path: "voice",
name: "voice",
component: placeholder,
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: "admin",
component: () => import("../views/AdminView.vue"),
meta: { title: "管理", requiresRole: ROLE_ADMIN },
redirect: { name: "admin-overview" },
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: "agent",
name: "agent",
component: () => import("../views/AgentView.vue"),
meta: { title: "代理", requiresRole: ROLE_AGENT },
},
];
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);
if (requiredRole != null && auth.roleId !== requiredRole) {
return { name: "home" };
}
});
export default router;