diff --git a/server/local-auth.mjs b/server/local-auth.mjs index 1818b27..c5508c9 100644 --- a/server/local-auth.mjs +++ b/server/local-auth.mjs @@ -229,9 +229,26 @@ export function createLocalUserAuth(pool) { async function getAdminSummary() { const [[row]] = await pool.execute( - `SELECT COUNT(*) AS total, SUM(CASE WHEN balance_cents < 0 THEN 1 ELSE 0 END) AS lowBalance FROM auth_users`, + `SELECT + COUNT(*) AS total, + SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) AS active, + SUM(CASE WHEN balance_cents < 0 THEN 1 ELSE 0 END) AS lowBalance, + SUM(balance_cents) AS totalBalanceCents + FROM auth_users`, ); - return { users: { total: Number(row.total ?? 0), lowBalance: Number(row.lowBalance ?? 0) }, routes: { total: 0, active: 0 } }; + return { + users: { + total: Number(row.total ?? 0), + active: Number(row.active ?? 0), + lowBalance: Number(row.lowBalance ?? 0), + totalBalanceCents: Number(row.totalBalanceCents ?? 0), + }, + usage24h: { count: 0, costCents: 0 }, + lowBalanceUsers: [], + recentUsage: [], + recentLedger: [], + llm: { keyCount: 0, selectedKeyName: null, globalModel: null }, + }; } return { diff --git a/src/admin/pages/DashboardPage.tsx b/src/admin/pages/DashboardPage.tsx index 5bd46c7..14fbd94 100644 --- a/src/admin/pages/DashboardPage.tsx +++ b/src/admin/pages/DashboardPage.tsx @@ -5,10 +5,10 @@ import type { AdminDashboardSummary } from '../../types'; import { formatTime, formatYuan } from '../utils/format'; const QUICK_LINKS = [ - { to: '/admin/users', label: '用户管理', desc: '创建账号、启用禁用' }, - { to: '/admin/billing/recharge', label: '充值', desc: '为用户账户充值' }, - { to: '/admin/providers', label: 'LLM Provider', desc: '模型 Key 与全局配置' }, - { to: '/admin/capabilities', label: '能力权限', desc: '扩展与工具开关' }, + { to: '/users', label: '用户管理', desc: '创建账号、启用禁用' }, + { to: '/billing/recharge', label: '充值', desc: '为用户账户充值' }, + { to: '/providers', label: 'LLM Provider', desc: '模型 Key 与全局配置' }, + { to: '/capabilities', label: '能力权限', desc: '扩展与工具开关' }, ] as const; export function DashboardPage() { @@ -32,6 +32,15 @@ export function DashboardPage() { void load(); }, [load]); + const safeSummary: AdminDashboardSummary = summary ?? { + users: { total: 0, active: 0, lowBalance: 0, totalBalanceCents: 0 }, + usage24h: { count: 0, costCents: 0 }, + lowBalanceUsers: [], + recentUsage: [], + recentLedger: [], + llm: null, + }; + return (
@@ -44,45 +53,45 @@ export function DashboardPage() {
用户总数
-
{loading ? '—' : summary?.users.total}
-
活跃 {loading ? '—' : summary?.users.active}
+
{loading ? '—' : safeSummary.users.total}
+
活跃 {loading ? '—' : safeSummary.users.active}
余额不足
-
{loading ? '—' : summary?.users.lowBalance}
+
{loading ? '—' : safeSummary.users.lowBalance}
普通用户余额 ≤ ¥0
账户总余额
- {loading ? '—' : `¥${formatYuan(summary?.users.totalBalanceCents ?? 0)}`} + {loading ? '—' : `¥${formatYuan(safeSummary.users.totalBalanceCents)}`}
24h 用量
-
{loading ? '—' : summary?.usage24h.count}
+
{loading ? '—' : safeSummary.usage24h.count}
- 扣费 ¥{loading ? '—' : formatYuan(summary?.usage24h.costCents ?? 0)} + 扣费 ¥{loading ? '—' : formatYuan(safeSummary.usage24h.costCents)}
LLM Provider
-
{loading ? '—' : (summary?.llm?.keyCount ?? '—')}
+
{loading ? '—' : (safeSummary.llm?.keyCount ?? '—')}
{loading ? '—' - : summary?.llm?.selectedKeyName - ? `当前 ${summary.llm.selectedKeyName}` + : safeSummary.llm?.selectedKeyName + ? `当前 ${safeSummary.llm.selectedKeyName}` : '未配置'}
- {!loading && summary && summary.lowBalanceUsers.length > 0 && ( + {!loading && safeSummary.lowBalanceUsers.length > 0 && (

余额告警

- + 去充值
@@ -96,7 +105,7 @@ export function DashboardPage() { - {summary.lowBalanceUsers.map((user) => ( + {safeSummary.lowBalanceUsers.map((user) => (
{user.displayName}
@@ -105,7 +114,7 @@ export function DashboardPage() { ¥{formatYuan(user.balanceCents)} 用户详情 @@ -131,12 +140,12 @@ export function DashboardPage() {
- {!loading && summary && ( + {!loading && ( <>

最近用量

- + 查看全部
@@ -151,14 +160,14 @@ export function DashboardPage() { - {summary.recentUsage.length === 0 ? ( + {safeSummary.recentUsage.length === 0 ? ( 暂无记录 ) : ( - summary.recentUsage.map((row) => ( + safeSummary.recentUsage.map((row) => ( {formatTime(row.createdAt)} @{row.username} @@ -177,7 +186,7 @@ export function DashboardPage() {

最近流水

- + 查看全部
@@ -192,14 +201,14 @@ export function DashboardPage() { - {summary.recentLedger.length === 0 ? ( + {safeSummary.recentLedger.length === 0 ? ( 暂无记录 ) : ( - summary.recentLedger.map((row) => ( + safeSummary.recentLedger.map((row) => ( {formatTime(row.createdAt)} @{row.username}