111 lines
3.4 KiB
Go
111 lines
3.4 KiB
Go
package coryCommon
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/net/gclient"
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
)
|
|
|
|
var key = "00b60ebda96849e694cb570e3d4f5c89"
|
|
|
|
// WeatherRep 返回参数 免费天气查询 https://dev.qweather.com/docs/api/weather/weather-daily-forecast/
|
|
type WeatherRep struct {
|
|
Code string `json:"code"`
|
|
UpdateTime string `json:"updateTime"`
|
|
FxLink string `json:"fxLink"`
|
|
Daily []struct {
|
|
FxDate string `json:"fxDate"`
|
|
Sunrise string `json:"sunrise"`
|
|
Sunset string `json:"sunset"`
|
|
Moonrise string `json:"moonrise"`
|
|
Moonset string `json:"moonset"`
|
|
MoonPhase string `json:"moonPhase"`
|
|
MoonPhaseIcon string `json:"moonPhaseIcon"`
|
|
TempMax string `json:"tempMax"`
|
|
TempMin string `json:"tempMin"`
|
|
IconDay string `json:"iconDay"`
|
|
TextDay string `json:"textDay"`
|
|
IconNight string `json:"iconNight"`
|
|
TextNight string `json:"textNight"`
|
|
Wind360Day string `json:"wind360Day"`
|
|
WindDirDay string `json:"windDirDay"`
|
|
WindScaleDay string `json:"windScaleDay"`
|
|
WindSpeedDay string `json:"windSpeedDay"`
|
|
Wind360Night string `json:"wind360Night"`
|
|
WindDirNight string `json:"windDirNight"`
|
|
WindScaleNight string `json:"windScaleNight"`
|
|
WindSpeedNight string `json:"windSpeedNight"`
|
|
Humidity string `json:"humidity"`
|
|
Precip string `json:"precip"`
|
|
Pressure string `json:"pressure"`
|
|
Vis string `json:"vis"`
|
|
Cloud string `json:"cloud"`
|
|
UvIndex string `json:"uvIndex"`
|
|
} `json:"daily"`
|
|
Refer struct {
|
|
Sources []string `json:"sources"`
|
|
License []string `json:"license"`
|
|
} `json:"refer"`
|
|
}
|
|
|
|
// Weather 传递经纬度 location := "116.41,39.92"
|
|
func Weather(location string) (we string) {
|
|
//请求路径
|
|
//key := ""
|
|
requestURL := "https://devapi.qweather.com/v7/weather/3d?location=" + location + "&key=" + key
|
|
|
|
response, err := gclient.New().ContentJson().ContentType("application/x-www-form-urlencoded").Get(gctx.New(), requestURL)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var dataInfo = ""
|
|
dataInfo = response.ReadAllString()
|
|
return dataInfo
|
|
}
|
|
|
|
type GridPointRes struct {
|
|
Code string `json:"code"`
|
|
UpdateTime string `json:"updateTime"`
|
|
FxLink string `json:"fxLink"`
|
|
Now struct {
|
|
ObsTime string `json:"obsTime"`
|
|
Temp string `json:"temp"`
|
|
Icon string `json:"icon"`
|
|
Text string `json:"text"`
|
|
Wind360 string `json:"wind360"`
|
|
WindDir string `json:"windDir"`
|
|
WindScale string `json:"windScale"`
|
|
WindSpeed string `json:"windSpeed"`
|
|
Humidity string `json:"humidity"`
|
|
Precip string `json:"precip"`
|
|
Pressure string `json:"pressure"`
|
|
Cloud string `json:"cloud"`
|
|
Dew string `json:"dew"`
|
|
} `json:"now"`
|
|
Refer struct {
|
|
Sources []string `json:"sources"`
|
|
License []string `json:"license"`
|
|
} `json:"refer"`
|
|
}
|
|
|
|
// 格点天气 GridPoint
|
|
func GridPoint(location string) (gp *GridPointRes, err error) {
|
|
//请求路径
|
|
//key := "00b60ebda96849e694cb570e3d4f5c89"
|
|
requestURL := "https://devapi.qweather.com/v7/grid-weather/now?location=" + location + "&key=" + key
|
|
|
|
response, err := gclient.New().ContentJson().ContentType("application/x-www-form-urlencoded").Get(gctx.New(), requestURL)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var dataInfo = ""
|
|
dataInfo = response.ReadAllString()
|
|
|
|
gp = new(GridPointRes)
|
|
err = json.Unmarshal([]byte(dataInfo), &gp)
|
|
|
|
fmt.Println(gp.Now.Icon)
|
|
return
|
|
}
|