116 lines
3.0 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import keycloak from '@/auth/keycloak'
const routes: RouteRecordRaw[] = [
{
path: '/login',
name: 'Login',
component: () => import('../views/Login.vue'),
meta: { requiresAuth: false }
},
{
path: '/',
component: () => import('../layouts/MainLayout.vue'),
redirect: '/dashboard',
meta: { requiresAuth: true },
children: [
{
path: 'dashboard',
name: 'Dashboard',
component: () => import('../views/dashboard/Dashboard.vue'),
meta: { title: '首页' }
},
{
path: 'equipment',
name: 'Equipment',
component: () => import('../views/equipment/EquipmentList.vue'),
meta: { title: '设备管理', requiresAdmin: true }
},
{
path: 'equipment/add',
name: 'EquipmentAdd',
component: () => import('../views/equipment/EquipmentForm.vue'),
meta: { title: '新增设备', requiresAdmin: true }
},
{
path: 'equipment/edit/:id',
name: 'EquipmentEdit',
component: () => import('../views/equipment/EquipmentForm.vue'),
meta: { title: '编辑设备', requiresAdmin: true }
},
{
path: 'borrow',
name: 'Borrow',
component: () => import('../views/borrow/BorrowList.vue'),
meta: { title: '借用管理' }
},
{
path: 'borrow/scan',
name: 'BorrowScan',
component: () => import('../views/borrow/BorrowScan.vue'),
meta: { title: '扫码出库' }
},
{
path: 'return/scan',
name: 'ReturnScan',
component: () => import('../views/borrow/ReturnScan.vue'),
meta: { title: '扫码入库' }
},
{
path: 'statistics',
name: 'Statistics',
component: () => import('../views/statistics/Statistics.vue'),
meta: { title: '统计分析' }
},
{
path: 'notifications',
name: 'Notifications',
component: () => import('../views/notifications/NotificationCenter.vue'),
meta: { title: '站内消息', requiresAdmin: true }
},
{
path: 'settings',
name: 'Settings',
component: () => import('../views/settings/Settings.vue'),
meta: { title: '系统设置', requiresAdmin: true }
}
]
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
router.beforeEach((to, _from, next) => {
const authStore = useAuthStore()
const authMode = import.meta.env.VITE_AUTH_MODE || 'keycloak'
if (to.path === '/login' && authStore.isAuthenticated) {
next('/dashboard')
return
}
if (to.meta.requiresAuth && !authStore.isAuthenticated) {
if (authMode === 'local') {
next('/login')
} else {
keycloak.login()
next(false)
}
return
}
if (to.meta.requiresAdmin && authStore.user?.role !== 'admin') {
next('/dashboard')
return
}
next()
})
export default router