From f60077e4c281bbc1e62b7b1c29291eb1a72603f2 Mon Sep 17 00:00:00 2001 From: fengyarnom Date: Fri, 16 May 2025 11:39:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 9 ++++++--- internal/handler/sensor.go | 16 +++++++++++----- static/index.html | 11 +++++++++-- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index dfd1625..f0f1dee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ -data/*.db -data/sensor.db -logs/ +data/*.db +data/sensor.db +logs/ +./tcp_server +data/* +./go_rain_dtu.exe \ No newline at end of file diff --git a/internal/handler/sensor.go b/internal/handler/sensor.go index e90fb7e..0679008 100644 --- a/internal/handler/sensor.go +++ b/internal/handler/sensor.go @@ -26,17 +26,23 @@ func (h *SensorHandler) GetAggregatedData(w http.ResponseWriter, r *http.Request interval = "1hour" } - endTime := time.Now() + // 获取中国时区 + chinaLoc, locErr := time.LoadLocation("Asia/Shanghai") + if locErr != nil { + chinaLoc = time.FixedZone("CST", 8*60*60) + } + + endTime := time.Now().In(chinaLoc) startTime := endTime.Add(-24 * time.Hour) // 默认显示24小时数据 if startStr := r.URL.Query().Get("start"); startStr != "" { - if t, err := time.Parse("2006-01-02T15:04", startStr); err == nil { - startTime = t + if t, parseErr := time.Parse(time.RFC3339, startStr); parseErr == nil { + startTime = t.In(chinaLoc) } } if endStr := r.URL.Query().Get("end"); endStr != "" { - if t, err := time.Parse("2006-01-02T15:04", endStr); err == nil { - endTime = t + if t, parseErr := time.Parse(time.RFC3339, endStr); parseErr == nil { + endTime = t.In(chinaLoc) } } diff --git a/static/index.html b/static/index.html index 36da218..0370f15 100644 --- a/static/index.html +++ b/static/index.html @@ -174,9 +174,9 @@ @@ -326,15 +326,22 @@ const startDateTime = new Date(startDate).toISOString(); const endDateTime = new Date(endDate).toISOString(); + // 加载状态指示 + document.getElementById('mainChart').style.opacity = 0.5; + fetch(`/api/data?interval=${interval}&start=${startDateTime}&end=${endDateTime}`) .then(response => response.json()) .then(data => { updateChart(data); updateTable(data); + // 恢复正常显示 + document.getElementById('mainChart').style.opacity = 1; }) .catch(error => { console.error('Error:', error); alert('获取数据失败,请检查网络连接'); + // 恢复正常显示 + document.getElementById('mainChart').style.opacity = 1; }); }