feat: 重构各业务模块管理页面与服务

This commit is contained in:
2026-08-05 17:12:00 +08:00
parent 80e6fccf05
commit fd39e1686a
163 changed files with 18409 additions and 13449 deletions

View File

@@ -1,5 +1,10 @@
import React, { useEffect, useState, useMemo } from 'react';
import React, { useState, useMemo } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useApiMutation } from '../../hooks/useApiMutation';
import { validateResponse } from '../../utils/validate';
import { classroomsSchema } from '../../api/schemas';
import {
App,
Table,
Button,
Modal,
@@ -50,9 +55,8 @@ const typeColor: Record<string, string> = {
};
const ClassroomsPage: React.FC = () => {
const { modal } = App.useApp();
const { hasPermission } = usePermission();
const [data, setData] = useState<any[]>([]);
const [loading, setLoading] = useState(false);
const [modalOpen, setModalOpen] = useState(false);
const [editing, setEditing] = useState<any>(null);
const [showArchived, setShowArchived] = useState(false);
@@ -62,6 +66,56 @@ const ClassroomsPage: React.FC = () => {
const [saving, setSaving] = useState(false);
const {
data = [],
isLoading,
isFetching,
} = useQuery<any[]>({
queryKey: ['classrooms', showArchived],
queryFn: async () => {
try {
return validateResponse<any[]>(
classroomsSchema,
await api.get('/classrooms', { params: { includeArchived: showArchived } }),
);
} catch (e: any) {
message.error(e?.message || '加载失败,请稍后重试');
return [];
}
},
});
const loading = isLoading || isFetching;
const saveMutation = useApiMutation(
async (values: Record<string, unknown>) =>
editing ? api.put(`/classrooms/${editing.id}`, values) : api.post('/classrooms', values),
{ invalidate: [['classrooms']] },
);
const saveCellMutation = useApiMutation(
async ({ record, field, value }: { record: any; field: string; value: unknown }) =>
api.put(`/classrooms/${record.id}`, { [field]: value }),
{ invalidate: [['classrooms']] },
);
const archiveMutation = useApiMutation(
async (id: number) => api.delete(`/classrooms/${id}`),
{ invalidate: [['classrooms']] },
);
const restoreMutation = useApiMutation(
async (id: number) => api.put(`/classrooms/${id}/restore`),
{ invalidate: [['classrooms']] },
);
const purgeMutation = useApiMutation(
async (id: number) => api.delete(`/classrooms/${id}/permanent`),
{ invalidate: [['classrooms']] },
);
const importMutation = useApiMutation(
async (formData: FormData) =>
api.post('/classrooms/import', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
}),
{ invalidate: [['classrooms']] },
);
const filteredData = useMemo(() => {
let result = data;
if (searchText) {
@@ -77,69 +131,67 @@ const ClassroomsPage: React.FC = () => {
return result;
}, [data, searchText, filterStatus]);
const fetchData = async () => {
setLoading(true);
try {
const res: any = await api.get('/classrooms', { params: { includeArchived: showArchived } });
setData(res);
} catch (e: any) {
message.error(e?.message || '加载失败,请稍后重试');
}
setLoading(false);
};
useEffect(() => {
fetchData();
}, [showArchived]);
const handleSave = async () => {
const values = await form.validateFields();
setSaving(true);
try {
if (editing) {
await api.put(`/classrooms/${editing.id}`, values);
message.success('更新成功');
} else {
await api.post('/classrooms', values);
message.success('创建成功');
}
await saveMutation.mutateAsync(values);
message.success(editing ? '更新成功' : '创建成功');
setModalOpen(false);
form.resetFields();
setEditing(null);
fetchData();
} catch (e: any) {
message.error(e?.message || '操作失败');
} catch {
// 错误提示由 useApiMutation 统一处理
} finally {
setSaving(false);
}
};
const saveCell = async (record: any, field: string, value: unknown) => {
await api.put(`/classrooms/${record.id}`, { [field]: value });
message.success('已保存');
await fetchData();
try {
await saveCellMutation.mutateAsync({ record, field, value });
message.success('已保存');
} catch {
// 错误提示由 useApiMutation 统一处理
}
};
const handleArchive = async (id: number) => {
try {
await api.delete(`/classrooms/${id}`);
await archiveMutation.mutateAsync(id);
message.success('已归档');
fetchData();
} catch (e: any) {
message.error(e?.message || '归档失败');
} catch {
// 错误提示由 useApiMutation 统一处理
}
};
const handleRestore = async (id: number) => {
try {
await api.put(`/classrooms/${id}/restore`);
await restoreMutation.mutateAsync(id);
message.success('已恢复');
fetchData();
} catch (e: any) {
message.error(e?.message || '恢复失败');
} catch {
// 错误提示由 useApiMutation 统一处理
}
};
const handlePurge = (id: number, name: string) => {
modal.confirm({
title: `永久删除教室「${name}」?`,
content: '删除后不可恢复,该教室及其排课、租赁、考勤机关联将被清除(存在关联数据时将无法删除)。确定继续?',
okText: '永久删除',
okButtonProps: { danger: true },
cancelText: '取消',
onOk: async () => {
try {
await purgeMutation.mutateAsync(id);
message.success('已永久删除(不可恢复)');
} catch {
// 错误提示由 useApiMutation 统一处理
}
},
});
};
const handleDownloadTemplate = () => {
const baseURL = import.meta.env.PROD
? '/api'
@@ -177,6 +229,7 @@ const ClassroomsPage: React.FC = () => {
</EditableCell>
),
},
{
title: '楼栋',
dataIndex: 'building',
@@ -192,6 +245,7 @@ const ClassroomsPage: React.FC = () => {
</EditableCell>
),
},
{
title: '楼层',
dataIndex: 'floor',
@@ -208,6 +262,7 @@ const ClassroomsPage: React.FC = () => {
</EditableCell>
),
},
{
title: '类型',
width: 90,
@@ -225,6 +280,7 @@ const ClassroomsPage: React.FC = () => {
</EditableCell>
),
},
{
title: '容量',
dataIndex: 'capacity',
@@ -242,6 +298,7 @@ const ClassroomsPage: React.FC = () => {
</EditableCell>
),
},
{
title: '状态',
width: 100,
@@ -278,22 +335,35 @@ const ClassroomsPage: React.FC = () => {
);
},
},
{
title: '操作',
width: 180,
render: (_: any, record: any) => (
<Space>
{record.status === 'archived' ? (
<Popconfirm title="确定恢复此教室?" onConfirm={() => handleRestore(record.id)}>
<PermissionButton
permission="classroom:edit"
size="small"
icon={<UndoOutlined />}
type="link"
>
</PermissionButton>
</Popconfirm>
<>
<Popconfirm title="确定恢复此教室?" onConfirm={() => handleRestore(record.id)}>
<PermissionButton
permission="classroom:edit"
size="small"
icon={<UndoOutlined />}
type="link"
>
</PermissionButton>
</Popconfirm>
{hasPermission('classroom:purge') ? (
<Button
size="small"
danger
type="link"
onClick={() => handlePurge(record.id, record.name)}
>
</Button>
) : null}
</>
) : (
<>
<PermissionButton
@@ -327,7 +397,7 @@ const ClassroomsPage: React.FC = () => {
),
},
],
[],
[handlePurge, hasPermission],
);
return (
@@ -413,15 +483,11 @@ const ClassroomsPage: React.FC = () => {
const formData = new FormData();
formData.append('file', file);
try {
const res: any = await api.post('/classrooms/import', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
const res: any = await importMutation.mutateAsync(formData);
message.success(res.message);
onSuccess?.(res);
fetchData();
} catch (e: any) {
message.error(e?.message || '导入失败');
onError?.(e);
} catch (e) {
onError?.(e as Error);
}
}}
>