mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-26 14:23:48 +00:00
开源准备
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
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;
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
import { createRouter, createWebHistory } from "vue-router";
|
||||
import BasicLayout from "@/layouts/basicLayout.vue";
|
||||
import Login from "@/views/system/auth/login.vue";
|
||||
import storage from "store";
|
||||
import store from "@/store";
|
||||
import { generator } from "./generateRouter";
|
||||
import { listToTree } from "@/utils/util";
|
||||
|
||||
const routes = [
|
||||
{ path: "/login", name: "Login", component: Login },
|
||||
{
|
||||
path: "/:catchAll(.*)",
|
||||
name: "404",
|
||||
meta: { title: "404" },
|
||||
component: () => import("../views/exception/404.vue"),
|
||||
},
|
||||
];
|
||||
|
||||
const rootRouter = {
|
||||
path: "/",
|
||||
name: "Index",
|
||||
redirect: "/dashboard/workplace",
|
||||
component: BasicLayout,
|
||||
children: [
|
||||
{
|
||||
path: "/profile",
|
||||
name: "Profile",
|
||||
meta: {
|
||||
title: "个人中心",
|
||||
keepAlive: true,
|
||||
},
|
||||
component: () => import("../views/current/profile.vue"),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes,
|
||||
});
|
||||
|
||||
router.beforeEach((to) => {
|
||||
const token = storage.get("Access-Token");
|
||||
|
||||
if (!token && to.name !== "Login") {
|
||||
return { name: "Login" };
|
||||
} else if (token && to.name === "Login") {
|
||||
return { name: "Index" };
|
||||
} else if (token && !store.state.user.hasGetRoute) {
|
||||
return store.dispatch("getUserInfo").then(() => {
|
||||
const routersTree = listToTree(store.state.user.routeList);
|
||||
const routerMap = generator(routersTree);
|
||||
rootRouter.children = [...rootRouter.children, ...routerMap];
|
||||
router.addRoute(rootRouter);
|
||||
return to.fullPath;
|
||||
});
|
||||
}
|
||||
});
|
||||
export default router;
|
||||
Reference in New Issue
Block a user