fix:修改前端显示时间错误
This commit is contained in:
parent
ab3b382032
commit
bcf7df31da
@ -47,7 +47,7 @@ func (dao *SensorDAO) GetAggregatedData(start, end time.Time, interval string) (
|
|||||||
case "5min":
|
case "5min":
|
||||||
query = `
|
query = `
|
||||||
SELECT
|
SELECT
|
||||||
MIN(timestamp) as ts,
|
DATE_FORMAT(timestamp, '%Y-%m-%dT%H:%i:%s') as ts,
|
||||||
ROUND(AVG(temperature)/10, 1) as avg_temp,
|
ROUND(AVG(temperature)/10, 1) as avg_temp,
|
||||||
MAX(rainfall) - MIN(rainfall) as rainfall,
|
MAX(rainfall) - MIN(rainfall) as rainfall,
|
||||||
ROUND(AVG(humidity)/10, 1) as avg_humidity,
|
ROUND(AVG(humidity)/10, 1) as avg_humidity,
|
||||||
@ -60,7 +60,7 @@ func (dao *SensorDAO) GetAggregatedData(start, end time.Time, interval string) (
|
|||||||
case "30min":
|
case "30min":
|
||||||
query = `
|
query = `
|
||||||
SELECT
|
SELECT
|
||||||
MIN(timestamp) as ts,
|
DATE_FORMAT(timestamp, '%Y-%m-%dT%H:%i:%s') as ts,
|
||||||
ROUND(AVG(temperature)/10, 1) as avg_temp,
|
ROUND(AVG(temperature)/10, 1) as avg_temp,
|
||||||
MAX(rainfall) - MIN(rainfall) as rainfall,
|
MAX(rainfall) - MIN(rainfall) as rainfall,
|
||||||
ROUND(AVG(humidity)/10, 1) as avg_humidity,
|
ROUND(AVG(humidity)/10, 1) as avg_humidity,
|
||||||
@ -73,7 +73,7 @@ func (dao *SensorDAO) GetAggregatedData(start, end time.Time, interval string) (
|
|||||||
default: // 1hour
|
default: // 1hour
|
||||||
query = `
|
query = `
|
||||||
SELECT
|
SELECT
|
||||||
MIN(timestamp) as ts,
|
DATE_FORMAT(timestamp, '%Y-%m-%dT%H:%i:%s') as ts,
|
||||||
ROUND(AVG(temperature)/10, 1) as avg_temp,
|
ROUND(AVG(temperature)/10, 1) as avg_temp,
|
||||||
MAX(rainfall) - MIN(rainfall) as rainfall,
|
MAX(rainfall) - MIN(rainfall) as rainfall,
|
||||||
ROUND(AVG(humidity)/10, 1) as avg_humidity,
|
ROUND(AVG(humidity)/10, 1) as avg_humidity,
|
||||||
@ -102,7 +102,12 @@ func (dao *SensorDAO) GetAggregatedData(start, end time.Time, interval string) (
|
|||||||
logger.Logger.Printf("扫描数据行失败: %v", err)
|
logger.Logger.Printf("扫描数据行失败: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
data.Timestamp, _ = time.Parse("2006-01-02 15:04:05", tsStr)
|
// 解析ISO格式的时间字符串
|
||||||
|
data.Timestamp, err = time.Parse("2006-01-02T15:04:05", tsStr)
|
||||||
|
if err != nil {
|
||||||
|
logger.Logger.Printf("解析时间失败: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
result = append(result, data)
|
result = append(result, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -45,7 +45,14 @@ func (h *SensorHandler) GetAggregatedData(w http.ResponseWriter, r *http.Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(data)
|
// 使用自定义的编码器,设置时间格式
|
||||||
|
encoder := json.NewEncoder(w)
|
||||||
|
encoder.SetIndent("", " ")
|
||||||
|
if err := encoder.Encode(data); err != nil {
|
||||||
|
logger.Logger.Printf("JSON编码失败: %v", err)
|
||||||
|
http.Error(w, "服务器内部错误", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理静态文件
|
// 处理静态文件
|
||||||
|
|||||||
@ -213,17 +213,15 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const labels = data.map(item => {
|
const labels = data.map(item => {
|
||||||
// 确保使用正确的时间格式解析
|
// 直接解析ISO格式的时间字符串
|
||||||
const date = new Date(item.timestamp.replace(' ', 'T') + 'Z');
|
const date = new Date(item.timestamp);
|
||||||
// 调整为本地时间
|
|
||||||
const localDate = new Date(date.getTime() + (8 * 60 * 60 * 1000));
|
|
||||||
|
|
||||||
// 格式化为中文日期时间格式
|
// 格式化为中文日期时间格式
|
||||||
return localDate.getFullYear() + '/' +
|
return date.getFullYear() + '/' +
|
||||||
(localDate.getMonth() + 1).toString().padStart(2, '0') + '/' +
|
(date.getMonth() + 1).toString().padStart(2, '0') + '/' +
|
||||||
localDate.getDate().toString().padStart(2, '0') + ' ' +
|
date.getDate().toString().padStart(2, '0') + ' ' +
|
||||||
localDate.getHours().toString().padStart(2, '0') + ':' +
|
date.getHours().toString().padStart(2, '0') + ':' +
|
||||||
localDate.getMinutes().toString().padStart(2, '0');
|
date.getMinutes().toString().padStart(2, '0');
|
||||||
});
|
});
|
||||||
|
|
||||||
mainChart = new Chart(ctx, {
|
mainChart = new Chart(ctx, {
|
||||||
@ -288,18 +286,16 @@
|
|||||||
|
|
||||||
data.forEach(item => {
|
data.forEach(item => {
|
||||||
const row = document.createElement('tr');
|
const row = document.createElement('tr');
|
||||||
// 确保使用正确的时间格式解析
|
// 直接解析ISO格式的时间字符串
|
||||||
const date = new Date(item.timestamp.replace(' ', 'T') + 'Z');
|
const date = new Date(item.timestamp);
|
||||||
// 调整为本地时间
|
|
||||||
const localDate = new Date(date.getTime() + (8 * 60 * 60 * 1000));
|
|
||||||
|
|
||||||
const formattedDate =
|
const formattedDate =
|
||||||
localDate.getFullYear() + '/' +
|
date.getFullYear() + '/' +
|
||||||
(localDate.getMonth() + 1).toString().padStart(2, '0') + '/' +
|
(date.getMonth() + 1).toString().padStart(2, '0') + '/' +
|
||||||
localDate.getDate().toString().padStart(2, '0') + ' ' +
|
date.getDate().toString().padStart(2, '0') + ' ' +
|
||||||
localDate.getHours().toString().padStart(2, '0') + ':' +
|
date.getHours().toString().padStart(2, '0') + ':' +
|
||||||
localDate.getMinutes().toString().padStart(2, '0') + ':' +
|
date.getMinutes().toString().padStart(2, '0') + ':' +
|
||||||
localDate.getSeconds().toString().padStart(2, '0');
|
date.getSeconds().toString().padStart(2, '0');
|
||||||
|
|
||||||
row.innerHTML = `
|
row.innerHTML = `
|
||||||
<td>${formattedDate}</td>
|
<td>${formattedDate}</td>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user