Paginate /用户列表 at 20 per page
Browse files客户要求超过 20 人翻页。
- config 里 USER_LIST_LIMIT 换成 USER_PAGE_SIZE(默认 20)
- finance.list_users() 支持 skip,配合 count_users() 做分页
- build_user_list_card() 接收页码,标题显示「共 N 人 · 第 X/Y 页」,
名次按全局排名编号(翻页后从 21 接着数,不会每页都从 1 开始)
- 底部给翻页提示 `/用户列表 <下一页>`,最后一页提示回首页
- app.py 的 parse_days 改名 parse_int_arg(现在也用于页码),
页码越界自动钳到 [1, 总页数]
- 卡片函数自身也做了页码钳制,避免调用方传错时显示「第 3/2 页」
已本地验证 25 人分两页的渲染,以及页码 0/1/2/3/99 和空列表的边界。
Co-Authored-By: Claude Code <noreply@anthropic.com>
- app.py +16 -7
- core/config.py +3 -2
- services/finance.py +3 -3
- utils/card_builder.py +19 -7
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import asyncio
|
| 2 |
import json
|
|
|
|
| 3 |
import os
|
| 4 |
import signal
|
| 5 |
import time
|
|
@@ -9,7 +10,7 @@ from aiohttp import web
|
|
| 9 |
from khl import Bot, Message, Event, EventTypes, api
|
| 10 |
from khl.card import CardMessage
|
| 11 |
|
| 12 |
-
from core.config import BOT_TOKEN, CMD_COOLDOWN,
|
| 13 |
from core.database import db
|
| 14 |
from services import finance, checkin
|
| 15 |
from utils import card_builder as cb
|
|
@@ -89,7 +90,11 @@ async def resolve_target(msg: Message, token: str):
|
|
| 89 |
return await finance.resolve_user(token)
|
| 90 |
|
| 91 |
|
| 92 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
if args and str(args[0]).isdigit():
|
| 94 |
return max(1, min(maximum, int(args[0])))
|
| 95 |
return default
|
|
@@ -208,12 +213,16 @@ async def cmd_query(msg: Message, *args):
|
|
| 208 |
|
| 209 |
|
| 210 |
@bot.command(name="用户列表", prefixes=["/"])
|
| 211 |
-
async def cmd_user_list(msg: Message):
|
| 212 |
await touch(msg)
|
| 213 |
# 同 /查询:需求原文未标注管理员权限,暂对所有人开放。
|
| 214 |
total = await finance.count_users()
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
|
| 219 |
# ============================================================
|
|
@@ -245,7 +254,7 @@ async def cmd_checkin_card(msg: Message):
|
|
| 245 |
async def cmd_my_records(msg: Message, *args):
|
| 246 |
await touch(msg)
|
| 247 |
uid = str(msg.author.id)
|
| 248 |
-
days =
|
| 249 |
records = await checkin.get_records(uid, days)
|
| 250 |
await reply_card(msg, cb.build_records_card(_display_name(msg.author), days, records))
|
| 251 |
|
|
@@ -256,7 +265,7 @@ async def cmd_all_records(msg: Message, *args):
|
|
| 256 |
if not permission.can_view_all(msg.author):
|
| 257 |
return await reply_card(msg, cb.build_error("❌ 你没有权限执行该操作"))
|
| 258 |
|
| 259 |
-
days =
|
| 260 |
records = await checkin.get_all_records(days)
|
| 261 |
await reply_card(msg, cb.build_all_records_card(days, records))
|
| 262 |
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import json
|
| 3 |
+
import math
|
| 4 |
import os
|
| 5 |
import signal
|
| 6 |
import time
|
|
|
|
| 10 |
from khl import Bot, Message, Event, EventTypes, api
|
| 11 |
from khl.card import CardMessage
|
| 12 |
|
| 13 |
+
from core.config import BOT_TOKEN, CMD_COOLDOWN, USER_PAGE_SIZE, logger
|
| 14 |
from core.database import db
|
| 15 |
from services import finance, checkin
|
| 16 |
from utils import card_builder as cb
|
|
|
|
| 90 |
return await finance.resolve_user(token)
|
| 91 |
|
| 92 |
|
| 93 |
+
def parse_int_arg(args, default: int, maximum: int) -> int:
|
| 94 |
+
"""从命令参数里取一个正整数,非法或超范围就夹到 [1, maximum]。
|
| 95 |
+
|
| 96 |
+
/打卡记录 用天数上限 90,/用户列表 用页码上限(总页数)。
|
| 97 |
+
"""
|
| 98 |
if args and str(args[0]).isdigit():
|
| 99 |
return max(1, min(maximum, int(args[0])))
|
| 100 |
return default
|
|
|
|
| 213 |
|
| 214 |
|
| 215 |
@bot.command(name="用户列表", prefixes=["/"])
|
| 216 |
+
async def cmd_user_list(msg: Message, *args):
|
| 217 |
await touch(msg)
|
| 218 |
# 同 /查询:需求原文未标注管理员权限,暂对所有人开放。
|
| 219 |
total = await finance.count_users()
|
| 220 |
+
pages = max(1, math.ceil(total / USER_PAGE_SIZE)) if total else 1
|
| 221 |
+
page = parse_int_arg(args, default=1, maximum=pages)
|
| 222 |
+
|
| 223 |
+
users = await finance.list_users(skip=(page - 1) * USER_PAGE_SIZE,
|
| 224 |
+
limit=USER_PAGE_SIZE)
|
| 225 |
+
await reply_card(msg, cb.build_user_list_card(users, total, page, USER_PAGE_SIZE))
|
| 226 |
|
| 227 |
|
| 228 |
# ============================================================
|
|
|
|
| 254 |
async def cmd_my_records(msg: Message, *args):
|
| 255 |
await touch(msg)
|
| 256 |
uid = str(msg.author.id)
|
| 257 |
+
days = parse_int_arg(args, default=7, maximum=90)
|
| 258 |
records = await checkin.get_records(uid, days)
|
| 259 |
await reply_card(msg, cb.build_records_card(_display_name(msg.author), days, records))
|
| 260 |
|
|
|
|
| 265 |
if not permission.can_view_all(msg.author):
|
| 266 |
return await reply_card(msg, cb.build_error("❌ 你没有权限执行该操作"))
|
| 267 |
|
| 268 |
+
days = parse_int_arg(args, default=7, maximum=90)
|
| 269 |
records = await checkin.get_all_records(days)
|
| 270 |
await reply_card(msg, cb.build_all_records_card(days, records))
|
| 271 |
|
core/config.py
CHANGED
|
@@ -62,8 +62,9 @@ MAX_SHIFT_MINUTES = int(os.environ.get("MAX_SHIFT_MINUTES", str(16 * 60)))
|
|
| 62 |
# 中国没有夏令时,固定 +8 完全等价。
|
| 63 |
CST = timezone(timedelta(hours=8))
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
# 命令冷却(秒),防止刷屏触发 KOOK 429
|
| 69 |
CMD_COOLDOWN = float(os.environ.get("CMD_COOLDOWN", "1.5"))
|
|
|
|
| 62 |
# 中国没有夏令时,固定 +8 完全等价。
|
| 63 |
CST = timezone(timedelta(hours=8))
|
| 64 |
|
| 65 |
+
# /用户列表 每页显示多少人。客户要求超过 20 人翻页。
|
| 66 |
+
# 实际卡片的行数受 KOOK 单条消息 8000 字符上限约束,20 行很安全。
|
| 67 |
+
USER_PAGE_SIZE = int(os.environ.get("USER_PAGE_SIZE", "20"))
|
| 68 |
|
| 69 |
# 命令冷却(秒),防止刷屏触发 KOOK 429
|
| 70 |
CMD_COOLDOWN = float(os.environ.get("CMD_COOLDOWN", "1.5"))
|
services/finance.py
CHANGED
|
@@ -153,9 +153,9 @@ async def get_balance(user_id: str):
|
|
| 153 |
return (doc or {}).get("balance", 0)
|
| 154 |
|
| 155 |
|
| 156 |
-
async def list_users(limit: int =
|
| 157 |
-
"""按余额倒序返回
|
| 158 |
-
cursor = db.users.find({}).sort("balance", -1).limit(limit)
|
| 159 |
return [doc async for doc in cursor]
|
| 160 |
|
| 161 |
|
|
|
|
| 153 |
return (doc or {}).get("balance", 0)
|
| 154 |
|
| 155 |
|
| 156 |
+
async def list_users(skip: int = 0, limit: int = 20):
|
| 157 |
+
"""按余额倒序返回一页用户。"""
|
| 158 |
+
cursor = db.users.find({}).sort("balance", -1).skip(skip).limit(limit)
|
| 159 |
return [doc async for doc in cursor]
|
| 160 |
|
| 161 |
|
utils/card_builder.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
"""卡片构建。所有对外的展示都在这里,业务代码只负责算数据。"""
|
|
|
|
|
|
|
| 2 |
from khl.card import Card, Module, Element, Types
|
| 3 |
|
| 4 |
from utils.money import fmt, fmt_plain
|
|
@@ -211,20 +213,30 @@ def build_adjust_card(shop_label: str, name: str, amount: int,
|
|
| 211 |
"✅" if is_add else "💸")
|
| 212 |
|
| 213 |
|
| 214 |
-
def build_user_list_card(users: list, total: int,
|
|
|
|
|
|
|
|
|
|
| 215 |
card = Card(color=COLOR_MAIN)
|
| 216 |
-
card.append(Module.Header(f"👥 用户余额(共 {total} 人)"))
|
|
|
|
| 217 |
if not users:
|
| 218 |
-
card.append(Module.Section(Element.Text(
|
|
|
|
|
|
|
| 219 |
return card
|
| 220 |
|
|
|
|
| 221 |
lines = []
|
| 222 |
-
for
|
|
|
|
| 223 |
name = doc.get("name") or doc.get("_id", "?")
|
| 224 |
-
lines.append(f"`{
|
| 225 |
|
| 226 |
-
if
|
| 227 |
-
lines.append(
|
|
|
|
|
|
|
| 228 |
|
| 229 |
card.append(Module.Section(Element.Text("\n".join(lines), type=Types.Text.KMD)))
|
| 230 |
return card
|
|
|
|
| 1 |
"""卡片构建。所有对外的展示都在这里,业务代码只负责算数据。"""
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
from khl.card import Card, Module, Element, Types
|
| 5 |
|
| 6 |
from utils.money import fmt, fmt_plain
|
|
|
|
| 213 |
"✅" if is_add else "💸")
|
| 214 |
|
| 215 |
|
| 216 |
+
def build_user_list_card(users: list, total: int, page: int, page_size: int):
|
| 217 |
+
"""分页展示用户余额。名次按全局排名编号,翻页后从 page*size+1 接着数。"""
|
| 218 |
+
pages = max(1, math.ceil(total / page_size)) if total else 1
|
| 219 |
+
page = max(1, min(page, pages)) # 防御:调用方传了越界页码时别显示成「第 3/2 页」
|
| 220 |
card = Card(color=COLOR_MAIN)
|
| 221 |
+
card.append(Module.Header(f"👥 用户余额(共 {total} 人 · 第 {page}/{pages} 页)"))
|
| 222 |
+
|
| 223 |
if not users:
|
| 224 |
+
card.append(Module.Section(Element.Text(
|
| 225 |
+
"还没有任何用户" if total == 0 else f"第 {page} 页是空的",
|
| 226 |
+
type=Types.Text.KMD)))
|
| 227 |
return card
|
| 228 |
|
| 229 |
+
start = (page - 1) * page_size
|
| 230 |
lines = []
|
| 231 |
+
for offset, doc in enumerate(users):
|
| 232 |
+
rank = start + offset + 1
|
| 233 |
name = doc.get("name") or doc.get("_id", "?")
|
| 234 |
+
lines.append(f"`{rank:>3}` {name} — **{fmt_plain(doc.get('balance', 0))}**")
|
| 235 |
|
| 236 |
+
if pages > 1:
|
| 237 |
+
lines.append("")
|
| 238 |
+
lines.append(f"*翻页:`/用户列表 {page + 1}`*" if page < pages
|
| 239 |
+
else f"*翻页:`/用户列表 1` 回到首页*")
|
| 240 |
|
| 241 |
card.append(Module.Section(Element.Text("\n".join(lines), type=Types.Text.KMD)))
|
| 242 |
return card
|