Files
xinfra/frontend/src/router/index.ts
T

162 lines
4.7 KiB
TypeScript
Raw Normal View History

2026-07-14 15:39:19 +08:00
import { createRouter, createWebHistory } from 'vue-router'
import { getToken } from '@/utils/auth'
import { consumeSSOToken, redirectToSSO } from '@/utils/sso'
import { useAuthStore } from '@/stores/auth'
import { authApi } from '@/api/auth'
2026-07-14 15:39:19 +08:00
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/login',
name: 'Login',
component: () => import('@/views/auth/Login.vue'),
meta: { requiresAuth: false },
},
{
path: '/',
component: () => import('@/layouts/DefaultLayout.vue'),
meta: { requiresAuth: true },
children: [
{
path: '',
redirect: '/dashboard',
},
{
path: 'dashboard',
name: 'Dashboard',
component: () => import('@/views/dashboard/Index.vue'),
meta: { title: '资源大盘' },
},
{
path: 'subsystem',
name: 'Subsystem',
component: () => import('@/views/subsystem/Navigation.vue'),
meta: { title: '子系统导航' },
},
{
path: 'subsystem/detail/:name',
name: 'SubsystemDetail',
component: () => import('@/views/subsystem/SubsystemDetail.vue'),
meta: { title: '子系统详情' },
},
{
path: 'subsystem/authz',
name: 'SubsystemAuthz',
component: () => import('@/views/subsystem/Authorization.vue'),
meta: { title: '子系统赋权' },
},
2026-07-14 15:39:19 +08:00
{
path: 'audit/login',
name: 'LoginAudit',
component: () => import('@/views/audit/LoginAudit.vue'),
meta: { title: '登录审计' },
},
{
path: 'audit/ops',
name: 'OpsAudit',
component: () => import('@/views/audit/OpsAudit.vue'),
meta: { title: '运维操作审计' },
},
{
path: 'resource/status',
name: 'ResourceStatus',
component: () => import('@/views/resource/StatusBoard.vue'),
meta: { title: '资源状态看板' },
},
{
path: 'resource/mgmt',
name: 'ResourceMgmt',
component: () => import('@/views/resource/Management.vue'),
meta: { title: '资源管理' },
},
{
path: 'cluster',
name: 'Cluster',
component: () => import('@/views/cluster/ClusterList.vue'),
meta: { title: '集群与容器' },
2026-07-17 10:53:08 +08:00
},
{
path: 'tenants',
name: 'Tenants',
component: () => import('@/views/resource/Tenants.vue'),
meta: { title: '业务配额' },
2026-07-14 15:39:19 +08:00
},
{
path: 'service/catalog',
name: 'ServiceCatalog',
component: () => import('@/views/service/Catalog.vue'),
meta: { title: '基础服务目录' },
},
{
path: 'service/mgmt',
name: 'ServiceMgmt',
component: () => import('@/views/service/Management.vue'),
meta: { title: '服务管理' },
},
{
path: 'tasks',
name: 'Tasks',
component: () => import('@/views/task/TaskCenter.vue'),
meta: { title: '任务中心' },
},
{
path: 'config',
name: 'ConfigCenter',
component: () => import('@/views/config/ConfigCenter.vue'),
meta: { title: '配置中心管理' },
},
{
path: 'monitor',
name: 'Monitor',
component: () => import('@/views/monitor/Monitor.vue'),
2026-07-17 14:11:09 +08:00
meta: { title: '监控看板' },
},
{
path: 'alert',
name: 'Alert',
component: () => import('@/views/monitor/Alert.vue'),
2026-07-17 14:11:09 +08:00
meta: { title: '告警信息' },
2026-07-14 15:39:19 +08:00
},
],
},
],
})
router.beforeEach(async (to) => {
const authStore = useAuthStore()
const ssoToken = consumeSSOToken()
if (ssoToken) {
authStore.setSessionToken(ssoToken)
}
const token = authStore.token || getToken()
2026-07-14 15:39:19 +08:00
if (to.meta.requiresAuth !== false && !token) {
const { data } = await authApi.getConfig()
if (!data.sso_enabled) {
return { path: '/login', query: to.fullPath === '/' ? {} : { redirect: to.fullPath } }
}
redirectToSSO()
return false
}
if (token && (!authStore.user || ssoToken)) {
try {
await authStore.refreshUser()
} catch {
authStore.clearAuth()
const { data } = await authApi.getConfig()
if (!data.sso_enabled) {
return { path: '/login', query: to.fullPath === '/' ? {} : { redirect: to.fullPath } }
}
redirectToSSO()
return false
}
}
if (to.path === '/login' && token) {
return '/'
2026-07-14 15:39:19 +08:00
}
return true
2026-07-14 15:39:19 +08:00
})
export default router