更新论坛功能

Change-Id: I0efd26d35cc4abb0c6d51ff1bff2cfd738f986dd
diff --git a/src/pages/Forum/posts-main/ForumPage.css b/src/pages/Forum/posts-main/ForumPage.css
new file mode 100644
index 0000000..3f26e17
--- /dev/null
+++ b/src/pages/Forum/posts-main/ForumPage.css
@@ -0,0 +1,96 @@
+.forum-page {
+    color: #fff;
+    background-color: #2d2d2d;
+    min-height: 100vh;
+    font-family: Arial, sans-serif;
+  }
+  
+  .forum-content {
+    padding: 20px;
+  }
+  
+  .post-list {
+    display: flex;
+    flex-direction: column;
+    gap: 20px;
+  }
+  
+  .post-card {
+    background-color: #4A3B34;
+    padding: 15px;
+    border-radius: 10px;
+    position: relative;
+  }
+  
+  .post-card-top {
+    display: flex;
+    justify-content: space-between;
+    align-items: center;
+  }
+  
+  .user-info {
+    display: flex;
+    align-items: center;
+  }
+  
+  .avatar {
+    width: 36px;
+    height: 36px;
+    border-radius: 50%;
+    margin-right: 10px;
+  }
+  
+  .nickname {
+    font-weight: bold;
+  }
+  
+  .cover-image {
+    max-height: 80px;
+    max-width: 120px;
+    object-fit: cover;
+    border-radius: 6px;
+  }
+  
+  .post-meta {
+    font-size: 12px;
+    margin-top: 5px;
+    color: #ddd;
+  }
+  
+  .post-actions {
+    display: flex;
+    gap: 15px;
+    margin: 10px 0;
+  }
+  
+  .icon-btn {
+    display: flex;
+    align-items: center;
+    gap: 5px;
+    background: none;
+    border: none;
+    color: #fff;
+    cursor: pointer;
+  }
+  
+  .btn-secondary {
+    display: inline-block;
+    background: #888;
+    color: #fff;
+    padding: 6px 12px;
+    text-decoration: none;
+    border-radius: 5px;
+    margin-top: 10px;
+  }
+  
+  .pagination {
+    margin-top: 20px;
+    display: flex;
+    justify-content: center;
+    gap: 10px;
+  }
+  
+  .error-text {
+    color: red;
+  }
+  
\ No newline at end of file
diff --git a/src/pages/Forum/posts-main/ForumPage.jsx b/src/pages/Forum/posts-main/ForumPage.jsx
new file mode 100644
index 0000000..5c7d622
--- /dev/null
+++ b/src/pages/Forum/posts-main/ForumPage.jsx
@@ -0,0 +1,177 @@
+// import React, { useState, useEffect } from 'react';
+// import { Link } from 'wouter';
+// import axios from 'axios';
+// import { GoodTwo, Comment } from '@icon-park/react';
+// import Header from '../../components/Header'; 
+// import './ForumPage.css';
+
+// const API_BASE = process.env.REACT_APP_API_BASE;
+
+// const ForumPage = () => {
+//   const [posts, setPosts] = useState([]);
+//   const [total, setTotal] = useState(0);
+//   const [page, setPage] = useState(1);
+//   const [size, setSize] = useState(10);
+//   const [loading, setLoading] = useState(true);
+//   const [errorMsg, setErrorMsg] = useState('');
+
+//   const totalPages = Math.ceil(total / size);
+
+//   useEffect(() => {
+//     const fetchPosts = async () => {
+//       setLoading(true);
+//       setErrorMsg('');
+//       try {
+//         const response = await axios.get(`${API_BASE}/echo/forum/posts/getAllPost`, {
+//           params: { page, size }
+//         });
+//         const postsData = response.data.posts || [];
+
+//         const userIds = [...new Set(postsData.map(post => post.user_id))];
+//         const userProfiles = await Promise.all(
+//           userIds.map(async id => {
+//             try {
+//               const res = await axios.get(`${API_BASE}/echo/user/profile`, {
+//                 params: { user_id: id }
+//               });
+//               return { id, profile: res.data };
+//             } catch {
+//               return { id, profile: { nickname: '未知用户', avatar_url: 'default-avatar.png' } };
+//             }
+//           })
+//         );
+
+//         const userMap = {};
+//         userProfiles.forEach(({ id, profile }) => {
+//           userMap[id] = profile;
+//         });
+
+//         const postsWithProfiles = postsData.map(post => ({
+//           ...post,
+//           userProfile: userMap[post.user_id] || { nickname: '未知用户', avatar_url: 'default-avatar.png' }
+//         }));
+
+//         setPosts(postsWithProfiles);
+//         setTotal(response.data.total || 0);
+//       } catch (error) {
+//         console.error('获取帖子失败:', error);
+//         setErrorMsg('加载失败,请稍后重试');
+//       } finally {
+//         setLoading(false);
+//       }
+//     };
+//     fetchPosts();
+//   }, [page, size]);
+
+//   const toggleLike = async (postId, liked) => {
+//     try {
+//       if (liked) {
+//         await axios.delete(`${API_BASE}/echo/forum/posts/${postId}/unlike`);
+//       } else {
+//         await axios.post(`${API_BASE}/echo/forum/posts/${postId}/like`);
+//       }
+
+//       setPosts(prevPosts =>
+//         prevPosts.map(post =>
+//           post.id === postId
+//             ? {
+//                 ...post,
+//                 liked: !liked,
+//                 likeCount: liked ? post.likeCount - 1 : post.likeCount + 1
+//               }
+//             : post
+//         )
+//       );
+//     } catch (error) {
+//       console.error('点赞操作失败:', error);
+//     }
+//   };
+
+//   return (
+//     <div className="forum-page">
+//       <Header /> {/* 使用 Header 组件 */}
+//       <div className="forum-content">
+//         <h2>论坛帖子列表</h2>
+
+//         {loading ? (
+//           <p>加载中...</p>
+//         ) : errorMsg ? (
+//           <p className="error-text">{errorMsg}</p>
+//         ) : posts.length === 0 ? (
+//           <p>暂无帖子。</p>
+//         ) : (
+//           <div className="post-list">
+//             {posts.map(post => (
+//               <div key={post.id} className="post-card">
+//                 <div className="post-card-top">
+//                   <div className="user-info">
+//                     <img className="avatar" src={post.userProfile.avatar_url} alt="头像" />
+//                     <span className="nickname">{post.userProfile.nickname}</span>
+//                   </div>
+//                   {post.cover_image_url && (
+//                     <img className="cover-image" src={post.cover_image_url} alt="封面" />
+//                   )}
+//                 </div>
+//                 <h3>{post.title}</h3>
+//                 <p className="post-meta">
+//                   发布时间:{new Date(post.created_at).toLocaleString()}
+//                 </p>
+//                 <div className="post-actions">
+//                   <button
+//                     className="icon-btn"
+//                     onClick={() => toggleLike(post.id, post.liked)}
+//                   >
+//                     <GoodTwo theme="outline" size="24" fill={post.liked ? '#f00' : '#fff'} />
+//                     <span>{post.likeCount || 0}</span>
+//                   </button>
+//                   <Link href={`/forum/post/${post.id}`} className="icon-btn">
+//                     <Comment theme="outline" size="24" fill="#fff" />
+//                     <span>{post.commentCount || 0}</span>
+//                   </Link>
+//                 </div>
+//                 <Link href={`/forum/post/${post.id}`} className="btn-secondary">查看详情</Link>
+//               </div>
+//             ))}
+//           </div>
+//         )}
+
+//         <div className="pagination">
+//           <button disabled={page === 1} onClick={() => setPage(page - 1)}>上一页</button>
+//           <span>第 {page} 页 / 共 {totalPages} 页</span>
+//           <button disabled={page === totalPages} onClick={() => setPage(page + 1)}>下一页</button>
+//         </div>
+//       </div>
+//     </div>
+//   );
+// };
+
+// export default ForumPage;
+
+import React, { useState } from 'react';
+import Header from '../../../components/Header';
+import SearchBar from './components/SearchBar';
+import CreatePostButton from './components/CreatePostButton';
+import PostList from './components/PostList';
+import './ForumPage.css';
+
+const ForumPage = () => {
+  const [searchQuery, setSearchQuery] = useState('');
+
+  const handleSearch = (query) => {
+    setSearchQuery(query);
+  };
+
+  return (
+    <div className="forum-page">
+      <Header />
+      <div className="toolbar">
+        <SearchBar onSearch={handleSearch} />
+        <CreatePostButton />
+      </div>
+      <PostList search={searchQuery} />
+    </div>
+  );
+};
+
+export default ForumPage;
+
diff --git a/src/pages/Forum/posts-main/components/CreatePostButton.css b/src/pages/Forum/posts-main/components/CreatePostButton.css
new file mode 100644
index 0000000..c118c7a
--- /dev/null
+++ b/src/pages/Forum/posts-main/components/CreatePostButton.css
@@ -0,0 +1,11 @@
+.create-post {
+    display: flex;
+    justify-content: center;
+    margin: 20px 0;
+  }
+  
+  .create-post-button {
+    display: flex;
+    align-items: center;
+  }
+  
\ No newline at end of file
diff --git a/src/pages/Forum/posts-main/components/CreatePostButton.jsx b/src/pages/Forum/posts-main/components/CreatePostButton.jsx
new file mode 100644
index 0000000..0632173
--- /dev/null
+++ b/src/pages/Forum/posts-main/components/CreatePostButton.jsx
@@ -0,0 +1,22 @@
+import React from 'react';
+import { useLocation } from 'wouter';
+import { Edit } from '@icon-park/react';
+import './CreatePostButton.css';
+
+const CreatePostButton = () => {
+  const [, navigate] = useLocation();
+
+  const goToCreatePost = () => {
+    navigate('/forum/create-post');
+  };
+
+  return (
+    <div className="create-post">
+      <button onClick={goToCreatePost} className="create-btn">
+        <Edit theme="outline" size="18" /> 发帖
+      </button>
+    </div>
+  );
+};
+
+export default CreatePostButton;
diff --git a/src/pages/Forum/posts-main/components/PostList.css b/src/pages/Forum/posts-main/components/PostList.css
new file mode 100644
index 0000000..d855c07
--- /dev/null
+++ b/src/pages/Forum/posts-main/components/PostList.css
@@ -0,0 +1,12 @@
+.post-list {
+    display: flex;
+    flex-direction: column;
+    gap: 16px;
+  }
+  
+  .post-card {
+    border: 1px solid #eee;
+    padding: 16px;
+    border-radius: 8px;
+  }
+  
\ No newline at end of file
diff --git a/src/pages/Forum/posts-main/components/PostList.jsx b/src/pages/Forum/posts-main/components/PostList.jsx
new file mode 100644
index 0000000..fa33dd5
--- /dev/null
+++ b/src/pages/Forum/posts-main/components/PostList.jsx
@@ -0,0 +1,126 @@
+import React, { useEffect, useState } from 'react';
+import axios from 'axios';
+import { Link } from 'wouter';
+import { GoodTwo, Comment } from '@icon-park/react';
+import './PostList.css';
+
+const API_BASE = process.env.REACT_APP_API_BASE;
+
+const PostList = ({ search }) => {
+  const [posts, setPosts] = useState([]);
+  const [page, setPage] = useState(1);
+  const [total, setTotal] = useState(0);
+  const [loading, setLoading] = useState(true);
+  const [errorMsg, setErrorMsg] = useState('');
+
+  const size = 10;
+  const totalPages = Math.ceil(total / size);
+
+  useEffect(() => {
+    const fetchPosts = async () => {
+      setLoading(true);
+      setErrorMsg('');
+      try {
+        const res = await axios.get(`${API_BASE}/echo/forum/posts/getAllPost`, {
+          params: { page, size }
+        });
+
+        const postsData = res.data.posts || [];
+        const userIds = [...new Set(postsData.map(p => p.user_id))];
+
+        const profiles = await Promise.all(userIds.map(async id => {
+          try {
+            const r = await axios.get(`${API_BASE}/echo/user/profile`, {
+              params: { user_id: id }
+            });
+            return { id, profile: r.data };
+          } catch {
+            return { id, profile: { nickname: '未知用户', avatar_url: 'default-avatar.png' } };
+          }
+        }));
+
+        const userMap = {};
+        profiles.forEach(({ id, profile }) => { userMap[id] = profile; });
+
+        const postsWithProfiles = postsData
+          .filter(post => post.title.includes(search))
+          .map(post => ({
+            ...post,
+            userProfile: userMap[post.user_id] || {}
+          }));
+
+        setPosts(postsWithProfiles);
+        setTotal(res.data.total || 0);
+      } catch (err) {
+        console.error('加载失败:', err);
+        setErrorMsg('加载失败,请稍后重试');
+      } finally {
+        setLoading(false);
+      }
+    };
+
+    fetchPosts();
+  }, [page, search]);
+
+  const toggleLike = async (postId, liked) => {
+    try {
+      if (liked) await axios.delete(`${API_BASE}/echo/forum/posts/${postId}/unlike`);
+      else await axios.post(`${API_BASE}/echo/forum/posts/${postId}/like`);
+
+      setPosts(posts =>
+        posts.map(post =>
+          post.id === postId
+            ? { ...post, liked: !liked, likeCount: liked ? post.likeCount - 1 : post.likeCount + 1 }
+            : post
+        )
+      );
+    } catch (err) {
+      console.error('点赞失败:', err);
+    }
+  };
+
+  return (
+    <div className="post-list">
+      {loading ? <p>加载中...</p> :
+        errorMsg ? <p className="error-text">{errorMsg}</p> :
+          posts.length === 0 ? <p>暂无帖子。</p> :
+            posts.map(post => (
+              <div key={post.id} className="post-card">
+                <div className="post-card-top">
+                  <div className="user-info">
+                    <img className="avatar" src={post.userProfile.avatar_url} alt="头像" />
+                    <span className="nickname">{post.userProfile.nickname}</span>
+                  </div>
+                  {post.cover_image_url && (
+                    <img className="cover-image" src={post.cover_image_url} alt="封面" />
+                  )}
+                </div>
+                <h3>{post.title}</h3>
+                <p className="post-meta">
+                  发布时间:{new Date(post.created_at).toLocaleString()}
+                </p>
+                <div className="post-actions">
+                  <button className="icon-btn" onClick={() => toggleLike(post.id, post.liked)}>
+                    <GoodTwo theme="outline" size="24" fill={post.liked ? '#f00' : '#fff'} />
+                    <span>{post.likeCount}</span>
+                  </button>
+                  <Link href={`/forum/post/${post.id}`} className="icon-btn">
+                    <Comment theme="outline" size="24" fill="#fff" />
+                    <span>{post.commentCount}</span>
+                  </Link>
+                </div>
+                <Link href={`/forum/post/${post.id}`} className="btn-secondary">查看详情</Link>
+              </div>
+            ))
+      }
+
+      <div className="pagination">
+        <button disabled={page === 1} onClick={() => setPage(page - 1)}>上一页</button>
+        <span>第 {page} 页 / 共 {totalPages} 页</span>
+        <button disabled={page === totalPages} onClick={() => setPage(page + 1)}>下一页</button>
+      </div>
+    </div>
+  );
+};
+
+export default PostList;
diff --git a/src/pages/Forum/posts-main/components/SearchBar.css b/src/pages/Forum/posts-main/components/SearchBar.css
new file mode 100644
index 0000000..b74f9d2
--- /dev/null
+++ b/src/pages/Forum/posts-main/components/SearchBar.css
@@ -0,0 +1,7 @@
+.search-bar {
+    display: flex;
+    align-items: center;
+    gap: 10px;
+    margin-bottom: 20px;
+  }
+  
\ No newline at end of file
diff --git a/src/pages/Forum/posts-main/components/SearchBar.jsx b/src/pages/Forum/posts-main/components/SearchBar.jsx
new file mode 100644
index 0000000..52b873d
--- /dev/null
+++ b/src/pages/Forum/posts-main/components/SearchBar.jsx
@@ -0,0 +1,28 @@
+import React, { useState } from 'react';
+import './SearchBar.css';
+
+const SearchBar = ({ onSearch }) => {
+  const [query, setQuery] = useState('');
+
+  const handleSearch = () => onSearch(query);
+  const handleReset = () => {
+    setQuery('');
+    onSearch('');
+  };
+
+  return (
+    <div className="search-bar">
+      <input
+        type="text"
+        value={query}
+        onChange={e => setQuery(e.target.value)}
+        placeholder="标题"
+        className="search-input"
+      />
+      <button onClick={handleSearch} className="search-btn">搜索</button>
+      <button onClick={handleReset} className="search-btn">重置</button>
+    </div>
+  );
+};
+
+export default SearchBar;