diff --git a/db/db.go b/db/db.go index 7ead111..92bbf3e 100644 --- a/db/db.go +++ b/db/db.go @@ -39,8 +39,9 @@ func createTables() { weatherTable := ` CREATE TABLE IF NOT EXISTS weathers ( project_id INTEGER PRIMARY KEY, - value TEXT NOT NULL, update_at TEXT NOT NULL, + weather_type_name TEXT NOT NULL, + value TEXT NOT NULL, FOREIGN KEY(project_id) REFERENCES projects(id) );` diff --git a/db/model/weather.go b/db/model/weather.go index 37e4ddc..f897087 100644 --- a/db/model/weather.go +++ b/db/model/weather.go @@ -7,9 +7,10 @@ import ( // Weather 天气 type Weather struct { - ProjectID int `db:"project_id" json:"-"` // 项目ID - UpdateAt string `db:"update_at" json:"update_at"` // 更新时间 - Value json.RawMessage `db:"value" json:"value"` // 天气信息 + ProjectID int `db:"project_id" json:"-"` // 项目ID + UpdateAt string `db:"update_at" json:"update_at"` // 更新时间 + WeatherTypeName string `db:"weather_type_name" json:"weather_type_name"` // 天气类型名称 + Value json.RawMessage `db:"value" json:"value"` // 天气信息 } // GetWeather 获取天气 @@ -27,8 +28,8 @@ func (w *Weather) UpdateWeather() (rer error) { return err } if count == 0 { - sql := "INSERT INTO weathers (project_id, update_at, value) VALUES (?, ?, ?)" - _, rer = db.DB.Exec(sql, w.ProjectID, w.UpdateAt, w.Value) + sql := "INSERT INTO weathers (project_id, update_at, weather_type_name, value) VALUES (?, ?, ?, ?)" + _, rer = db.DB.Exec(sql, w.ProjectID, w.UpdateAt, w.WeatherTypeName, w.Value) return } // 更新天气 diff --git a/qweather/get.go b/qweather/get.go index 0187a60..8a97b50 100644 --- a/qweather/get.go +++ b/qweather/get.go @@ -7,9 +7,9 @@ import ( ) // 获取天气 -func Get(location_id, weather_type_id int, dev bool) (json.RawMessage, error) { +func Get(location_id int, weather_type_name string, dev bool) (json.RawMessage, error) { // 生成url - url, err := makeURL(dev, location_id, weather_type_id) + url, err := makeURL(dev, location_id, weather_type_name) if err != nil { return nil, err } diff --git a/qweather/url.go b/qweather/url.go index df5e081..a682aab 100644 --- a/qweather/url.go +++ b/qweather/url.go @@ -18,26 +18,20 @@ var ( ) // makeURL 生成url -func makeURL(dev bool, location_id, weather_type_id int) (rv string, rer error) { +func makeURL(dev bool, location_id int, weather_type_name string) (rv string, rer error) { // 获取位置信息 localtion := model.Localtion{ID: location_id} err := localtion.GetLocaltionByID() if err != nil { return "", err } - // 获取天气类型 - weatherType := model.WeatherType{Id: weather_type_id} - err = weatherType.GetWeatherTypeByID() - if err != nil { - return "", err - } if dev { rv = c_DEVURL } else { rv = c_PROURL } rv += c_VERSION - rv += weatherType.Name + rv += weather_type_name rv += "?key=" if dev { rv += c_DEVKEY diff --git a/weather b/weather index 330ea50..0cb96e3 100755 Binary files a/weather and b/weather differ diff --git a/web/app/weather/api/weather.go b/web/app/weather/api/weather.go index c260c90..0fd1900 100644 --- a/web/app/weather/api/weather.go +++ b/web/app/weather/api/weather.go @@ -42,9 +42,16 @@ func GetWeatherByCode(c *fiber.Ctx) error { } if flag { // 更新天气 + // 获取天气类型 + weatherType := model.WeatherType{Id: project.WeatherTypeID} + err = weatherType.GetWeatherTypeByID() + if err != nil { + return response.Error(c, response.InternalServerError, "获取天气类型失败: "+err.Error()) + } // 获取天气 + weather.WeatherTypeName = weatherType.Name weather.UpdateAt = time.Now().Local().Format("2006-01-02 15:04:05") - weather.Value, err = qweather.Get(project.LocaltionID, project.WeatherTypeID, project.Dev) + weather.Value, err = qweather.Get(project.LocaltionID, weather.WeatherTypeName, project.Dev) if err != nil { return response.Error(c, response.InternalServerError, "获取天气失败: "+err.Error()) }