-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkuaidi100.go
201 lines (192 loc) · 5.22 KB
/
kuaidi100.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package mcommon
import (
"crypto/md5"
"encoding/hex"
"fmt"
"strings"
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"github.com/parnurzeal/gorequest"
)
/*
0 在途 快件处于运输过程中
1 揽收 快件已由快递公司揽收
2 疑难 快递100无法解析的状态,或者是需要人工介入的状态, 比方说收件人电话错误。
3 签收 正常签收
4 退签 货物退回发货人并签收
5 派件 货物正在进行派件
6 退回 货物正处于返回发货人的途中
7 转投 货物转给其他快递公司邮寄
10 待清关 货物等待清关
11 清关中 货物正在清关流程中
12 已清关 货物已完成清关流程
13 清关异常 货物在清关过程中出现异常
14 拒签 收件人明确拒收
*/
const (
Kuaidi100StateOnTheWay = 0
Kuaidi100StateCollect = 1
Kuaidi100StateDifficult = 2
Kuaidi100StateSignFor = 3
Kuaidi100StateReturnSignFor = 4
Kuaidi100StateDispatch = 5
Kuaidi100StateReturnOnTheWay = 6
Kuaidi100StateSwitching = 7
Kuaidi100StateToBeCleared = 10
Kuaidi100StateClearing = 11
Kuaidi100StateCleared = 12
Kuaidi100StateClearError = 13
Kuaidi100StateReject = 14
)
// StKuaidi100PollResp 推送
type StKuaidi100PollResp struct {
Result bool `json:"result"`
ReturnCode string `json:"returnCode"`
Message string `json:"message"`
}
// StKuaidi100GetResp 获取
type StKuaidi100GetResp struct {
Message string `json:"message"`
Nu string `json:"nu"`
Ischeck string `json:"ischeck"`
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"`
AreaName string `json:"areaName"`
Status string `json:"status"`
} `json:"data"`
State string `json:"state"`
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 struct {
Number string `json:"number"`
Name string `json:"name"`
} `json:"to"`
} `json:"routeInfo"`
IsLoop bool `json:"isLoop"`
}
// StKuaidi100CbBody 回调
type StKuaidi100CbBody struct {
Message string `json:"message"`
ComOld string `json:"comOld"`
Status string `json:"status"`
LastResult struct {
Nu string `json:"nu"`
Message string `json:"message"`
Ischeck string `json:"ischeck"`
Com string `json:"com"`
Condition string `json:"condition"`
Status string `json:"status"`
State string `json:"state"`
Data []struct {
Time string `json:"time"`
AreaName string `json:"areaName,omitempty"`
Status string `json:"status"`
AreaCode string `json:"areaCode,omitempty"`
Context string `json:"context"`
Ftime string `json:"ftime"`
} `json:"data"`
} `json:"lastResult"`
ComNew string `json:"comNew"`
Billstatus string `json:"billstatus"`
AutoCheck string `json:"autoCheck"`
}
// Kuaidi100Poll 订阅邮件推送
func Kuaidi100Poll(key, company, number, tel, callbackurl string) (*StKuaidi100PollResp, error) {
retryCount := 0
reqBody := gin.H{
"company": company,
"number": number,
"key": key,
"parameters": gin.H{
"callbackurl": callbackurl,
"resultv2": 1,
"phone": tel,
},
}
reqBytes, err := jsoniter.Marshal(reqBody)
if err != nil {
return nil, err
}
GotoHttpRetry:
_, body, errs := gorequest.New().
Post("https://poll.kuaidi100.com/poll").
Send("schema=json").
Send(fmt.Sprintf("param=%s", reqBytes)).
EndBytes()
if errs != nil {
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, errs[0]
}
var apiResp StKuaidi100PollResp
err = jsoniter.Unmarshal(body, &apiResp)
if err != nil {
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, fmt.Errorf("json err: %w %s", err, body)
}
if "200" != apiResp.ReturnCode && "501" != apiResp.ReturnCode {
return nil, fmt.Errorf("wx jscode err: %s %s", apiResp.ReturnCode, apiResp.Message)
}
return &apiResp, nil
}
// Kuaidi100Query 获取快递信息
func Kuaidi100Query(customer, key, company, number, tel string) (*StKuaidi100GetResp, error) {
retryCount := 0
reqBody := gin.H{
"com": company,
"num": number,
"phone": tel,
"resultv2": 2,
}
reqBytes, err := jsoniter.Marshal(reqBody)
if err != nil {
return nil, err
}
rawStr := fmt.Sprintf("%s%s%s", reqBytes, key, customer)
data := []byte(rawStr)
r := md5.Sum(data)
signedString := hex.EncodeToString(r[:])
sign := strings.ToUpper(signedString)
GotoHttpRetry:
_, body, errs := gorequest.New().
Post("https://poll.kuaidi100.com/poll/query.do").
Send(fmt.Sprintf("customer=%s", customer)).
Send(fmt.Sprintf("sign=%s", sign)).
Send(fmt.Sprintf("param=%s", reqBytes)).
EndBytes()
if errs != nil {
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, errs[0]
}
var apiResp StKuaidi100GetResp
err = jsoniter.Unmarshal(body, &apiResp)
if err != nil {
retryCount++
if retryCount < 3 {
goto GotoHttpRetry
}
return nil, fmt.Errorf("json err: %w %s", err, body)
}
return &apiResp, nil
}