blob: ecf4ee1fcd7cb05a9764080f2701bfd80e37e104 [file] [log] [blame]
ym923f0485a52025-06-09 20:18:10 +08001import React, { useEffect, useState } from 'react';
2import {
3 Table,
4 Button,
5 Modal,
6 Image,
7 message,
8 Tag,
9 Space,
10 Spin,
11 Tooltip,
12} from 'antd';
13import { ExclamationCircleOutlined } from '@ant-design/icons';
14import {
15 getAllRequests,
16 deleteRequest,
17} from '../api/request';
18import { useNavigate } from 'react-router-dom';
19
20const { confirm } = Modal;
21
22const RequestAdminPanel = () => {
23 const [requests, setRequests] = useState([]);
24 const [loading, setLoading] = useState(false);
25 const navigate = useNavigate();
26
27 // 获取所有求助帖
28 const fetchRequests = async () => {
29 setLoading(true);
30 try {
31 const data = await getAllRequests();
32 setRequests(data);
33 } catch (error) {
34 message.error('获取求助帖失败');
35 } finally {
36 setLoading(false);
37 }
38 };
39
40 useEffect(() => {
41 fetchRequests();
42 }, []);
43
44 // 删除确认弹窗
45 const showDeleteConfirm = (requestid) => {
46 confirm({
47 title: '确认删除该求助帖吗?',
48 icon: <ExclamationCircleOutlined />,
49 okText: '删除',
50 okType: 'danger',
51 cancelText: '取消',
52 onOk() {
53 handleDelete(requestid);
54 },
55 });
56 };
57
58 // 删除求助帖
59 const handleDelete = async (requestid) => {
60 try {
61 const success = await deleteRequest(requestid);
62 if (success) {
63 message.success('删除成功');
64 fetchRequests();
65 } else {
66 message.error('删除失败');
67 }
68 } catch {
69 message.error('删除请求失败');
70 }
71 };
72
73 // 处理按钮示例
74 const handleProcess = (request) => {
75 navigate(`/process/${request.requestid}`, {
76 state: {
77 requestid: request.requestid,
78 helpedid: request.userid,
79 loaduser: request.loaduser,
80 money: request.money,
81 torrentid: request.torrentid
82 },
83 });
84 };
85
86 console.log('请求数据:', requests);
87
88 // 表格列定义
89 const columns = [
90 {
91 title: 'ID',
92 dataIndex: 'requestid',
93 key: 'requestid',
94 width: 60,
95 fixed: 'left',
96 },
97 {
98 title: '发帖ID',
99 dataIndex: 'userid',
100 key: 'userid',
101 width: 100,
102 },
103 {
104 title: '上传者ID',
105 dataIndex: 'loaduser',
106 key: 'loaduser',
107 width: 100,
108 render: (val) => val ?? <Tag color="default">目前没有</Tag>,
109 },
110 {
111 title: '资源名字',
112 dataIndex: 'name',
113 key: 'name',
114 width: 120,
115 ellipsis: true,
116 },
117 {
118 title: '内容描述',
119 dataIndex: 'plot',
120 key: 'plot',
121 ellipsis: { showTitle: false },
122 render: (text) => (
123 <Tooltip placement="topLeft" title={text}>
124 {text}
125 </Tooltip>
126 ),
127 },
128 {
129 title: '悬赏金额',
130 dataIndex: 'money',
131 key: 'money',
132 width: 80,
133 render: (money) => <Tag color="volcano">{money}元</Tag>,
134 },
135 {
136 title: '照片',
137 dataIndex: 'photo',
138 key: 'photo',
139 width: 100,
140 render: (url) =>
141 url ? (
142 <Image
143 src={`http://localhost:8080${url}`}
144 width={80}
145 height={80}
146 style={{ objectFit: 'cover' }}
147 placeholder={<Spin />}
148 />
149 ) : (
150 <Tag color="default">无</Tag>
151 ),
152 },
153 {
154 title: '年份',
155 dataIndex: 'year',
156 key: 'year',
157 width: 80,
158 },
159 {
160 title: '国家',
161 dataIndex: 'country',
162 key: 'country',
163 width: 100,
164 },
165 {
166 title: '种子号',
167 dataIndex: 'torrentid',
168 key: 'torrentid',
169 width: 120,
170 render: (val) =>
171 val !== null && val !== undefined ? (
172 <Tag color="green">{val}</Tag>
173 ) : (
174 <Tag color="default">暂无</Tag>
175 ),
176 },
177 {
178 title: '发布时间',
179 dataIndex: 'requesttime',
180 key: 'requesttime',
181 width: 180,
182 render: (text) => {
183 if (!text) return <Tag color="default">暂无</Tag>;
184 return new Date(text).toLocaleString();
185 },
186 },
187 {
188 title: '操作',
189 key: 'action',
190 fixed: 'right',
191 width: 150,
192 render: (_, record) => (
193 <Space size="middle">
194 <Button
195 type="primary"
196 onClick={() => handleProcess(record)}
197 >
198 处理
199 </Button>
200 <Button
201 danger
202 onClick={() => showDeleteConfirm(record.requestid)}
203 >
204 删除
205 </Button>
206 </Space>
207 ),
208 },
209 ];
210
211 return (
212 <div style={{ padding: 20 }}>
213 <h2 style={{ marginBottom: 20 }}>求助帖管理面板</h2>
214 <Table
215 rowKey="requestid"
216 columns={columns}
217 dataSource={requests}
218 loading={loading}
219 scroll={{ x: 1600 }}
220 pagination={{ pageSize: 10 }}
221 bordered
222 size="middle"
223 />
224 </div>
225 );
226};
227
228export default RequestAdminPanel;