mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-17 09:50:31 +08:00
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import { createRouter, createWebHashHistory } from 'vue-router';
|
|
import MainRoutes from './MainRoutes';
|
|
import AuthRoutes from './AuthRoutes';
|
|
import ChatBoxRoutes from './ChatBoxRoutes';
|
|
import { useAuthStore } from '@/stores/auth';
|
|
import { useRouterLoadingStore } from '@/stores/routerLoading';
|
|
|
|
export const router = createRouter({
|
|
history: createWebHashHistory(import.meta.env.BASE_URL),
|
|
routes: [
|
|
MainRoutes,
|
|
AuthRoutes,
|
|
ChatBoxRoutes
|
|
]
|
|
});
|
|
|
|
interface AuthStore {
|
|
username: string;
|
|
returnUrl: string | null;
|
|
login(username: string, password: string): Promise<void>;
|
|
logout(): void;
|
|
has_token(): boolean;
|
|
}
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
if (from.name && from.path !== to.path) {
|
|
const loadingStore = useRouterLoadingStore();
|
|
loadingStore.start();
|
|
}
|
|
|
|
const publicPages = ['/auth/login'];
|
|
const authRequired = !publicPages.includes(to.path);
|
|
const auth: AuthStore = useAuthStore();
|
|
|
|
// 如果用户已登录且试图访问登录页面,则重定向到首页
|
|
if (to.path === '/auth/login' && auth.has_token()) {
|
|
return next(auth.returnUrl || '/');
|
|
}
|
|
|
|
if (to.matched.some((record) => record.meta.requiresAuth)) {
|
|
if (authRequired && !auth.has_token()) {
|
|
auth.returnUrl = to.fullPath;
|
|
return next('/auth/login');
|
|
} else next();
|
|
} else {
|
|
next();
|
|
}
|
|
});
|
|
|
|
router.afterEach(() => {
|
|
const loadingStore = useRouterLoadingStore();
|
|
loadingStore.finish();
|
|
});
|