刘嘉昕 | 92011cf | 2025-06-09 17:48:31 +0800 | [diff] [blame] | 1 | import React, { useEffect, useState } from 'react'; |
| 2 | import { useNavigate } from 'react-router-dom'; |
| 3 | import '../RecommendPreview.css'; |
| 4 | |
| 5 | const RecommendPreview = () => { |
| 6 | |
| 7 | const [recommendList, setRecommendList] = useState([]); |
| 8 | const navigate = useNavigate(); |
| 9 | |
| 10 | const storedUser = localStorage.getItem('user'); |
| 11 | //let currentUserId = null; // 初始化为 null |
| 12 | let currentUserId = null; // 初始化为 null |
| 13 | |
| 14 | if (storedUser) { |
| 15 | try { |
| 16 | const parsedUser = JSON.parse(storedUser); |
| 17 | currentUserId = parsedUser.userid; // 直接赋值 |
| 18 | } catch (error) { |
| 19 | console.error('解析用户数据失败:', error); |
| 20 | // 可以在这里处理 JSON 解析错误(如数据损坏) |
| 21 | } |
| 22 | } else { |
| 23 | console.log('用户未登录'); |
| 24 | } |
| 25 | |
| 26 | // 现在 currentUserId 可以在后续逻辑中使用 |
| 27 | console.log('当前用户ID:', currentUserId); |
| 28 | |
| 29 | useEffect(() => { |
| 30 | const fetchRecommendList = async () => { |
| 31 | try { |
| 32 | const res = await fetch(`http://localhost:8080/recommend/list?userId=${currentUserId}`); |
| 33 | //const res = await fetch('http://localhost:8080/torrent/list'); |
| 34 | const data = await res.json(); |
| 35 | console.log('推荐列表数据:', data); |
| 36 | setRecommendList(data.slice(0, 8)); // 只展示前8个 |
| 37 | } catch (err) { |
| 38 | console.error('推荐获取失败:', err); |
| 39 | } |
| 40 | }; |
| 41 | fetchRecommendList(); |
| 42 | }, []); |
| 43 | |
| 44 | const formatFileSize = (bytes) => { |
| 45 | if (!bytes) return '0 Bytes'; |
| 46 | const sizes = ['Bytes', 'KB', 'MB', 'GB']; |
| 47 | const i = Math.floor(Math.log(bytes) / Math.log(1024)); |
| 48 | return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i]; |
| 49 | }; |
| 50 | |
| 51 | const formatDate = (dateString) => { |
| 52 | const date = new Date(dateString); |
| 53 | return date.toLocaleDateString(); |
| 54 | }; |
| 55 | |
| 56 | return ( |
| 57 | <div className="recommend-horizontal-container"> |
| 58 | <div className="recommend-header"> |
| 59 | <h2>为你推荐</h2> |
| 60 | <button className="more-btn" onClick={() => navigate('/recommend')}> |
| 61 | 更多 |
| 62 | </button> |
| 63 | </div> |
| 64 | |
| 65 | <div className="recommend-horizontal-grid"> |
| 66 | {recommendList.map((torrent) => ( |
| 67 | <div key={torrent.torrentid} className="horizontal-card"> |
| 68 | {torrent.coverImagePath ? ( |
| 69 | <img src={torrent.coverImagePath} alt="封面" className="card-cover" /> |
| 70 | ) : ( |
| 71 | <div className="card-no-cover">无封面</div> |
| 72 | )} |
| 73 | <div className="card-info"> |
| 74 | <div className="card-title" title={torrent.torrentTitle || torrent.filename}> |
| 75 | {torrent.torrentTitle || torrent.filename} |
| 76 | </div> |
| 77 | <div className="card-description"> |
| 78 | {torrent.description || '暂无描述'} |
| 79 | </div> |
| 80 | <div className="card-meta"> |
| 81 | <span>{formatDate(torrent.uploadTime)}</span> |
| 82 | <span>{formatFileSize(torrent.torrentSize)}</span> |
| 83 | </div> |
| 84 | </div> |
| 85 | </div> |
| 86 | ))} |
| 87 | </div> |
| 88 | </div> |
| 89 | ); |
| 90 | }; |
| 91 | |
| 92 | export default RecommendPreview; |