api.go 6.5 KB

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