weather.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package services
  2. import (
  3. "encoding/json"
  4. "github.com/aiscrm/redisgo"
  5. "goiot-cronapi/common"
  6. "goiot-cronapi/models"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "time"
  11. )
  12. // GetWeatherFromRedis 从Redis中获取天气信息
  13. func GetWeatherFromRedis(cityId int64) models.Weather {
  14. //内网地址:r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com
  15. //外网地址:r-bp1g868m1d89jycev0pd.redis.rds.aliyuncs.com
  16. rds, err := redisgo.New(
  17. redisgo.Options{
  18. Addr: "r-bp1g868m1d89jycev0487.redis.rds.aliyuncs.com:6379",
  19. Password: "Meiyoumima1234",
  20. Prefix: "aiscrm_",
  21. })
  22. if err != nil {
  23. panic(err)
  24. }
  25. var weather models.Weather
  26. key := "CITY:" + strconv.FormatInt(cityId, 10)
  27. exists, _ := rds.Exists(key)
  28. if !exists {
  29. return weather
  30. }
  31. data, _ := rds.GetString(key)
  32. if err := json.Unmarshal([]byte(data), &weather); err == nil {
  33. return weather
  34. }
  35. return weather
  36. }
  37. // RequestCondition 从墨迹天气接口获取天气信息
  38. func RequestCondition(cityId int64) (*models.ConditionResp, error) {
  39. url := "http://aliv13.data.moji.com/whapi/json/alicityweather/condition?cityId=" + strconv.FormatInt(cityId, 10)
  40. req, err := http.NewRequest("POST", url, nil)
  41. if err != nil {
  42. return nil, err
  43. }
  44. req.Header.Set("Authorization", common.MJAppCode)
  45. client := &http.Client{Timeout: time.Second * 10}
  46. // Send request
  47. resp, err := client.Do(req)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer func() {
  52. _ = resp.Body.Close()
  53. }()
  54. body, err := ioutil.ReadAll(resp.Body)
  55. if err != nil {
  56. return nil, err
  57. }
  58. var cr models.ConditionResp
  59. if err := json.Unmarshal(body, &cr); err != nil {
  60. return nil, err
  61. }
  62. return &cr, nil
  63. }
  64. // RequestAqi 从墨迹天气接口获取空气质量
  65. func RequestAqi(cityId int64) (*models.AqiResp, error) {
  66. url := "http://aliv13.data.moji.com/whapi/json/alicityweather/aqi?cityId=" + strconv.FormatInt(cityId, 10)
  67. req, err := http.NewRequest("POST", url, nil)
  68. if err != nil {
  69. return nil, err
  70. }
  71. req.Header.Set("Authorization", common.MJAppCode)
  72. client := &http.Client{Timeout: time.Second * 10}
  73. // Send request
  74. resp, err := client.Do(req)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer func() {
  79. _ = resp.Body.Close()
  80. }()
  81. body, err := ioutil.ReadAll(resp.Body)
  82. if err != nil {
  83. return nil, err
  84. }
  85. var ar models.AqiResp
  86. if err := json.Unmarshal(body, &ar); err != nil {
  87. return nil, err
  88. }
  89. return &ar, nil
  90. }
  91. func GetAirLevel(aqistr string) string {
  92. aqi, _ := strconv.Atoi(aqistr)
  93. level := "其他"
  94. if aqi > 0 && aqi <= 50 {
  95. level = "优"
  96. } else if aqi > 50 && aqi <= 100 {
  97. level = "良"
  98. } else if aqi > 100 && aqi <= 150 {
  99. level = "轻度污染"
  100. } else if aqi > 150 && aqi <= 200 {
  101. level = "中度污染"
  102. } else if aqi > 200 && aqi <= 300 {
  103. level = "重度污染"
  104. } else if aqi > 300 && aqi <= 500 {
  105. level = "严重污染"
  106. } else if aqi > 500 {
  107. level = "爆表"
  108. }
  109. return level
  110. }
  111. func GetWeatherIcon(condition string) string {
  112. iconName := common.WeatherIconMap[condition]
  113. return common.ConfigInfo.FileServUrl + "/moji/" + iconName + ".png"
  114. }