blob: adb2bb3ee5ab4d48922b08fbc350bf0fe98cfce8 [file] [log] [blame]
22301009ecc1c1c2025-04-09 21:56:23 +08001import React, { useState, useEffect } from 'react';
22301009ecc1c1c2025-04-09 21:56:23 +08002import { Link } from 'wouter';
22301009afbcf4b2025-04-10 16:08:39 +08003import axios from 'axios';
22301009afbcf4b2025-04-10 16:08:39 +08004import Recommend from './Recommend/Recommend';
22301009f9641c52025-04-15 21:14:56 +08005import Header from '../../components/Header'; // 引入 Header 组件
22301009ecc1c1c2025-04-09 21:56:23 +08006import './SeedList.css';
2230100980aaf0d2025-06-05 23:20:05 +08007import { useUser } from '../../context/UserContext';
22301009afbcf4b2025-04-10 16:08:39 +08008
22301009ecc1c1c2025-04-09 21:56:23 +08009const SeedList = () => {
10 const [seeds, setSeeds] = useState([]);
22301009ecc1c1c2025-04-09 21:56:23 +080011 const [loading, setLoading] = useState(true);
12 const [searchTerm, setSearchTerm] = useState('');
22301009ecc1c1c2025-04-09 21:56:23 +080013 const [sortOption, setSortOption] = useState('最新');
22301009afbcf4b2025-04-10 16:08:39 +080014 const [activeTab, setActiveTab] = useState('种子列表');
15 const [filters, setFilters] = useState({});
16 const [selectedFilters, setSelectedFilters] = useState({});
2230100980aaf0d2025-06-05 23:20:05 +080017 const [tagMode, setTagMode] = useState('any'); // 与接口对应,any / all
22301009afbcf4b2025-04-10 16:08:39 +080018 const [errorMsg, setErrorMsg] = useState('');
2230100980aaf0d2025-06-05 23:20:05 +080019 const { user } = useUser();
20
22301009ecc1c1c2025-04-09 21:56:23 +080021
22301009afbcf4b2025-04-10 16:08:39 +080022 const TAGS = ['猜你喜欢', '电影', '电视剧', '动漫', '音乐', '游戏', '综艺', '软件', '体育', '学习', '纪录片', '其他'];
22301009ecc1c1c2025-04-09 21:56:23 +080023
22301009afbcf4b2025-04-10 16:08:39 +080024 const CATEGORY_MAP = {
2230100980aaf0d2025-06-05 23:20:05 +080025 '电影': '电影',
26 '电视剧': '电视剧',
27 '动漫': '动漫',
28 '音乐': '音乐',
29 '游戏': '游戏',
30 '综艺': '综艺',
31 '软件': '软件',
32 '体育': '体育',
33 '学习': '学习',
34 '纪录片': '纪录片',
35 '其他': '其他',
36 '猜你喜欢': '',
37 '种子列表': '',
22301009afbcf4b2025-04-10 16:08:39 +080038 };
39
40 const buildQueryParams = () => {
2230100980aaf0d2025-06-05 23:20:05 +080041 const category = CATEGORY_MAP[activeTab] || '';
42 const orderKey = sortOption === '最新' ? ['upload_time'] : (sortOption === '最热' ? ['downloads'] : ['upload_time']);
22301009afbcf4b2025-04-10 16:08:39 +080043 const params = {
22301009afbcf4b2025-04-10 16:08:39 +080044 page: 1,
2230100980aaf0d2025-06-05 23:20:05 +080045 size: 20,
46 orderKey,
47 orderDesc: true,
22301009ecc1c1c2025-04-09 21:56:23 +080048 };
49
2230100980aaf0d2025-06-05 23:20:05 +080050 if (searchTerm.trim()) {
51 params.title = searchTerm.trim();
52 }
53 if (category) {
54 params.category = category;
55 }
22301009afbcf4b2025-04-10 16:08:39 +080056
57 const tags = Object.entries(selectedFilters)
2230100980aaf0d2025-06-05 23:20:05 +080058 .filter(([_, value]) => value !== '不限')
59 .map(([_, value]) => value);
22301009afbcf4b2025-04-10 16:08:39 +080060
61 if (tags.length > 0) {
2230100980aaf0d2025-06-05 23:20:05 +080062 params.tags = tags;
63 params.tagMode = tagMode; // any 或 all
22301009afbcf4b2025-04-10 16:08:39 +080064 }
65
66 return params;
67 };
68
69 const fetchSeeds = async () => {
2230100980aaf0d2025-06-05 23:20:05 +080070 if (activeTab === '猜你喜欢') return;
22301009afbcf4b2025-04-10 16:08:39 +080071 setLoading(true);
72 setErrorMsg('');
73 try {
74 const params = buildQueryParams();
223010094158f3a2025-06-06 19:59:10 +080075 const response = await axios.get('/seeds/list', params);
76 // const response = await axios.get('/seeds/list', { params });
Krishya6bf199c2025-06-06 21:14:23 +080077
2230100980aaf0d2025-06-05 23:20:05 +080078 const data = response.data;
22301009afbcf4b2025-04-10 16:08:39 +080079
2230100980aaf0d2025-06-05 23:20:05 +080080 if (data.code !== 0) {
81 throw new Error(data.msg || '获取失败');
82 }
83
84 setSeeds(data.data || []);
22301009afbcf4b2025-04-10 16:08:39 +080085 } catch (error) {
86 console.error('获取种子列表失败:', error);
87 setErrorMsg(error.message || '获取失败,请稍后再试。');
88 setSeeds([]);
22301009afbcf4b2025-04-10 16:08:39 +080089 } finally {
90 setLoading(false);
91 }
92 };
93
94 const fetchFilterOptions = async () => {
2230100980aaf0d2025-06-05 23:20:05 +080095 if (activeTab === '猜你喜欢' || !CATEGORY_MAP[activeTab]) return;
22301009afbcf4b2025-04-10 16:08:39 +080096 const category = CATEGORY_MAP[activeTab];
97 try {
2230100980aaf0d2025-06-05 23:20:05 +080098 const res = await axios.get(`/seed-filters?category=${category}`);
223010095b28c672025-04-10 20:12:45 +080099 const filterData = res.data || {};
100 setFilters(filterData);
101
22301009afbcf4b2025-04-10 16:08:39 +0800102 const defaultSelections = {};
223010095b28c672025-04-10 20:12:45 +0800103 for (const key in filterData) {
22301009afbcf4b2025-04-10 16:08:39 +0800104 defaultSelections[key] = '不限';
105 }
106 setSelectedFilters(defaultSelections);
107 } catch (err) {
108 console.error('获取筛选项失败:', err);
109 setFilters({});
110 setSelectedFilters({});
111 }
112 };
22301009ecc1c1c2025-04-09 21:56:23 +0800113
114 useEffect(() => {
2230100980aaf0d2025-06-05 23:20:05 +0800115 fetchFilterOptions();
22301009afbcf4b2025-04-10 16:08:39 +0800116 }, [activeTab]);
22301009ecc1c1c2025-04-09 21:56:23 +0800117
22301009afbcf4b2025-04-10 16:08:39 +0800118 useEffect(() => {
2230100980aaf0d2025-06-05 23:20:05 +0800119 fetchSeeds();
22301009afbcf4b2025-04-10 16:08:39 +0800120 }, [activeTab, sortOption, selectedFilters, tagMode, searchTerm]);
22301009ecc1c1c2025-04-09 21:56:23 +0800121
2230100980aaf0d2025-06-05 23:20:05 +0800122 // ✅ 修改后的下载函数
22301009afbcf4b2025-04-10 16:08:39 +0800123 const handleDownload = async (seedId) => {
2230100980aaf0d2025-06-05 23:20:05 +0800124 if (!user || !user.userId) {
125 alert('请先登录再下载种子文件');
126 return;
127 }
22301009afbcf4b2025-04-10 16:08:39 +0800128
129 try {
2230100980aaf0d2025-06-05 23:20:05 +0800130 const response = await axios.get(`/seeds/${seedId}/download`, {
131 params: {
132 passkey: user.userId,
133 },
22301009afbcf4b2025-04-10 16:08:39 +0800134 responseType: 'blob'
135 });
136
137 const blob = new Blob([response.data], { type: 'application/x-bittorrent' });
138 const downloadUrl = URL.createObjectURL(blob);
139 const a = document.createElement('a');
140 a.href = downloadUrl;
141 a.download = `${seedId}.torrent`;
142 a.click();
143 URL.revokeObjectURL(downloadUrl);
144 } catch (error) {
145 console.error('下载失败:', error);
146 alert('下载失败,请稍后再试。');
22301009ecc1c1c2025-04-09 21:56:23 +0800147 }
22301009afbcf4b2025-04-10 16:08:39 +0800148 };
22301009ecc1c1c2025-04-09 21:56:23 +0800149
223010095b28c672025-04-10 20:12:45 +0800150 const handleFilterChange = (key, value) => {
2230100980aaf0d2025-06-05 23:20:05 +0800151 setSelectedFilters(prev => ({
152 ...prev,
223010095b28c672025-04-10 20:12:45 +0800153 [key]: value
154 }));
155 };
156
157 const clearFilter = (key) => {
2230100980aaf0d2025-06-05 23:20:05 +0800158 setSelectedFilters(prev => ({
159 ...prev,
223010095b28c672025-04-10 20:12:45 +0800160 [key]: '不限'
161 }));
162 };
163
22301009ecc1c1c2025-04-09 21:56:23 +0800164 return (
Krishyaf1d0ea82025-05-03 17:01:58 +0800165 <div className="seed-list-container">
2230100980aaf0d2025-06-05 23:20:05 +0800166 <Header />
22301009ecc1c1c2025-04-09 21:56:23 +0800167
22301009ecc1c1c2025-04-09 21:56:23 +0800168 <div className="controls">
169 <input
170 type="text"
171 placeholder="搜索种子..."
172 value={searchTerm}
173 onChange={(e) => setSearchTerm(e.target.value)}
174 className="search-input"
175 />
2230100980aaf0d2025-06-05 23:20:05 +0800176 <select
177 value={sortOption}
178 onChange={(e) => setSortOption(e.target.value)}
179 className="sort-select"
180 >
22301009ecc1c1c2025-04-09 21:56:23 +0800181 <option value="最新">最新</option>
182 <option value="最热">最热</option>
22301009afbcf4b2025-04-10 16:08:39 +0800183 </select>
2230100980aaf0d2025-06-05 23:20:05 +0800184 <select
185 value={tagMode}
186 onChange={(e) => setTagMode(e.target.value)}
187 className="tag-mode-select"
188 >
22301009afbcf4b2025-04-10 16:08:39 +0800189 <option value="any">包含任意标签</option>
190 <option value="all">包含所有标签</option>
22301009ecc1c1c2025-04-09 21:56:23 +0800191 </select>
192 </div>
193
194 <div className="tag-filters">
2230100980aaf0d2025-06-05 23:20:05 +0800195 {TAGS.map(tag => (
22301009ecc1c1c2025-04-09 21:56:23 +0800196 <button
197 key={tag}
2230100980aaf0d2025-06-05 23:20:05 +0800198 className={`tag-button ${activeTab === tag ? 'active-tag' : ''}`}
22301009ecc1c1c2025-04-09 21:56:23 +0800199 onClick={() => {
22301009afbcf4b2025-04-10 16:08:39 +0800200 setActiveTab(tag);
201 setFilters({});
202 setSelectedFilters({});
22301009ecc1c1c2025-04-09 21:56:23 +0800203 }}
204 >
205 {tag}
206 </button>
207 ))}
208 </div>
209
2230100980aaf0d2025-06-05 23:20:05 +0800210 {activeTab !== '猜你喜欢' && Object.keys(filters).length > 0 && (
22301009afbcf4b2025-04-10 16:08:39 +0800211 <div className="filter-bar">
212 {Object.entries(filters).map(([key, options]) => (
213 <div className="filter-group" key={key}>
214 <label>{key}:</label>
215 <select
216 value={selectedFilters[key]}
223010095b28c672025-04-10 20:12:45 +0800217 onChange={(e) => handleFilterChange(key, e.target.value)}
22301009afbcf4b2025-04-10 16:08:39 +0800218 >
2230100980aaf0d2025-06-05 23:20:05 +0800219 {options.map(opt => (
22301009afbcf4b2025-04-10 16:08:39 +0800220 <option key={opt} value={opt}>{opt}</option>
221 ))}
222 </select>
2230100980aaf0d2025-06-05 23:20:05 +0800223 {selectedFilters[key] !== '不限' && (
224 <button
225 className="clear-filter-btn"
226 onClick={() => clearFilter(key)}
227 >
228
229 </button>
223010095b28c672025-04-10 20:12:45 +0800230 )}
22301009afbcf4b2025-04-10 16:08:39 +0800231 </div>
232 ))}
233 </div>
234 )}
235
22301009ecc1c1c2025-04-09 21:56:23 +0800236 <div className="seed-list-content">
2230100980aaf0d2025-06-05 23:20:05 +0800237 {activeTab === '猜你喜欢' ? (
22301009afbcf4b2025-04-10 16:08:39 +0800238 <Recommend />
2230100980aaf0d2025-06-05 23:20:05 +0800239 ) : loading ? (
22301009ecc1c1c2025-04-09 21:56:23 +0800240 <p>加载中...</p>
2230100980aaf0d2025-06-05 23:20:05 +0800241 ) : errorMsg ? (
22301009afbcf4b2025-04-10 16:08:39 +0800242 <p className="error-text">{errorMsg}</p>
2230100980aaf0d2025-06-05 23:20:05 +0800243 ) : seeds.length === 0 ? (
22301009ecc1c1c2025-04-09 21:56:23 +0800244 <p>未找到符合条件的种子。</p>
245 ) : (
Krishya25590de2025-04-21 19:03:49 +0800246 <div className="seed-list-card">
247 <div className="seed-list-header">
248 <div className="seed-header-cover"></div>
249 <div className="seed-header-title">种子名称</div>
250 <div className="seed-header-size">大小</div>
251 <div className="seed-header-upload-time">上传时间</div>
252 <div className="seed-header-downloads">下载次数</div>
253 <div className="seed-header-actions">操作</div>
254 </div>
255 <div className="seed-list-body">
2230100980aaf0d2025-06-05 23:20:05 +0800256 {seeds.map((seed, index) => {
257 // 处理 tags 字段,兼容字符串和数组
258 let tagsArray = [];
259 if (seed.tags) {
260 if (Array.isArray(seed.tags)) {
261 tagsArray = seed.tags;
262 } else if (typeof seed.tags === 'string') {
263 try {
264 tagsArray = JSON.parse(seed.tags);
265 if (!Array.isArray(tagsArray)) {
266 // 解析后不是数组,按逗号分割
267 tagsArray = seed.tags.split(',').map(t => t.trim()).filter(t => t);
268 }
269 } catch {
270 // 解析失败,按逗号分割
271 tagsArray = seed.tags.split(',').map(t => t.trim()).filter(t => t);
272 }
273 }
274 }
275
276 return (
277
278 <Link to={`/seed/${seed.id}`} key={index} className="seed-item-link">
279 <div className="seed-item">
223010094158f3a2025-06-06 19:59:10 +0800280 {seed.imageUrl && (
2230100980aaf0d2025-06-05 23:20:05 +0800281 <img
223010094158f3a2025-06-06 19:59:10 +0800282 src={seed.imageUrl}
2230100980aaf0d2025-06-05 23:20:05 +0800283 alt={seed.title}
284 className="seed-item-cover"
285 />
286 )}
223010094158f3a2025-06-06 19:59:10 +0800287
2230100980aaf0d2025-06-05 23:20:05 +0800288 <div className="seed-item-title">
289 <div className="seed-title-row">
290 <h3 className="seed-title">{seed.title}</h3>
291 <div className="seed-tags">
292 {tagsArray.map((tag, i) => (
293 <span key={i} className="tag-label">{tag}</span>
294 ))}
295 </div>
Krishya25590de2025-04-21 19:03:49 +0800296 </div>
297 </div>
2230100980aaf0d2025-06-05 23:20:05 +0800298 <div className="seed-item-size">{seed.size || '未知'}</div>
299 <div className="seed-item-upload-time">{seed.upload_time?.split('T')[0] || '未知'}</div>
300 <div className="seed-item-downloads">{seed.downloads ?? 0} 次下载</div>
301 <div
302 className="seed-item-actions"
303 onClick={e => e.stopPropagation()} // 阻止事件冒泡,避免跳转
Krishya25590de2025-04-21 19:03:49 +0800304 >
2230100980aaf0d2025-06-05 23:20:05 +0800305 <button
306 className="btn-primary"
307 onClick={e => {
308 e.preventDefault();
309 e.stopPropagation();
310 handleDownload(seed.id);
311 }}
312 >
313 下载
314 </button>
315 <button
316 className="btn-outline"
317 onClick={async (e) => {
318 e.preventDefault();
319 e.stopPropagation();
320
321 if (!user || !user.userId) {
322 alert('请先登录再收藏');
323 return;
324 }
325
326 try {
327 const res = await axios.post(`/seeds/${seed.id}/favorite-toggle`, null, {
328 params: { user_id: user.userId },
329 });
330
331 if (res.data.code === 0) {
332 alert('操作成功'); // 你可以改成 toast 或 icon 状态提示
333 } else {
334 alert(res.data.msg || '操作失败');
335 }
336 } catch (err) {
337 console.error('收藏失败:', err);
338 alert('收藏失败,请稍后再试。');
339 }
340 }}
341>
342 收藏
343</button>
344
345 </div>
Krishya25590de2025-04-21 19:03:49 +0800346 </div>
2230100980aaf0d2025-06-05 23:20:05 +0800347 </Link>
348 );
349 })}
Krishya25590de2025-04-21 19:03:49 +0800350 </div>
22301009ecc1c1c2025-04-09 21:56:23 +0800351 </div>
352 )}
353 </div>
354 </div>
355 );
356};
357
358export default SeedList;