From 6e49867d6f307c9dc4ee243cd2b0c986d9c0898c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Thu, 20 Jul 2023 10:43:28 +0800 Subject: [PATCH] - update kuaidi100 --- service/kuaidi100/client.go | 37 ++++++++++++++++++ service/kuaidi100/config.go | 17 +++++++++ service/kuaidi100/const.go | 9 +++++ service/kuaidi100/get.go | 9 +++++ service/kuaidi100/poll.go | 40 +++++++++++++++++++ service/kuaidi100/poll.query.go | 68 +++++++++++++++++++++++++++++++++ service/kuaidi100/request.go | 49 ++++++++++++++++++++++++ service/kuaidi100/sign.go | 10 +++++ 8 files changed, 239 insertions(+) create mode 100644 service/kuaidi100/client.go create mode 100644 service/kuaidi100/config.go create mode 100644 service/kuaidi100/const.go create mode 100644 service/kuaidi100/get.go create mode 100644 service/kuaidi100/poll.go create mode 100644 service/kuaidi100/poll.query.go create mode 100644 service/kuaidi100/request.go create mode 100644 service/kuaidi100/sign.go diff --git a/service/kuaidi100/client.go b/service/kuaidi100/client.go new file mode 100644 index 00000000..622b3cb7 --- /dev/null +++ b/service/kuaidi100/client.go @@ -0,0 +1,37 @@ +package kuaidi100 + +import ( + "github.com/dtapps/go-library/utils/golog" + "github.com/dtapps/go-library/utils/gorequest" +) + +// ClientConfig 实例配置 +type ClientConfig struct { + Customer string // 授权码 + Key string // 密钥 +} + +// Client 实例 +type Client struct { + requestClient *gorequest.App // 请求服务 + config struct { + customer string // 授权码 + key string // 密钥 + } + zap struct { + status bool // 状态 + client *golog.ApiZapLog // 日志服务 + } +} + +// NewClient 创建实例化 +func NewClient(config *ClientConfig) (*Client, error) { + + c := &Client{} + + c.config.customer = config.Customer + + c.requestClient = gorequest.NewHttp() + + return c, nil +} diff --git a/service/kuaidi100/config.go b/service/kuaidi100/config.go new file mode 100644 index 00000000..07c7fc66 --- /dev/null +++ b/service/kuaidi100/config.go @@ -0,0 +1,17 @@ +package kuaidi100 + +import "github.com/dtapps/go-library/utils/golog" + +func (c *Client) Config(customer string) *Client { + c.config.customer = customer + return c +} + +// ConfigZapClientFun 日志配置 +func (c *Client) ConfigZapClientFun(apiZapLogFun golog.ApiZapLogFun) { + apiZapLog := apiZapLogFun() + if apiZapLog != nil { + c.zap.client = apiZapLog + c.zap.status = true + } +} diff --git a/service/kuaidi100/const.go b/service/kuaidi100/const.go new file mode 100644 index 00000000..12431333 --- /dev/null +++ b/service/kuaidi100/const.go @@ -0,0 +1,9 @@ +package kuaidi100 + +const ( + apiUrl = "https://poll.kuaidi100.com" +) + +const ( + LogTable = "kuaidi100" +) diff --git a/service/kuaidi100/get.go b/service/kuaidi100/get.go new file mode 100644 index 00000000..f22deba8 --- /dev/null +++ b/service/kuaidi100/get.go @@ -0,0 +1,9 @@ +package kuaidi100 + +func (c *Client) GetCustomer() string { + return c.config.customer +} + +func (c *Client) GetKey() string { + return c.config.key +} diff --git a/service/kuaidi100/poll.go b/service/kuaidi100/poll.go new file mode 100644 index 00000000..22e84d68 --- /dev/null +++ b/service/kuaidi100/poll.go @@ -0,0 +1,40 @@ +package kuaidi100 + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type PollResponse struct { + Result bool `json:"result"` + ReturnCode string `json:"returnCode"` + Message string `json:"message"` +} + +type PollResult struct { + Result PollResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newPollResult(result PollResponse, body []byte, http gorequest.Response) *PollResult { + return &PollResult{Result: result, Body: body, Http: http} +} + +// Poll 实时快递查询接口 +// https://api.kuaidi100.com/document/5f0ffb5ebc8da837cbd8aefc +func (c *Client) Poll(ctx context.Context, notMustParams ...gorequest.Params) (*PollResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, apiUrl+"/poll", params, http.MethodPost) + if err != nil { + return newPollResult(PollResponse{}, request.ResponseBody, request), err + } + // 定义 + var response PollResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newPollResult(response, request.ResponseBody, request), err +} diff --git a/service/kuaidi100/poll.query.go b/service/kuaidi100/poll.query.go new file mode 100644 index 00000000..3f702506 --- /dev/null +++ b/service/kuaidi100/poll.query.go @@ -0,0 +1,68 @@ +package kuaidi100 + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "github.com/dtapps/go-library/utils/gorequest" + "net/http" +) + +type PollQueryResponse struct { + Message string `json:"message"` // 消息体,请忽略 + Nu string `json:"nu"` // 单号 + Ischeck string `json:"ischeck"` // 是否签收标记,0未签收,1已签收,请忽略,明细状态请参考state字段 + Com string `json:"com"` // 快递公司编码,一律用小写字母 + Status string `json:"status"` // 通讯状态,请忽略 + Data []struct { + Time string `json:"time"` // 时间,原始格式 + Context string `json:"context"` // 内容 + Ftime string `json:"ftime"` // 格式化后时间 + AreaCode string `json:"areaCode"` // 本数据元对应的行政区域的编码,实时查询接口中提交resultv2=1或者resultv2=4标记后才会出现 + AreaName string `json:"areaName"` // 本数据元对应的行政区域的名称,实时查询接口中提交resultv2=1或者resultv2=4标记后才会出现 + Status string `json:"status"` // 本数据元对应的物流状态名称或者高级状态名称,实时查询接口中提交resultv2=1或者resultv2=4标记后才会出现 + Location string `json:"location"` // 本数据元对应的快件当前地点,实时查询接口中提交resultv2=4标记后才会出现 + AreaCenter string `json:"areaCenter"` // 本数据元对应的行政区域经纬度,实时查询接口中提交resultv2=4标记后才会出现 + AreaPinYin string `json:"areaPinYin"` // 本数据元对应的行政区域拼音,实时查询接口中提交resultv2=4标记后才会出现 + StatusCode string `json:"statusCode"` // 本数据元对应的高级物流状态值,实时查询接口中提交resultv2=4标记后才会出现 + } `json:"data"` // 最新查询结果,数组,包含多项,全量,倒序(即时间最新的在最前),每项都是对象,对象包含字段请展开 + State string `json:"state"` // 快递单当前状态,默认为0在途,1揽收,2疑难,3签收,4退签,5派件,8清关,14拒签等10个基础物流状态,如需要返回高级物流状态,请参考 resultv2 传值 + Condition string `json:"condition"` // 快递单明细状态标记,暂未实现,请忽略 + RouteInfo struct { + From struct { + Number string `json:"number"` + Name string `json:"name"` + } `json:"from"` + Cur struct { + Number string `json:"number"` + Name string `json:"name"` + } `json:"cur"` + To interface{} `json:"to"` + } `json:"routeInfo"` + IsLoop bool `json:"isLoop"` +} + +type PollQueryResult struct { + Result PollQueryResponse // 结果 + Body []byte // 内容 + Http gorequest.Response // 请求 +} + +func newPollQueryResult(result PollQueryResponse, body []byte, http gorequest.Response) *PollQueryResult { + return &PollQueryResult{Result: result, Body: body, Http: http} +} + +// PollQuery 实时快递查询接口 +// https://api.kuaidi100.com/document/5f0ffb5ebc8da837cbd8aefc +func (c *Client) PollQuery(ctx context.Context, notMustParams ...gorequest.Params) (*PollQueryResult, error) { + // 参数 + params := gorequest.NewParamsWith(notMustParams...) + // 请求 + request, err := c.request(ctx, apiUrl+"/poll/query.do", params, http.MethodPost) + if err != nil { + return newPollQueryResult(PollQueryResponse{}, request.ResponseBody, request), err + } + // 定义 + var response PollQueryResponse + err = gojson.Unmarshal(request.ResponseBody, &response) + return newPollQueryResult(response, request.ResponseBody, request), err +} diff --git a/service/kuaidi100/request.go b/service/kuaidi100/request.go new file mode 100644 index 00000000..d2641dcd --- /dev/null +++ b/service/kuaidi100/request.go @@ -0,0 +1,49 @@ +package kuaidi100 + +import ( + "context" + "github.com/dtapps/go-library/utils/gojson" + "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) { + + newParams := gorequest.NewParams() + + // 公共参数 + newParams["customer"] = c.GetCustomer() + + // 请求参数 + newParams["param"] = gojson.JsonEncodeNoError(params) + + // 签名 + newParams["sign"] = c.getSign(gojson.JsonEncodeNoError(params)) + + // 创建请求 + client := c.requestClient + + // 设置请求地址 + client.SetUri(url) + + // 设置方式 + client.SetMethod(method) + + // 设置格式 + client.SetContentTypeForm() + + // 设置参数 + client.SetParams(params) + + // 发起请求 + request, err := client.Request(ctx) + if err != nil { + return gorequest.Response{}, err + } + + // 记录日志 + if c.zap.status { + go c.zap.client.Middleware(ctx, request) + } + + return request, err +} diff --git a/service/kuaidi100/sign.go b/service/kuaidi100/sign.go new file mode 100644 index 00000000..932a6c13 --- /dev/null +++ b/service/kuaidi100/sign.go @@ -0,0 +1,10 @@ +package kuaidi100 + +import ( + "fmt" + "github.com/dtapps/go-library/utils/gomd5" +) + +func (c *Client) getSign(param string) string { + return gomd5.ToUpper(fmt.Sprintf("%s%s%s", param, c.GetKey(), c.GetCustomer())) +}