blob: 407306bc446adb30c70b2affe27fca823ec885f1 [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);
wht3da09832025-06-09 02:02:14 +080011 const [isFavorite, setIsFavorite] = React.useState(false);
12 const match = document.cookie.match('(^|;)\\s*userId=([^;]+)');
13 const userId = match ? match[2] : null;
rhjc6a4ee02025-06-06 00:45:18 +080014
wht3da09832025-06-09 02:02:14 +080015 // 下载种子
rhjc6a4ee02025-06-06 00:45:18 +080016 const handleClick = () => {
17 // 构造下载 URL,包含 userId 和 torrentId 参数
18 console.log(torrentId)
223011330f9623f2025-06-06 00:22:05 +080019 const downloadUrl = `${API_BASE_URL}/api/get-torrent?userId=${encodeURIComponent(userId)}&torrentId=${encodeURIComponent(torrentId)}`;
wht10563a82025-06-08 15:52:18 +080020
rhjc6a4ee02025-06-06 00:45:18 +080021 // 发起 GET 请求下载文件
22 fetch(downloadUrl)
23 .then(response => {
24 if (!response.ok) {
25 throw new Error('下载失败');
26 }
27 return response.blob();
28 })
29 .then(blob => {
30 // 创建下载链接并触发下载
31 const url = window.URL.createObjectURL(blob);
32 const a = document.createElement('a');
33 a.href = url;
34 a.download = `torrent-${torrentId}.torrent`;
35 document.body.appendChild(a);
36 a.click();
37 window.URL.revokeObjectURL(url);
38 document.body.removeChild(a);
39 })
40 .catch(error => {
41 console.error('下载错误:', error);
42 alert('下载失败: ' + error.message);
43 });
44 };
45
wht3da09832025-06-09 02:02:14 +080046 // 收藏种子
wht10563a82025-06-08 15:52:18 +080047 const handleFavorite = () => {
wht3da09832025-06-09 02:02:14 +080048 fetch(`${API_BASE_URL}/api/add-favorite`, {
49 method: "POST",
50 headers: {
51 "Content-Type": "application/json"
52 },
53 body: JSON.stringify({
54 userid: userId,
55 seedid: torrentId
56 })
57 })
58 .then(response => {
59 if (!response.ok) {
60 throw new Error('收藏失败');
61 }
62 return response.json();
63 })
64 .then(data => {
65 setIsFavorite(true);
66 alert("收藏成功!");
67 })
68 .catch(error => {
69 console.error('收藏错误:', error);
70 alert('收藏失败: ' + error.message);
71 });
wht10563a82025-06-08 15:52:18 +080072 };
73
rhjc6a4ee02025-06-06 00:45:18 +080074 React.useEffect(() => {
75 setLoading(true);
76 setError(null);
223011330f9623f2025-06-06 00:22:05 +080077 fetch(`${API_BASE_URL}/api/torrent-detail?id=${encodeURIComponent(torrentId)}`)
rhjc6a4ee02025-06-06 00:45:18 +080078 .then(res => {
79 if (!res.ok) throw new Error('网络错误');
80 return res.json();
81 })
82 .then(data => {
83 setDetail(data);
84 setLoading(false);
85 })
86 .catch(err => {
87 setError(err.message);
88 setLoading(false);
89 });
90 }, [torrentId]);
91
92 if (loading) return <div className="container"><h1>加载中...</h1></div>;
93 if (error) return <div className="container"><h1>加载失败: {error}</h1></div>;
94 if (!detail) return <div className="container"><h1>未找到详情</h1></div>;
9563036699e95ae32025-06-02 21:42:11 +080095
96 return (
wht10563a82025-06-08 15:52:18 +080097 <div className="container" style={{ display: "flex", justifyContent: "center", alignItems: "center", minHeight: "100vh" }}>
98 <div
99 style={{
100 background: "#fff",
101 borderRadius: 16,
102 boxShadow: "0 4px 24px #e0e7ff",
103 padding: "36px 48px",
104 maxWidth: 540,
105 width: "100%",
106 marginTop: 48,
107 }}
108 >
109 <h1 style={{ color: "#1976d2", fontWeight: 700, marginBottom: 24, fontSize: 28, letterSpacing: 1 }}>种子详情页</h1>
110 <div style={{ marginBottom: 18 }}>
111 <div style={{ fontSize: 20, fontWeight: 600, marginBottom: 8, color: "#222" }}>
112 标题:{detail.title || `种子${torrentId}`}
113 </div>
114 <div style={{ fontSize: 16, color: "#555", marginBottom: 8 }}>
115 简介:{detail.description || `这是种子${torrentId}的详细信息。`}
116 </div>
117 </div>
118 <div style={{ display: "flex", gap: 24, marginTop: 32, justifyContent: "center" }}>
119 <button
120 style={{
121 padding: "10px 32px",
122 fontSize: "16px",
123 cursor: "pointer",
124 background: "linear-gradient(90deg, #42a5f5 0%, #1976d2 100%)",
125 color: "#fff",
126 border: "none",
127 borderRadius: "8px",
128 fontWeight: 600,
129 boxShadow: "0 2px 8px #b2d8ea",
130 transition: "background 0.2s",
131 }}
132 onClick={handleClick}
133 >
134 下载
135 </button>
136 <button
137 style={{
138 padding: "10px 32px",
139 fontSize: "16px",
140 cursor: "pointer",
141 background: isFavorite
142 ? "linear-gradient(90deg, #ffb74d 0%, #ff9800 100%)"
143 : "linear-gradient(90deg, #f0f0f0 0%, #bdbdbd 100%)",
144 color: isFavorite ? "#fff" : "#333",
145 border: "none",
146 borderRadius: "8px",
147 fontWeight: 600,
148 boxShadow: isFavorite ? "0 2px 8px #ffe0b2" : "0 2px 8px #e0e7ff",
149 transition: "background 0.2s",
150 }}
151 onClick={handleFavorite}
152 >
153 {isFavorite ? "已收藏" : "收藏"}
154 </button>
155 </div>
9563036699e95ae32025-06-02 21:42:11 +0800156 </div>
157 </div>
158 );
159}