diff --git a/src/routes/index.js b/src/routes/index.js
index f6688c75..4aa05b45 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -1,61 +1,92 @@
// src/routes/index.js
// 路由渲染器 - 根据配置自动生成 Routes
-import React from 'react';
+import React, { useMemo, Suspense } from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { Box, useColorMode } from '@chakra-ui/react';
// 路由配置
-import { routeConfig, PROTECTION_MODES, getMainLayoutRoutes, getStandaloneRoutes } from './routeConfig';
+import { PROTECTION_MODES, getMainLayoutRoutes, getStandaloneRoutes } from './routeConfig';
-// 布局组件 (非懒加载)
-import Auth from '../layouts/Auth';
-import HomeLayout from '../layouts/Home';
-import MainLayout from '../layouts/MainLayout';
+// 布局组件 (非懒加载) - 使用路径别名
+import Auth from '@layouts/Auth';
+import HomeLayout from '@layouts/Home';
+import MainLayout from '@layouts/MainLayout';
-// 保护路由组件
-import ProtectedRoute from '../components/ProtectedRoute';
-import ProtectedRouteRedirect from '../components/ProtectedRouteRedirect';
+// 保护路由组件 - 使用路径别名
+import ProtectedRoute from '@components/ProtectedRoute';
+import ProtectedRouteRedirect from '@components/ProtectedRouteRedirect';
+
+// 错误处理和加载组件 - 使用路径别名
+import ErrorBoundary from '@components/ErrorBoundary';
+import PageLoader from '@components/Loading/PageLoader';
+
+// ==================== 常量配置 ====================
+
+/**
+ * 特殊布局组件映射表
+ * 用于将字符串标识符映射到实际的组件
+ * 性能:O(1) 查找,优于 if-else 链
+ */
+const LAYOUT_COMPONENTS = {
+ Auth,
+ HomeLayout,
+};
+
+/**
+ * 保护模式包装器映射表
+ * 根据保护模式选择对应的保护组件
+ */
+const PROTECTION_WRAPPER_MAP = {
+ [PROTECTION_MODES.MODAL]: ProtectedRoute,
+ [PROTECTION_MODES.REDIRECT]: ProtectedRouteRedirect,
+};
+
+// ==================== 辅助函数 ====================
/**
* 根据保护模式包装组件
+ *
+ * @param {React.ComponentType} Component - 要包装的组件
+ * @param {string} protection - 保护模式 (modal/redirect/public)
+ * @returns {React.ReactElement} 包装后的组件
+ *
+ * 优化:使用映射表替代 if-else,提升代码可维护性和性能
*/
function wrapWithProtection(Component, protection) {
- if (protection === PROTECTION_MODES.MODAL) {
- return (
-
-
-
- );
+ const WrapperComponent = PROTECTION_WRAPPER_MAP[protection];
+
+ // 如果没有对应的保护组件(PUBLIC 模式),直接返回
+ if (!WrapperComponent) {
+ return ;
}
- if (protection === PROTECTION_MODES.REDIRECT) {
- return (
-
-
-
- );
- }
-
- // PUBLIC - 无保护
- return ;
+ return (
+
+
+
+ );
}
/**
* 渲染单个路由
+ *
+ * @param {Object} routeItem - 路由配置项
+ * @param {string} routeItem.path - 路由路径
+ * @param {React.ComponentType|string} routeItem.component - 组件或组件标识符
+ * @param {string} routeItem.protection - 保护模式
+ * @param {number} index - 路由索引(用于 key)
+ * @returns {React.ReactElement} Route 组件
+ *
+ * 优化:
+ * - 使用对象映射替代 if-else 查找组件
+ * - 使用路径+索引作为 key,确保唯一性
*/
function renderRoute(routeItem, index) {
const { path, component, protection } = routeItem;
- // 处理特殊组件(非懒加载)
- let Component;
- if (component === 'Auth') {
- Component = Auth;
- } else if (component === 'HomeLayout') {
- Component = HomeLayout;
- } else {
- Component = component;
- }
+ // 解析组件:优先查找特殊组件映射,否则直接使用(懒加载组件)
+ const Component = LAYOUT_COMPONENTS[component] || component;
return (
getMainLayoutRoutes(), []);
+ const standaloneRoutes = useMemo(() => getStandaloneRoutes(), []);
return (
-
- {/* 带导航栏的主布局 - 所有需要导航栏的页面都在这里 */}
- }>
- {mainLayoutRoutes.map((route, index) => renderRoute(route, index))}
-
+ {/* Suspense 统一处理懒加载组件 */}
+ }>
+ {/* ErrorBoundary 隔离路由错误,防止整个应用崩溃 */}
+
+
+ {/*
+ 带导航栏的主布局 - 嵌套路由
+ 所有 layout: 'main' 的路由都会在 MainLayout 的 中渲染
+ */}
+ }>
+ {mainLayoutRoutes.map(renderRoute)}
+
- {/* 不使用布局的路由 */}
- {standaloneRoutes.map((route, index) => renderRoute(route, index))}
+ {/*
+ 不使用布局的路由(如登录页)
+ layout: 'none' 的路由独立渲染
+ */}
+ {standaloneRoutes.map(renderRoute)}
- {/* 默认重定向到首页 */}
- } />
+ {/* 默认重定向到首页 */}
+ } />
- {/* 404 页面 */}
- } />
-
+ {/* 404 页面 - 捕获所有未匹配的路由 */}
+ } />
+
+
+
);
}