33 lines
942 B
TypeScript
33 lines
942 B
TypeScript
import { createRouter, createWebHistory } from "vue-router";
|
|
|
|
import { routes } from "@/router/routes";
|
|
import { useAuthStore } from "@/stores/auth.store";
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
routes,
|
|
});
|
|
|
|
router.afterEach((to) => {
|
|
document.title = `${String(to.meta.title ?? "风险预测")} | 蝶雅风险预测`;
|
|
});
|
|
|
|
router.beforeEach(async (to) => {
|
|
const auth = useAuthStore();
|
|
if (!auth.isAuthenticated && auth.token) await auth.restore();
|
|
if (to.meta.public && auth.isAuthenticated) return { name: "assessment" };
|
|
if (
|
|
to.meta.requiresAuth !== false &&
|
|
!to.meta.public &&
|
|
!auth.isAuthenticated
|
|
) {
|
|
return { name: "login", query: { redirect: to.fullPath } };
|
|
}
|
|
const permission = to.meta.permission;
|
|
if (typeof permission === "string" && !auth.hasPermission(permission))
|
|
return { name: "assessment" };
|
|
return true;
|
|
});
|
|
|
|
export default router;
|