blob: abcba7df8dfc6699eb0a000677119b4574116d7d [file] [log] [blame]
2230113338fd3882025-06-06 23:33:57 +08001import React, { useState, useEffect } from "react";
wht6a1b6782025-06-06 19:14:59 +08002import { useNavigate } from "react-router-dom";
2230113338fd3882025-06-06 23:33:57 +08003import { API_BASE_URL } from "./config";
wht6a1b6782025-06-06 19:14:59 +08004
5// 示例数据
6const posts = [
7 {
8 post_id: "1",
9 title: "欢迎来到G10论坛",
10 content: "这里是G10 PT站的官方论坛,欢迎大家发帖交流!",
11 author_id: "u1",
12 author_name: "Alice",
13 created_at: "2024-06-01 12:00",
14 reply_count: 3,
15 view_count: 120,
16 },
17 {
18 post_id: "2",
19 title: "经典电影合集",
20 content: "90年代经典电影大家有什么推荐吗?",
21 author_id: "u2",
22 author_name: "Bob",
23 created_at: "2024-06-02 09:30",
24 reply_count: 1,
25 view_count: 45,
26 },
27];
28
29export default function ForumPage() {
30 const navigate = useNavigate();
2230113338fd3882025-06-06 23:33:57 +080031 const [posts, setPosts] = useState([]);
32
33 useEffect(() => {
34 // get userId from cookie
35 const match = document.cookie.match('(^|;)\\s*userId=([^;]+)');
36 const userId = match ? match[2] : null;
37 console.log("User ID from cookie:", userId);
38 if (!userId) return;
39 fetch(`${API_BASE_URL}/api/forum`)
40 .then(res => res.json())
41 .then(data => setPosts(data))
42 .catch(() => setPosts([]));
43 }, []);
44
wht6a1b6782025-06-06 19:14:59 +080045 return (
46 <div style={{ maxWidth: 700, margin: "40px auto" }}>
47 <div style={{ display: "flex", alignItems: "center", marginBottom: 24 }}>
48 <h2 style={{ color: "#1a237e", margin: 0, marginRight: 24 }}>G10 论坛</h2>
49 <input
50 type="text"
51 placeholder="搜索帖子内容"
52 style={{
53 flex: 1,
54 fontSize: 16,
55 padding: "8px 14px",
56 border: "1px solid #bfcfff",
57 borderRadius: 8,
58 outline: "none",
59 minWidth: 0,
60 }}
61 />
62 <button
63 style={{
64 marginLeft: 8,
65 fontSize: 16,
66 padding: "8px 18px",
67 border: "1px solid #bfcfff",
68 background: "#e0e7ff",
69 borderRadius: 8,
70 cursor: "pointer",
71 }}
72 >
73 🔍
74 </button>
75 </div>
76 {posts.map(post => (
77 <div
78 key={post.post_id}
79 style={{
80 background: "#fff",
81 borderRadius: 12,
82 boxShadow: "0 2px 8px #e0e7ff",
83 padding: 24,
84 marginBottom: 24,
85 cursor: "pointer",
86 transition: "box-shadow 0.2s",
87 }}
88 onClick={() => navigate(`/forum/${post.post_id}`)}
89 >
90 <div style={{ fontSize: 15, color: "#1976d2", marginBottom: 6 }}>
91 {post.author_name} · {post.created_at}
92 </div>
93 <div style={{ fontWeight: 600, fontSize: 20, marginBottom: 8, color: "#222" }}>
94 {post.title}
95 </div>
96 <div style={{ fontSize: 16, color: "#444", marginBottom: 18 }}>
97 {post.content}
98 </div>
99 <div style={{ display: "flex", justifyContent: "flex-start", gap: 32, fontSize: 14, color: "#888" }}>
100 <span>回复数: {post.reply_count}</span>
101 <span>观看数: {post.view_count}</span>
102 </div>
103 </div>
104 ))}
105 </div>
106 );
107}