74 lines
1.7 KiB
TypeScript
74 lines
1.7 KiB
TypeScript
|
|
import { createRouter, createWebHistory } from "vue-router";
|
||
|
|
|
||
|
|
import { pinia } from "@/stores/pinia";
|
||
|
|
import { useAuthStore } from "@/stores/auth";
|
||
|
|
|
||
|
|
export const router = createRouter({
|
||
|
|
history: createWebHistory(),
|
||
|
|
routes: [
|
||
|
|
{
|
||
|
|
path: "/login",
|
||
|
|
name: "login",
|
||
|
|
component: () => import("@/views/LoginView.vue"),
|
||
|
|
meta: { title: "登录" },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: "/",
|
||
|
|
component: () => import("@/layouts/AppShell.vue"),
|
||
|
|
meta: { requiresAuth: true },
|
||
|
|
children: [
|
||
|
|
{ path: "", redirect: "/accounts" },
|
||
|
|
{
|
||
|
|
path: "accounts",
|
||
|
|
name: "accounts",
|
||
|
|
component: () => import("@/views/AccountsView.vue"),
|
||
|
|
meta: { title: "账号管理" },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: "audits",
|
||
|
|
name: "audits",
|
||
|
|
component: () => import("@/views/AuditLogsView.vue"),
|
||
|
|
meta: { title: "审计日志" },
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: "profile",
|
||
|
|
name: "profile",
|
||
|
|
component: () => import("@/views/ProfileView.vue"),
|
||
|
|
meta: { title: "个人资料" },
|
||
|
|
},
|
||
|
|
],
|
||
|
|
},
|
||
|
|
{
|
||
|
|
path: "/:pathMatch(.*)*",
|
||
|
|
redirect: "/",
|
||
|
|
},
|
||
|
|
],
|
||
|
|
});
|
||
|
|
|
||
|
|
router.beforeEach((to) => {
|
||
|
|
const auth = useAuthStore(pinia);
|
||
|
|
if (!auth.initialized) {
|
||
|
|
auth.hydrate();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (to.meta.requiresAuth && !auth.isAuthenticated) {
|
||
|
|
return {
|
||
|
|
name: "login",
|
||
|
|
query: to.fullPath !== "/" ? { redirect: to.fullPath } : undefined,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
if (to.name === "login" && auth.isAuthenticated) {
|
||
|
|
return { name: "accounts" };
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
});
|
||
|
|
|
||
|
|
router.afterEach((to) => {
|
||
|
|
if (typeof document !== "undefined") {
|
||
|
|
const t = (to.meta.title as string | undefined) ?? "运营后台";
|
||
|
|
document.title = `${t} · 省心推`;
|
||
|
|
}
|
||
|
|
});
|