55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
/**
|
|
* app/router/guards.ts
|
|
* 全局路由守卫:认证、权限、旧 URL 兼容、document.title。
|
|
*/
|
|
import type { Router } from 'vue-router';
|
|
import { config } from '@/config';
|
|
import { adminModulePath } from '@/features/admin/constants/modules';
|
|
import { useAuthStore } from '@/features/auth/stores/auth.store';
|
|
import { DEFAULT_CHAT_AGENT_ID, isPublicPath } from '@/shared/constants/routes';
|
|
|
|
export function setupRouterGuards(router: Router): void {
|
|
router.beforeEach((to, _from, next) => {
|
|
const auth = useAuthStore();
|
|
auth.syncFromStorage();
|
|
|
|
if (to.path === '/admin' && typeof to.query.module === 'string' && to.query.module) {
|
|
next(adminModulePath(to.query.module));
|
|
return;
|
|
}
|
|
|
|
if (to.matched.some((r) => r.meta.requiresAdmin)) {
|
|
if (!auth.isAdmin) {
|
|
next('/workspace/applications');
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (to.path === '/') {
|
|
if (auth.isLoggedIn) {
|
|
next(`/chat/${DEFAULT_CHAT_AGENT_ID}`);
|
|
return;
|
|
}
|
|
next('/login');
|
|
return;
|
|
}
|
|
|
|
if (to.path === '/login' && auth.isLoggedIn) {
|
|
next('/workspace/applications');
|
|
return;
|
|
}
|
|
|
|
if (!auth.isLoggedIn && !isPublicPath(to.path)) {
|
|
next('/login');
|
|
return;
|
|
}
|
|
|
|
next();
|
|
});
|
|
|
|
router.afterEach((to) => {
|
|
const baseTitle = config.appTitle || 'AI工作台';
|
|
document.title = to.meta.title ? `${String(to.meta.title)} - ${baseTitle}` : baseTitle;
|
|
});
|
|
}
|