新增受保护路由组件以增强认证逻辑,更新知识库管理页面的测试用例以集成用户事件,提升测试的准确性和稳定性。同时,优化相关依赖项配置,确保与最新库版本兼容,提升代码可维护性和用户体验。

This commit is contained in:
zyh
2025-04-11 13:44:22 +00:00
parent c0a623bf23
commit 6d53da5880
7 changed files with 262 additions and 104 deletions

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect, createContext, useContext } from 'react';
import React, { useState, useEffect} from 'react';
import { createRoot } from 'react-dom/client';
import {
createBrowserRouter,
@@ -18,7 +18,6 @@ import {
Switch, Badge, Image, Upload, Divider, Descriptions,
Popconfirm, Tag, Statistic, DatePicker, Radio, Progress, Tabs, List, Alert, Collapse, Empty, Drawer
} from 'antd';
import zhCN from "antd/locale/zh_CN";
import {
MenuFoldOutlined,
MenuUnfoldOutlined,
@@ -31,8 +30,6 @@ import {
BookOutlined,
FileOutlined,
PieChartOutlined,
UploadOutlined,
GlobalOutlined,
VerticalAlignTopOutlined,
CloseOutlined,
SearchOutlined
@@ -40,27 +37,15 @@ import {
import {
QueryClient,
QueryClientProvider,
useQuery,
useMutation,
useQueryClient
} from '@tanstack/react-query';
import axios from 'axios';
import dayjs from 'dayjs';
import weekday from 'dayjs/plugin/weekday';
import localeData from 'dayjs/plugin/localeData';
import { uploadMinIOWithPolicy } from '@d8d-appcontainer/api';
import type { MinioUploadPolicy } from '@d8d-appcontainer/types';
import { Line, Pie, Column } from "@ant-design/plots";
import 'dayjs/locale/zh-cn';
import type {
GlobalConfig
} from '../share/types.ts';
import {
EnableStatus, DeleteStatus, ThemeMode, FontSize, CompactMode
} from '../share/types.ts';
import { getEnumOptions } from './utils.ts';
import {
AuthProvider,
@@ -81,7 +66,7 @@ import {ThemeSettingsPage} from './pages_theme_settings.tsx'
import { ChartDashboardPage } from './pages_chart.tsx';
import { LoginMapPage } from './pages_map.tsx';
import { LoginPage } from './pages_login_reg.tsx';
import { ProtectedRoute } from './components_protected_route.tsx';
// 配置 dayjs 插件
dayjs.extend(weekday);
@@ -440,34 +425,7 @@ const MainLayout = () => {
);
};
// 受保护的路由组件
const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
const { isAuthenticated, isLoading } = useAuth();
const navigate = useNavigate();
useEffect(() => {
// 只有在加载完成且未认证时才重定向
if (!isLoading && !isAuthenticated) {
navigate('/admin/login', { replace: true });
}
}, [isAuthenticated, isLoading, navigate]);
// 显示加载状态,直到认证检查完成
if (isLoading) {
return (
<div className="flex justify-center items-center h-screen">
<div className="loader ease-linear rounded-full border-4 border-t-4 border-gray-200 h-12 w-12"></div>
</div>
);
}
// 如果未认证且不再加载中,不显示任何内容(等待重定向)
if (!isAuthenticated) {
return null;
}
return children;
};
// 错误页面组件
const ErrorPage = () => {