From d830097b003fffbc1219ef21ff9176296b624d13 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 5 Nov 2026 19:35:30 +0800 Subject: [PATCH] Show Beijing time across admin UI and ntfy payloads. Format ISO/unix stamps with Asia/Shanghai so join leads and alerts match China local clock. Co-authored-by: Cursor --- apps/admin/src/components/wxUi.tsx | 17 ++++++++- apps/api/services/ebook_svc/services/ntfy.py | 15 ++++++++ apps/api/services/ebook_svc/services/track.py | 1 + apps/com/admin/src/lib/beijingTime.ts | 38 +++++++++++++++++++ apps/com/admin/src/pages/Events.tsx | 5 +-- apps/com/admin/src/pages/Operations.tsx | 3 +- apps/com/admin/src/pages/Orders.tsx | 5 +-- apps/com/admin/src/pages/Pricing.tsx | 7 +++- apps/com/admin/src/pages/Switches.tsx | 7 +++- apps/com/admin/src/pages/Users.tsx | 5 +-- apps/com/admin/src/pages/socialMeta.ts | 11 ++---- src/lib/ntfy-rich.ts | 5 ++- 12 files changed, 97 insertions(+), 22 deletions(-) create mode 100644 apps/com/admin/src/lib/beijingTime.ts diff --git a/apps/admin/src/components/wxUi.tsx b/apps/admin/src/components/wxUi.tsx index 09ac15c..08f1058 100644 --- a/apps/admin/src/components/wxUi.tsx +++ b/apps/admin/src/components/wxUi.tsx @@ -126,9 +126,24 @@ export function fmtYuan(fen: number) { return `¥${(Number(fen || 0) / 100).toFixed(2)}` } +/** 管理后台统一北京时间(Asia/Shanghai)。 */ export function fmtTime(sec?: number | null) { if (!sec) return '—' - return new Date(sec * 1000).toLocaleString() + const ms = sec < 1e12 ? sec * 1000 : sec + try { + return new Intl.DateTimeFormat('zh-CN', { + timeZone: 'Asia/Shanghai', + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: false, + }).format(new Date(ms)) + } catch { + return new Date(ms).toISOString().slice(0, 19).replace('T', ' ') + } } export function shortId(id?: string | null, keep = 8) { diff --git a/apps/api/services/ebook_svc/services/ntfy.py b/apps/api/services/ebook_svc/services/ntfy.py index 6972ecc..79b792a 100644 --- a/apps/api/services/ebook_svc/services/ntfy.py +++ b/apps/api/services/ebook_svc/services/ntfy.py @@ -2,7 +2,9 @@ from __future__ import annotations import logging +from datetime import datetime from typing import Any +from zoneinfo import ZoneInfo import httpx @@ -11,6 +13,19 @@ from ebook_svc.config import settings logger = logging.getLogger(__name__) DEFAULT_ICON = "https://nomadro.com/icon-192.png" +_BEIJING = ZoneInfo("Asia/Shanghai") + + +def format_beijing_time(ts: float | int | None = None) -> str: + """Admin / ntfy 统一北京时间。""" + try: + if ts is None: + d = datetime.now(_BEIJING) + else: + d = datetime.fromtimestamp(float(ts), tz=_BEIJING) + return d.strftime("%Y-%m-%d %H:%M:%S") + except Exception: + return datetime.now(_BEIJING).strftime("%Y-%m-%d %H:%M:%S") async def notify( diff --git a/apps/api/services/ebook_svc/services/track.py b/apps/api/services/ebook_svc/services/track.py index 81df773..7e4aba2 100644 --- a/apps/api/services/ebook_svc/services/track.py +++ b/apps/api/services/ebook_svc/services/track.py @@ -242,6 +242,7 @@ def format_event_message(ev: dict[str, Any], *, openid: str | None, client: str bullets: list[tuple[str, str]] = [ ("用户", _short_openid(openid)), ("渠道", _channel_label(client)), + ("时间", f"{ntfy_svc.format_beijing_time(ev.get('ts'))}(北京时间)"), ] if ev.get("page"): bullets.append(("页面", str(ev["page"]))) diff --git a/apps/com/admin/src/lib/beijingTime.ts b/apps/com/admin/src/lib/beijingTime.ts new file mode 100644 index 0000000..b8c827b --- /dev/null +++ b/apps/com/admin/src/lib/beijingTime.ts @@ -0,0 +1,38 @@ +/** Admin UI clock: always Asia/Shanghai (北京时间). */ + +const BEIJING: Intl.DateTimeFormatOptions = { + timeZone: 'Asia/Shanghai', + year: 'numeric', + month: '2-digit', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + hour12: false, +} + +function toDate(v: unknown): Date | null { + if (v == null || v === '') return null + if (typeof v === 'number' && Number.isFinite(v)) { + return new Date(v < 1e12 ? v * 1000 : v) + } + const s = String(v).trim() + if (!s) return null + if (/^\d+$/.test(s)) { + const n = Number(s) + return new Date(n < 1e12 ? n * 1000 : n) + } + const d = new Date(s) + return Number.isNaN(d.getTime()) ? null : d +} + +/** Format any ISO / unix / Date-like value as 北京时间. */ +export function formatBeijingTime(v: unknown): string { + const d = toDate(v) + if (!d) return '—' + try { + return new Intl.DateTimeFormat('zh-CN', BEIJING).format(d) + } catch { + return d.toISOString().slice(0, 19).replace('T', ' ') + } +} diff --git a/apps/com/admin/src/pages/Events.tsx b/apps/com/admin/src/pages/Events.tsx index 9c6ebc7..2ffdefc 100644 --- a/apps/com/admin/src/pages/Events.tsx +++ b/apps/com/admin/src/pages/Events.tsx @@ -1,5 +1,6 @@ import { useCallback, useEffect, useMemo, useState } from 'react' import { api } from '../api/client' +import { formatBeijingTime } from '../lib/beijingTime' type TrackEvent = { id: number @@ -54,9 +55,7 @@ function clientLabel(c: string) { } function fmtTime(ts: number) { - if (!ts) return '—' - const d = new Date(ts < 1e12 ? ts * 1000 : ts) - return d.toLocaleString('zh-CN', { hour12: false }) + return formatBeijingTime(ts) } function shortId(id: string, n = 8) { diff --git a/apps/com/admin/src/pages/Operations.tsx b/apps/com/admin/src/pages/Operations.tsx index 8450ef9..704e7b9 100644 --- a/apps/com/admin/src/pages/Operations.tsx +++ b/apps/com/admin/src/pages/Operations.tsx @@ -1,5 +1,6 @@ import { useCallback, useEffect, useState } from 'react' import { api } from '../api/client' +import { formatBeijingTime } from '../lib/beijingTime' type Usage = { totalBytes: number; usedBytes: number; usedPercent: number } type Health = { @@ -35,7 +36,7 @@ export default function Operations() { {error &&
{error} {data ? '下方保留上次检查结果。' : ''}
} {data ? <> -
{error ? '状态更新失败' : data.ok ? '核心服务运行正常' : '部分服务需要检查'}

上次检查:{new Date(data.checkedAt).toLocaleString('zh-CN', { hour12: false })}

+
{error ? '状态更新失败' : data.ok ? '核心服务运行正常' : '部分服务需要检查'}

上次检查:{formatBeijingTime(data.checkedAt)}(北京时间)

{data.checks.map(check =>
{check.ok ? '可用' : '异常'}

{check.name}

{check.latencyMs} ms

{check.status ? `HTTP ${check.status}` : '连接失败或检查超时'}

)}
{(['memory', 'disk'] as const).map(key => { const usage = data.resources[key] diff --git a/apps/com/admin/src/pages/Orders.tsx b/apps/com/admin/src/pages/Orders.tsx index f510f4a..383584b 100644 --- a/apps/com/admin/src/pages/Orders.tsx +++ b/apps/com/admin/src/pages/Orders.tsx @@ -1,5 +1,6 @@ import { useCallback, useEffect, useMemo, useState } from 'react' import { api } from '../api/client' +import { formatBeijingTime } from '../lib/beijingTime' import { orderState } from '../lib/orders' import { downloadCsv } from '../lib/csv' @@ -17,9 +18,7 @@ type Order = { type StatusTab = 'all' | 'paid' | 'pending' | 'other' function fmtTime(ts: number) { - if (!ts) return '—' - const d = new Date(ts < 1e12 ? ts * 1000 : ts) - return d.toLocaleString('zh-CN', { hour12: false }) + return formatBeijingTime(ts) } function fmtYuan(fen: number) { diff --git a/apps/com/admin/src/pages/Pricing.tsx b/apps/com/admin/src/pages/Pricing.tsx index 8001a61..a9dcab0 100644 --- a/apps/com/admin/src/pages/Pricing.tsx +++ b/apps/com/admin/src/pages/Pricing.tsx @@ -5,6 +5,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react' import { Link, useParams } from 'react-router-dom' import { api } from '../api/client' +import { formatBeijingTime } from '../lib/beijingTime' import { useConfigLive } from '../lib/useConfigLive' import { useDraft } from '../lib/useDraft' import UnsavedChanges from '../components/UnsavedChanges' @@ -205,7 +206,11 @@ export default function Pricing() { {cfg.flags?.payEnabled !== false ? '支付开' : '支付关'} v{cfg.version ?? '—'} - {cfg.updatedAt ? {cfg.updatedAt} : null} + {cfg.updatedAt ? ( + + {formatBeijingTime(cfg.updatedAt)} + + ) : null}
diff --git a/apps/com/admin/src/pages/Switches.tsx b/apps/com/admin/src/pages/Switches.tsx index f674528..d620f96 100644 --- a/apps/com/admin/src/pages/Switches.tsx +++ b/apps/com/admin/src/pages/Switches.tsx @@ -5,6 +5,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react' import { Link, useParams } from 'react-router-dom' import { api } from '../api/client' +import { formatBeijingTime } from '../lib/beijingTime' import { useConfigLive } from '../lib/useConfigLive' import { useDraft } from '../lib/useDraft' import UnsavedChanges from '../components/UnsavedChanges' @@ -260,7 +261,11 @@ export default function Switches() { ? '当前免费' : `现价 ¥${((Number(cfg.copy?.vipPriceFen) || 0) / 100).toFixed((Number(cfg.copy?.vipPriceFen) || 0) % 100 === 0 ? 0 : 2)}`} - {cfg.updatedAt ? {cfg.updatedAt} : null} + {cfg.updatedAt ? ( + + {formatBeijingTime(cfg.updatedAt)} + + ) : null} diff --git a/apps/com/admin/src/pages/Users.tsx b/apps/com/admin/src/pages/Users.tsx index 621e168..34a907c 100644 --- a/apps/com/admin/src/pages/Users.tsx +++ b/apps/com/admin/src/pages/Users.tsx @@ -1,5 +1,6 @@ import { useCallback, useEffect, useMemo, useState } from 'react' import { api } from '../api/client' +import { formatBeijingTime } from '../lib/beijingTime' type UnlockRow = { book_id: string; unlocked_at: number; source: string } @@ -17,9 +18,7 @@ type UserRow = { type Tab = 'all' | 'unlocked' | 'vip' | 'locked' | 'blocked' function fmtTime(ts: number) { - if (!ts) return '—' - const d = new Date(ts < 1e12 ? ts * 1000 : ts) - return d.toLocaleString('zh-CN', { hour12: false }) + return formatBeijingTime(ts) } function shortId(id: string, n = 10) { diff --git a/apps/com/admin/src/pages/socialMeta.ts b/apps/com/admin/src/pages/socialMeta.ts index 88e18f7..61c569b 100644 --- a/apps/com/admin/src/pages/socialMeta.ts +++ b/apps/com/admin/src/pages/socialMeta.ts @@ -1,5 +1,7 @@ /** 游民社区后台 · 分区与字段元数据 */ +import { formatBeijingTime } from '../lib/beijingTime' + export type SectionKey = | 'overview' | 'messages' @@ -318,14 +320,9 @@ export function labelOf(key: string): string { return FIELD_LABEL[key] || key } +/** 管理后台统一北京时间(Asia/Shanghai)。 */ export function fmtTime(v: unknown) { - if (v == null || v === '') return '—' - const s = String(v) - if (/^\d+$/.test(s)) { - const n = Number(s) - return new Date(n < 1e12 ? n * 1000 : n).toLocaleString('zh-CN', { hour12: false }) - } - return s.slice(0, 19).replace('T', ' ') + return formatBeijingTime(v) } export function short(v: unknown, n = 48) { diff --git a/src/lib/ntfy-rich.ts b/src/lib/ntfy-rich.ts index 4af9ebd..706ef60 100644 --- a/src/lib/ntfy-rich.ts +++ b/src/lib/ntfy-rich.ts @@ -61,7 +61,7 @@ function shortUid(uid: string | undefined): string { function fmtTime(ts?: number): string { const d = ts && Number.isFinite(ts) ? new Date(ts) : new Date(); try { - return new Intl.DateTimeFormat("zh-CN", { + const stamp = new Intl.DateTimeFormat("zh-CN", { timeZone: "Asia/Shanghai", year: "numeric", month: "2-digit", @@ -71,8 +71,9 @@ function fmtTime(ts?: number): string { second: "2-digit", hour12: false, }).format(d); + return `${stamp}(北京时间)`; } catch { - return d.toISOString(); + return `${d.toISOString()}(北京时间)`; } }