fix(admin): 优化admin管理台细节问题

This commit is contained in:
sophie committed 2026-06-23 19:17:34 +08:00
1 parent a89491f4b1
commit 0d3a812e16
7 files changed
+132 -86

No files matched your search

@@ -19,6 +19,7 @@ import {
unbindAdminWecomUser,
} from '@/features/admin/api/admin.api';
import { useMessage } from '@/shared/composables/useMessage';
import { formatLocalDateTime } from '@/shared/utils/dateTime';
import type { AdminUserRow } from '@/features/admin/types/admin.types';
import {
createDefaultWecomSystemSettings,
@@ -425,7 +426,7 @@ export function useWecomAdmin() {
return;
}
const rows = filteredWecomLoginAudits.value.map((row) => ({
Time: row.created_at || '',
Time: formatLocalDateTime(row.created_at),
Source: row.source || '',
Username: row.username || '',
Status: row.status || '',
@@ -6,6 +6,7 @@
import { inject } from 'vue';
import { useI18n } from 'vue-i18n';
import { Download } from 'lucide-vue-next';
import { formatLocalDateTime } from '@/shared/utils/dateTime';
import { WECOM_ADMIN_KEY } from '../wecom.context';
const { t } = useI18n();
@@ -40,9 +41,9 @@ const {
</button>
</div>
</div>
<div class="admin-table-wrap max-h-[560px]">
<div class="admin-table-wrap">
<table class="admin-table">
<thead class="sticky top-0 z-10 admin-table-sticky-head">
<thead>
<tr>
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.auditHeaders.time') }}</th>
<th class="px-4 py-3 text-xs">{{ t('admin.wecom.auditHeaders.source') }}</th>
@@ -57,7 +58,7 @@ const {
<td colspan="6" class="px-4 admin-table-empty">{{ t('admin.wecom.noAuditData') }}</td>
</tr>
<tr v-for="row in filteredWecomLoginAudits" :key="row.id">
<td class="px-4 py-3 text-xs text-muted whitespace-nowrap">{{ row.created_at || '-' }}</td>
<td class="px-4 py-3 text-xs text-muted whitespace-nowrap" :title="row.created_at || '-'">{{ formatLocalDateTime(row.created_at) }}</td>
<td class="px-4 py-3 text-xs text-secondary">{{ t('admin.wecom.sources.' + row.source) }}</td>
<td class="px-4 py-3 text-xs text-secondary">{{ row.username || '-' }}</td>
<td class="px-4 py-3">
@@ -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');
});
});
+33
View File
@@ -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())}`;
}