From a0e6bcb3311400ae7220b844d54a84248dbe37c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 28 Mar 2023 22:46:29 +0800 Subject: [PATCH] - update qq --- CHANGELOG.md | 1 + service/qq/client.go | 35 +++++++++++++++++++++++++ service/qq/const.go | 9 +++++++ service/qq/get.go | 11 ++++++++ service/qq/ip.go | 59 +++++++++++++++++++++++++++++++++++++++++++ service/qq/request.go | 41 ++++++++++++++++++++++++++++++ 6 files changed, 156 insertions(+) create mode 100644 service/qq/client.go create mode 100644 service/qq/const.go create mode 100644 service/qq/get.go create mode 100644 service/qq/ip.go create mode 100644 service/qq/request.go diff --git a/CHANGELOG.md b/CHANGELOG.md index cad4e2e2..16c66018 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - add [caiyunapp](service%2Fcaiyunapp) - add [amap](service%2Famap) +- add [qq](service%2Fqq) ## v1.0.109 diff --git a/service/qq/client.go b/service/qq/client.go new file mode 100644 index 00000000..ae52f62b --- /dev/null +++ b/service/qq/client.go @@ -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 +} diff --git a/service/qq/const.go b/service/qq/const.go new file mode 100644 index 00000000..392d3c2f --- /dev/null +++ b/service/qq/const.go @@ -0,0 +1,9 @@ +package qq + +const ( + apiUrl = "https://apis.map.qq.com" +) + +const ( + LogTable = "qq" +) diff --git a/service/qq/get.go b/service/qq/get.go new file mode 100644 index 00000000..e1115f64 --- /dev/null +++ b/service/qq/get.go @@ -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 +} diff --git a/service/qq/ip.go b/service/qq/ip.go new file mode 100644 index 00000000..a68cc316 --- /dev/null +++ b/service/qq/ip.go @@ -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 +} diff --git a/service/qq/request.go b/service/qq/request.go new file mode 100644 index 00000000..8a0ed726 --- /dev/null +++ b/service/qq/request.go @@ -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 +}