更新论坛功能
Change-Id: I0efd26d35cc4abb0c6d51ff1bff2cfd738f986dd
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;