From 752882a0220b0632a7a4b49ae1cf80d2dbad70d4 Mon Sep 17 00:00:00 2001 From: shaw Date: Tue, 6 Jan 2026 10:13:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Token=20=E7=BB=9F=E8=AE=A1=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20M=20=E5=8D=95=E4=BD=8D=E5=B9=B6=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=20lint=20=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 用户仪表盘 Token 统计卡片支持 K/M 单位自动切换 - 更新 formatTokensK 工具函数支持百万级显示 - 修复 setup.go 中未检查返回值的 errcheck 错误 --- backend/internal/setup/setup.go | 4 ++-- .../src/components/user/dashboard/UserDashboardStats.vue | 6 +++++- frontend/src/utils/format.ts | 8 +++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/internal/setup/setup.go b/backend/internal/setup/setup.go index 4b1ca7b2..65118161 100644 --- a/backend/internal/setup/setup.go +++ b/backend/internal/setup/setup.go @@ -39,8 +39,8 @@ func GetDataDir() string { // Try to check if writable by creating a temp file testFile := dockerDataDir + "/.write_test" if f, err := os.Create(testFile); err == nil { - f.Close() - os.Remove(testFile) + _ = f.Close() + _ = os.Remove(testFile) return dockerDataDir } } diff --git a/frontend/src/components/user/dashboard/UserDashboardStats.vue b/frontend/src/components/user/dashboard/UserDashboardStats.vue index d375ba88..dfba3a51 100644 --- a/frontend/src/components/user/dashboard/UserDashboardStats.vue +++ b/frontend/src/components/user/dashboard/UserDashboardStats.vue @@ -153,6 +153,10 @@ const formatBalance = (b: number) => const formatNumber = (n: number) => n.toLocaleString() const formatCost = (c: number) => c.toFixed(4) -const formatTokens = (t: number) => (t >= 1000 ? `${(t / 1000).toFixed(1)}K` : t.toString()) +const formatTokens = (t: number) => { + if (t >= 1_000_000) return `${(t / 1_000_000).toFixed(1)}M` + if (t >= 1000) return `${(t / 1000).toFixed(1)}K` + return t.toString() +} const formatDuration = (ms: number) => ms >= 1000 ? `${(ms / 1000).toFixed(2)}s` : `${ms.toFixed(0)}ms` diff --git a/frontend/src/utils/format.ts b/frontend/src/utils/format.ts index d54e5015..2dc8da4e 100644 --- a/frontend/src/utils/format.ts +++ b/frontend/src/utils/format.ts @@ -174,10 +174,12 @@ export function formatCostFixed(amount: number, fractionDigits: number = 4): str } /** - * 格式化 token 数量(>=1000 显示为 K,保留 1 位小数) + * 格式化 token 数量(>=1M 显示为 M,>=1K 显示为 K,保留 1 位小数) * @param tokens token 数量 - * @returns 格式化后的字符串,如 "950", "1.2K" + * @returns 格式化后的字符串,如 "950", "1.2K", "3.5M" */ export function formatTokensK(tokens: number): string { - return tokens >= 1000 ? `${(tokens / 1000).toFixed(1)}K` : tokens.toString() + if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M` + if (tokens >= 1000) return `${(tokens / 1000).toFixed(1)}K` + return tokens.toString() }