956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 1 | import React from 'react';
|
| 2 | import { useParams } from 'react-router-dom';
|
| 3 | import './App.css';
|
22301133 | 0f9623f | 2025-06-06 00:22:05 +0800 | [diff] [blame^] | 4 | import { API_BASE_URL } from "./config";
|
956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 5 |
|
| 6 | export default function TorrentDetailPage() {
|
| 7 | const { torrentId } = useParams();
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 8 | 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)
|
22301133 | 0f9623f | 2025-06-06 00:22:05 +0800 | [diff] [blame^] | 17 | const downloadUrl = `${API_BASE_URL}/api/get-torrent?userId=${encodeURIComponent(userId)}&torrentId=${encodeURIComponent(torrentId)}`;
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 18 |
|
| 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);
|
22301133 | 0f9623f | 2025-06-06 00:22:05 +0800 | [diff] [blame^] | 47 | fetch(`${API_BASE_URL}/api/torrent-detail?id=${encodeURIComponent(torrentId)}`)
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 48 | .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>;
|
956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 65 |
|
| 66 | return (
|
| 67 | <div className="container">
|
| 68 | <h1>种子详情页</h1>
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 69 | <h2 style={{ fontSize: 'inherit', fontWeight: 'normal', textAlign: 'left' }}>标题: {detail.title || `种子${torrentId}`}</h2>
|
| 70 | <p style={{ fontSize: 'inherit', textAlign: 'left' }}>简介: {detail.description || `这是种子${torrentId}的详细信息。`}</p>
|
956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 71 | <div style={{ textAlign: 'center', marginTop: '20px' }}>
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 72 | <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 | >
|
956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 83 | 下载
|
| 84 | </button>
|
| 85 | </div>
|
| 86 | </div>
|
| 87 | );
|
| 88 | }
|