import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router' import { getToken } from '@/utils/auth' import NProgress from 'nprogress' import 'nprogress/nprogress.css' // NProgress 配置 NProgress.configure({ showSpinner: false }) const Layout = () => import('@/layout/index.vue') const routes: RouteRecordRaw[] = [ { path: '/login', name: 'Login', component: () => import('@/views/login/index.vue'), meta: { title: '登录', hidden: true } }, { path: '/', component: Layout, redirect: '/dashboard', children: [ { path: 'dashboard', name: 'Dashboard', component: () => import('@/views/dashboard/index.vue'), meta: { title: '仪表盘', icon: 'DataLine' } }, { path: 'machine', name: 'Machine', component: () => import('@/views/machine/index.vue'), meta: { title: '机器管理', icon: 'Monitor' } }, { path: 'camera', name: 'Camera', component: () => import('@/views/camera/index.vue'), meta: { title: '摄像头管理', icon: 'VideoCamera' } }, { path: 'camera/channel/:deviceId', name: 'CameraChannel', component: () => import('@/views/camera/channel.vue'), meta: { title: '通道列表', hidden: true } }, { path: 'camera/video/:deviceId/:channelId', name: 'CameraVideo', component: () => import('@/views/camera/video.vue'), meta: { title: '视频播放', hidden: true } }, { path: 'stream-test', name: 'StreamTest', component: () => import('@/views/camera/stream-test.vue'), meta: { title: 'Stream 测试', icon: 'Monitor' } }, { path: 'stream/videos', name: 'StreamVideos', component: () => import('@/views/stream/video-list.vue'), meta: { title: '视频管理', icon: 'Film' } }, { path: 'stream/live', name: 'StreamLive', component: () => import('@/views/stream/live-list.vue'), meta: { title: '直播管理', icon: 'VideoCameraFilled' } }, { path: 'stream/config', name: 'StreamConfig', component: () => import('@/views/stream/config.vue'), meta: { title: 'Stream 配置', icon: 'Setting' } }, { path: 'user', name: 'User', component: () => import('@/views/user/index.vue'), meta: { title: '用户管理', icon: 'User' } }, { path: 'stats', name: 'Stats', component: () => import('@/views/stats/index.vue'), meta: { title: '观看统计', icon: 'DataAnalysis' } }, { path: 'audit', name: 'Audit', component: () => import('@/views/audit/index.vue'), meta: { title: '审计日志', icon: 'Document' } }, { path: 'demo/direct-url', name: 'DirectUrl', component: () => import('@/views/demo/direct-url.vue'), meta: { title: '直接 URL', icon: 'Link' } }, { path: 'demo/cloudflare-stream', name: 'CloudflareStream', component: () => import('@/views/demo/cloudflare-stream.vue'), meta: { title: 'Cloudflare Stream', icon: 'VideoCamera' } }, { path: 'demo/rtsp-stream', name: 'RtspStream', component: () => import('@/views/demo/rtsp-stream.vue'), meta: { title: 'RTSP 流', icon: 'Connection' } }, { path: 'demo/sample-videos', name: 'SampleVideos', component: () => import('@/views/demo/sample-videos.vue'), meta: { title: '测试视频', icon: 'Film' } } ] }, { path: '/:pathMatch(.*)*', redirect: '/' } ] const router = createRouter({ history: createWebHistory(), routes }) // 白名单(无需登录即可访问) const whiteList = ['/login', '/stream-test'] router.beforeEach((to, _from, next) => { // 开始进度条 NProgress.start() // 统一使用系统名称作为标题 document.title = '摄像头管理系统' const hasToken = getToken() if (hasToken) { if (to.path === '/login') { next({ path: '/' }) } else { next() } } else if (whiteList.includes(to.path)) { next() } else { next(`/login?redirect=${to.fullPath}`) } }) router.afterEach(() => { // 结束进度条 NProgress.done() }) export default router