From 3188f4d0588ed4574541b7c90331dc811952cc13 Mon Sep 17 00:00:00 2001 From: shaw Date: Wed, 30 Jul 2025 12:13:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(admin-spa):=20=E4=BF=AE=E5=A4=8D=E6=97=B6?= =?UTF-8?q?=E5=8C=BA=E8=BD=AC=E6=8D=A2=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 getSystemTimezoneDay 函数:正确使用 Date.UTC 创建UTC时间,避免本地时区干扰 - 修复自定义时间范围转换:使用 Date.UTC 正确转换系统时区时间到UTC - 解决了昨天/前天日期计算错误的问题 - 解决了自定义时间选择器8小时偏差的问题 --- web/admin-spa/src/stores/dashboard.js | 53 +++++++++++++-------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/web/admin-spa/src/stores/dashboard.js b/web/admin-spa/src/stores/dashboard.js index f0ddd08e..35d07d45 100644 --- a/web/admin-spa/src/stores/dashboard.js +++ b/web/admin-spa/src/stores/dashboard.js @@ -91,27 +91,26 @@ export const useDashboardStore = defineStore('dashboard', () => { } // 辅助函数:获取系统时区某一天的起止UTC时间 - // 输入:一个系统时区的日期对象 + // 输入:一个本地时间的日期对象(如用户选择的日期) // 输出:该日期在系统时区的0点/23:59对应的UTC时间 - function getSystemTimezoneDay(systemTzDate, startOfDay = true) { - const offset = dashboardData.value.systemTimezone || 8 + function getSystemTimezoneDay(localDate, startOfDay = true) { + const systemTz = dashboardData.value.systemTimezone || 8 - // 获取系统时区日期的年月日 - const year = systemTzDate.getFullYear() - const month = systemTzDate.getMonth() - const day = systemTzDate.getDate() + // 获取本地日期的年月日(这是用户想要查看的日期) + const year = localDate.getFullYear() + const month = localDate.getMonth() + const day = localDate.getDate() - // 创建系统时区的日期时间 if (startOfDay) { - // 系统时区 YYYY-MM-DD 00:00:00 - const systemDateTime = new Date(year, month, day, 0, 0, 0, 0) - // 转换为UTC时间:减去时区偏移 - return new Date(systemDateTime.getTime() - offset * 3600000) + // 创建UTC时间,使其在系统时区(UTC+8)显示为 YYYY-MM-DD 00:00:00 + // 例如:要在UTC+8显示为 2025-07-28 00:00:00,UTC时间应该是 2025-07-27 16:00:00 + const utcDate = new Date(Date.UTC(year, month, day, 0 - systemTz, 0, 0, 0)) + return utcDate } else { - // 系统时区 YYYY-MM-DD 23:59:59.999 - const systemDateTime = new Date(year, month, day, 23, 59, 59, 999) - // 转换为UTC时间:减去时区偏移 - return new Date(systemDateTime.getTime() - offset * 3600000) + // 创建UTC时间,使其在系统时区(UTC+8)显示为 YYYY-MM-DD 23:59:59.999 + // 例如:要在UTC+8显示为 2025-07-28 23:59:59,UTC时间应该是 2025-07-28 15:59:59 + const utcDate = new Date(Date.UTC(year, month, day, 24 - systemTz - 1, 59, 59, 999)) + return utcDate } } @@ -188,12 +187,11 @@ export const useDashboardStore = defineStore('dashboard', () => { const [year, month, day] = datePart.split('-').map(Number) const [hours, minutes, seconds] = timePart.split(':').map(Number) - // 创建系统时区的Date对象 - const systemDate = new Date(year, month - 1, day, hours, minutes, seconds) - - // 转换为UTC - const utcTime = systemDate.getTime() - (systemTz * 3600000) - return new Date(utcTime).toISOString() + // 创建UTC时间,使其在系统时区显示为用户选择的时间 + // 例如:用户选择 UTC+8 的 2025-07-25 00:00:00 + // 对应的UTC时间是 2025-07-24 16:00:00 + const utcDate = new Date(Date.UTC(year, month - 1, day, hours - systemTz, minutes, seconds)) + return utcDate.toISOString() } url += `&startDate=${encodeURIComponent(convertToUTC(dateFilter.value.customRange[0]))}` @@ -281,12 +279,11 @@ export const useDashboardStore = defineStore('dashboard', () => { const [year, month, day] = datePart.split('-').map(Number) const [hours, minutes, seconds] = timePart.split(':').map(Number) - // 创建系统时区的Date对象 - const systemDate = new Date(year, month - 1, day, hours, minutes, seconds) - - // 转换为UTC - const utcTime = systemDate.getTime() - (systemTz * 3600000) - return new Date(utcTime).toISOString() + // 创建UTC时间,使其在系统时区显示为用户选择的时间 + // 例如:用户选择 UTC+8 的 2025-07-25 00:00:00 + // 对应的UTC时间是 2025-07-24 16:00:00 + const utcDate = new Date(Date.UTC(year, month - 1, day, hours - systemTz, minutes, seconds)) + return utcDate.toISOString() } url += `&startDate=${encodeURIComponent(convertToUTC(dateFilter.value.customRange[0]))}`