fix(排行榜): top 参数吃掉了分页,全服 Top100 每页都是同样 100 条
/rankings/users 里 top>0 时直接 limit(top).offset(0),limit 和 offset 全被 覆盖;total 又是全量人数,于是分页器算出几十页、页页内容相同。 改成 top 只当上限:total 按 top 截断,分页在这 N 条之内正常推进,越界页 返回空且不发 SQL。top=0(管理端)行为不变。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -163,10 +163,14 @@ accountRoutes.get("/rankings/users", async (c) => {
|
|||||||
)
|
)
|
||||||
const [totalRow] = await db.select({ value: count() }).from(schema.userProfile)
|
const [totalRow] = await db.select({ value: count() }).from(schema.userProfile)
|
||||||
.innerJoin(schema.user, eq(schema.userProfile.userId, schema.user.id)).where(where)
|
.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)
|
.innerJoin(schema.user, eq(schema.userProfile.userId, schema.user.id)).where(where)
|
||||||
.orderBy(desc(schema.userProfile.acceptedNumber), asc(schema.userProfile.submissionNumber))
|
.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({
|
const results = rows.map(({ profile, user }) => rankProfileSchema.parse({
|
||||||
id: profile.id,
|
id: profile.id,
|
||||||
user: sampleUser(user, profile.realName),
|
user: sampleUser(user, profile.realName),
|
||||||
@@ -174,7 +178,7 @@ accountRoutes.get("/rankings/users", async (c) => {
|
|||||||
submissionNumber: profile.submissionNumber,
|
submissionNumber: profile.submissionNumber,
|
||||||
mood: profile.mood,
|
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) => {
|
accountRoutes.get("/rankings/activity", async (c) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user