{check.name}
{check.latencyMs} ms{check.status ? `HTTP ${check.status}` : '连接失败或检查超时'}
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 &&
上次检查:{new Date(data.checkedAt).toLocaleString('zh-CN', { hour12: false })}
上次检查:{formatBeijingTime(data.checkedAt)}(北京时间)
{check.status ? `HTTP ${check.status}` : '连接失败或检查超时'}