kuaiDi100.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package service
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. )
  12. type KdState struct {
  13. Message string `json:"message"`
  14. State string `json:"state"`
  15. Status string `json:"status"`
  16. IsCheck string `json:"ischeck"`
  17. }
  18. var KD100Flags = map[string]string{
  19. "ane66": "安能快递",
  20. "debangwuliu": "德邦物流",
  21. "debangkuaidi": "德邦快递",
  22. "ems": "EMS",
  23. "guotongkuaidi": "国通快递",
  24. "huitongkuaidi": "百世快递",
  25. "jd": "京东物流",
  26. "kuayue": "跨越速运",
  27. "pjbest": "品骏快递",
  28. "shentong": "申通快递",
  29. "shunfeng": "顺丰速运",
  30. "suer": "速尔快递",
  31. "xinfengwuliu": "信丰物流",
  32. "youshuwuliu": "优速物流",
  33. "youzhengguonei": "邮政快递包裹",
  34. "yuantong": "圆通速递",
  35. "yuantongguoji": "圆通国际",
  36. "yunda": "韵达快递",
  37. "zhaijisong": "宅急送",
  38. "zhongtong": "中通快递",
  39. "ewe": "EWE全球快递",
  40. "quanyikuaidi": "全一快递",
  41. "tiantian": "天天快递",
  42. "sxjdfreight": "顺心捷达",
  43. "dhl": "DHL-中国件",
  44. "tnt": "TNT",
  45. "other": "其它快递",
  46. }
  47. // GetKDStatus 获取快递跟踪信息
  48. func GetKDStatus(com, num string) string {
  49. fmt.Printf("查询物流公司为 %v, 快递为编号为 %v 的快递\n", KD100Flags[com], num)
  50. key := "jqayXRgj8154" //客户授权key
  51. customer := "E59437A1C1C69273AEB48F587CEF57B4" //查询公司编号
  52. postUrl := "https://poll.kuaidi100.com/poll/query.do" //实时查询请求地址
  53. paramData := make(map[string]string)
  54. paramData["com"] = com //快递公司编码
  55. paramData["num"] = num //快递单号
  56. paramDataSlice, _ := json.Marshal(paramData)
  57. paramjson := string(paramDataSlice)
  58. sign := strings.ToUpper(GetMD5Encode(paramjson + key + customer))
  59. //POST请求需要三个参数,分别为customer(CustomerId)和sign(签名)和param(参数)
  60. postRes, postErr := http.PostForm(postUrl, url.Values{"customer": {customer}, "sign": {sign}, "param": {paramjson}})
  61. if postErr != nil {
  62. fmt.Println("查询失败" + postErr.Error())
  63. return "查询失败"
  64. }
  65. postBody, err := ioutil.ReadAll(postRes.Body)
  66. if err != nil {
  67. fmt.Println("查询失败,请至快递公司官网自行查询" + err.Error())
  68. return "查询失败,请至快递公司官网自行查询"
  69. }
  70. resp := KdState{}
  71. err = json.Unmarshal(postBody, &resp)
  72. if err != nil {
  73. fmt.Println("json.Unmarshal error", err.Error())
  74. return "查询失败"
  75. }
  76. if resp.IsCheck == "1" {
  77. fmt.Println("查询成功,快递已签收")
  78. } else {
  79. fmt.Println("查询成功,快递尚未签收")
  80. }
  81. return resp.IsCheck
  82. }
  83. // GetMD5Encode 返回一个32位md5加密后的字符串
  84. func GetMD5Encode(data string) string {
  85. h := md5.New()
  86. h.Write([]byte(data))
  87. return hex.EncodeToString(h.Sum(nil))
  88. }