fix(admin): 优化admin管理台细节问题
This commit is contained in:
1 parent
a89491f4b1
commit
0d3a812e16
7 files changed
+132
-86
No files matched your search
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* shared/utils/dateTime.test.ts
|
||||
* Purpose: protect API timestamp parsing used by audit and admin views.
|
||||
* Dependencies: Vitest and dateTime helpers.
|
||||
* Flow: validate UTC normalization first, then display fallback behavior.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { formatLocalDateTime, parseApiUtcDate } from './dateTime';
|
||||
|
||||
describe('dateTime utils', () => {
|
||||
it('treats API timestamps without timezone suffix as UTC', () => {
|
||||
expect(parseApiUtcDate('2026-06-18T02:32:37.221365').toISOString()).toBe('2026-06-18T02:32:37.221Z');
|
||||
});
|
||||
|
||||
it('formats empty and invalid values predictably', () => {
|
||||
expect(formatLocalDateTime(null)).toBe('-');
|
||||
expect(formatLocalDateTime('not-a-date')).toBe('not-a-date');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* shared/utils/dateTime.ts
|
||||
* Purpose: shared date parsing and display helpers for API timestamps.
|
||||
* Dependencies: browser Date API only.
|
||||
* Flow: views/composables pass backend ISO strings in, helpers normalize UTC and format in the user's local timezone.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parse an API ISO timestamp as UTC when the backend omits an explicit timezone suffix.
|
||||
* Use this for database-created timestamps that are serialized like `2026-06-18T02:32:37.221365`.
|
||||
*
|
||||
* @param iso - ISO 8601 timestamp, with or without `Z` / `+08:00` style timezone suffix.
|
||||
* @returns Date parsed from the normalized timestamp.
|
||||
*/
|
||||
export function parseApiUtcDate(iso: string): Date {
|
||||
const normalized = /(?:Z|[+-]\d{2}:\d{2})$/i.test(iso) ? iso : `${iso}Z`;
|
||||
return new Date(normalized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an API timestamp as `YYYY-MM-DD HH:mm:ss` in the current user's browser timezone.
|
||||
* Empty values return `-`; invalid dates fall back to the original string for easier troubleshooting.
|
||||
*
|
||||
* @param iso - API timestamp string.
|
||||
* @returns Local timezone display text.
|
||||
*/
|
||||
export function formatLocalDateTime(iso?: string | null): string {
|
||||
if (!iso) return '-';
|
||||
const d = parseApiUtcDate(iso);
|
||||
if (Number.isNaN(d.getTime())) return iso;
|
||||
const pad = (n: number) => String(n).padStart(2, '0');
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
|
||||
}
|
||||
Reference in new issue
Block a user