diff --git a/backend/app/routers/yichens.py b/backend/app/routers/yichens.py index 3fd6a80..de8e152 100644 --- a/backend/app/routers/yichens.py +++ b/backend/app/routers/yichens.py @@ -105,7 +105,7 @@ def get_user_stats(db: Session = Depends(get_coolbot_db)): sellers=result[2] or 0 ) -@router.get("/posts", response_model=List[PostItem]) +@router.get("/posts") def get_posts( limit: int = Query(20, ge=1, le=500), offset: int = Query(0, ge=0), @@ -114,33 +114,41 @@ def get_posts( 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 - FROM yichens_posts - WHERE 1=1 - """ + """获取帖子列表 - 支持全局搜索,返回总数和分页信息""" + # 构建WHERE条件 + where_clauses = ["1=1"] params = {"limit": limit, "offset": offset} if category: - query += " AND category = :category" + where_clauses.append("category = :category") params["category"] = category if post_type: - query += " AND post_type = :post_type" + where_clauses.append("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)" + where_clauses.append("(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" + where_sql = " AND ".join(where_clauses) - results = db.execute(text(query), params).fetchall() + # 查询总数 + count_query = f"SELECT COUNT(*) FROM yichens_posts WHERE {where_sql}" + total_count = db.execute(text(count_query), {k: v for k, v in params.items() if k not in ['limit', 'offset']}).scalar() or 0 - return [PostItem( + # 查询数据 + data_query = f""" + SELECT post_id, title, content, category, post_type, price, + author_username, post_time, reply_count, view_count, url + FROM yichens_posts + WHERE {where_sql} + ORDER BY post_time DESC LIMIT :limit OFFSET :offset + """ + results = db.execute(text(data_query), params).fetchall() + + posts = [PostItem( post_id=r[0], title=r[1] or "", content=r[2] or "", @@ -153,6 +161,13 @@ def get_posts( view_count=r[9] or 0, url=r[10] ) for r in results] + + return { + "posts": posts, + "total": total_count, + "page": offset // limit + 1, + "page_size": limit + } @router.get("/users", response_model=List[UserItem]) def get_users( diff --git a/frontend/src/pages/YichensBoard.jsx b/frontend/src/pages/YichensBoard.jsx index cc866f4..f2a5a6a 100644 --- a/frontend/src/pages/YichensBoard.jsx +++ b/frontend/src/pages/YichensBoard.jsx @@ -65,8 +65,11 @@ export default function YichensBoard() { data = data.filter(p => p.category && !p.category.includes('龙') && !p.category.includes('纪念钞') && !p.category.includes('蛇') && !p.category.includes('马')) } } - setPosts(data) - setTotalPosts(todayStats.total || 0) + // 支持新格式 {posts:[], total:xxx} 或旧格式 [{},{}] + const postsArray = data.posts || data + const totalCount = data.total || todayStats.total || postsArray.length + setPosts(postsArray) + setTotalPosts(totalCount) } catch(e) { console.error('YichensBoard: fetchPosts error', e) setError(e.message) @@ -209,7 +212,7 @@ export default function YichensBoard() {