Files
2021-09-30 12:09:45 +08:00

212 lines
6.0 KiB
Go

package platform
import (
"SciencesServer/utils"
)
// 高德API
type Gaode struct{}
type (
Location struct {
Province string `json:"province"`
City string `json:"city"`
AdCode string `json:"adcode"`
Rectangle string `json:"rectangle"`
}
// GaoDeLocationResponse 第三方位置响应参数
GaoDeLocationResponse struct {
Status string `json:"status"`
Count string `json:"count"`
Info string `json:"info"`
InfoCode string `json:"infocode"`
GeoCodes []struct {
FormattedAddress string `json:"formatted_address"`
Country string `json:"country"`
Province string `json:"province"`
CityCode string `json:"citycode"`
City string `json:"city"`
District string `json:"district"`
Township []string `json:"township"`
Neighborhood []struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"neighborhood"`
Building []struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"building"`
AdCode string `json:"adcode"`
Street string `json:"street"`
Number string `json:"number"`
Location string `json:"location"`
Level string `json:"level"`
} `json:"geocodes"`
}
// GaoDeLocationReverseResponse 第三方反向位置响应参数
GaoDeLocationReverseResponse struct {
Status string `json:"status"`
Count string `json:"count"`
Info string `json:"info"`
InfoCode string `json:"infocode"`
ReGeoCode struct {
FormattedAddress string `json:"formatted_address"`
AddressComponent struct {
Country string `json:"country"`
Province string `json:"province"`
City string `json:"city"`
CityCode string `json:"citycode"`
District string `json:"district"`
AdCode string `json:"adcode"`
Township string `json:"township"`
TownCode string `json:"town_code"`
Neighborhood struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"neighborhood"`
Building struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"building"`
StreetNumber struct {
Street string `json:"street"`
Number string `json:"number"`
Location string `json:"location"`
Direction string `json:"direction"`
Distance string `json:"distance"`
} `json:"streetNumber"`
BusinessAreas []struct {
Location string `json:"location"`
Name string `json:"name"`
ID string `json:"id"`
} `json:"businessAreas"`
} `json:"addressComponent"`
} `json:"regeocode"`
}
// GaoDeLocationIPResponse IP定位查询
GaoDeLocationIPResponse struct {
Status string `json:"status"`
Count string `json:"count"`
Info string `json:"info"`
InfoCode string `json:"infocode"`
Province interface{} `json:"province"`
City interface{} `json:"city"`
AdCode interface{} `json:"adcode"`
Rectangle interface{} `json:"rectangle"`
}
// GaoDeWeatherResponse 第三方天气响应参数
GaoDeWeatherResponse struct {
Status string `json:"status"`
Count string `json:"count"`
Info string `json:"info"`
InfoCode string `json:"infocode"`
Lives []struct {
Province string `json:"province"`
City string `json:"city"`
AdCode string `json:"adcode"`
Weather string `json:"weather"`
Temperature string `json:"temperature"`
WindDirection string `json:"winddirection"`
WindPower string `json:"windpower"`
Humidity string `json:"humidity"`
ReportTime string `json:"reporttime"`
} `json:"lives"`
}
)
const (
GDKey = "11b1e9db2756e3482b10754bf59d0d94"
gaoDeRequestURLForLocation string = "https://restapi.amap.com/v3/geocode/geo"
gaoDeRequestURLForLocationReverse string = "https://restapi.amap.com/v3/geocode/regeo"
gaoDeRequestURLForLocationIP string = "https://restapi.amap.com/v3/ip"
gaoDeRequestURLForLocationWeather string = "https://restapi.amap.com/v3/weather/weatherInfo"
)
// Location 位置信息
func (this *Gaode) Location(address, city string) (*GaoDeLocationResponse, error) {
params := map[string]interface{}{
"key": GDKey, "address": "address", "city": city,
}
client := utils.NewClient(gaoDeRequestURLForLocation, utils.MethodForGet, params)
resp, err := client.Request(utils.RequestBodyFormatForFormData)
if err != nil {
return nil, err
}
response := new(GaoDeLocationResponse)
_ = utils.FromJSONBytes(resp, response)
return response, nil
}
// LocationReverse 反向位置
func (this *Gaode) LocationReverse(location string) (*GaoDeLocationReverseResponse, error) {
params := map[string]interface{}{
"key": GDKey, "location": location,
}
client := utils.NewClient(gaoDeRequestURLForLocationReverse, utils.MethodForGet, params)
resp, err := client.Request(utils.RequestBodyFormatForFormData)
if err != nil {
return nil, err
}
response := new(GaoDeLocationReverseResponse)
_ = utils.FromJSONBytes(resp, response)
return response, nil
}
// LocationIP IP查询
func (this *Gaode) LocationIP(ip string) (*GaoDeLocationIPResponse, error) {
params := map[string]interface{}{
"key": GDKey,
}
if ip != "" {
params["ip"] = ip
}
client := utils.NewClient(gaoDeRequestURLForLocationIP, utils.MethodForGet, params)
resp, err := client.Request(utils.RequestBodyFormatForFormData)
if err != nil {
return nil, err
}
response := new(GaoDeLocationIPResponse)
_ = utils.FromJSONBytes(resp, response)
return response, nil
}
// Weather 天气信息
func (this *Gaode) Weather(city string) (*GaoDeWeatherResponse, error) {
params := map[string]interface{}{
"key": GDKey, "city": city,
}
client := utils.NewClient(gaoDeRequestURLForLocationWeather, utils.MethodForGet, params)
resp, err := client.Request(utils.RequestBodyFormatForFormData)
if err != nil {
return nil, err
}
response := new(GaoDeWeatherResponse)
_ = utils.FromJSONBytes(resp, response)
return response, nil
}
func NewGaode() *Gaode {
return &Gaode{}
}