api.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package api
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. api = "https://api.seniverse.com/v3/weather/now.json?language=zh-Hans&unit=c&location="
  13. dayApi = "https://api.seniverse.com/v3/weather/daily.json?language=zh-Hans&unit=c&start=-1&days=5&location="
  14. airApi = "https://api.seniverse.com/v3/air/now.json?language=zh-Hans&scope=city&location="
  15. )
  16. // WeatherResp 天气
  17. type WeatherResp struct {
  18. Results []struct {
  19. Location struct {
  20. Id string `json:"id"`
  21. Name string `json:"name"`
  22. Country string `json:"country"`
  23. Path string `json:"path"`
  24. Timezone string `json:"timezone"`
  25. TimezoneOffset string `json:"timezone_offset"`
  26. } `json:"location"`
  27. Now struct {
  28. Text string `json:"text"`
  29. Code string `json:"code"`
  30. Temperature string `json:"temperature"`
  31. FeelsLike string `json:"feels_like"`
  32. Pressure string `json:"pressure"`
  33. Humidity string `json:"humidity"`
  34. Visibility string `json:"visibility"`
  35. WindDirection string `json:"wind_direction"`
  36. WindDirectionDegree string `json:"wind_direction_degree"`
  37. WindSpeed string `json:"wind_speed"`
  38. WindScale string `json:"wind_scale"`
  39. Clouds string `json:"clouds"`
  40. DewPoint string `json:"dew_point"`
  41. } `json:"now"`
  42. LastUpdate time.Time `json:"last_update"`
  43. }
  44. }
  45. type XinZhiDayWeatherInfo struct {
  46. Results []struct {
  47. Location struct {
  48. Id string `json:"id"`
  49. Name string `json:"name"`
  50. Country string `json:"country"`
  51. Path string `json:"path"`
  52. Timezone string `json:"timezone"`
  53. TimezoneOffset string `json:"timezone_offset"`
  54. } `json:"location"`
  55. Daily []struct {
  56. Date string `json:"date"`
  57. TextDay string `json:"text_day"`
  58. CodeDay string `json:"code_day"`
  59. TextNight string `json:"text_night"`
  60. CodeNight string `json:"code_night"`
  61. High string `json:"high"`
  62. Low string `json:"low"`
  63. Rainfall string `json:"rainfall"`
  64. Precip string `json:"precip"`
  65. WindDirection string `json:"wind_direction"`
  66. WindDirectionDegree string `json:"wind_direction_degree"`
  67. WindSpeed string `json:"wind_speed"`
  68. WindScale string `json:"wind_scale"`
  69. Humidity string `json:"humidity"`
  70. } `json:"daily"`
  71. LastUpdate time.Time `json:"last_update"`
  72. } `json:"results"`
  73. }
  74. type AirInfo struct {
  75. Results []struct {
  76. Location struct {
  77. Id string `json:"id"`
  78. Name string `json:"name"`
  79. Country string `json:"country"`
  80. Path string `json:"path"`
  81. Timezone string `json:"timezone"`
  82. TimezoneOffset string `json:"timezone_offset"`
  83. } `json:"location"`
  84. Air struct {
  85. City struct {
  86. Aqi string `json:"aqi"` // 空气质量指数(AQI)是描述空气质量状况的定量指数
  87. Pm25 string `json:"pm25"` // PM2.5颗粒物(粒径小于等于2.5μm)1小时平均值。单位:μg/m³
  88. Pm10 string `json:"pm10"` // PM10颗粒物(粒径小于等于10μm)1小时平均值。单位:μg/m³
  89. So2 string `json:"so2"` // 二氧化硫1小时平均值。单位:μg/m³
  90. No2 string `json:"no2"` // 二氧化氮1小时平均值。单位:μg/m³
  91. Co string `json:"co"` // 一氧化碳1小时平均值。单位:mg/m³
  92. O3 string `json:"o3"` // 臭氧1小时平均值。单位:μg/m³
  93. PrimaryPollutant string `json:"primary_pollutant"` // 首要污染物
  94. Quality string `json:"quality"` // 空气质量类别,有“优、良、轻度污染、中度污染、重度污染、严重污染”6类
  95. LastUpdate time.Time `json:"last_update"` // 数据发布时间
  96. } `json:"city"`
  97. } `json:"air"`
  98. LastUpdate time.Time `json:"last_update"`
  99. }
  100. }
  101. type WeatherInfoResp struct {
  102. Location string `json:"location"`
  103. Text string `json:"text"`
  104. Code string `json:"code"`
  105. Temperature int `json:"temperature"`
  106. Humidity int `json:"humidity"`
  107. AQI int `json:"aqi"`
  108. PM25 int `json:"pm25"`
  109. PM25Text string `json:"pm25_text"`
  110. PM10 int `json:"pm10"`
  111. Quality string `json:"quality"`
  112. LastUpdate time.Time `json:"last_update"`
  113. }
  114. func GetWeatherInfo(location, key string) (*WeatherInfoResp, error) {
  115. result := new(WeatherInfoResp)
  116. info, err := GetWeatherInfoByLocation(location, key)
  117. if err != nil {
  118. return nil, err
  119. }
  120. if len(info.Results) == 0 {
  121. return nil, errors.New(fmt.Sprintf("天气查询失败:%s", location))
  122. }
  123. result.Location = info.Results[0].Location.Name
  124. result.Text = info.Results[0].Now.Text
  125. result.Code = info.Results[0].Now.Code
  126. result.Temperature, _ = strconv.Atoi(info.Results[0].Now.Temperature)
  127. result.Humidity, _ = strconv.Atoi(info.Results[0].Now.Humidity)
  128. result.LastUpdate = info.Results[0].LastUpdate
  129. airInfo, err := GetAirInfoByLocation(location, key)
  130. if err != nil {
  131. return nil, err
  132. }
  133. result.PM25, _ = strconv.Atoi(airInfo.Results[0].Air.City.Pm25)
  134. if result.PM25 <= 35 {
  135. result.PM25Text = "优"
  136. } else if result.PM25 <= 75 {
  137. result.PM25Text = "良"
  138. } else {
  139. result.PM25Text = "污染"
  140. }
  141. result.AQI, _ = strconv.Atoi(airInfo.Results[0].Air.City.Aqi)
  142. result.PM10, _ = strconv.Atoi(airInfo.Results[0].Air.City.Pm10)
  143. result.Quality = airInfo.Results[0].Air.City.Quality
  144. if strings.Contains(airInfo.Results[0].Air.City.Quality, "污染") {
  145. result.Quality = "污染"
  146. }
  147. return result, nil
  148. }
  149. func GetWeatherInfoByLocation(location, key string) (*WeatherResp, error) {
  150. ctx := context.Background()
  151. url := fmt.Sprintf("%s%s&key=%s", api, location, key)
  152. res := g.Client().GetVar(ctx, url)
  153. var result WeatherResp
  154. err := res.Scan(&result)
  155. if err != nil {
  156. return nil, errors.New("天气查询失败")
  157. }
  158. return &result, nil
  159. }
  160. func GetDayWeatherInfoByLocation(location, key string) (*XinZhiDayWeatherInfo, error) {
  161. ctx := context.Background()
  162. url := fmt.Sprintf("%s%s&key=%s", dayApi, location, key)
  163. res := g.Client().GetVar(ctx, url)
  164. var result XinZhiDayWeatherInfo
  165. err := res.Scan(&result)
  166. if err != nil {
  167. return nil, errors.New("天气查询失败")
  168. }
  169. return &result, nil
  170. }
  171. func GetAirInfoByLocation(location, key string) (*AirInfo, error) {
  172. ctx := context.Background()
  173. url := fmt.Sprintf("%s%s&key=%s", airApi, location, key)
  174. res := g.Client().GetVar(ctx, url)
  175. var result AirInfo
  176. err := res.Scan(&result)
  177. if err != nil {
  178. return nil, errors.New("天气查询失败")
  179. }
  180. return &result, nil
  181. }