package models import ( "encoding/json" "github.com/aiscrm/redisgo" "strconv" ) type ConditionResp struct { Code int `json:"code"` Data struct { Condition struct { Condition string `json:"condition"` // 实时天气 Humidity string `json:"humidity"` // 湿度 Temp string `json:"temp"` // 温度 Tips string `json:"tips"` // 提示 WindDir string `json:"windDir"` // 风向 WindLevel string `json:"windLevel"` // 风级 WindSpeed string `json:"windSpeed"` // 风速 } `json:"condition"` } `json:"data"` } type AqiResp struct { Code int `json:"code"` Data struct { Aqi struct { Value string `json:"value"` // 空气质量指数值 PM25 string `json:"pm25"` // PM2.5指数 Rank string `json:"rank"` // 全国排名 } `json:"aqi"` } `json:"data"` } type Weather struct { CityId int64 `json:"cityId" gorm:"column:cityId"` // 城市ID Temp string `json:"temp" gorm:"column:temp"` // 温度 Humidity string `json:"humidity" gorm:"column:humidity"` // 湿度 Condition string `json:"condition" gorm:"-"` // 天气 Aqi string `json:"aqi" gorm:"-"` // 空气指数 AqiLevel string `json:"aqiLevel" gorm:"-"` // 空气指数等级 PM25 string `json:"pm25" gorm:"column:pm25"` // PM2.5 Province string `json:"province" gorm:"-"` // 省 City string `json:"city" gorm:"-"` // 市 Area string `json:"area" gorm:"-"` // 区 Icon string `json:"icon" gorm:"-"` // 天气图标 Created string `json:"created" gorm:"-"` // 创建日期 } func (w *Weather) SaveToRedis() error { //内网地址:r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com //外网地址:r-bp1g868m1d89jycev0pd.redis.rds.aliyuncs.com rds, err := redisgo.New( redisgo.Options{ Addr: "r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com:6379", Password: "Meiyoumima1234", Prefix: "aiscrm_", }) if err != nil { panic(err) } jsonBytes, err := json.Marshal(w) if err != nil { return err } key := "CITY:" + strconv.FormatInt(w.CityId, 10) err = rds.Set(key, string(jsonBytes), 900) return err }