blob: ab34d98fafa1016dbfd4c27deb68afb37aed1b88 [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
wht10563a82025-06-08 15:52:18 +080013 const [isFavorite, setIsFavorite] = React.useState(false); // 收藏状态
rhjc6a4ee02025-06-06 00:45:18 +080014
15 const handleClick = () => {
16 // 构造下载 URL,包含 userId 和 torrentId 参数
17 console.log(torrentId)
223011330f9623f2025-06-06 00:22:05 +080018 const downloadUrl = `${API_BASE_URL}/api/get-torrent?userId=${encodeURIComponent(userId)}&torrentId=${encodeURIComponent(torrentId)}`;
wht10563a82025-06-08 15:52:18 +080019
rhjc6a4ee02025-06-06 00:45:18 +080020 // 发起 GET 请求下载文件
21 fetch(downloadUrl)
22 .then(response => {
23 if (!response.ok) {
24 throw new Error('下载失败');
25 }
26 return response.blob();
27 })
28 .then(blob => {
29 // 创建下载链接并触发下载
30 const url = window.URL.createObjectURL(blob);
31 const a = document.createElement('a');
32 a.href = url;
33 a.download = `torrent-${torrentId}.torrent`;
34 document.body.appendChild(a);
35 a.click();
36 window.URL.revokeObjectURL(url);
37 document.body.removeChild(a);
38 })
39 .catch(error => {
40 console.error('下载错误:', error);
41 alert('下载失败: ' + error.message);
42 });
43 };
44
wht10563a82025-06-08 15:52:18 +080045 // 收藏按钮示例逻辑(仅前端切换)
46 const handleFavorite = () => {
47 setIsFavorite(fav => !fav);
48 };
49
rhjc6a4ee02025-06-06 00:45:18 +080050 React.useEffect(() => {
51 setLoading(true);
52 setError(null);
223011330f9623f2025-06-06 00:22:05 +080053 fetch(`${API_BASE_URL}/api/torrent-detail?id=${encodeURIComponent(torrentId)}`)
rhjc6a4ee02025-06-06 00:45:18 +080054 .then(res => {
55 if (!res.ok) throw new Error('网络错误');
56 return res.json();
57 })
58 .then(data => {
59 setDetail(data);
60 setLoading(false);
61 })
62 .catch(err => {
63 setError(err.message);
64 setLoading(false);
65 });
66 }, [torrentId]);
67
68 if (loading) return <div className="container"><h1>加载中...</h1></div>;
69 if (error) return <div className="container"><h1>加载失败: {error}</h1></div>;
70 if (!detail) return <div className="container"><h1>未找到详情</h1></div>;
9563036699e95ae32025-06-02 21:42:11 +080071
72 return (
wht10563a82025-06-08 15:52:18 +080073 <div className="container" style={{ display: "flex", justifyContent: "center", alignItems: "center", minHeight: "100vh" }}>
74 <div
75 style={{
76 background: "#fff",
77 borderRadius: 16,
78 boxShadow: "0 4px 24px #e0e7ff",
79 padding: "36px 48px",
80 maxWidth: 540,
81 width: "100%",
82 marginTop: 48,
83 }}
84 >
85 <h1 style={{ color: "#1976d2", fontWeight: 700, marginBottom: 24, fontSize: 28, letterSpacing: 1 }}>种子详情页</h1>
86 <div style={{ marginBottom: 18 }}>
87 <div style={{ fontSize: 20, fontWeight: 600, marginBottom: 8, color: "#222" }}>
88 标题:{detail.title || `种子${torrentId}`}
89 </div>
90 <div style={{ fontSize: 16, color: "#555", marginBottom: 8 }}>
91 简介:{detail.description || `这是种子${torrentId}的详细信息。`}
92 </div>
93 </div>
94 <div style={{ display: "flex", gap: 24, marginTop: 32, justifyContent: "center" }}>
95 <button
96 style={{
97 padding: "10px 32px",
98 fontSize: "16px",
99 cursor: "pointer",
100 background: "linear-gradient(90deg, #42a5f5 0%, #1976d2 100%)",
101 color: "#fff",
102 border: "none",
103 borderRadius: "8px",
104 fontWeight: 600,
105 boxShadow: "0 2px 8px #b2d8ea",
106 transition: "background 0.2s",
107 }}
108 onClick={handleClick}
109 >
110 下载
111 </button>
112 <button
113 style={{
114 padding: "10px 32px",
115 fontSize: "16px",
116 cursor: "pointer",
117 background: isFavorite
118 ? "linear-gradient(90deg, #ffb74d 0%, #ff9800 100%)"
119 : "linear-gradient(90deg, #f0f0f0 0%, #bdbdbd 100%)",
120 color: isFavorite ? "#fff" : "#333",
121 border: "none",
122 borderRadius: "8px",
123 fontWeight: 600,
124 boxShadow: isFavorite ? "0 2px 8px #ffe0b2" : "0 2px 8px #e0e7ff",
125 transition: "background 0.2s",
126 }}
127 onClick={handleFavorite}
128 >
129 {isFavorite ? "已收藏" : "收藏"}
130 </button>
131 </div>
9563036699e95ae32025-06-02 21:42:11 +0800132 </div>
133 </div>
134 );
135}