init
This commit is contained in:
parent
68c47e1023
commit
ccb2ae442a
135
gps_tool/an_na_qi_client.go
Normal file
135
gps_tool/an_na_qi_client.go
Normal file
@ -0,0 +1,135 @@
|
||||
package gps_tool
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AnNaQiGpsClient struct {
|
||||
AppCode string
|
||||
Host string
|
||||
}
|
||||
|
||||
func NewAnNaQiGpsClient(appCode string) *AnNaQiGpsClient {
|
||||
return &AnNaQiGpsClient{
|
||||
AppCode: appCode,
|
||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
||||
}
|
||||
}
|
||||
|
||||
func (n *AnNaQiGpsClient) GetGpsInfo(longitude, latitude float64) (res *AddressComponent, err error) {
|
||||
lo := strconv.FormatFloat(longitude, 'f', 6, 64)
|
||||
la := strconv.FormatFloat(latitude, 'f', 6, 64)
|
||||
location := lo + "," + la
|
||||
// 拼接URL
|
||||
var fullURL string
|
||||
fullURL, err = url.JoinPath(n.Host, "geocode/regeo_query")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取gps:%s信息失败,拼接路径错误", location)
|
||||
}
|
||||
param := "location=" + location
|
||||
// 创建请求
|
||||
var req *http.Request
|
||||
req, err = http.NewRequest(http.MethodPost, fullURL, bytes.NewBuffer([]byte(param)))
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取gps:%s信息失败,创建请求失败", location)
|
||||
}
|
||||
// 设置请求头
|
||||
req.Header.Add("Authorization", "APPCODE "+n.AppCode)
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
|
||||
|
||||
// 发送请求
|
||||
// 创建HTTP客户端
|
||||
client := &http.Client{}
|
||||
client.Timeout = 5 * time.Second
|
||||
var resp *http.Response
|
||||
resp, err = client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取gps:%s信息失败,发送请求失败", location)
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err = Body.Close()
|
||||
if err != nil {
|
||||
log.Printf("获取gps:%s信息,关闭响应体失败: %+v\n", location, err)
|
||||
}
|
||||
}(resp.Body)
|
||||
// 读取响应体
|
||||
var body []byte
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取gps:%s信息失败,读取响应体失败", location)
|
||||
}
|
||||
|
||||
// 解析JSON响应
|
||||
var apiResult ApiResult
|
||||
err = json.Unmarshal(body, &apiResult)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取gps:%s信息失败,解析JSON响应失败,%s", location, string(body))
|
||||
}
|
||||
// 检查API返回状态
|
||||
if apiResult.Code != 200 {
|
||||
return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败,%+v", location, apiResult))
|
||||
}
|
||||
|
||||
if len(apiResult.Data.Regeocodes) > 0 {
|
||||
return &apiResult.Data.Regeocodes[0].AddressComponent, nil
|
||||
}
|
||||
return nil, errors.New(fmt.Sprintf("获取gps:%s信息失败,%+v", location, apiResult))
|
||||
}
|
||||
|
||||
type ApiResult struct {
|
||||
Data Data `json:"data"`
|
||||
Msg string `json:"msg"`
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
TaskNo string `json:"taskNo"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Regeocodes []Regeocode `json:"regeocodes"`
|
||||
}
|
||||
|
||||
type Regeocode struct {
|
||||
FormattedAddress string `json:"formatted_address"`
|
||||
AddressComponent AddressComponent `json:"addressComponent"`
|
||||
}
|
||||
|
||||
type AddressComponent struct {
|
||||
BusinessAreas [][]interface{} `json:"businessAreas"`
|
||||
Country string `json:"country"`
|
||||
Province string `json:"province"`
|
||||
Citycode string `json:"citycode"`
|
||||
City string `json:"city"`
|
||||
Adcode string `json:"adcode"`
|
||||
StreetNumber StreetNumber `json:"streetNumber"`
|
||||
Towncode string `json:"towncode"`
|
||||
District string `json:"district"`
|
||||
Neighborhood Neighborhood `json:"neighborhood"`
|
||||
Township string `json:"township"`
|
||||
Building Building `json:"building"`
|
||||
}
|
||||
type StreetNumber struct {
|
||||
Number string `json:"number"`
|
||||
Distance string `json:"distance"`
|
||||
Street string `json:"street"`
|
||||
Location string `json:"location"`
|
||||
Direction string `json:"direction"`
|
||||
}
|
||||
|
||||
type Neighborhood struct {
|
||||
Name []interface{} `json:"name"`
|
||||
Type []interface{} `json:"type"`
|
||||
}
|
||||
|
||||
type Building struct {
|
||||
Name []interface{} `json:"name"`
|
||||
Type []interface{} `json:"type"`
|
||||
}
|
46
gps_tool/an_na_qi_client_test.go
Normal file
46
gps_tool/an_na_qi_client_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
package gps_tool
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAnNaQiGpsClient_GetGpsInfo(t *testing.T) {
|
||||
type fields struct {
|
||||
AppCode string
|
||||
Host string
|
||||
}
|
||||
type args struct {
|
||||
longitude float64
|
||||
latitude float64
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantRes interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test1",
|
||||
fields: fields{
|
||||
AppCode: "",
|
||||
Host: "https://jmgeocode.market.alicloudapi.com",
|
||||
},
|
||||
args: args{
|
||||
longitude: 109.770280,
|
||||
latitude: 23.565979,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
n := &AnNaQiGpsClient{
|
||||
AppCode: tt.fields.AppCode,
|
||||
Host: tt.fields.Host,
|
||||
}
|
||||
gotRes, err := n.GetGpsInfo(tt.args.longitude, tt.args.latitude)
|
||||
log.Println(gotRes, err)
|
||||
})
|
||||
}
|
||||
}
|
@ -12,19 +12,19 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type HuaChenIpInfo struct {
|
||||
type HuaChenIpClient struct {
|
||||
AppCode string
|
||||
Host string
|
||||
}
|
||||
|
||||
func NewHuaChenIpInfo(appCode string) *HuaChenIpInfo {
|
||||
return &HuaChenIpInfo{
|
||||
func NewHuaChenIpClient(appCode string) *HuaChenIpClient {
|
||||
return &HuaChenIpClient{
|
||||
AppCode: appCode,
|
||||
Host: "https://c2ba.api.huachen.cn",
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HuaChenIpInfo) GetIpInfo(ip string) (res *ApiResult, err error) {
|
||||
func (h *HuaChenIpClient) GetIpInfo(ip string) (res *ApiResult, err error) {
|
||||
if ip == "" {
|
||||
return nil, errors.New("请输入ip地址")
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (h *HuaChenIpInfo) GetIpInfo(ip string) (res *ApiResult, err error) {
|
||||
client := &http.Client{}
|
||||
// 创建请求
|
||||
var req *http.Request
|
||||
req, err = http.NewRequest("GET", fullURL, nil)
|
||||
req, err = http.NewRequest(http.MethodGet, fullURL, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "获取ip:%s信息失败,创建请求失败", ip)
|
||||
}
|
44
ip_tool/hua_chen_client_test.go
Normal file
44
ip_tool/hua_chen_client_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package ip_tool
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHuaChenIpClient_GetIpInfo(t *testing.T) {
|
||||
type fields struct {
|
||||
AppCode string
|
||||
Host string
|
||||
}
|
||||
type args struct {
|
||||
ip string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
wantRes *ApiResult
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test1",
|
||||
fields: fields{
|
||||
AppCode: "",
|
||||
Host: "https://c2ba.api.huachen.cn",
|
||||
},
|
||||
args: args{
|
||||
ip: "8.138.116.112",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := &HuaChenIpClient{
|
||||
AppCode: tt.fields.AppCode,
|
||||
Host: tt.fields.Host,
|
||||
}
|
||||
gotRes, err := h.GetIpInfo(tt.args.ip)
|
||||
log.Println(gotRes, err)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user