调试种子列表、下载种子
Change-Id: Iaf8fc868ae0f26af06e7e1e0ef4af436cd4f959d
diff --git a/src/pages/SeedList/SeedDetail/SeedDetail.jsx b/src/pages/SeedList/SeedDetail/SeedDetail.jsx
index 0a3ba4d..10cf65d 100644
--- a/src/pages/SeedList/SeedDetail/SeedDetail.jsx
+++ b/src/pages/SeedList/SeedDetail/SeedDetail.jsx
@@ -1,167 +1,219 @@
import React, { useEffect, useState } from 'react';
import axios from 'axios';
-import { useParams } from 'react-router-dom';
+import { useParams } from 'wouter';
import Header from '../../../components/Header';
import './SeedDetail.css';
-
+import { useUser } from '../../../context/UserContext';
const SeedDetail = () => {
- const { seed_id } = useParams();
- const [seed, setSeed] = useState(null);
- const [error, setError] = useState(null);
- const [comments, setComments] = useState([]);
- const [newComment, setNewComment] = useState('');
+ const params = useParams();
+ const seed_id = params.id;
- useEffect(() => {
- axios
- .get(`/echo/seeds/${seed_id}`)
- .then((res) => {
- if (res.data.status === 'success') {
- setSeed(res.data.seed);
- } else {
- setError('未能获取种子信息');
- }
- })
- .catch(() => {
- setError('获取种子详情失败');
+ const [seed, setSeed] = useState(null);
+ const [coverImage, setCoverImage] = useState(null);
+ const [error, setError] = useState(null);
+ const [comments, setComments] = useState([]);
+ const [newComment, setNewComment] = useState('');
+
+ const { user } = useUser();
+
+
+ // 格式化图片 URL
+ const formatImageUrl = (url) => {
+ if (!url) return '';
+ const filename = url.split('/').pop();
+ return `http://localhost:8080/uploads/torrents/${filename}`;
+ };
+
+ useEffect(() => {
+ if (!seed_id) {
+ setError('无效的种子ID');
+ return;
+ }
+
+ const fetchSeedDetail = async () => {
+ try {
+ const res = await axios.post(`/seeds/info/${seed_id}`);
+ if (res.data.code === 0) {
+ const seedData = res.data.data;
+
+ // 处理封面图
+ let cover = seedData.imageUrl;
+ if (!cover && seedData.imgUrl) {
+ const imgs = seedData.imgUrl
+ .split(',')
+ .map((i) => i.trim())
+ .filter(Boolean);
+ cover = imgs.length > 0 ? formatImageUrl(imgs[0]) : null;
+ }
+ setCoverImage(cover);
+ setSeed(seedData);
+ setError(null);
+ } else {
+ setError('未能获取种子信息');
+ }
+ } catch (err) {
+ console.error('请求种子详情出错:', err);
+ setError('获取种子详情失败');
+ }
+ };
+
+ const fetchComments = async () => {
+ try {
+ const res = await axios.get(`/seeds/${seed_id}/comments`);
+ if (res.data.code === 0) {
+ setComments(res.data.data || []);
+ } else {
+ setComments([]);
+ }
+ } catch {
+ setComments([]);
+ }
+ };
+
+ fetchSeedDetail();
+ fetchComments();
+ }, [seed_id]);
+
+ const handleDownload = async (seedId) => {
+ if (!user || !user.userId) {
+ alert('请先登录再下载种子文件');
+ return;
+ }
+
+ try {
+ const response = await axios.get(`/seeds/${seedId}/download`, {
+ params: {
+ passkey: user.userId,
+ },
+ responseType: 'blob'
});
- // 模拟获取评论数据,实际需要调用 API
- axios.get(`/echo/seeds/${seed_id}/comments`)
- .then((res) => {
- if (res.data.status === 'success') {
- setComments(res.data.comments);
- }
- })
- .catch(() => {
- console.log('获取评论失败');
- });
- }, [seed_id]);
-
- const handleDownload = async () => {
- if (seed) {
- const peer_id = 'echo-' + Math.random().toString(36).substring(2, 10);
- const ip = '127.0.0.1';
- const port = 6881;
- const uploaded = 0;
- const downloaded = 0;
- const left = 0;
-
- try {
- const response = await axios.get(`/echo/seeds/${seed.seed_id}/download`, {
- params: { peer_id, ip, port, uploaded, downloaded, left },
- responseType: 'blob'
- });
-
- const blob = new Blob([response.data], { type: 'application/x-bittorrent' });
- const downloadUrl = URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.href = downloadUrl;
- a.download = `${seed.seed_id}.torrent`;
- a.click();
- URL.revokeObjectURL(downloadUrl);
- } catch (error) {
- console.error('下载失败:', error);
- alert('下载失败,请稍后再试。');
- }
+ const blob = new Blob([response.data], { type: 'application/x-bittorrent' });
+ const downloadUrl = URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = downloadUrl;
+ a.download = `${seedId}.torrent`;
+ a.click();
+ URL.revokeObjectURL(downloadUrl);
+ } catch (error) {
+ console.error('下载失败:', error);
+ alert('下载失败,请稍后再试。');
}
};
- const handleCollect = () => {
- // 这里可以添加收藏逻辑,调用相应 API
- alert('已收藏');
- };
- const handleAddComment = () => {
- if (newComment.trim()) {
- // 这里可以添加评论逻辑,调用相应 API
- setComments([...comments, { content: newComment, user: '用户' }]);
- setNewComment('');
- }
- };
+ const handleCollect = () => {
+ alert('已收藏');
+ };
- if (error) {
- return (
- <div className="seed-detail-page">
- <Header />
- <div className="seed-detail">
- <p className="error-text">{error}</p>
- </div>
- </div>
- );
+ const handleAddComment = () => {
+ if (newComment.trim()) {
+ setComments([...comments, { content: newComment, user: '用户' }]);
+ setNewComment('');
}
+ };
- if (!seed) {
- return (
- <div className="seed-detail-page">
- <Header />
- <div className="seed-detail">
- <p>加载中...</p>
- </div>
- </div>
- );
- }
-
+ if (error) {
return (
- <div className="seed-detail-page">
- <Header />
- <div className="seed-detail">
- <h1>{seed.title}</h1>
- <div className="seed-header-container">
- <div className="seed-info">
- <div className="seed-basic-info">
- <p><strong>分类:</strong>{seed.category}</p>
- <p><strong>发布时间:</strong>{new Date(seed.upload_time).toLocaleString()}</p>
- <p><strong>标签:</strong>{seed.tags.join(' / ')}</p>
- <p><strong>简介:</strong>{seed.description}</p>
- <p><strong>大小:</strong>{seed.size} GB</p>
- <p><strong>分辨率:</strong>{seed.resolution}</p>
- <p><strong>片长:</strong>{seed.duration}</p>
- <p><strong>地区:</strong>{seed.region}</p>
- <p><strong>下载次数:</strong>{seed.downloads}</p>
- </div>
- {(seed.category === '电影' || seed.category === '电视剧') && (
- <div className="seed-media-info">
- <p><strong>导演:</strong>{seed.director}</p>
- <p><strong>编剧:</strong>{seed.writer}</p>
- <p><strong>主演:</strong>{seed.actors.join(' / ')}</p>
- </div>
- )}
- </div>
- <img src={seed.cover_url} alt={seed.title} className="cover-image" />
- </div>
- <div className="action-buttons">
- <button className="btn" onClick={handleDownload}>下载</button>
- <button className="btn" onClick={handleCollect}>收藏</button>
- </div>
- <hr className="divider" />
- {/* 评论部分 */}
- <h3>评论区</h3>
- <div className="comments-section">
- <div className="comments-list">
- {comments.map((comment, index) => (
- <div key={index} className="comment">
- <p className="comment-user">{comment.user}</p>
- <p className="comment-content">{comment.content}</p>
- </div>
- ))}
- </div>
- <div className="add-comment-form">
- <textarea
- placeholder="输入你的评论..."
- value={newComment}
- onChange={(e) => setNewComment(e.target.value)}
- />
- <div className="comment-options">
- <button className="btn" onClick={handleAddComment}>发布评论</button>
- </div>
- </div>
- </div>
- </div>
+ <div className="seed-detail-page">
+ <Header />
+ <div className="seed-detail">
+ <p className="error-text">{error}</p>
</div>
+ </div>
);
+ }
+
+ if (!seed) {
+ return (
+ <div className="seed-detail-page">
+ <Header />
+ <div className="seed-detail">
+ <p>加载中...</p>
+ </div>
+ </div>
+ );
+ }
+
+ const tags = seed.tags
+ ? Array.isArray(seed.tags)
+ ? seed.tags
+ : typeof seed.tags === 'string'
+ ? seed.tags.split(',').map((t) => t.trim())
+ : []
+ : [];
+
+ const actors = seed.actors
+ ? Array.isArray(seed.actors)
+ ? seed.actors
+ : typeof seed.actors === 'string'
+ ? seed.actors.split(',').map((a) => a.trim())
+ : []
+ : [];
+
+ return (
+ <div className="seed-detail-page">
+ <Header />
+ <div className="seed-detail">
+ <h1>{seed.title}</h1>
+ <div className="seed-header-container">
+ <div className="seed-info">
+ <div className="seed-basic-info">
+ <p><strong>分类:</strong>{seed.category || '未知'}</p>
+ <p><strong>发布时间:</strong>{seed.upload_time ? new Date(seed.upload_time).toLocaleString() : '未知'}</p>
+ <p><strong>标签:</strong>{tags.join(' / ')}</p>
+ <p><strong>简介:</strong>{seed.description || '无'}</p>
+ <p><strong>大小:</strong>{seed.size || '未知'}</p>
+ <p><strong>分辨率:</strong>{seed.resolution || '未知'}</p>
+ <p><strong>片长:</strong>{seed.duration || '未知'}</p>
+ <p><strong>地区:</strong>{seed.region || '未知'}</p>
+ <p><strong>下载次数:</strong>{seed.downloads ?? 0}</p>
+ </div>
+ {(seed.category === '电影' || seed.category === '电视剧') && (
+ <div className="seed-media-info">
+ <p><strong>导演:</strong>{seed.director || '未知'}</p>
+ <p><strong>编剧:</strong>{seed.writer || '未知'}</p>
+ <p><strong>主演:</strong>{actors.join(' / ')}</p>
+ </div>
+ )}
+ </div>
+ <img
+ src={coverImage || '/default-cover.png'}
+ alt={seed.title}
+ className="cover-image"
+ />
+ </div>
+ <div className="action-buttons">
+ <button className="btn" onClick={() => handleDownload(seed.id)}>下载</button>
+ <button className="btn" onClick={handleCollect}>收藏</button>
+ </div>
+ <hr className="divider" />
+ <h3>评论区</h3>
+ <div className="comments-section">
+ <div className="comments-list">
+ {comments.map((comment, index) => (
+ <div key={index} className="comment">
+ <p className="comment-user">{comment.user}</p>
+ <p className="comment-content">{comment.content}</p>
+ </div>
+ ))}
+ </div>
+ <div className="add-comment-form">
+ <textarea
+ placeholder="输入你的评论..."
+ value={newComment}
+ onChange={(e) => setNewComment(e.target.value)}
+ />
+ <div className="comment-options">
+ <button className="btn" onClick={handleAddComment}>发布评论</button>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
};
export default SeedDetail;
-
\ No newline at end of file