From 81917c281e3f398313bb5e67f8f17ceae8af37d0 Mon Sep 17 00:00:00 2001 From: shaw Date: Wed, 30 Jul 2025 12:38:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(admin-spa):=20=E5=BD=BB=E5=BA=95=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=97=B6=E5=8C=BA=E8=AE=A1=E7=AE=97=E5=92=8C=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 getSystemTimezoneDay 函数:正确计算UTC+8时区对应的UTC时间 - 修复时间筛选框显示:昨天/前天显示正确的系统时区日期 - 修复自定义时间范围:正确处理系统时区到UTC的转换 - 简化时区计算逻辑,避免复杂的条件判断 --- web/admin-spa/src/stores/dashboard.js | 71 ++++++++++++++++++--------- 1 file changed, 48 insertions(+), 23 deletions(-) diff --git a/web/admin-spa/src/stores/dashboard.js b/web/admin-spa/src/stores/dashboard.js index 728cb74c..19a83bda 100644 --- a/web/admin-spa/src/stores/dashboard.js +++ b/web/admin-spa/src/stores/dashboard.js @@ -103,15 +103,15 @@ export const useDashboardStore = defineStore('dashboard', () => { const day = localDate.getDate() if (startOfDay) { - // 创建UTC时间,使其在系统时区(UTC+8)显示为 YYYY-MM-DD 00:00:00 - // 例如:要在UTC+8显示为 2025-07-29 00:00:00,UTC时间应该是 2025-07-28 16:00:00 - const utcDate = new Date(Date.UTC(year, month, day - 1, 24 - systemTz, 0, 0, 0)) - return utcDate + // 系统时区(UTC+8)的 YYYY-MM-DD 00:00:00 + // 对应的UTC时间是前一天的16:00 + // 例如:UTC+8的2025-07-29 00:00:00 = UTC的2025-07-28 16:00:00 + return new Date(Date.UTC(year, month, day - 1, 16, 0, 0, 0)) } else { - // 创建UTC时间,使其在系统时区(UTC+8)显示为 YYYY-MM-DD 23:59:59.999 - // 例如:要在UTC+8显示为 2025-07-29 23:59:59,UTC时间应该是 2025-07-29 15:59:59 - const utcDate = new Date(Date.UTC(year, month, day, 24 - systemTz - 1, 59, 59, 999)) - return utcDate + // 系统时区(UTC+8)的 YYYY-MM-DD 23:59:59 + // 对应的UTC时间是当天的15:59:59 + // 例如:UTC+8的2025-07-29 23:59:59 = UTC的2025-07-29 15:59:59 + return new Date(Date.UTC(year, month, day, 15, 59, 59, 999)) } } @@ -411,22 +411,47 @@ export const useDashboardStore = defineStore('dashboard', () => { dateFilter.value.customEnd = endDate.toISOString().split('T')[0] // 设置 customRange 为 Element Plus 需要的格式 - // 显示系统时区的时间 - const formatDateForDisplay = (utcDate) => { - const systemTzDate = getDateInSystemTimezone(utcDate) - const year = systemTzDate.getUTCFullYear() - const month = String(systemTzDate.getUTCMonth() + 1).padStart(2, '0') - const day = String(systemTzDate.getUTCDate()).padStart(2, '0') - const hours = String(systemTzDate.getUTCHours()).padStart(2, '0') - const minutes = String(systemTzDate.getUTCMinutes()).padStart(2, '0') - const seconds = String(systemTzDate.getUTCSeconds()).padStart(2, '0') - return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` + // 对于小时粒度的昨天/前天,需要特殊处理显示 + if (trendGranularity.value === 'hour' && (preset === 'yesterday' || preset === 'dayBefore')) { + // 获取本地日期 + const targetDate = new Date() + if (preset === 'yesterday') { + targetDate.setDate(targetDate.getDate() - 1) + } else { + targetDate.setDate(targetDate.getDate() - 2) + } + + // 显示系统时区的完整一天 + const year = targetDate.getFullYear() + const month = String(targetDate.getMonth() + 1).padStart(2, '0') + const day = String(targetDate.getDate()).padStart(2, '0') + + dateFilter.value.customRange = [ + `${year}-${month}-${day} 00:00:00`, + `${year}-${month}-${day} 23:59:59` + ] + } else { + // 其他情况:近24小时或天粒度 + const formatDateForDisplay = (date) => { + // 固定使用UTC+8来显示时间 + const systemTz = 8 + const tzOffset = systemTz * 60 * 60 * 1000 + const localTime = new Date(date.getTime() + tzOffset) + + const year = localTime.getUTCFullYear() + const month = String(localTime.getUTCMonth() + 1).padStart(2, '0') + const day = String(localTime.getUTCDate()).padStart(2, '0') + const hours = String(localTime.getUTCHours()).padStart(2, '0') + const minutes = String(localTime.getUTCMinutes()).padStart(2, '0') + const seconds = String(localTime.getUTCSeconds()).padStart(2, '0') + return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` + } + + dateFilter.value.customRange = [ + formatDateForDisplay(startDate), + formatDateForDisplay(endDate) + ] } - - dateFilter.value.customRange = [ - formatDateForDisplay(startDate), - formatDateForDisplay(endDate) - ] } // 触发数据刷新