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