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
|
wht | 2bf8f80 | 2025-06-08 15:52:18 +0800 | [diff] [blame^] | 13 | const [isFavorite, setIsFavorite] = React.useState(false); // 收藏状态
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 14 |
|
| 15 | const handleClick = () => {
|
| 16 | // 构造下载 URL,包含 userId 和 torrentId 参数
|
| 17 | console.log(torrentId)
|
22301133 | 0f9623f | 2025-06-06 00:22:05 +0800 | [diff] [blame] | 18 | const downloadUrl = `${API_BASE_URL}/api/get-torrent?userId=${encodeURIComponent(userId)}&torrentId=${encodeURIComponent(torrentId)}`;
|
wht | 2bf8f80 | 2025-06-08 15:52:18 +0800 | [diff] [blame^] | 19 |
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 20 | // 发起 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 |
|
wht | 2bf8f80 | 2025-06-08 15:52:18 +0800 | [diff] [blame^] | 45 | // 收藏按钮示例逻辑(仅前端切换)
|
| 46 | const handleFavorite = () => {
|
| 47 | setIsFavorite(fav => !fav);
|
| 48 | };
|
| 49 |
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 50 | React.useEffect(() => {
|
| 51 | setLoading(true);
|
| 52 | setError(null);
|
22301133 | 0f9623f | 2025-06-06 00:22:05 +0800 | [diff] [blame] | 53 | fetch(`${API_BASE_URL}/api/torrent-detail?id=${encodeURIComponent(torrentId)}`)
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 54 | .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>;
|
956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 71 |
|
| 72 | return (
|
wht | 2bf8f80 | 2025-06-08 15:52:18 +0800 | [diff] [blame^] | 73 | <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>
|
956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 132 | </div>
|
| 133 | </div>
|
| 134 | );
|
| 135 | }
|