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);
|
wht | 3da0983 | 2025-06-09 02:02:14 +0800 | [diff] [blame^] | 11 | const [isFavorite, setIsFavorite] = React.useState(false);
|
| 12 | const match = document.cookie.match('(^|;)\\s*userId=([^;]+)');
|
| 13 | const userId = match ? match[2] : null;
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 14 |
|
wht | 3da0983 | 2025-06-09 02:02:14 +0800 | [diff] [blame^] | 15 | // 下载种子
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 16 | const handleClick = () => {
|
| 17 | // 构造下载 URL,包含 userId 和 torrentId 参数
|
| 18 | console.log(torrentId)
|
22301133 | 0f9623f | 2025-06-06 00:22:05 +0800 | [diff] [blame] | 19 | const downloadUrl = `${API_BASE_URL}/api/get-torrent?userId=${encodeURIComponent(userId)}&torrentId=${encodeURIComponent(torrentId)}`;
|
wht | 10563a8 | 2025-06-08 15:52:18 +0800 | [diff] [blame] | 20 |
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 21 | // 发起 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 |
|
wht | 3da0983 | 2025-06-09 02:02:14 +0800 | [diff] [blame^] | 46 | // 收藏种子
|
wht | 10563a8 | 2025-06-08 15:52:18 +0800 | [diff] [blame] | 47 | const handleFavorite = () => {
|
wht | 3da0983 | 2025-06-09 02:02:14 +0800 | [diff] [blame^] | 48 | 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 | });
|
wht | 10563a8 | 2025-06-08 15:52:18 +0800 | [diff] [blame] | 72 | };
|
| 73 |
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 74 | React.useEffect(() => {
|
| 75 | setLoading(true);
|
| 76 | setError(null);
|
22301133 | 0f9623f | 2025-06-06 00:22:05 +0800 | [diff] [blame] | 77 | fetch(`${API_BASE_URL}/api/torrent-detail?id=${encodeURIComponent(torrentId)}`)
|
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 78 | .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>;
|
956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 95 |
|
| 96 | return (
|
wht | 10563a8 | 2025-06-08 15:52:18 +0800 | [diff] [blame] | 97 | <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>
|
956303669 | 9e95ae3 | 2025-06-02 21:42:11 +0800 | [diff] [blame] | 156 | </div>
|
| 157 | </div>
|
| 158 | );
|
| 159 | }
|