v1.2.74 - 搜索返回总数+分页修复
This commit is contained in:
parent
e5a6a5928e
commit
49b68e802e
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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() {
|
|||
|
||||
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '12px 16px', background: '#1e293b', borderRadius: 12, marginTop: 16 }}>
|
||||
<div style={{ color: '#9ca3af', fontSize: 12 }}>
|
||||
共 {totalPosts} 条帖子,{Math.ceil(totalPosts / 390)} 页,当前第 {page} 页
|
||||
共 {totalPosts} 条结果,{Math.ceil(totalPosts / 390)} 页,当前第 {page} 页
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 8 }}>
|
||||
{page > 1 ? (
|
||||
|
|
@ -217,7 +220,7 @@ export default function YichensBoard() {
|
|||
) : (
|
||||
<span style={{ padding: '6px 12px', borderRadius: 6, background: '#374151', color: '#6b7280', fontSize: 12 }}>上一页</span>
|
||||
)}
|
||||
{posts.length >= 390 ? (
|
||||
{posts.length >= 390 && totalPosts > page * 390 ? (
|
||||
<button onClick={() => { const newPage = page + 1; setPage(newPage); fetchPosts(newPage, categoryFilter, searchKeyword) }} style={{ padding: '6px 12px', borderRadius: 6, border: 'none', background: '#3b82f6', color: '#fff', cursor: 'pointer', fontSize: 12 }}>下一页</button>
|
||||
) : (
|
||||
<span style={{ padding: '6px 12px', borderRadius: 6, background: '#374151', color: '#6b7280', fontSize: 12 }}>下一页</span>
|
||||
|
|
|
|||
Loading…
Reference in New Issue