weather.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package models
  2. import (
  3. "encoding/json"
  4. "github.com/aiscrm/redisgo"
  5. "strconv"
  6. )
  7. type ConditionResp struct {
  8. Code int `json:"code"`
  9. Data struct {
  10. Condition struct {
  11. Condition string `json:"condition"` // 实时天气
  12. Humidity string `json:"humidity"` // 湿度
  13. Temp string `json:"temp"` // 温度
  14. Tips string `json:"tips"` // 提示
  15. WindDir string `json:"windDir"` // 风向
  16. WindLevel string `json:"windLevel"` // 风级
  17. WindSpeed string `json:"windSpeed"` // 风速
  18. } `json:"condition"`
  19. } `json:"data"`
  20. }
  21. type AqiResp struct {
  22. Code int `json:"code"`
  23. Data struct {
  24. Aqi struct {
  25. Value string `json:"value"` // 空气质量指数值
  26. PM25 string `json:"pm25"` // PM2.5指数
  27. Rank string `json:"rank"` // 全国排名
  28. } `json:"aqi"`
  29. } `json:"data"`
  30. }
  31. type Weather struct {
  32. CityId int64 `json:"cityId" gorm:"column:cityId"` // 城市ID
  33. Temp string `json:"temp" gorm:"column:temp"` // 温度
  34. Humidity string `json:"humidity" gorm:"column:humidity"` // 湿度
  35. Condition string `json:"condition" gorm:"-"` // 天气
  36. Aqi string `json:"aqi" gorm:"-"` // 空气指数
  37. AqiLevel string `json:"aqiLevel" gorm:"-"` // 空气指数等级
  38. PM25 string `json:"pm25" gorm:"column:pm25"` // PM2.5
  39. Province string `json:"province" gorm:"-"` // 省
  40. City string `json:"city" gorm:"-"` // 市
  41. Area string `json:"area" gorm:"-"` // 区
  42. Icon string `json:"icon" gorm:"-"` // 天气图标
  43. Created string `json:"created" gorm:"-"` // 创建日期
  44. }
  45. func (w *Weather) SaveToRedis() error {
  46. //内网地址:r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com
  47. //外网地址:r-bp1g868m1d89jycev0pd.redis.rds.aliyuncs.com
  48. rds, err := redisgo.New(
  49. redisgo.Options{
  50. Addr: "r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com:6379",
  51. Password: "Meiyoumima1234",
  52. Prefix: "aiscrm_",
  53. })
  54. if err != nil {
  55. panic(err)
  56. }
  57. jsonBytes, err := json.Marshal(w)
  58. if err != nil {
  59. return err
  60. }
  61. key := "CITY:" + strconv.FormatInt(w.CityId, 10)
  62. err = rds.Set(key, string(jsonBytes), 900)
  63. return err
  64. }