- update qq

master
李光春 1 year ago
parent 0e39809c43
commit a0e6bcb331

@ -2,6 +2,7 @@
- add [caiyunapp](service%2Fcaiyunapp)
- add [amap](service%2Famap)
- add [qq](service%2Fqq)
## v1.0.109

@ -0,0 +1,35 @@
package qq
import (
"github.com/dtapps/go-library/utils/golog"
"github.com/dtapps/go-library/utils/gorequest"
)
// ClientConfig 实例配置
type ClientConfig struct {
Key string
}
// Client 实例
type Client struct {
requestClient *gorequest.App // 请求服务
config struct {
key string
}
log struct {
status bool // 状态
client *golog.ApiClient // 日志服务
}
}
// NewClient 创建实例化
func NewClient(config *ClientConfig) (*Client, error) {
c := &Client{}
c.config.key = config.Key
c.requestClient = gorequest.NewHttp()
return c, nil
}

@ -0,0 +1,9 @@
package qq
const (
apiUrl = "https://apis.map.qq.com"
)
const (
LogTable = "qq"
)

@ -0,0 +1,11 @@
package qq
import "github.com/dtapps/go-library/utils/golog"
func (c *Client) GetKey() string {
return c.config.key
}
func (c *Client) GetLog() *golog.ApiClient {
return c.log.client
}

@ -0,0 +1,59 @@
package qq
import (
"context"
"github.com/dtapps/go-library/utils/gojson"
"github.com/dtapps/go-library/utils/gorequest"
"net/http"
)
type IpResponse struct {
Status int `json:"status"` // 状态码0为正常其它为异常
Message string `json:"message"` // 对status的描述
RequestId string `json:"request_id"`
Result struct {
Ip string `json:"ip"` // 用于定位的IP地址
Location struct {
Lat float64 `json:"lat"` // 纬度
Lng float64 `json:"lng"` // 经度
} `json:"location"` // 定位坐标。注IP定位服务精确到市级该位置为IP地址所属的行政区划政府坐标
AdInfo struct {
Nation string `json:"nation"` // 国家
Province string `json:"province"` // 国家代码ISO3166标准3位数字码
City string `json:"city"` // 省
District string `json:"district"` // 市
Adcode int `json:"adcode"` // 区
NationCode int `json:"nation_code"` // 行政区划代码
} `json:"ad_info"` // 定位行政区划信息
} `json:"result"` // IP定位结果
}
type IpResult struct {
Result IpResponse // 结果
Body []byte // 内容
Http gorequest.Response // 请求
}
func newIpResult(result IpResponse, body []byte, http gorequest.Response) *IpResult {
return &IpResult{Result: result, Body: body, Http: http}
}
// Ip IP定位
// https://lbs.amap.com/api/webservice/guide/api/ipconfig
func (c *Client) Ip(ctx context.Context, ip string, notMustParams ...gorequest.Params) (*IpResult, error) {
// 参数
params := gorequest.NewParamsWith(notMustParams...)
params.Set("key", c.GetKey())
params.Set("ip", ip)
params.Set("output", "JSON")
// 请求
request, err := c.request(ctx, apiUrl+"/ws/location/v1/ip", params, http.MethodGet)
if err != nil {
return newIpResult(IpResponse{}, request.ResponseBody, request), err
}
// 定义
var response IpResponse
err = gojson.Unmarshal(request.ResponseBody, &response)
return newIpResult(response, request.ResponseBody, request), err
}

@ -0,0 +1,41 @@
package qq
import (
"context"
"github.com/dtapps/go-library"
"github.com/dtapps/go-library/utils/gorequest"
)
func (c *Client) request(ctx context.Context, url string, params map[string]interface{}, method string) (gorequest.Response, error) {
// 创建请求
client := c.requestClient
// 设置请求地址
client.SetUri(url)
// 设置方式
client.SetMethod(method)
// 设置格式
client.SetContentTypeJson()
// 设置参数
client.SetParams(params)
// 传入SDk版本
client.AfferentSdkUserVersion(go_library.Version())
// 发起请求
request, err := client.Request(ctx)
if err != nil {
return gorequest.Response{}, err
}
// 记录日志
if c.log.status {
go c.log.client.Middleware(ctx, request, go_library.Version())
}
return request, err
}
Loading…
Cancel
Save