刘嘉昕 | 92011cf | 2025-06-09 17:48:31 +0800 | [diff] [blame] | 1 | import React, { useEffect, useState } from 'react'; |
| 2 | import '../torrentlist.css'; |
| 3 | |
| 4 | const RecommendAll = () => { |
| 5 | const [torrents, setTorrents] = useState([]); |
| 6 | const storedUser = localStorage.getItem('user'); |
| 7 | //let currentUserId = null; // 初始化为 null |
| 8 | let currentUserId = null; // 初始化为 null |
| 9 | |
| 10 | if (storedUser) { |
| 11 | try { |
| 12 | const parsedUser = JSON.parse(storedUser); |
| 13 | currentUserId = parsedUser.userid; // 直接赋值 |
| 14 | } catch (error) { |
| 15 | console.error('解析用户数据失败:', error); |
| 16 | // 可以在这里处理 JSON 解析错误(如数据损坏) |
| 17 | } |
| 18 | } else { |
| 19 | console.log('用户未登录'); |
| 20 | } |
| 21 | |
| 22 | // 现在 currentUserId 可以在后续逻辑中使用 |
| 23 | console.log('当前用户ID:', currentUserId); |
| 24 | |
| 25 | |
| 26 | useEffect(() => { |
| 27 | const fetchAll = async () => { |
| 28 | const res = await fetch(`http://localhost:8080/recommend/list?userId=${currentUserId}`); |
| 29 | //const res = await fetch('http://localhost:8080/torrent/list'); |
| 30 | const data = await res.json(); |
| 31 | setTorrents(data); |
| 32 | }; |
| 33 | fetchAll(); |
| 34 | }, []); |
| 35 | |
| 36 | const formatFileSize = (bytes) => { |
| 37 | if (!bytes) return '0 Bytes'; |
| 38 | const sizes = ['Bytes', 'KB', 'MB', 'GB']; |
| 39 | const i = Math.floor(Math.log(bytes) / Math.log(1024)); |
| 40 | return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i]; |
| 41 | }; |
| 42 | |
| 43 | return ( |
| 44 | <div className="torrents-container"> |
| 45 | <h2 style={{ fontSize: 24, fontWeight: 'bold', marginBottom: 16 }}>全部推荐种子</h2> |
| 46 | <div className="torrents1-grid"> |
| 47 | {torrents.map(torrent => ( |
| 48 | <div key={torrent.torrentid} className="torrent-card"> |
| 49 | <div className="cover"> |
| 50 | {torrent.coverImagePath ? ( |
| 51 | <img src={torrent.coverImagePath} alt="封面" className="cover-image" /> |
| 52 | ) : ( |
| 53 | <div className="no-cover">无封面</div> |
| 54 | )} |
| 55 | </div> |
| 56 | |
| 57 | <div className="info"> |
| 58 | <h3 className="title" title={torrent.filename}> |
| 59 | {torrent.torrentTitle || torrent.filename} |
| 60 | </h3> |
| 61 | <p className="description" title={torrent.description}> |
| 62 | {torrent.description || '暂无描述'} |
| 63 | </p> |
| 64 | |
| 65 | <div className="details"> |
| 66 | <span>大小: {formatFileSize(torrent.torrentSize)}</span> |
| 67 | <span>上传时间: {new Date(torrent.uploadTime).toLocaleDateString()}</span> |
| 68 | <span>下载次数: {torrent.downloadCount}</span> |
| 69 | </div> |
| 70 | |
| 71 | <div className="actions"> |
| 72 | <button className="btn btn-download">下载</button> |
| 73 | <a href={`/torrent/${torrent.torrentid}`} className="btn btn-detail">详情</a> |
| 74 | </div> |
| 75 | </div> |
| 76 | </div> |
| 77 | ))} |
| 78 | </div> |
| 79 | </div> |
| 80 | ); |
| 81 | }; |
| 82 | |
| 83 | export default RecommendAll; |