You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.3 KiB

import { defineAsyncComponent } from "vue";
1 year ago
import { createRouter, createWebHashHistory } from "vue-router";
import { useTitle } from "@vueuse/core";
import NProgress from "../lib/nprogress/nprogress.vite-esm.js";
import { isLogin, hasPermission } from "../api/user.js";
import { useAppStore } from "../store/index.js";
import { listToTree } from "../utils/index.js";
import { connection, connect } from "../signalr/index.js";
import remoteRoutes from "./routes.js";
1 year ago
NProgress.configure({ showSpinner: false });
const routes = [
{
path: "/login",
component: () => import("../views/login.js"),
meta: {
title: "登录",
isHidden: true,
1 year ago
},
},
{
path: "/403",
component: () => import("../views/403.js"),
meta: {
title: "权限不足",
isHidden: true,
1 year ago
},
},
{
path: "/:pathMatch(.*)*",
component: () => import("../views/404.js"),
meta: {
title: "无法找到",
isHidden: true,
1 year ago
},
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
router.beforeEach(async (to, from, next) => {
NProgress.start();
try {
if (to.path !== "/login") {
if (!(await isLogin())) {
next({ path: "/login", query: { redirect: to.fullPath } });
} else {
if (!to.meta.public && to.meta.hasPermission === false) {
1 year ago
next({ path: "/403", query: { redirect: to.fullPath } });
} else {
next();
}
}
} else {
next();
}
} catch (error) {
NProgress.done();
}
});
router.afterEach((to) => {
1 year ago
try {
if (!to.meta.isHidden) {
1 year ago
const appStore = useAppStore();
appStore.add(to);
}
if (to.meta.title) {
useTitle().value = `${to.meta.title}`;
}
to.meta.cache = new Map();
1 year ago
} finally {
NProgress.done();
}
});
const refreshRouter = async () => {
//await connect();
1 year ago
const appStore = useAppStore();
const permissions = appStore.user.permissions;
const serverRoutes = JSON.parse(JSON.stringify(remoteRoutes));
const setPermission = (list, parent = null) => {
list.forEach((o) => {
// full path
o.meta.path = `${parent === null ? "/" : parent.meta.path + "/"}${o.path}`;
// full name
o.meta.fullName = `${parent === null ? "" : parent.meta.title + " > "}${o.meta.title}`;
// permission
if (o.meta.type === "page" || o.meta.type === "button") {
if (!o.meta.public) {
o.meta.hasPermission = !!permissions[o.meta.permission];
}
}
// component
if (o.meta?.type === "page") {
if (!o.component) {
o.component = o.path;
}
if (o.component.constructor === String) {
o.component = () => import(`../views${o.meta.path}.js`);
}
}
// children
if (o.children?.length) {
setPermission(o.children, o);
if (o.meta.type === "page") {
o.meta.children = o.children;
delete o.children;
}
}
});
};
setPermission(serverRoutes);
router.removeRoute("layout");
const layout = {
1 year ago
name: "layout",
path: "/",
redirect: "/home",
component: () => import("../layouts/index.js"),
1 year ago
children: serverRoutes,
};
router.addRoute("/", layout);
1 year ago
};
export default router;
export { refreshRouter };