|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package kd100
-
- import (
- "applet/app/utils"
- "applet/app/utils/logx"
- "crypto/tls"
- "encoding/json"
- "fmt"
- "github.com/pkg/errors"
- "io/ioutil"
- "net/http"
- "net/url"
- "strings"
- "time"
- )
-
- type kd100 struct {
- key string //客户授权key
- customer string //查询公司编号
- }
-
- const postUrl = "https://poll.kuaidi100.com/poll/query.do"
-
- func NewKd100(key, customer string) *kd100 {
- return &kd100{
- key: key,
- customer: customer,
- }
- }
-
- func (kd *kd100) Query(num, comCode string) (map[string]interface{}, error) {
- paramData := make(map[string]string)
- paramData["com"] = comCode //快递公司编码
- paramData["num"] = num //快递单号
-
- str, _ := json.Marshal(paramData)
- paramJson := string(str)
- sign := kd.getSign(paramJson)
-
- // 解决 Go http请求报错x509 certificate signed by unknown authority 问题
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- }
-
- client := &http.Client{
- Timeout: 15 * time.Second,
- Transport: tr,
- }
-
- postRes, postErr := client.PostForm(postUrl, url.Values{"customer": {kd.customer}, "sign": {sign}, "param": {paramJson}})
- if postErr != nil {
- _ = logx.Error(postErr)
- return nil, errors.New(postErr.Error())
- }
- postBody, err := ioutil.ReadAll(postRes.Body)
- if err != nil {
- _ = logx.Error(postErr)
- return nil, errors.New("查询失败,请至快递公司官网自行查询")
- }
-
- fmt.Println(string(postBody))
-
- resp := make(map[string]interface{})
- err = json.Unmarshal(postBody, &resp)
- if err != nil {
- _ = logx.Error(postErr)
- return nil, errors.New("查询失败,请至快递公司官网自行查询")
- }
-
- defer func() {
- _ = postRes.Body.Close()
- }()
-
- return resp, nil
- }
-
- func (kd *kd100) getSign(params string) string {
- return strings.ToUpper(utils.Md5(params + kd.key + kd.customer))
- }
|