From 36e4ac2f782b66e4713ace1bf417044dd5eefdb3 Mon Sep 17 00:00:00 2001 From: yuetsh <517252939@qq.com> Date: Sun, 16 Aug 2026 10:08:43 -0600 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=92=E8=A1=8C=E6=A6=9C):=20top=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=90=83=E6=8E=89=E4=BA=86=E5=88=86=E9=A1=B5?= =?UTF-8?q?=EF=BC=8C=E5=85=A8=E6=9C=8D=20Top100=20=E6=AF=8F=E9=A1=B5?= =?UTF-8?q?=E9=83=BD=E6=98=AF=E5=90=8C=E6=A0=B7=20100=20=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /rankings/users 里 top>0 时直接 limit(top).offset(0),limit 和 offset 全被 覆盖;total 又是全量人数,于是分页器算出几十页、页页内容相同。 改成 top 只当上限:total 按 top 截断,分页在这 N 条之内正常推进,越界页 返回空且不发 SQL。top=0(管理端)行为不变。 Co-Authored-By: Claude Opus 5 --- apps/api/src/routes/account.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/api/src/routes/account.ts b/apps/api/src/routes/account.ts index 532ff50..df0360d 100644 --- a/apps/api/src/routes/account.ts +++ b/apps/api/src/routes/account.ts @@ -163,10 +163,14 @@ accountRoutes.get("/rankings/users", async (c) => { ) const [totalRow] = await db.select({ value: count() }).from(schema.userProfile) .innerJoin(schema.user, eq(schema.userProfile.userId, schema.user.id)).where(where) - const rows = await db.select({ profile: schema.userProfile, user: schema.user }).from(schema.userProfile) + // top 只是「榜单取前 N 名」的上限,分页仍要在这 N 条之内生效: + // 否则 top=100 时每页都返回同样的 100 条,而 total 又是全量人数,翻页翻不动 + const total = top > 0 ? Math.min(totalRow?.value ?? 0, top) : (totalRow?.value ?? 0) + const pageLimit = top > 0 ? Math.max(0, Math.min(limit, top - offset)) : limit + const rows = pageLimit === 0 ? [] : await db.select({ profile: schema.userProfile, user: schema.user }).from(schema.userProfile) .innerJoin(schema.user, eq(schema.userProfile.userId, schema.user.id)).where(where) .orderBy(desc(schema.userProfile.acceptedNumber), asc(schema.userProfile.submissionNumber)) - .limit(top > 0 ? Math.min(top, 250) : limit).offset(top > 0 ? 0 : offset) + .limit(pageLimit).offset(offset) const results = rows.map(({ profile, user }) => rankProfileSchema.parse({ id: profile.id, user: sampleUser(user, profile.realName), @@ -174,7 +178,7 @@ accountRoutes.get("/rankings/users", async (c) => { submissionNumber: profile.submissionNumber, mood: profile.mood, })) - return success(c, userRankSchema.parse({ results, total: totalRow?.value ?? 0 })) + return success(c, userRankSchema.parse({ results, total })) }) accountRoutes.get("/rankings/activity", async (c) => {