blob: 0a3ba4dd83d8745994bbab305ad57c586b3bf924 [file] [log] [blame]
223010095b28c672025-04-10 20:12:45 +08001import React, { useEffect, useState } from 'react';
2import axios from 'axios';
3import { useParams } from 'react-router-dom';
4import Header from '../../../components/Header';
5import './SeedDetail.css';
6
Krishya2283d882025-05-27 22:25:19 +08007
223010095b28c672025-04-10 20:12:45 +08008
9const SeedDetail = () => {
Krishyac0f7e9b2025-04-22 15:28:28 +080010 const { seed_id } = useParams();
11 const [seed, setSeed] = useState(null);
12 const [error, setError] = useState(null);
13 const [comments, setComments] = useState([]);
14 const [newComment, setNewComment] = useState('');
223010095b28c672025-04-10 20:12:45 +080015
Krishyac0f7e9b2025-04-22 15:28:28 +080016 useEffect(() => {
17 axios
Krishya2283d882025-05-27 22:25:19 +080018 .get(`/echo/seeds/${seed_id}`)
Krishyac0f7e9b2025-04-22 15:28:28 +080019 .then((res) => {
20 if (res.data.status === 'success') {
21 setSeed(res.data.seed);
22 } else {
23 setError('未能获取种子信息');
24 }
25 })
26 .catch(() => {
27 setError('获取种子详情失败');
28 });
29
30 // 模拟获取评论数据,实际需要调用 API
Krishya2283d882025-05-27 22:25:19 +080031 axios.get(`/echo/seeds/${seed_id}/comments`)
Krishyac0f7e9b2025-04-22 15:28:28 +080032 .then((res) => {
33 if (res.data.status === 'success') {
34 setComments(res.data.comments);
35 }
36 })
37 .catch(() => {
38 console.log('获取评论失败');
39 });
40 }, [seed_id]);
41
42 const handleDownload = async () => {
43 if (seed) {
44 const peer_id = 'echo-' + Math.random().toString(36).substring(2, 10);
45 const ip = '127.0.0.1';
46 const port = 6881;
47 const uploaded = 0;
48 const downloaded = 0;
49 const left = 0;
50
51 try {
Krishya2283d882025-05-27 22:25:19 +080052 const response = await axios.get(`/echo/seeds/${seed.seed_id}/download`, {
Krishyac0f7e9b2025-04-22 15:28:28 +080053 params: { peer_id, ip, port, uploaded, downloaded, left },
54 responseType: 'blob'
55 });
56
57 const blob = new Blob([response.data], { type: 'application/x-bittorrent' });
58 const downloadUrl = URL.createObjectURL(blob);
59 const a = document.createElement('a');
60 a.href = downloadUrl;
61 a.download = `${seed.seed_id}.torrent`;
62 a.click();
63 URL.revokeObjectURL(downloadUrl);
64 } catch (error) {
65 console.error('下载失败:', error);
66 alert('下载失败,请稍后再试。');
67 }
223010095b28c672025-04-10 20:12:45 +080068 }
Krishyac0f7e9b2025-04-22 15:28:28 +080069 };
223010095b28c672025-04-10 20:12:45 +080070
Krishyac0f7e9b2025-04-22 15:28:28 +080071 const handleCollect = () => {
72 // 这里可以添加收藏逻辑,调用相应 API
73 alert('已收藏');
74 };
75
76 const handleAddComment = () => {
77 if (newComment.trim()) {
78 // 这里可以添加评论逻辑,调用相应 API
79 setComments([...comments, { content: newComment, user: '用户' }]);
80 setNewComment('');
81 }
82 };
83
84 if (error) {
85 return (
86 <div className="seed-detail-page">
87 <Header />
88 <div className="seed-detail">
89 <p className="error-text">{error}</p>
90 </div>
91 </div>
92 );
93 }
94
95 if (!seed) {
96 return (
97 <div className="seed-detail-page">
98 <Header />
99 <div className="seed-detail">
100 <p>加载中...</p>
101 </div>
102 </div>
103 );
104 }
105
223010095b28c672025-04-10 20:12:45 +0800106 return (
Krishyac0f7e9b2025-04-22 15:28:28 +0800107 <div className="seed-detail-page">
108 <Header />
109 <div className="seed-detail">
110 <h1>{seed.title}</h1>
111 <div className="seed-header-container">
112 <div className="seed-info">
113 <div className="seed-basic-info">
114 <p><strong>分类:</strong>{seed.category}</p>
115 <p><strong>发布时间:</strong>{new Date(seed.upload_time).toLocaleString()}</p>
116 <p><strong>标签:</strong>{seed.tags.join(' / ')}</p>
117 <p><strong>简介:</strong>{seed.description}</p>
118 <p><strong>大小:</strong>{seed.size} GB</p>
119 <p><strong>分辨率:</strong>{seed.resolution}</p>
120 <p><strong>片长:</strong>{seed.duration}</p>
121 <p><strong>地区:</strong>{seed.region}</p>
122 <p><strong>下载次数:</strong>{seed.downloads}</p>
123 </div>
124 {(seed.category === '电影' || seed.category === '电视剧') && (
125 <div className="seed-media-info">
126 <p><strong>导演:</strong>{seed.director}</p>
127 <p><strong>编剧:</strong>{seed.writer}</p>
128 <p><strong>主演:</strong>{seed.actors.join(' / ')}</p>
129 </div>
130 )}
131 </div>
132 <img src={seed.cover_url} alt={seed.title} className="cover-image" />
133 </div>
134 <div className="action-buttons">
135 <button className="btn" onClick={handleDownload}>下载</button>
136 <button className="btn" onClick={handleCollect}>收藏</button>
137 </div>
138 <hr className="divider" />
139 {/* 评论部分 */}
140 <h3>评论区</h3>
141 <div className="comments-section">
142 <div className="comments-list">
143 {comments.map((comment, index) => (
144 <div key={index} className="comment">
145 <p className="comment-user">{comment.user}</p>
146 <p className="comment-content">{comment.content}</p>
147 </div>
148 ))}
149 </div>
150 <div className="add-comment-form">
151 <textarea
152 placeholder="输入你的评论..."
153 value={newComment}
154 onChange={(e) => setNewComment(e.target.value)}
155 />
156 <div className="comment-options">
157 <button className="btn" onClick={handleAddComment}>发布评论</button>
158 </div>
159 </div>
160 </div>
161 </div>
223010095b28c672025-04-10 20:12:45 +0800162 </div>
223010095b28c672025-04-10 20:12:45 +0800163 );
223010095b28c672025-04-10 20:12:45 +0800164};
165
166export default SeedDetail;
Krishyac0f7e9b2025-04-22 15:28:28 +0800167