题库导入功能完成,考试计划功能完成。
This commit is contained in:
@@ -18,6 +18,7 @@ const HomePage = () => {
|
||||
const [form] = Form.useForm();
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [historyOptions, setHistoryOptions] = useState<{ value: string; label: string; phone: string }[]>([]);
|
||||
const [isSearching, setIsSearching] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
// 加载历史记录
|
||||
@@ -32,13 +33,19 @@ const HomePage = () => {
|
||||
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));
|
||||
|
||||
// 更新本地历史选项
|
||||
setHistoryOptions(newHistory.map(item => ({
|
||||
value: item.name,
|
||||
label: item.name,
|
||||
phone: item.phone
|
||||
})));
|
||||
};
|
||||
|
||||
const handleNameSelect = (value: string, option: any) => {
|
||||
@@ -47,6 +54,38 @@ const HomePage = () => {
|
||||
}
|
||||
};
|
||||
|
||||
const handleNameChange = async (value: string) => {
|
||||
if (!value) return;
|
||||
|
||||
// 先检查本地历史记录
|
||||
const localOption = historyOptions.find(option => option.value === value);
|
||||
if (localOption && localOption.phone) {
|
||||
form.setFieldsValue({ phone: localOption.phone });
|
||||
return;
|
||||
}
|
||||
|
||||
// 本地没有则从服务器查询
|
||||
try {
|
||||
setIsSearching(true);
|
||||
const response = await userAPI.getUsersByName(value) as any;
|
||||
|
||||
if (response.success && response.data && response.data.length > 0) {
|
||||
// 假设返回的是数组,取第一个匹配的用户
|
||||
const user = response.data[0];
|
||||
if (user && user.phone) {
|
||||
form.setFieldsValue({ phone: user.phone });
|
||||
// 将查询结果保存到本地历史记录
|
||||
saveToHistory(value, user.phone);
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('查询用户失败:', error);
|
||||
// 查询失败不提示用户,保持原有逻辑
|
||||
} finally {
|
||||
setIsSearching(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (values: { name: string; phone: string; password?: string }) => {
|
||||
try {
|
||||
setLoading(true);
|
||||
@@ -59,7 +98,7 @@ const HomePage = () => {
|
||||
}
|
||||
|
||||
// 创建用户或登录
|
||||
const response = await userAPI.createUser(values) as any;
|
||||
const response = await userAPI.validateUserInfo(values) as any;
|
||||
|
||||
if (response.success) {
|
||||
setUser(response.data);
|
||||
@@ -107,6 +146,7 @@ const HomePage = () => {
|
||||
<AutoComplete
|
||||
options={historyOptions}
|
||||
onSelect={handleNameSelect}
|
||||
onChange={handleNameChange}
|
||||
placeholder="请输入您的姓名"
|
||||
size="large"
|
||||
filterOption={(inputValue, option) =>
|
||||
|
||||
Reference in New Issue
Block a user