面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

80 lines
1.8 KiB

  1. package kd100
  2. import (
  3. "applet/app/utils"
  4. "applet/app/utils/logx"
  5. "crypto/tls"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/pkg/errors"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. "time"
  14. )
  15. type kd100 struct {
  16. key string //客户授权key
  17. customer string //查询公司编号
  18. }
  19. const postUrl = "https://poll.kuaidi100.com/poll/query.do"
  20. func NewKd100(key, customer string) *kd100 {
  21. return &kd100{
  22. key: key,
  23. customer: customer,
  24. }
  25. }
  26. func (kd *kd100) Query(num, comCode string) (map[string]interface{}, error) {
  27. paramData := make(map[string]string)
  28. paramData["com"] = comCode //快递公司编码
  29. paramData["num"] = num //快递单号
  30. str, _ := json.Marshal(paramData)
  31. paramJson := string(str)
  32. sign := kd.getSign(paramJson)
  33. // 解决 Go http请求报错x509 certificate signed by unknown authority 问题
  34. tr := &http.Transport{
  35. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  36. }
  37. client := &http.Client{
  38. Timeout: 15 * time.Second,
  39. Transport: tr,
  40. }
  41. postRes, postErr := client.PostForm(postUrl, url.Values{"customer": {kd.customer}, "sign": {sign}, "param": {paramJson}})
  42. if postErr != nil {
  43. _ = logx.Error(postErr)
  44. return nil, errors.New(postErr.Error())
  45. }
  46. postBody, err := ioutil.ReadAll(postRes.Body)
  47. if err != nil {
  48. _ = logx.Error(postErr)
  49. return nil, errors.New("查询失败,请至快递公司官网自行查询")
  50. }
  51. fmt.Println(string(postBody))
  52. resp := make(map[string]interface{})
  53. err = json.Unmarshal(postBody, &resp)
  54. if err != nil {
  55. _ = logx.Error(postErr)
  56. return nil, errors.New("查询失败,请至快递公司官网自行查询")
  57. }
  58. defer func() {
  59. _ = postRes.Body.Close()
  60. }()
  61. return resp, nil
  62. }
  63. func (kd *kd100) getSign(params string) string {
  64. return strings.ToUpper(utils.Md5(params + kd.key + kd.customer))
  65. }