完成租户登录

This commit is contained in:
2025-10-29 23:07:53 +08:00
parent b9938b4c0d
commit ddf90424ba
43 changed files with 7761 additions and 474 deletions
+78 -34
View File
@@ -2,6 +2,7 @@ import { createRouter, createWebHashHistory } from "vue-router";
import { convertMenusToRoutes } from "./dynamicRoutes";
// 静态路由:登录页独立,404 页面独立
// 仪表盘路由作为静态路由,永远放在第一个位置
const staticRoutes = [
{
path: "/login",
@@ -13,8 +14,22 @@ const staticRoutes = [
path: "/",
name: "Main",
component: () => import("@/views/Main.vue"),
redirect: "/dashboard", // 默认重定向,动态路由加载后会更新
children: [], // 所有页面路由都会作为 children 添加到这里
redirect: "/dashboard", // 默认重定向到仪表盘
children: [
// 仪表盘路由:静态路由,永远放在第一个
{
path: "dashboard",
name: "Dashboard",
component: () => import("@/views/dashboard/index.vue"),
meta: {
requiresAuth: true,
title: "仪表盘",
icon: "fa-solid fa-gauge",
id: 1,
isStatic: true // 标记为静态路由
}
}
],
meta: { requiresAuth: true }
},
{
@@ -76,15 +91,18 @@ function addDynamicRoutes(menus) {
return;
}
const dynamicRoutes = convertMenusToRoutes(menus);
// 过滤掉 ID 为 1 的仪表盘菜单,因为它是静态路由
const filteredMenus = menus.filter(menu => menu.id !== 1);
const dynamicRoutes = convertMenusToRoutes(filteredMenus);
// 打印路由树结构(只打印路径信息,不序列化组件)
console.log('生成的路由树:', dynamicRoutes.map(r => ({
path: r.path,
name: r.name,
hasComponent: !!r.component,
childrenCount: r.children?.length || 0
})));
// console.log('生成的路由树:', dynamicRoutes.map(r => ({
// path: r.path,
// name: r.name,
// hasComponent: !!r.component,
// childrenCount: r.children?.length || 0
// })));
// 获取主路由
const mainRoute = router.getRoutes().find(r => r.name === 'Main');
@@ -93,45 +111,71 @@ function addDynamicRoutes(menus) {
return;
}
// 获取现有的静态路由(仪表盘路由)
const staticChildren = mainRoute.children || [];
const dashboardRoute = staticChildren.find(r => r.name === 'Dashboard');
// 移除旧的主路由
router.removeRoute('Main');
// 查找第一个有效路由作为默认重定向
const firstRoute = findFirstValidRoute(dynamicRoutes);
let redirectPath = "/dashboard"; // 默认值
if (firstRoute && firstRoute.meta?.menuPath) {
redirectPath = firstRoute.meta.menuPath;
}
// 重新添加主路由,包含所有动态子路由
// 重新添加主路由,仪表盘路由放在第一个,后面跟动态路由
router.addRoute({
...mainRoute,
redirect: redirectPath,
children: dynamicRoutes // 所有动态路由作为 Main 的 children
redirect: "/dashboard", // 始终重定向到仪表盘
children: [
// 仪表盘静态路由永远放在第一个
...(dashboardRoute ? [dashboardRoute] : []),
// 动态路由跟在后面
...dynamicRoutes
]
});
// 添加知识库的子路由(详情页和编辑页)
// 直接在 Main 路由下添加完整路径的子路由
router.addRoute('Main', {
path: 'apps/knowledge/detail/:id',
name: 'apps-knowledge-detail',
component: () => import('@/views/apps/knowledge/components/detail.vue'),
meta: {
requiresAuth: true,
title: '知识详情'
}
});
router.addRoute('Main', {
path: 'apps/knowledge/edit/:id',
name: 'apps-knowledge-edit',
component: () => import('@/views/apps/knowledge/components/edit.vue'),
meta: {
requiresAuth: true,
title: '编辑知识'
}
});
dynamicRoutesAdded = true;
// 打印路由信息用于调试
const finalMainRoute = router.getRoutes().find(r => r.name === 'Main');
console.log('动态路由已添加:', {
redirect: redirectPath,
childrenCount: dynamicRoutes.length,
childrenPaths: finalMainRoute?.children?.map(c => ({
path: c.path,
name: c.name,
metaPath: c.meta?.menuPath
}))
});
// console.log('动态路由已添加:', {
// redirect: "/dashboard",
// staticRoutesCount: dashboardRoute ? 1 : 0,
// dynamicRoutesCount: dynamicRoutes.length,
// totalChildrenCount: (dashboardRoute ? 1 : 0) + dynamicRoutes.length,
// childrenPaths: finalMainRoute?.children?.map(c => ({
// path: c.path,
// name: c.name,
// isStatic: c.meta?.isStatic || false,
// metaPath: c.meta?.menuPath
// }))
// });
// 测试路由解析(使用完整路径)
const testResolve = router.resolve('/dashboard');
console.log('测试路由解析 /dashboard:', {
matched: testResolve.matched.map(m => ({ name: m.name, path: m.path })),
fullPath: testResolve.fullPath,
name: testResolve.name,
hasMatched: testResolve.matched.length > 0
});
// console.log('测试路由解析 /dashboard:', {
// matched: testResolve.matched.map(m => ({ name: m.name, path: m.path })),
// fullPath: testResolve.fullPath,
// name: testResolve.name,
// hasMatched: testResolve.matched.length > 0
// });
}
// 查找第一个有效的路由(有组件的路由)