import React, { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import { Card, Statistic, Row, Col, Typography, message, Space, Avatar, Button, Modal } from 'antd' import { FileTextOutlined, CheckCircleOutlined, UnorderedListOutlined, EditOutlined, RocketOutlined, BookOutlined, UserOutlined, LogoutOutlined, SettingOutlined, UnorderedListOutlined as ListOutlined, } from '@ant-design/icons' import * as questionApi from '../api/question' import type { Statistics } from '../types/question' import styles from './Home.module.less' const { Title, Paragraph, Text } = Typography // 题型配置 - 使用数据库中的实际类型 const questionTypes = [ { key: 'multiple-choice', title: '选择题', icon: , color: '#1677ff', description: '基础知识考察', }, { key: 'multiple-selection', title: '多选题', icon: , color: '#52c41a', description: '综合能力提升', }, { key: 'true-false', title: '判断题', icon: , color: '#fa8c16', description: '快速判断训练', }, { key: 'fill-in-blank', title: '填空题', icon: , color: '#722ed1', description: '填空补充练习', }, { key: 'short-answer', title: '简答题', icon: , color: '#eb2f96', description: '深度理解练习', }, ] interface UserInfo { username: string nickname: string avatar: string } const Home: React.FC = () => { const navigate = useNavigate() const [userInfo, setUserInfo] = useState(null) const [statistics, setStatistics] = useState({ total_questions: 0, answered_questions: 0, correct_answers: 0, wrong_questions: 0, accuracy: 0, }) // 加载统计数据 const loadStatistics = async () => { try { const res = await questionApi.getStatistics() if (res.success && res.data) { setStatistics(res.data) } } catch (error) { console.error('加载统计失败:', error) } } // 加载用户信息 useEffect(() => { const token = localStorage.getItem('token') const savedUserInfo = localStorage.getItem('user') if (token && savedUserInfo) { try { setUserInfo(JSON.parse(savedUserInfo)) } catch (e) { console.error('解析用户信息失败', e) } } }, []) useEffect(() => { loadStatistics() }, []) // 点击题型卡片 const handleTypeClick = async (type: string) => { try { // 加载该题型的题目列表 const res = await questionApi.getQuestions({ type }) if (res.success && res.data && res.data.length > 0) { // 跳转到答题页面,并传递题型参数 navigate(`/question?type=${type}`) } else { message.warning('该题型暂无题目') } } catch (error) { message.error('加载题目失败') } } // 退出登录 const handleLogout = () => { Modal.confirm({ title: '确定要退出登录吗?', onOk: () => { localStorage.removeItem('token') localStorage.removeItem('user') setUserInfo(null) message.success('已退出登录') navigate('/login') }, }) } return (
{/* 头部 */}
AnKao 刷题 选择题型开始练习
{/* 用户信息 */} {userInfo && (
} />
{userInfo.nickname} @{userInfo.username}
)}
{/* 统计卡片 */} {/* 题型选择 */}
<FileTextOutlined /> 选择题型 {questionTypes.map(type => ( handleTypeClick(type.key)} styles={{ body: { textAlign: 'center', padding: '20px 12px', } }} >
{type.icon}
{type.title} {type.description}
))}
{/* 快速开始 */}
<RocketOutlined /> 快速开始 navigate('/question')} >
随机练习 从所有题型中随机抽取
navigate('/wrong-questions')} >
错题本 复习错题,巩固薄弱知识点
navigate('/question-list')} >
题目列表 查看所有题目和答案
{/* 仅 yanlongqi 用户显示题库管理 */} {userInfo?.username === 'yanlongqi' && ( navigate('/question-management')} >
题库管理 添加、编辑和删除题目
)}
) } export default Home