blob: 2d91b30ced224896e29dfd9ba2a4174eb821a82c [file] [log] [blame]
Akane12173a7bb972025-06-01 01:05:27 +08001import React, { useState, useEffect } from 'react';
2import { useNavigate, useLocation } from 'react-router-dom';
3import { getUserTorrents, deleteTorrent } from '../../api/personal';
Akane121765b61a72025-05-17 13:52:25 +08004import './personalSubpage.css';
5
6const Upload = ({ onLogout }) => {
7 const navigate = useNavigate();
8 const location = useLocation();
Akane12173a7bb972025-06-01 01:05:27 +08009 const [torrents, setTorrents] = useState([]);
10 const [loading, setLoading] = useState(true);
11 const [error, setError] = useState(null);
12 const [pagination, setPagination] = useState({
13 page: 1,
14 size: 5,
15 total: 0
16 });
17
18
19
20 // 格式化日期
21 const formatDate = (dateString) => {
22 const date = new Date(dateString);
23 return date.toLocaleString();
24 };
25
26 // 获取上传记录
27 const fetchTorrents = async () => {
28 try {
29 setLoading(true);
30 const { records, total } = await getUserTorrents(pagination.page, pagination.size);
31 setTorrents(records);
32 setPagination(prev => ({ ...prev, total }));
33 } catch (err) {
34 setError(err.message);
35 } finally {
36 setLoading(false);
37 }
38 };
39
40
41
42 // 删除种子
43 const handleDelete = async (id) => {
44 if (window.confirm('确定要删除这个种子吗?此操作不可撤销!')) {
45 try {
46 await deleteTorrent(id);
47 // 删除成功后刷新列表
48 fetchTorrents();
49 } catch (err) {
50 alert('删除失败: ' + err.message);
51 }
52 }
53 };
54
55 // 计算总页数
56 const totalPages = Math.ceil(pagination.total / pagination.size);
57
58 // 生成页码数组
59 const getPageNumbers = () => {
60 const pages = [];
61 const maxVisiblePages = 5; // 最多显示5个页码
62
63 let startPage = Math.max(1, pagination.page - Math.floor(maxVisiblePages / 2));
64 let endPage = Math.min(totalPages, startPage + maxVisiblePages - 1);
65
66 // 调整起始页码,确保始终显示 maxVisiblePages 个页码(如果总页数足够)
67 if (endPage - startPage + 1 < maxVisiblePages) {
68 startPage = Math.max(1, endPage - maxVisiblePages + 1);
69 }
70
71 for (let i = startPage; i <= endPage; i++) {
72 pages.push(i);
73 }
74
75 return pages;
76 };
77
78 // 直接跳转到指定页码
79 const jumpToPage = (page) => {
80 if (page >= 1 && page <= totalPages && page !== pagination.page) {
81 setPagination(prev => ({ ...prev, page }));
82 }
83 };
84
85 // 分页改变
86 const handlePageChange = (newPage) => {
87 setPagination(prev => ({ ...prev, page: newPage }));
88 };
89
90 useEffect(() => {
91 fetchTorrents();
92 }, [pagination.page]);
Akane121765b61a72025-05-17 13:52:25 +080093
94 const handleBack = () => {
Akane121765b61a72025-05-17 13:52:25 +080095 navigate('/personal', {
96 state: {
Akane12173a7bb972025-06-01 01:05:27 +080097 fromSubpage: true,
98 dashboardTab: location.state?.dashboardTab
Akane121765b61a72025-05-17 13:52:25 +080099 },
Akane12173a7bb972025-06-01 01:05:27 +0800100 replace: true
Akane121765b61a72025-05-17 13:52:25 +0800101 });
102 };
Akane12173a7bb972025-06-01 01:05:27 +0800103
104 if (loading) {
105 return <div className="subpage-container">加载中...</div>;
106 }
107
108 if (error) {
109 return <div className="subpage-container">错误: {error}</div>;
110 }
111
Akane121765b61a72025-05-17 13:52:25 +0800112 return (
113 <div className="subpage-container">
Akane12173a7bb972025-06-01 01:05:27 +0800114 <button className="back-button" onClick={handleBack}>
Akane121765b61a72025-05-17 13:52:25 +0800115 返回个人中心
116 </button>
117
118 <h2 className="page-title">上传记录</h2>
119
120 <table className="uploads-table">
121 <thead>
122 <tr>
123 <th>资源名称</th>
124 <th>大小</th>
Akane121765b61a72025-05-17 13:52:25 +0800125 <th>上传时间</th>
Akane12173a7bb972025-06-01 01:05:27 +0800126 <th>下载次数</th>
Akane121765b61a72025-05-17 13:52:25 +0800127 <th>操作</th>
128 </tr>
129 </thead>
130 <tbody>
Akane12173a7bb972025-06-01 01:05:27 +0800131 {torrents.map(torrent => (
132 <tr key={torrent.id} className="list-item">
133 <td>{torrent.torrentName}</td>
134 <td>{torrent.formattedSize}</td>
135 <td>{formatDate(torrent.createTime)}</td>
136 <td>{torrent.downloadCount || 0}</td>
Akane121765b61a72025-05-17 13:52:25 +0800137 <td>
Akane12173a7bb972025-06-01 01:05:27 +0800138 <button
139 className="action-btn delete-btn"
140 onClick={() => handleDelete(torrent.id)}
141 >
142 删除
143 </button>
Akane121765b61a72025-05-17 13:52:25 +0800144 </td>
145 </tr>
146 ))}
147 </tbody>
148 </table>
Akane12173a7bb972025-06-01 01:05:27 +0800149
150 {/* 修改后的分页控件 */}
151 <div className="pagination">
152 <button
153 disabled={pagination.page <= 1}
154 onClick={() => jumpToPage(1)}
155 className="page-nav"
156 >
157 首页
158 </button>
159 <button
160 disabled={pagination.page <= 1}
161 onClick={() => jumpToPage(pagination.page - 1)}
162 className="page-nav"
163 >
164 上一页
165 </button>
166
167 {/* 显示页码 */}
168 {getPageNumbers().map(number => (
169 <button
170 key={number}
171 onClick={() => jumpToPage(number)}
172 className={`page-number ${pagination.page === number ? 'active' : ''}`}
173 >
174 {number}
175 </button>
176 ))}
177
178 {/* 显示省略号(如果有更多页) */}
179 {totalPages > getPageNumbers()[getPageNumbers().length - 1] && (
180 <span className="ellipsis">...</span>
181 )}
182
183 <button
184 disabled={pagination.page >= totalPages}
185 onClick={() => jumpToPage(pagination.page + 1)}
186 className="page-nav"
187 >
188 下一页
189 </button>
190 <button
191 disabled={pagination.page >= totalPages}
192 onClick={() => jumpToPage(totalPages)}
193 className="page-nav"
194 >
195 末页
196 </button>
197
198 <div className="page-info">
199 <span> {totalPages} 页</span>
200 <span>跳至</span>
201 <input
202 type="number"
203 min="1"
204 max={totalPages}
205 onKeyDown={(e) => {
206 if (e.key === 'Enter') {
207 const page = parseInt(e.target.value);
208 if (!isNaN(page)) {
209 jumpToPage(Math.max(1, Math.min(page, totalPages)));
210 e.target.value = '';
211 }
212 }
213 }}
214 />
215 <span>页</span>
216 </div>
217 </div>
Akane121765b61a72025-05-17 13:52:25 +0800218 </div>
219 );
220};
221
222export default Upload;