fix: 时间修复
This commit is contained in:
parent
94308d81a0
commit
2599b00626
27
api/api.go
27
api/api.go
@ -55,9 +55,19 @@ func handleLatestRawData(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
result := map[string]interface{}{
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
"formatted_time": time.Now().Format("2006-01-02 15:04:05"),
|
||||
result := map[string]interface{}{}
|
||||
|
||||
// 使用数据库中的时间戳,如果可用
|
||||
if weatherData != nil && !weatherData.Timestamp.IsZero() {
|
||||
result["timestamp"] = weatherData.Timestamp.Format(time.RFC3339)
|
||||
result["formatted_time"] = weatherData.Timestamp.Format("2006-01-02 15:04:05")
|
||||
} else if rainData != nil && !rainData.Timestamp.IsZero() {
|
||||
result["timestamp"] = rainData.Timestamp.Format(time.RFC3339)
|
||||
result["formatted_time"] = rainData.Timestamp.Format("2006-01-02 15:04:05")
|
||||
} else {
|
||||
// 如果都没有时间戳,则使用当前时间作为 fallback
|
||||
result["timestamp"] = time.Now().Format(time.RFC3339)
|
||||
result["formatted_time"] = time.Now().Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
if weatherData != nil {
|
||||
@ -68,15 +78,22 @@ func handleLatestRawData(w http.ResponseWriter, r *http.Request) {
|
||||
result["wind_direction_360"] = weatherData.WindDirection360
|
||||
result["atm_pressure"] = weatherData.AtmPressure
|
||||
result["solar_radiation"] = weatherData.SolarRadiation
|
||||
result["rainfall"] = weatherData.Rainfall
|
||||
result["weather_rainfall"] = weatherData.Rainfall // 区分气象站的雨量
|
||||
}
|
||||
|
||||
if rainData != nil {
|
||||
result["rainfall"] = rainData.TotalRainfall
|
||||
result["total_rainfall"] = rainData.TotalRainfall // 使用 total_rainfall 表示累计雨量
|
||||
result["daily_rainfall"] = rainData.DailyRainfall
|
||||
result["instant_rainfall"] = rainData.InstantRainfall
|
||||
}
|
||||
|
||||
// 为了兼容旧代码,仍然提供一个 rainfall 字段,优先使用雨量计的数据
|
||||
if rainData != nil {
|
||||
result["rainfall"] = rainData.TotalRainfall
|
||||
} else if weatherData != nil {
|
||||
result["rainfall"] = weatherData.Rainfall
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
|
||||
@ -372,7 +372,8 @@
|
||||
document.getElementById('latest-wind-direction').textContent = data.wind_direction_360 || '--';
|
||||
document.getElementById('latest-atm-pressure').textContent = data.atm_pressure ? data.atm_pressure.toFixed(1) : '--';
|
||||
document.getElementById('latest-solar-radiation').textContent = data.solar_radiation || '--';
|
||||
document.getElementById('latest-rainfall').textContent = data.rainfall ? data.rainfall.toFixed(1) : '--';
|
||||
// 使用 total_rainfall 作为累计雨量,如果没有则回退到 rainfall
|
||||
document.getElementById('latest-rainfall').textContent = data.total_rainfall !== undefined ? data.total_rainfall.toFixed(1) : (data.rainfall ? data.rainfall.toFixed(1) : '--');
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('获取最新传感器数据失败:', error);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user