All files / src/router index.js

83.84% Statements 109/130
100% Branches 0/0
100% Functions 0/0
83.84% Lines 109/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * 路由模块
 * Vue Router 4 路由配置,包含路由守卫和页面导航逻辑
 * 
 * @fileOverview 前端路由配置模块,基于Vue Router 4实现
 * @author InspectionFlowSystem Team
 * @version 1.0.0
 * @since 2025-09-16
 * 
 * @example
 * // 在main.js中使用
 * import router from './router/index.js';
 * app.use(router);
 */
 
import { createRouter, createWebHistory } from 'vue-router';
import { isTokenValid } from '../views/logic/Login.logic';
// 导入页面组件
import Login from '../views/Login.vue';
import Home from '../views/Home.vue';
import Dashboard from '../views/Dashboard.vue';
import Inspect from '../views/Inspect.vue';
import Detect from '../views/Detect.vue';
import User from '../views/User.vue';
import Tray from '../views/Tray.vue';
import Logs from '../views/Logs.vue';
import Data from '../views/Data.vue';
import Role from '../views/Role.vue';
import Device from '../views/Device.vue';
import NotFound from '../views/NotFound.vue';
 
/**
 * 路由配置数组
 * @constant {Array} routes
 * @description 定义应用的所有路由规则,包括页面组件和元数据
 * 
 * @property {string} path - 路由路径
 * @property {Component} component - 路由对应的Vue组件
 * @property {Object} meta - 路由元数据,包含权限控制等信息
 * @property {Array} children - 子路由配置
 * 
 * @since 1.0.0
 */
 
const routes = [
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    meta: { requiresAuth: true },
    children: [
      { path: '', component: Dashboard },
      { path: 'inspect', component: Inspect },
      { path: 'detect', component: Detect },
      { path: 'user', component: User },
      { path: 'tray', component: Tray },
      { path: 'data', component: Data },
      { path: 'logs', component: Logs },
      { path: 'role', component: Role },
      { path: 'device', component: Device }
    ]
  },
  { path: '/', redirect: '/login' },
  // 404页面 - 必须放在最后,捕获所有未匹配的路由
  { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }
];
 
/**
 * 路由器实例
 * @constant {Router} router
 * @description 创建并配置Vue Router实例
 * 
 * @property {RouterHistory} history - 路由历史模式配置
 * @property {Array} routes - 路由配置数组
 * 
 * @since 1.0.0
 */
const router = createRouter({
    history: createWebHistory(),
    routes,
});
 
/**
 * 全局前置路由守卫
 * @function beforeEach
 * @param {RouteLocationNormalized} to - 目标路由对象
 * @param {RouteLocationNormalized} from - 当前路由对象  
 * @param {NavigationGuardNext} next - 路由导航函数
 * @description 在每次路由跳转前进行权限验证和登录状态检查
 * 
 * @example
 * // 路由守卫自动执行,无需手动调用
 * // 访问需要认证的页面时会自动检查登录状态
 * 
 * @since 1.0.0
 * 
 * @see {@link isTokenValid} - Token有效性验证函数
 */
router.beforeEach((to, from, next) => {
  const tokenValid = isTokenValid();

  // 对于404页面,不需要登录验证
  if (to.name === 'NotFound') {
    next();
    return;
  }

  // 已登录访问/login自动跳转到/home
  if (to.path === '/login' && tokenValid) {
    next('/home');
    return;
  }

  // 需要登录的页面未登录则跳转到/login
  if (to.meta.requiresAuth && !tokenValid) {
    next('/login');
    return;
  }

  next();
});
 
/**
 * 导出路由器实例
 * @exports router
 * @description 导出配置好的路由器实例,供main.js使用
 * 
 * @since 1.0.0
 */
export default router;