75 lines
1.7 KiB
JavaScript
75 lines
1.7 KiB
JavaScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
const routes = [
|
|
{
|
|
path: '/login',
|
|
name: 'Login',
|
|
component: () => import('@/views/Login.vue'),
|
|
},
|
|
{
|
|
path: '/',
|
|
component: () => import('@/views/Layout.vue'),
|
|
redirect: '/dashboard',
|
|
children: [
|
|
{
|
|
path: 'dashboard',
|
|
name: 'Dashboard',
|
|
component: () => import('@/views/Dashboard.vue'),
|
|
meta: { title: '仪表盘' },
|
|
},
|
|
{
|
|
path: 'devices',
|
|
name: 'Devices',
|
|
component: () => import('@/views/Devices.vue'),
|
|
meta: { title: '设备列表' },
|
|
},
|
|
{
|
|
path: 'alerts',
|
|
name: 'Alerts',
|
|
component: () => import('@/views/Alerts.vue'),
|
|
meta: { title: '告警记录' },
|
|
},
|
|
{
|
|
path: 'stats',
|
|
name: 'Stats',
|
|
component: () => import('@/views/Stats.vue'),
|
|
meta: { title: '统计分析' },
|
|
},
|
|
{
|
|
path: 'settings',
|
|
name: 'Settings',
|
|
component: () => import('@/views/Settings.vue'),
|
|
meta: { title: '系统设置', adminOnly: true },
|
|
},
|
|
],
|
|
},
|
|
]
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory(),
|
|
routes,
|
|
})
|
|
|
|
// 默认运行在内网免登录模式;需要 Casdoor 时构建时设置 VITE_AUTH_ENABLED=true。
|
|
const authEnabled = import.meta.env.VITE_AUTH_ENABLED === 'true'
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
if (!authEnabled) {
|
|
if (to.name === 'Login') {
|
|
next({ name: 'Dashboard' })
|
|
} else {
|
|
next()
|
|
}
|
|
return
|
|
}
|
|
|
|
const token = localStorage.getItem('token')
|
|
if (to.name !== 'Login' && !token) {
|
|
next({ name: 'Login' })
|
|
} else {
|
|
next()
|
|
}
|
|
})
|
|
|
|
export default router
|