增加返回天气类型
							parent
							
								
									cc6e9e3549
								
							
						
					
					
						commit
						a31d2259c2
					
				
							
								
								
									
										3
									
								
								db/db.go
								
								
								
								
							
							
						
						
									
										3
									
								
								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)
 | 
			
		||||
	);`
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,6 +9,7 @@ import (
 | 
			
		|||
type Weather struct {
 | 
			
		||||
	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"`                         // 天气信息
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
	}
 | 
			
		||||
	// 更新天气
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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())
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue