diff --git a/zhandianxinxi/光谱数据监控/src/router/index.js b/zhandianxinxi/光谱数据监控/src/router/index.js index 9085495..044addc 100644 --- a/zhandianxinxi/光谱数据监控/src/router/index.js +++ b/zhandianxinxi/光谱数据监控/src/router/index.js @@ -1,28 +1,41 @@ import { createRouter, createWebHistory } from 'vue-router' +import { ElMessage } from 'element-plus' -// 假设你的组件都在 views 目录下 -// Dashboard 作为首页,通常直接引入 +// 1. 引入登录页面(建议新建 views/Login.vue) +import Login from '../views/Login.vue' +// 2. 首页组件 import Dashboard from '../views/Dashboard.vue' const routes = [ { path: '/', + name: 'Login', + component: Login, + meta: { title: '系统登录' } + }, + { + path: '/dashboard', name: 'Dashboard', component: Dashboard, - meta: { title: '设备监控总览' } + meta: { title: '设备监控总览', requiresAuth: true } }, { path: '/data-monitor', name: 'CrawledData', - // 路由懒加载:只有访问该页面时才加载组件,提升首页加载速度 + // 路由懒加载 component: () => import('../views/DataMonitor.vue'), - meta: { title: '数据爬取监控' } + meta: { title: '数据爬取监控', requiresAuth: true } }, { path: '/logs', name: 'MaintenanceLogs', component: () => import('../views/MaintenanceLogs.vue'), - meta: { title: '维修日志中心' } + meta: { title: '维修日志中心', requiresAuth: true } + }, + // 捕获所有未定义的路径,跳转回登录页或首页 + { + path: '/:pathMatch(.*)*', + redirect: '/' } ] @@ -31,12 +44,30 @@ const router = createRouter({ routes }) -// 全局路由守卫:自动修改浏览器标签页标题 +// ======================= +// 核心:全局路由守卫 +// ======================= router.beforeEach((to, from, next) => { + // 1. 设置页面标题 if (to.meta.title) { document.title = to.meta.title } - next() + + // 2. 检查登录状态 (从本地存储获取) + const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true' + + // 3. 鉴权逻辑 + if (to.meta.requiresAuth && !isLoggedIn) { + // 如果页面需要登录但用户未登录,强行拦截并跳转到登录页 + ElMessage.error('请先登录系统') + next({ name: 'Login' }) + } else if (to.name === 'Login' && isLoggedIn) { + // 如果用户已登录却尝试访问登录页,直接送他去首页 + next({ name: 'Dashboard' }) + } else { + // 否则正常放行 + next() + } }) export default router diff --git a/zhandianxinxi/光谱数据监控/src/views/Dashboard.vue b/zhandianxinxi/光谱数据监控/src/views/Dashboard.vue index 284f6b3..125ba56 100644 --- a/zhandianxinxi/光谱数据监控/src/views/Dashboard.vue +++ b/zhandianxinxi/光谱数据监控/src/views/Dashboard.vue @@ -13,40 +13,27 @@