优化推荐系统和冷启动

Change-Id: I93d3091f249f2396a25702e01eb8dd5a9e95e8bc
diff --git a/front/src/HomePage.js b/front/src/HomePage.js
index 026f122..088b284 100644
--- a/front/src/HomePage.js
+++ b/front/src/HomePage.js
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { useEffect, useState } from "react";
 import HomeIcon from "@mui/icons-material/Home";
 import MovieIcon from "@mui/icons-material/Movie";
 import TvIcon from "@mui/icons-material/Tv";
@@ -28,40 +28,40 @@
     { label: "求种", icon: <HelpIcon className="emerald-nav-icon" />, path: "/begseed", type: "help" },
 ];
 
-// 示例种子数据
-const exampleSeeds = [
-    {
-        id: 1,
-        tags: "电影,科幻",
-        title: "三体 1080P 蓝光",
-        popularity: 123,
-        user: { username: "Alice" },
-    },
-    {
-        id: 2,
-        tags: "动漫,热血",
-        title: "灌篮高手 国语配音",
-        popularity: 88,
-        user: { username: "Bob" },
-    },
-    {
-        id: 3,
-        tags: "音乐,流行",
-        title: "周杰伦-稻香",
-        popularity: 56,
-        user: { username: "Jay" },
-    },
-    {
-        id: 4,
-        tags: "剧集,悬疑",
-        title: "隐秘的角落",
-        popularity: 77,
-        user: { username: "小明" },
-    },
-];
-
 export default function HomePage() {
     const navigate = useNavigate();
+    const [recommendSeeds, setRecommendSeeds] = useState([]);
+    const [loading, setLoading] = useState(true);
+
+    useEffect(() => {
+        // 获取当前登录用户ID
+        const match = document.cookie.match('(^|;)\\s*userId=([^;]+)');
+        const userId = match ? match[2] : null;
+
+        if (!userId) {
+            setRecommendSeeds([]);
+            setLoading(false);
+            return;
+        }
+
+        setLoading(true);
+        fetch("http://10.126.59.25:5000/recommend", {
+            method: "POST",
+            headers: {
+                "Content-Type": "application/json"
+            },
+            body: JSON.stringify({ user_id: userId })
+        })
+            .then(res => res.json())
+            .then(data => {
+                setRecommendSeeds(Array.isArray(data.recommend) ? data.recommend : []);
+                setLoading(false);
+            })
+            .catch(() => {
+                setRecommendSeeds([]);
+                setLoading(false);
+            });
+    }, []);
 
     return (
         <div className="emerald-home-container">
@@ -134,24 +134,38 @@
                         <thead>
                             <tr>
                                 <th>分类标签</th>
-                                <th>资源标题</th>
-                                <th>热门指数</th>
+                                <th>标题</th>
                                 <th>发布者</th>
+                                <th>大小</th>
+                                <th>热度</th>
+                                <th>折扣倍率</th>
                             </tr>
                         </thead>
                         <tbody>
-                            {exampleSeeds.map((seed) => (
-                                <tr key={seed.id}>
-                                    <td>{seed.tags}</td>
-                                    <td>
-                                        <a href={`/torrent/${seed.id}`}>
-                                            {seed.title}
-                                        </a>
-                                    </td>
-                                    <td>{seed.popularity}</td>
-                                    <td>{seed.user.username}</td>
+                            {loading ? (
+                                <tr>
+                                    <td colSpan={6} style={{ textAlign: "center", color: "#888" }}>正在加载推荐种子...</td>
                                 </tr>
-                            ))}
+                            ) : recommendSeeds.length === 0 ? (
+                                <tr>
+                                    <td colSpan={6} style={{ textAlign: "center", color: "#888" }}>暂无推荐数据</td>
+                                </tr>
+                            ) : (
+                                recommendSeeds.map((seed) => (
+                                    <tr key={seed.seed_id}>
+                                        <td>{seed.tags}</td>
+                                        <td>
+                                            <a href={`/torrent/${seed.seed_id}`}>
+                                                {seed.title}
+                                            </a>
+                                        </td>
+                                        <td>{seed.username}</td>
+                                        <td>{seed.size}</td>
+                                        <td>{seed.popularity}</td>
+                                        <td>{seed.discount == null ? 1 : seed.discount}</td>
+                                    </tr>
+                                ))
+                            )}
                         </tbody>
                     </table>
                 </div>