diff --git a/backend/app/routers/yichens.py b/backend/app/routers/yichens.py index 296d4d0..3fd6a80 100644 --- a/backend/app/routers/yichens.py +++ b/backend/app/routers/yichens.py @@ -111,9 +111,10 @@ def get_posts( offset: int = Query(0, ge=0), category: Optional[str] = None, post_type: Optional[str] = None, + keyword: Optional[str] = None, db: Session = Depends(get_coolbot_db) ): - """获取帖子列表""" + """获取帖子列表 - 支持全局搜索""" query = """ SELECT post_id, title, content, category, post_type, price, author_username, post_time, reply_count, view_count, url @@ -130,6 +131,11 @@ def get_posts( query += " AND post_type = :post_type" params["post_type"] = post_type + # 全局搜索 - 在标题、内容、分类、联系方式中搜索 + if keyword: + query += " AND (title ILIKE :keyword OR content ILIKE :keyword OR category ILIKE :keyword OR author_username ILIKE :keyword)" + params["keyword"] = f"%{keyword}%" + query += " ORDER BY post_time DESC LIMIT :limit OFFSET :offset" results = db.execute(text(query), params).fetchall() diff --git a/frontend/src/pages/YichensBoard.jsx b/frontend/src/pages/YichensBoard.jsx index 6f8594c..cc866f4 100644 --- a/frontend/src/pages/YichensBoard.jsx +++ b/frontend/src/pages/YichensBoard.jsx @@ -35,16 +35,18 @@ export default function YichensBoard() { } } - const fetchPosts = async (p, cat) => { + const fetchPosts = async (p, cat, kw) => { setLoading(true) setError(null) const currentPage = p !== undefined ? p : page const currentCat = cat !== undefined ? cat : categoryFilter + const searchKw = kw !== undefined ? kw : searchKeyword - let url = '/api/yichens/posts?limit=50&offset=' + ((currentPage - 1) * 390) + let url = '/api/yichens/posts?limit=390&offset=' + ((currentPage - 1) * 390) if (postTypeFilter === 'deal') url += '&post_type=deal' else if (postTypeFilter === 'want') url += '&post_type=want' else if (postTypeFilter === 'other') url += '&post_type=normal' + if (searchKw && searchKw.trim()) url += '&keyword=' + encodeURIComponent(searchKw.trim()) try { console.log('YichensBoard: 请求 posts', url) @@ -63,17 +65,6 @@ export default function YichensBoard() { data = data.filter(p => p.category && !p.category.includes('龙') && !p.category.includes('纪念钞') && !p.category.includes('蛇') && !p.category.includes('马')) } } - if (searchKeyword) { - const kw = searchKeyword.trim() - if (kw) { - data = data.filter(p => - (p.title && p.title.includes(kw)) || - (p.content && p.content.includes(kw)) || - (p.category && p.category.includes(kw)) || - (p.contact && p.contact.includes(kw)) - ) - } - } setPosts(data) setTotalPosts(todayStats.total || 0) } catch(e) { @@ -218,16 +209,16 @@ export default function YichensBoard() {
- 共 {totalPosts} 条帖子,{Math.ceil(totalPosts / 50)} 页,当前第 {page} 页 + 共 {totalPosts} 条帖子,{Math.ceil(totalPosts / 390)} 页,当前第 {page} 页
{page > 1 ? ( - + ) : ( 上一页 )} - {posts.length >= 50 ? ( - + {posts.length >= 390 ? ( + ) : ( 下一页 )}