blob: 8d3ee432e9f6f0b2271beb469cd584acf4e8e131 [file] [log] [blame]
9563036699e95ae32025-06-02 21:42:11 +08001import React from 'react';
2import { useParams } from 'react-router-dom';
3import './App.css';
4
5export default function TorrentDetailPage() {
6 const { torrentId } = useParams();
kjWei53b79ae2025-06-05 09:18:35 +00007 const [detail, setDetail] = React.useState(null);
8 const [loading, setLoading] = React.useState(true);
9 const [error, setError] = React.useState(null);
10 // 假设你从某个地方获取了 userId(例如登录状态、localStorage 等)
11 const [userId] = React.useState('user1550e8400-e29b-41d4-a716-44665544000023'); // 替换为实际的用户 ID
12
13 const handleClick = () => {
14 // 构造下载 URL,包含 userId 和 torrentId 参数
15 console.log(torrentId)
16 const downloadUrl = `http://192.168.5.9:8080/api/get-torrent?userId=${encodeURIComponent(userId)}&torrentId=${encodeURIComponent(torrentId)}`;
17
18 // 发起 GET 请求下载文件
19 fetch(downloadUrl)
20 .then(response => {
21 if (!response.ok) {
22 throw new Error('下载失败');
23 }
24 return response.blob();
25 })
26 .then(blob => {
27 // 创建下载链接并触发下载
28 const url = window.URL.createObjectURL(blob);
29 const a = document.createElement('a');
30 a.href = url;
31 a.download = `torrent-${torrentId}.torrent`;
32 document.body.appendChild(a);
33 a.click();
34 window.URL.revokeObjectURL(url);
35 document.body.removeChild(a);
36 })
37 .catch(error => {
38 console.error('下载错误:', error);
39 alert('下载失败: ' + error.message);
40 });
41 };
42
43 React.useEffect(() => {
44 setLoading(true);
45 setError(null);
46 fetch(`http://192.168.5.9:8080/api/torrent-detail?id=${encodeURIComponent(torrentId)}`)
47 .then(res => {
48 if (!res.ok) throw new Error('网络错误');
49 return res.json();
50 })
51 .then(data => {
52 setDetail(data);
53 setLoading(false);
54 })
55 .catch(err => {
56 setError(err.message);
57 setLoading(false);
58 });
59 }, [torrentId]);
60
61 if (loading) return <div className="container"><h1>加载中...</h1></div>;
62 if (error) return <div className="container"><h1>加载失败: {error}</h1></div>;
63 if (!detail) return <div className="container"><h1>未找到详情</h1></div>;
9563036699e95ae32025-06-02 21:42:11 +080064
65 return (
66 <div className="container">
67 <h1>种子详情页</h1>
kjWei53b79ae2025-06-05 09:18:35 +000068 <h2 style={{ fontSize: 'inherit', fontWeight: 'normal', textAlign: 'left' }}>标题: {detail.title || `种子${torrentId}`}</h2>
69 <p style={{ fontSize: 'inherit', textAlign: 'left' }}>简介: {detail.description || `这是种子${torrentId}的详细信息。`}</p>
9563036699e95ae32025-06-02 21:42:11 +080070 <div style={{ textAlign: 'center', marginTop: '20px' }}>
kjWei53b79ae2025-06-05 09:18:35 +000071 <button
72 style={{
73 padding: '10px 20px',
74 fontSize: '16px',
75 cursor: 'pointer',
76 backgroundColor: '#d3f0ff',
77 border: 'none',
78 borderRadius: '4px'
79 }}
80 onClick={handleClick}
81 >
9563036699e95ae32025-06-02 21:42:11 +080082 下载
83 </button>
84 </div>
85 </div>
86 );
87}