mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-26 14:23:48 +00:00
发布v2.0.0分支
This commit is contained in:
@@ -1,119 +0,0 @@
|
||||
import { createRouter, createWebHistory } from "vue-router";
|
||||
import storage from "store";
|
||||
|
||||
import BasicLayout from "@/layouts/basicLayout.vue";
|
||||
import { listToTree } from "@/utils/util";
|
||||
import { useUserStore } from "@/store/index";
|
||||
|
||||
const modules = import.meta.glob("../views/**/**.vue");
|
||||
|
||||
export const generator = (routers) => {
|
||||
return routers.map((item) => {
|
||||
const currentRouter = {
|
||||
path: item.route_path,
|
||||
name: item.route_name,
|
||||
component: item.component_path
|
||||
? modules[`../views/${item.component_path}.vue`]
|
||||
: null,
|
||||
redirect: item.redirect,
|
||||
meta: {
|
||||
title: item.name,
|
||||
icon: item.icon || undefined,
|
||||
keepAlive: item.cache,
|
||||
hidden: item.hidden,
|
||||
order: item.order,
|
||||
},
|
||||
};
|
||||
if (item.children && item.children.length > 0) {
|
||||
currentRouter.children = generator(item.children);
|
||||
}
|
||||
return currentRouter;
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
const rootRouter = {
|
||||
path: "/",
|
||||
name: "Index",
|
||||
redirect: "/dashboard",
|
||||
component: BasicLayout,
|
||||
children: [
|
||||
{
|
||||
path: "/profile",
|
||||
name: "Profile",
|
||||
meta: {
|
||||
title: "个人中心",
|
||||
keepAlive: true,
|
||||
},
|
||||
component: () => import("../views/current/profile.vue"),
|
||||
},
|
||||
{
|
||||
path: "/configinfo",
|
||||
name: "Configinfo",
|
||||
meta: {
|
||||
title: "配置中心",
|
||||
keepAlive: true,
|
||||
},
|
||||
component: () => import("../views/current/configinfo.vue"),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
|
||||
const routes = [
|
||||
{ path: "/login",
|
||||
name: "Login",
|
||||
component: () => import("../views/system/auth/login.vue"), },
|
||||
{
|
||||
path: "/403",
|
||||
name: "403",
|
||||
meta: { title: "403" },
|
||||
component: () => import("../views/exception/403.vue"),
|
||||
},
|
||||
{
|
||||
path: "/404",
|
||||
name: "404",
|
||||
meta: { title: "404" },
|
||||
component: () => import("../views/exception/404.vue"),
|
||||
},
|
||||
{
|
||||
path: "/500",
|
||||
name: "500",
|
||||
meta: { title: "500" },
|
||||
component: () => import("../views/exception/500.vue"),
|
||||
},
|
||||
{
|
||||
path: "/:catchAll(.*)",
|
||||
name: "404",
|
||||
meta: { title: "404" },
|
||||
component: () => import("../views/exception/404.vue"),
|
||||
},
|
||||
{
|
||||
...rootRouter,
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes,
|
||||
});
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
const token = storage.get("Access-Token");
|
||||
const userStore = useUserStore();
|
||||
|
||||
if (!token && to.name !== "Login") {
|
||||
return { name: "Login" };
|
||||
} else if (token && to.name === "Login") {
|
||||
return { name: "Index" };
|
||||
} else if (token && !userStore.hasGetRoute) {
|
||||
await userStore.getUserInfo();
|
||||
const routersTree = listToTree(userStore.routeList);
|
||||
const routerMap = generator(routersTree);
|
||||
rootRouter.children = [...rootRouter.children, ...routerMap];
|
||||
router.addRoute(rootRouter);
|
||||
return to.fullPath;
|
||||
}
|
||||
});
|
||||
export default router;
|
||||
@@ -0,0 +1,81 @@
|
||||
import type { App } from "vue";
|
||||
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";
|
||||
|
||||
export const Layout = () => import("@/layouts/index.vue");
|
||||
|
||||
/**
|
||||
* 静态路由
|
||||
*/
|
||||
export const constantRoutes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: "/redirect",
|
||||
meta: { hidden: true },
|
||||
component: Layout,
|
||||
children: [
|
||||
{
|
||||
path: "/redirect/:path(.*)",
|
||||
component: () => import("@/views/redirect/index.vue"),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: "/login",
|
||||
name: "Login",
|
||||
meta: { hidden: true },
|
||||
component: () => import("@/views/system/auth/index.vue"),
|
||||
},
|
||||
{
|
||||
path: "/401",
|
||||
name: "401",
|
||||
meta: { hidden: true, title: "401" },
|
||||
component: () => import("@/views/error/401.vue"),
|
||||
},
|
||||
{
|
||||
path: "/404",
|
||||
name: "404",
|
||||
meta: { hidden: true, title: "404" },
|
||||
component: () => import("@/views/error/404.vue"),
|
||||
},
|
||||
{
|
||||
path: "/500",
|
||||
name: "500",
|
||||
meta: { hidden: true, title: "500" },
|
||||
component: () => import("@/views/error/500.vue"),
|
||||
},
|
||||
// 以下内容必须放在后面
|
||||
{
|
||||
path: "/",
|
||||
name: "/Index",
|
||||
redirect: "/dashboard/workplace",
|
||||
component: Layout,
|
||||
children: [
|
||||
{
|
||||
path: "profile",
|
||||
name: "Profile",
|
||||
meta: { title: "个人中心", icon: "user", hidden: true },
|
||||
component: () => import("@/views/current/profile.vue"),
|
||||
}
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
* 创建路由
|
||||
*/
|
||||
const router = createRouter({
|
||||
history: createWebHashHistory(),
|
||||
routes: constantRoutes,
|
||||
// 刷新时,滚动条位置还原
|
||||
scrollBehavior: () => ({ left: 0, top: 0 }),
|
||||
});
|
||||
|
||||
// 全局注册 router
|
||||
// 为了捕获并处理全局错误,在注册路由时添加错误处理
|
||||
export function setupRouter(app: App<Element>) {
|
||||
app.config.errorHandler = (err, instance, info) => {
|
||||
console.error('全局错误捕获:', err, '实例:', instance, '信息:', info);
|
||||
};
|
||||
app.use(router);
|
||||
}
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user