From 02ac24ba886515692bcc1febdea61ac27f815d83 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Sun, 9 Aug 2026 21:14:18 +0800 Subject: [PATCH] feat: port pagination and drag-drop upload from upstream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - GET /api/posts now supports page/limit, returns {data, pagination} - Feed: pagination controls (‹ page numbers ›), reset to page 1 on sort change - NewPost: dashed drop zone with click/drag-drop image upload, 5MB limit - UserProfile adapts to paginated response --- app/api/posts/route.js | 35 +++++++++++++++++-------- components/Feed.js | 55 +++++++++++++++++++++++++++++++++++---- components/NewPost.js | 37 ++++++++++++++++++++++---- components/UserProfile.js | 4 +-- lib/api.js | 4 +-- 5 files changed, 110 insertions(+), 25 deletions(-) diff --git a/app/api/posts/route.js b/app/api/posts/route.js index 21cda61..f017e8e 100644 --- a/app/api/posts/route.js +++ b/app/api/posts/route.js @@ -1,4 +1,4 @@ -// GET /api/posts, POST /api/posts — 支持 ?sort= & ?user_id= 筛选 +// GET /api/posts, POST /api/posts — 支持 ?sort= & ?user_id= & 分页 import pool from '@/lib/db'; import { getUser } from '@/lib/auth'; @@ -6,9 +6,22 @@ export async function GET(req) { const url = new URL(req.url); const sort = url.searchParams.get('sort') === 'hot' ? 'updated_at' : 'created_at'; const userId = url.searchParams.get('user_id'); + const page = parseInt(url.searchParams.get('page')) || 1; + const limit = parseInt(url.searchParams.get('limit')) || 20; + const offset = (page - 1) * limit; try { - let query = ` + const params = []; + let where = ''; + if (userId) { + where = ' WHERE p.user_id = $1'; + params.push(userId); + } + + const countResult = await pool.query(`SELECT COUNT(*) as total FROM posts p${where}`, params); + const total = parseInt(countResult.rows[0]?.total || 0); + + const r = await pool.query(` SELECT p.*, u.username, @@ -16,15 +29,15 @@ export async function GET(req) { (SELECT COUNT(*) FROM replies WHERE post_id = p.id) as reply_count FROM posts p JOIN users u ON p.user_id = u.id - `; - const params = []; - if (userId) { - query += ' WHERE p.user_id = $1'; - params.push(userId); - } - query += ` ORDER BY ${sort} DESC`; - const r = await pool.query(query, params); - return Response.json(r.rows); + ${where} + ORDER BY ${sort} DESC + LIMIT $${params.length + 1} OFFSET $${params.length + 2} + `, [...params, limit, offset]); + + return Response.json({ + data: r.rows, + pagination: { page, limit, total, totalPages: Math.ceil(total / limit) }, + }); } catch { return Response.json({ error: '服务器错误' }, { status: 500 }); } diff --git a/components/Feed.js b/components/Feed.js index c97c503..a6741d6 100644 --- a/components/Feed.js +++ b/components/Feed.js @@ -15,26 +15,34 @@ export default function Feed({ onOpenModal, onReport }) { const { currentUser, sort, setSort, openPost, openUser, navigate, refreshKey } = useApp(); const [posts, setPosts] = useState(null); const [error, setError] = useState(null); + const [page, setPage] = useState(1); + const [totalPages, setTotalPages] = useState(1); + const PAGE_SIZE = 20; - const load = useCallback(async () => { + const load = useCallback(async (targetPage) => { setPosts(null); setError(null); try { - const data = await API.getPosts(sort); - setPosts(data || []); + const result = await API.getPosts(sort, null, targetPage, PAGE_SIZE); + setPosts(result.data || []); + setTotalPages(result.pagination?.totalPages || 1); + setPage(result.pagination?.page || 1); } catch (err) { setError(err.message); setPosts([]); } }, [sort, refreshKey]); - useEffect(() => { load(); }, [load]); + useEffect(() => { load(page); }, [load]); + + // 排序切换时回第一页 + useEffect(() => { setPage(1); }, [sort]); const handleDelete = async (postId) => { if (!confirm('确定要删除这条帖子吗?')) return; try { await API.deletePost(postId); - load(); + load(page); } catch (err) { alert('删除失败:' + err.message); } @@ -113,6 +121,43 @@ export default function Feed({ onOpenModal, onReport }) { }) )} + {totalPages > 1 && posts && posts.length > 0 && ( +