blob: 9aa9ba03129d9e3e73acf750aa605e2bd0e98723 [file] [log] [blame]
刘嘉昕9cdeca22025-06-03 16:54:30 +08001// // src/pages/TorrentDetail.jsx
2// import React, { useEffect, useState } from 'react';
3// import { useParams } from 'react-router-dom';
4// import axios from 'axios';
5
6// const TorrentDetail = () => {
7// const { id } = useParams(); // 获取 URL 中的 torrentid
8// const [torrent, setTorrent] = useState(null);
9// const [seeders, setSeeders] = useState([]);
10// const [loading, setLoading] = useState(true);
11// useEffect(() => {
12// console.log("Received Torrent:", torrent);
13// }, [torrent]);
14// useEffect(() => {
15// axios.get(`http://localhost:8080/torrent/${id}`)
16// .then(res => setTorrent(res.data))
17// setTorrent(res.data);
18// return axios.get(`http://localhost:8080/torrent/${res.data.infoHash}/seeders`);
19// }).then(res =>
20// setSeeders(res.data))
21// .catch(err => {
22// console.error("Error fetching torrent details:", err)
23// .finally(() => setLoading(false));
24// },[id]);
25
26// if (!torrent) return <div className="p-4">加载中...</div>;
27
28// // 格式化文件大小
29// const formatSize = (bytes) => {
30// if (bytes === 0) return '0 B';
31// const k = 1024;
32// const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
33// const i = Math.floor(Math.log(bytes) / Math.log(k));
34// return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
35// };
36
37// // 格式化速度
38// const formatSpeed = (bytesPerSec) => {
39// if (bytesPerSec < 1024) return bytesPerSec.toFixed(2) + ' B/s';
40// if (bytesPerSec < 1024 * 1024) return (bytesPerSec / 1024).toFixed(2) + ' KB/s';
41// return (bytesPerSec / (1024 * 1024)).toFixed(2) + ' MB/s';
42// };
43// return (
44// <div className="max-w-2xl mx-auto p-6 bg-white shadow rounded mt-6">
45// <h1 className="text-2xl font-bold mb-4">{torrent.torrentTitle}</h1>
46
47// <div className="space-y-2 text-gray-700">
48// <p><strong>简介:</strong>{torrent.description}</p>
49// {/* 修复上传人显示 */}
50// <p><strong>上传人:</strong>{torrent.uploader_id !== undefined ? torrent.uploader_id : '未知用户'}</p>
51// <p><strong>上传时间:</strong>{new Date(torrent.uploadTime).toLocaleString()}</p>
52// <p><strong>下载数:</strong>{torrent.downloadCount}</p>
53// <p><strong>文件大小:</strong>{torrent.torrentSize} B</p>
54// <p><strong>文件分辨率:</strong>{torrent.dpi}</p>
55// <p><strong>文件字幕:</strong>{torrent.caption}</p>
56// <p><strong>最后做种时间:</strong>{new Date(torrent.lastseed).toLocaleString()}</p>
57// </div>
58// {/* 做种者列表 */}
59// <div className="mt-6">
60// <h2 className="text-xl font-semibold mb-3">做种用户 ({seeders.length})</h2>
61// {seeders.length > 0 ? (
62// <div className="overflow-x-auto">
63// <table className="min-w-full bg-white border border-gray-200">
64// <thead className="bg-gray-100">
65// <tr>
66// <th className="py-2 px-4 border-b">用户名</th>
67// <th className="py-2 px-4 border-b">已上传</th>
68// <th className="py-2 px-4 border-b">上传速度</th>
69// <th className="py-2 px-4 border-b">已下载</th>
70// <th className="py-2 px-4 border-b">下载速度</th>
71// <th className="py-2 px-4 border-b">客户端</th>
72// <th className="py-2 px-4 border-b">最后活动</th>
73// </tr>
74// </thead>
75// <tbody>
76// {seeders.map((seeder, index) => (
77// <tr key={index} className={index % 2 === 0 ? 'bg-gray-50' : 'bg-white'}>
78// <td className="py-2 px-4 border-b text-center">{seeder.username}</td>
79// <td className="py-2 px-4 border-b text-center">{formatSize(seeder.uploaded)}</td>
80// <td className="py-2 px-4 border-b text-center text-green-600">
81// {formatSpeed(seeder.uploadSpeed)}
82// </td>
83// <td className="py-2 px-4 border-b text-center">{formatSize(seeder.downloaded)}</td>
84// <td className="py-2 px-4 border-b text-center">
85// {seeder.downloadSpeed > 0 ? formatSpeed(seeder.downloadSpeed) : '-'}
86// </td>
87// <td className="py-2 px-4 border-b text-center">{seeder.client}</td>
88// <td className="py-2 px-4 border-b text-center">
89// {new Date(seeder.lastActive).toLocaleTimeString()}
90// </td>
91// </tr>
92// ))}
93// </tbody>
94// </table>
95// </div>
96// ) : (
97// <div className="p-4 text-center text-gray-500 bg-gray-50 rounded">
98// 当前没有用户在做种
99// </div>
100// )}
101// </div>
102// </div>
103// );
104// };
105
106// export default TorrentDetail;
107// src/pages/TorrentDetail.jsx
108import React, { useEffect, useState } from 'react';
109import { useParams } from 'react-router-dom';
110import axios from 'axios';
111
112const TorrentDetail = () => {
113 const { id } = useParams();
114 const [torrent, setTorrent] = useState(null);
115 const [seeders, setSeeders] = useState([]);
116 const [loading, setLoading] = useState(true);
117
118 useEffect(() => {
119 // 获取种子基本信息
120 axios.get(`http://localhost:8080/torrent/${id}`)
121 .then(res => {
122 setTorrent(res.data);
123 // 获取做种者信息
124 return axios.get(`http://localhost:8080/torrent/${res.data.infoHash}/seeders`);
125 })
126 .then(res => setSeeders(res.data))
127 .catch(err => console.error('获取数据失败', err))
128 .finally(() => setLoading(false));
129 }, [id]);
130
131 if (loading) return <div className="p-4">加载中...</div>;
132 if (!torrent) return <div className="p-4">种子不存在</div>;
133 console.log('Received Torrent:', torrent);
134 console.log('Received Seeders:', seeders);
135
136 // 格式化文件大小
137 const formatSize = (bytes) => {
138 if (bytes === 0) return '0 B';
139 const k = 1024;
140 const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
141 const i = Math.floor(Math.log(bytes) / Math.log(k));
142 return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
143 };
144
145 // 格式化速度
146 const formatSpeed = (bytesPerSec) => {
147 if (bytesPerSec < 1024) return bytesPerSec.toFixed(2) + ' B/s';
148 if (bytesPerSec < 1024 * 1024) return (bytesPerSec / 1024).toFixed(2) + ' KB/s';
149 return (bytesPerSec / (1024 * 1024)).toFixed(2) + ' MB/s';
150 };
151
152 return (
153 <div className="max-w-4xl mx-auto p-6 bg-white shadow rounded mt-6">
154 <h1 className="text-2xl font-bold mb-4">{torrent.torrentTitle}</h1>
155
156 {/* 种子基本信息 */}
157 <div className="mb-8 p-4 bg-gray-50 rounded">
158 <h2 className="text-xl font-semibold mb-3">基本信息</h2>
159 <div className="grid grid-cols-1 md:grid-cols-2 gap-4 text-gray-700">
160 <div>
161 <p><strong>简介:</strong>{torrent.description || '暂无简介'}</p>
162 <p><strong>上传人:</strong>{torrent.uploader_id || '未知用户'}</p>
163 <p><strong>上传时间:</strong>{new Date(torrent.uploadTime).toLocaleString()}</p>
164 <p><strong>文件大小:</strong>{formatSize(torrent.torrentSize)}</p>
165 </div>
166 <div>
167 <p><strong>下载数:</strong>{torrent.downloadCount || 0}</p>
168 <p><strong>做种数:</strong>{seeders.length}</p>
169 <p><strong>文件分辨率:</strong>{torrent.dpi || '未知'}</p>
170 <p><strong>文件字幕:</strong>{torrent.caption || '无'}</p>
171 <p><strong>最后做种时间:</strong>{torrent.lastseed ? new Date(torrent.lastseed).toLocaleString() : '暂无'}</p>
172 </div>
173 </div>
174 </div>
175
176 {/* 做种者列表 */}
177 <div className="mt-6">
178 <h2 className="text-xl font-semibold mb-3">做种用户 ({seeders.length})</h2>
179 {seeders.length > 0 ? (
180 <div className="overflow-x-auto">
181 <table className="min-w-full bg-white border border-gray-200">
182 <thead className="bg-gray-100">
183 <tr>
184 <th className="py-2 px-4 border-b">用户名</th>
185 <th className="py-2 px-4 border-b">已上传</th>
186 <th className="py-2 px-4 border-b">上传时间</th>
187 <th className="py-2 px-4 border-b">完成时间</th>
188 <th className="py-2 px-4 border-b">上传速度</th>
189 <th className="py-2 px-4 border-b">已下载</th>
190 <th className="py-2 px-4 border-b">下载速度</th>
191 <th className="py-2 px-4 border-b">客户端</th>
192 <th className="py-2 px-4 border-b">端口</th>
193 <th className="py-2 px-4 border-b">最后活动</th>
194 </tr>
195 </thead>
196 <tbody>
197 {seeders.map((seeder, index) => (
198 <tr key={index} className={index % 2 === 0 ? 'bg-gray-50' : 'bg-white'}>
199 <td className="py-2 px-4 border-b text-center">{seeder.username}</td>
200 <td className="py-2 px-4 border-b text-center">{formatSize(seeder.uploaded)}</td>
201 <td className="py-2 px-4 border-b text-center text-green-600">
202 {new Date(seeder.createdAt).toLocaleString()}
203 </td>
204 <td className="py-2 px-4 border-b text-center text-green-600">
205 {new Date(seeder.completed_time).toLocaleString()}
206 </td>
207 <td className="py-2 px-4 border-b text-center text-green-600">
208 {formatSpeed(seeder.uploadSpeed)}
209 </td>
210 <td className="py-2 px-4 border-b text-center">{formatSize(seeder.downloaded)}</td>
211 <td className="py-2 px-4 border-b text-center">
212 {seeder.downloadSpeed > 0 ? formatSpeed(seeder.downloadSpeed) : '-'}
213 </td>
214 <td className="py-2 px-4 border-b text-center">{seeder.client}</td>
215 <td className="py-2 px-4 border-b text-center">{seeder.port}</td>
216 <td className="py-2 px-4 border-b text-center">
217 {seeder.lastEvent}
218 </td>
219 </tr>
220 ))}
221 </tbody>
222 </table>
223 </div>
224 ) : (
225 <div className="p-4 text-center text-gray-500 bg-gray-50 rounded">
226 当前没有用户在做种
227 </div>
228 )}
229 </div>
230 </div>
231 );
232};
233
234export default TorrentDetail;