blob: a010840620006156282f428bc941a9cd3b36637d [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 });
2230100980aaf0d2025-06-05 23:20:05 +080077 const data = response.data;
22301009afbcf4b2025-04-10 16:08:39 +080078
2230100980aaf0d2025-06-05 23:20:05 +080079 if (data.code !== 0) {
80 throw new Error(data.msg || '获取失败');
81 }
82
83 setSeeds(data.data || []);
22301009afbcf4b2025-04-10 16:08:39 +080084 } catch (error) {
85 console.error('获取种子列表失败:', error);
86 setErrorMsg(error.message || '获取失败,请稍后再试。');
87 setSeeds([]);
22301009afbcf4b2025-04-10 16:08:39 +080088 } finally {
89 setLoading(false);
90 }
91 };
92
93 const fetchFilterOptions = async () => {
2230100980aaf0d2025-06-05 23:20:05 +080094 if (activeTab === '猜你喜欢' || !CATEGORY_MAP[activeTab]) return;
22301009afbcf4b2025-04-10 16:08:39 +080095 const category = CATEGORY_MAP[activeTab];
96 try {
2230100980aaf0d2025-06-05 23:20:05 +080097 const res = await axios.get(`/seed-filters?category=${category}`);
223010095b28c672025-04-10 20:12:45 +080098 const filterData = res.data || {};
99 setFilters(filterData);
100
22301009afbcf4b2025-04-10 16:08:39 +0800101 const defaultSelections = {};
223010095b28c672025-04-10 20:12:45 +0800102 for (const key in filterData) {
22301009afbcf4b2025-04-10 16:08:39 +0800103 defaultSelections[key] = '不限';
104 }
105 setSelectedFilters(defaultSelections);
106 } catch (err) {
107 console.error('获取筛选项失败:', err);
108 setFilters({});
109 setSelectedFilters({});
110 }
111 };
22301009ecc1c1c2025-04-09 21:56:23 +0800112
113 useEffect(() => {
2230100980aaf0d2025-06-05 23:20:05 +0800114 fetchFilterOptions();
22301009afbcf4b2025-04-10 16:08:39 +0800115 }, [activeTab]);
22301009ecc1c1c2025-04-09 21:56:23 +0800116
22301009afbcf4b2025-04-10 16:08:39 +0800117 useEffect(() => {
2230100980aaf0d2025-06-05 23:20:05 +0800118 fetchSeeds();
22301009afbcf4b2025-04-10 16:08:39 +0800119 }, [activeTab, sortOption, selectedFilters, tagMode, searchTerm]);
22301009ecc1c1c2025-04-09 21:56:23 +0800120
2230100980aaf0d2025-06-05 23:20:05 +0800121 // ✅ 修改后的下载函数
22301009afbcf4b2025-04-10 16:08:39 +0800122 const handleDownload = async (seedId) => {
2230100980aaf0d2025-06-05 23:20:05 +0800123 if (!user || !user.userId) {
124 alert('请先登录再下载种子文件');
125 return;
126 }
22301009afbcf4b2025-04-10 16:08:39 +0800127
128 try {
2230100980aaf0d2025-06-05 23:20:05 +0800129 const response = await axios.get(`/seeds/${seedId}/download`, {
130 params: {
131 passkey: user.userId,
132 },
22301009afbcf4b2025-04-10 16:08:39 +0800133 responseType: 'blob'
134 });
135
136 const blob = new Blob([response.data], { type: 'application/x-bittorrent' });
137 const downloadUrl = URL.createObjectURL(blob);
138 const a = document.createElement('a');
139 a.href = downloadUrl;
140 a.download = `${seedId}.torrent`;
141 a.click();
142 URL.revokeObjectURL(downloadUrl);
143 } catch (error) {
144 console.error('下载失败:', error);
145 alert('下载失败,请稍后再试。');
22301009ecc1c1c2025-04-09 21:56:23 +0800146 }
22301009afbcf4b2025-04-10 16:08:39 +0800147 };
22301009ecc1c1c2025-04-09 21:56:23 +0800148
223010095b28c672025-04-10 20:12:45 +0800149 const handleFilterChange = (key, value) => {
2230100980aaf0d2025-06-05 23:20:05 +0800150 setSelectedFilters(prev => ({
151 ...prev,
223010095b28c672025-04-10 20:12:45 +0800152 [key]: value
153 }));
154 };
155
156 const clearFilter = (key) => {
2230100980aaf0d2025-06-05 23:20:05 +0800157 setSelectedFilters(prev => ({
158 ...prev,
223010095b28c672025-04-10 20:12:45 +0800159 [key]: '不限'
160 }));
161 };
162
22301009ecc1c1c2025-04-09 21:56:23 +0800163 return (
Krishyaf1d0ea82025-05-03 17:01:58 +0800164 <div className="seed-list-container">
2230100980aaf0d2025-06-05 23:20:05 +0800165 <Header />
22301009ecc1c1c2025-04-09 21:56:23 +0800166
22301009ecc1c1c2025-04-09 21:56:23 +0800167 <div className="controls">
168 <input
169 type="text"
170 placeholder="搜索种子..."
171 value={searchTerm}
172 onChange={(e) => setSearchTerm(e.target.value)}
173 className="search-input"
174 />
2230100980aaf0d2025-06-05 23:20:05 +0800175 <select
176 value={sortOption}
177 onChange={(e) => setSortOption(e.target.value)}
178 className="sort-select"
179 >
22301009ecc1c1c2025-04-09 21:56:23 +0800180 <option value="最新">最新</option>
181 <option value="最热">最热</option>
22301009afbcf4b2025-04-10 16:08:39 +0800182 </select>
2230100980aaf0d2025-06-05 23:20:05 +0800183 <select
184 value={tagMode}
185 onChange={(e) => setTagMode(e.target.value)}
186 className="tag-mode-select"
187 >
22301009afbcf4b2025-04-10 16:08:39 +0800188 <option value="any">包含任意标签</option>
189 <option value="all">包含所有标签</option>
22301009ecc1c1c2025-04-09 21:56:23 +0800190 </select>
191 </div>
192
193 <div className="tag-filters">
2230100980aaf0d2025-06-05 23:20:05 +0800194 {TAGS.map(tag => (
22301009ecc1c1c2025-04-09 21:56:23 +0800195 <button
196 key={tag}
2230100980aaf0d2025-06-05 23:20:05 +0800197 className={`tag-button ${activeTab === tag ? 'active-tag' : ''}`}
22301009ecc1c1c2025-04-09 21:56:23 +0800198 onClick={() => {
22301009afbcf4b2025-04-10 16:08:39 +0800199 setActiveTab(tag);
200 setFilters({});
201 setSelectedFilters({});
22301009ecc1c1c2025-04-09 21:56:23 +0800202 }}
203 >
204 {tag}
205 </button>
206 ))}
207 </div>
208
2230100980aaf0d2025-06-05 23:20:05 +0800209 {activeTab !== '猜你喜欢' && Object.keys(filters).length > 0 && (
22301009afbcf4b2025-04-10 16:08:39 +0800210 <div className="filter-bar">
211 {Object.entries(filters).map(([key, options]) => (
212 <div className="filter-group" key={key}>
213 <label>{key}:</label>
214 <select
215 value={selectedFilters[key]}
223010095b28c672025-04-10 20:12:45 +0800216 onChange={(e) => handleFilterChange(key, e.target.value)}
22301009afbcf4b2025-04-10 16:08:39 +0800217 >
2230100980aaf0d2025-06-05 23:20:05 +0800218 {options.map(opt => (
22301009afbcf4b2025-04-10 16:08:39 +0800219 <option key={opt} value={opt}>{opt}</option>
220 ))}
221 </select>
2230100980aaf0d2025-06-05 23:20:05 +0800222 {selectedFilters[key] !== '不限' && (
223 <button
224 className="clear-filter-btn"
225 onClick={() => clearFilter(key)}
226 >
227
228 </button>
223010095b28c672025-04-10 20:12:45 +0800229 )}
22301009afbcf4b2025-04-10 16:08:39 +0800230 </div>
231 ))}
232 </div>
233 )}
234
22301009ecc1c1c2025-04-09 21:56:23 +0800235 <div className="seed-list-content">
2230100980aaf0d2025-06-05 23:20:05 +0800236 {activeTab === '猜你喜欢' ? (
22301009afbcf4b2025-04-10 16:08:39 +0800237 <Recommend />
2230100980aaf0d2025-06-05 23:20:05 +0800238 ) : loading ? (
22301009ecc1c1c2025-04-09 21:56:23 +0800239 <p>加载中...</p>
2230100980aaf0d2025-06-05 23:20:05 +0800240 ) : errorMsg ? (
22301009afbcf4b2025-04-10 16:08:39 +0800241 <p className="error-text">{errorMsg}</p>
2230100980aaf0d2025-06-05 23:20:05 +0800242 ) : seeds.length === 0 ? (
22301009ecc1c1c2025-04-09 21:56:23 +0800243 <p>未找到符合条件的种子。</p>
244 ) : (
Krishya25590de2025-04-21 19:03:49 +0800245 <div className="seed-list-card">
246 <div className="seed-list-header">
247 <div className="seed-header-cover"></div>
248 <div className="seed-header-title">种子名称</div>
249 <div className="seed-header-size">大小</div>
250 <div className="seed-header-upload-time">上传时间</div>
251 <div className="seed-header-downloads">下载次数</div>
252 <div className="seed-header-actions">操作</div>
253 </div>
254 <div className="seed-list-body">
2230100980aaf0d2025-06-05 23:20:05 +0800255 {seeds.map((seed, index) => {
256 // 处理 tags 字段,兼容字符串和数组
257 let tagsArray = [];
258 if (seed.tags) {
259 if (Array.isArray(seed.tags)) {
260 tagsArray = seed.tags;
261 } else if (typeof seed.tags === 'string') {
262 try {
263 tagsArray = JSON.parse(seed.tags);
264 if (!Array.isArray(tagsArray)) {
265 // 解析后不是数组,按逗号分割
266 tagsArray = seed.tags.split(',').map(t => t.trim()).filter(t => t);
267 }
268 } catch {
269 // 解析失败,按逗号分割
270 tagsArray = seed.tags.split(',').map(t => t.trim()).filter(t => t);
271 }
272 }
273 }
274
275 return (
276
277 <Link to={`/seed/${seed.id}`} key={index} className="seed-item-link">
278 <div className="seed-item">
223010094158f3a2025-06-06 19:59:10 +0800279 {seed.imageUrl && (
2230100980aaf0d2025-06-05 23:20:05 +0800280 <img
223010094158f3a2025-06-06 19:59:10 +0800281 src={seed.imageUrl}
2230100980aaf0d2025-06-05 23:20:05 +0800282 alt={seed.title}
283 className="seed-item-cover"
284 />
285 )}
223010094158f3a2025-06-06 19:59:10 +0800286
2230100980aaf0d2025-06-05 23:20:05 +0800287 <div className="seed-item-title">
288 <div className="seed-title-row">
289 <h3 className="seed-title">{seed.title}</h3>
290 <div className="seed-tags">
291 {tagsArray.map((tag, i) => (
292 <span key={i} className="tag-label">{tag}</span>
293 ))}
294 </div>
Krishya25590de2025-04-21 19:03:49 +0800295 </div>
296 </div>
2230100980aaf0d2025-06-05 23:20:05 +0800297 <div className="seed-item-size">{seed.size || '未知'}</div>
298 <div className="seed-item-upload-time">{seed.upload_time?.split('T')[0] || '未知'}</div>
299 <div className="seed-item-downloads">{seed.downloads ?? 0} 次下载</div>
300 <div
301 className="seed-item-actions"
302 onClick={e => e.stopPropagation()} // 阻止事件冒泡,避免跳转
Krishya25590de2025-04-21 19:03:49 +0800303 >
2230100980aaf0d2025-06-05 23:20:05 +0800304 <button
305 className="btn-primary"
306 onClick={e => {
307 e.preventDefault();
308 e.stopPropagation();
309 handleDownload(seed.id);
310 }}
311 >
312 下载
313 </button>
314 <button
315 className="btn-outline"
316 onClick={async (e) => {
317 e.preventDefault();
318 e.stopPropagation();
319
320 if (!user || !user.userId) {
321 alert('请先登录再收藏');
322 return;
323 }
324
325 try {
326 const res = await axios.post(`/seeds/${seed.id}/favorite-toggle`, null, {
327 params: { user_id: user.userId },
328 });
329
330 if (res.data.code === 0) {
331 alert('操作成功'); // 你可以改成 toast 或 icon 状态提示
332 } else {
333 alert(res.data.msg || '操作失败');
334 }
335 } catch (err) {
336 console.error('收藏失败:', err);
337 alert('收藏失败,请稍后再试。');
338 }
339 }}
340>
341 收藏
342</button>
343
344 </div>
Krishya25590de2025-04-21 19:03:49 +0800345 </div>
2230100980aaf0d2025-06-05 23:20:05 +0800346 </Link>
347 );
348 })}
Krishya25590de2025-04-21 19:03:49 +0800349 </div>
22301009ecc1c1c2025-04-09 21:56:23 +0800350 </div>
351 )}
352 </div>
353 </div>
354 );
355};
356
357export default SeedList;