import { useState, useEffect } from 'react'; import { Card, Form, Input, Button, message, Typography, AutoComplete } from 'antd'; import { useNavigate } from 'react-router-dom'; import { useUser } from '../contexts'; import { userAPI } from '../services/api'; import { validateUserForm } from '../utils/validation'; const { Title } = Typography; interface LoginHistory { name: string; phone: string; } const HomePage = () => { const navigate = useNavigate(); const { setUser } = useUser(); const [form] = Form.useForm(); const [loading, setLoading] = useState(false); const [historyOptions, setHistoryOptions] = useState<{ value: string; label: string; phone: string }[]>([]); useEffect(() => { // 加载历史记录 const history = JSON.parse(localStorage.getItem('loginHistory') || '[]'); setHistoryOptions(history.map((item: LoginHistory) => ({ value: item.name, label: item.name, phone: item.phone }))); }, []); const saveToHistory = (name: string, phone: string) => { const history: LoginHistory[] = JSON.parse(localStorage.getItem('loginHistory') || '[]'); // 移除已存在的同名记录(为了更新位置到最前,或者保持最新) // 简单起见,如果已存在,先移除 const filtered = history.filter(item => item.name !== name); // 添加到头部 filtered.unshift({ name, phone }); // 保留前5条 const newHistory = filtered.slice(0, 5); localStorage.setItem('loginHistory', JSON.stringify(newHistory)); }; const handleNameSelect = (value: string, option: any) => { if (option.phone) { form.setFieldsValue({ phone: option.phone }); } }; const handleSubmit = async (values: { name: string; phone: string; password?: string }) => { try { setLoading(true); // 验证表单 const validation = validateUserForm(values.name, values.phone); if (!validation.valid) { message.error(validation.nameError || validation.phoneError); return; } // 创建用户或登录 const response = await userAPI.createUser(values) as any; if (response.success) { setUser(response.data); saveToHistory(values.name, values.phone); message.success('登录成功,请选择考试科目'); setTimeout(() => { navigate('/subjects'); }, 1000); } } catch (error: any) { message.error(error.message || '登录失败'); } finally { setLoading(false); } }; return (
问卷调查系统

请填写您的基本信息开始答题

option!.value.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1 } />
管理员登录
); }; export default HomePage;