65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
import { createRouter, createWebHistory } from "vue-router";
|
|
import NProgress from "nprogress";
|
|
import { message } from "antdv-next";
|
|
import "nprogress/nprogress.css";
|
|
import routes from "./routes";
|
|
import { useAuthStore } from "@/stores/auth";
|
|
import { usePermissionStore } from "@/stores/permissions";
|
|
|
|
NProgress.configure({ showSpinner: false });
|
|
|
|
function getRedirectPath(value) {
|
|
return typeof value === "string" && value.startsWith("/") && !value.startsWith("//")
|
|
? value
|
|
: "/dashboard";
|
|
}
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes,
|
|
scrollBehavior: () => ({ top: 0 }),
|
|
});
|
|
|
|
router.beforeEach(async (to) => {
|
|
NProgress.start();
|
|
document.title = to.meta.title
|
|
? `${to.meta.title} - FMS 货代物流管理系统`
|
|
: "FMS 货代物流管理系统";
|
|
|
|
const authStore = useAuthStore();
|
|
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
|
|
return { name: "login", query: { redirect: to.fullPath } };
|
|
}
|
|
if (to.name === "login" && authStore.isAuthenticated) {
|
|
return getRedirectPath(to.query.redirect);
|
|
}
|
|
|
|
if (to.meta.moduleId) {
|
|
const permissionStore = usePermissionStore();
|
|
try {
|
|
await permissionStore.load();
|
|
if (!permissionStore.canAccess(to.meta.moduleId)) {
|
|
message.warning("没有该模块的访问权限");
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
message.error(error.message || "权限加载失败,请重新登录");
|
|
permissionStore.clear();
|
|
authStore.clearSession();
|
|
return { name: "login", query: { redirect: to.fullPath } };
|
|
}
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
router.afterEach(() => {
|
|
NProgress.done();
|
|
});
|
|
|
|
router.onError(() => {
|
|
NProgress.done();
|
|
});
|
|
|
|
export default router;
|