Skip to content

Commit b1eed77

Browse files
committed
完成及时配送相关接口
1 parent f69ddc4 commit b1eed77

12 files changed

+3347
-194
lines changed

Readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ fmt.Printf("返回结果: %#v", res)
959959
// 接收并处理异步结果
960960
srv, err := NewServer("mock-app-id", "mock-access-token", aesKey, "mock-mch-id", "mock-api-key", false, func(mix *Mixture) bool {
961961
if mix.MsgType != weapp.MsgEvent {
962-
if mix.Event != weapp.EventAddNearbyPoiAuditInfo {
962+
if mix.Event != weapp.EventNearbyPoiAuditInfoAdd {
963963
if mix.AuditID == res.Data.AuditID {
964964

965965
fmt.Printf("返回结果: %#v", mix)

common_error.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,31 @@ import "errors"
44

55
// CommonError 微信返回错误信息
66
type CommonError struct {
7-
ErrCode int `json:"errcode"`
8-
ErrMSG string `json:"errmsg"`
7+
ErrCode int `json:"errcode"` // 错误码
8+
ErrMSG string `json:"errmsg"` // 错误描述
99
}
1010

1111
// GetResponseError 获取微信服务器错返回误信息
1212
func (err *CommonError) GetResponseError() error {
13-
if err.ErrCode == 0 {
14-
return nil
13+
if err.ErrCode != 0 {
14+
return errors.New(err.ErrMSG)
1515
}
1616

17-
return errors.New(err.ErrMSG)
17+
return nil
18+
}
19+
20+
// CommonResult 微信返回错误信息
21+
type CommonResult struct {
22+
ResultCode int `json:"resultcode"` // 错误码
23+
ResultMsg string `json:"resultmsg"` // 错误描述
24+
}
25+
26+
// GetResponseError 获取微信服务器错返回误信息
27+
func (err *CommonResult) GetResponseError() error {
28+
29+
if err.ResultCode != 0 {
30+
return errors.New(err.ResultMsg)
31+
}
32+
33+
return nil
1834
}

express.go

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ type ExpressOrder struct {
1515
Service ExpressService `json:"service"` // 服务类型
1616
}
1717

18+
// ExpreseeUserInfo 收件人/发件人信息
19+
type ExpreseeUserInfo struct {
20+
Name string `json:"name"` // 收件人/发件人姓名,不超过64字节
21+
Tel string `json:"tel,omitempty"` // 收件人/发件人座机号码,若不填写则必须填写 mobile,不超过32字节
22+
Mobile string `json:"mobile,omitempty"` // 收件人/发件人手机号码,若不填写则必须填写 tel,不超过32字节
23+
Company string `json:"company,omitempty"` // 收件人/发件人公司名称,不超过64字节
24+
PostCode string `json:"post_code,omitempty"` // 收件人/发件人邮编,不超过10字节
25+
Country string `json:"country,omitempty"` // 收件人/发件人国家,不超过64字节
26+
Province string `json:"province"` // 收件人/发件人省份,比如:"广东省",不超过64字节
27+
City string `json:"city"` // 收件人/发件人市/地区,比如:"广州市",不超过64字节
28+
Area string `json:"area"` // 收件人/发件人区/县,比如:"海珠区",不超过64字节
29+
Address string `json:"address"` // 收件人/发件人详细地址,比如:"XX路XX号XX大厦XX",不超过512字节
30+
}
31+
1832
// ExpressCargo 包裹信息
1933
type ExpressCargo struct {
2034
Count uint `json:"count"` // 包裹数量

express_business.go

+12-26
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,6 @@ type ExpressOrderCreator struct {
160160
TagID uint `json:"tagid,omitempty"` //订单标签id,用于平台型小程序区分平台上的入驻方,tagid须与入驻方账号一一对应,非平台型小程序无需填写该字段
161161
}
162162

163-
// ExpreseeUserInfo 收件人/发件人信息
164-
type ExpreseeUserInfo struct {
165-
Name string `json:"name"` // 收件人/发件人姓名,不超过64字节
166-
Tel string `json:"tel,omitempty"` // 收件人/发件人座机号码,若不填写则必须填写 mobile,不超过32字节
167-
Mobile string `json:"mobile,omitempty"` // 收件人/发件人手机号码,若不填写则必须填写 tel,不超过32字节
168-
Company string `json:"company,omitempty"` // 收件人/发件人公司名称,不超过64字节
169-
PostCode string `json:"post_code,omitempty"` // 收件人/发件人邮编,不超过10字节
170-
Country string `json:"country,omitempty"` // 收件人/发件人国家,不超过64字节
171-
Province string `json:"province"` // 收件人/发件人省份,比如:"广东省",不超过64字节
172-
City string `json:"city"` // 收件人/发件人市/地区,比如:"广州市",不超过64字节
173-
Area string `json:"area"` // 收件人/发件人区/县,比如:"海珠区",不超过64字节
174-
Address string `json:"address"` // 收件人/发件人详细地址,比如:"XX路XX号XX大厦XX",不超过512字节
175-
}
176-
177163
// InsureStatus 保价状态
178164
type InsureStatus = uint8
179165

@@ -183,8 +169,8 @@ const (
183169
Insured = 1 // 保价
184170
)
185171

186-
// AddOrderResponse 创建订单返回数据
187-
type AddOrderResponse struct {
172+
// CreateExpressOrderResponse 创建订单返回数据
173+
type CreateExpressOrderResponse struct {
188174
CommonError
189175
OrderID string `json:"order_id"` // 订单ID,下单成功时返回
190176
WaybillID string `json:"waybill_id"` // 运单ID,下单成功时返回
@@ -196,20 +182,20 @@ type AddOrderResponse struct {
196182
DeliveryResultmsg string `json:"delivery_resultmsg"` // 快递侧错误信息,下单失败时返回
197183
}
198184

199-
// Add 生成运单
185+
// Create 生成运单
200186
// token 接口调用凭证
201-
func (creator *ExpressOrderCreator) Add(token string) (*AddOrderResponse, error) {
187+
func (creator *ExpressOrderCreator) Create(token string) (*CreateExpressOrderResponse, error) {
202188
api := baseURL + apiAddExpressOrder
203-
return creator.add(api, token)
189+
return creator.create(api, token)
204190
}
205191

206-
func (creator *ExpressOrderCreator) add(api, token string) (*AddOrderResponse, error) {
192+
func (creator *ExpressOrderCreator) create(api, token string) (*CreateExpressOrderResponse, error) {
207193
url, err := tokenAPI(api, token)
208194
if err != nil {
209195
return nil, err
210196
}
211197

212-
res := new(AddOrderResponse)
198+
res := new(CreateExpressOrderResponse)
213199
if err := postJSON(url, creator, res); err != nil {
214200
return nil, err
215201
}
@@ -361,25 +347,25 @@ type QuotaGetter struct {
361347
BizID string `json:"biz_id"` // 快递公司客户编码
362348
}
363349

364-
// GetQuotaResponse 电子面单余额
365-
type GetQuotaResponse struct {
350+
// QuotaGetResponse 电子面单余额
351+
type QuotaGetResponse struct {
366352
CommonError
367353
Number uint // 电子面单余额
368354
}
369355

370356
// Get 获取电子面单余额。仅在使用加盟类快递公司时,才可以调用。
371-
func (getter *QuotaGetter) Get(token string) (*GetQuotaResponse, error) {
357+
func (getter *QuotaGetter) Get(token string) (*QuotaGetResponse, error) {
372358
api := baseURL + apiGetQuota
373359
return getter.get(api, token)
374360
}
375361

376-
func (getter *QuotaGetter) get(api, token string) (*GetQuotaResponse, error) {
362+
func (getter *QuotaGetter) get(api, token string) (*QuotaGetResponse, error) {
377363
url, err := tokenAPI(api, token)
378364
if err != nil {
379365
return nil, err
380366
}
381367

382-
res := new(GetQuotaResponse)
368+
res := new(QuotaGetResponse)
383369
if err := postJSON(url, getter, res); err != nil {
384370
return nil, err
385371
}

express_business_test.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ func TestAddExpressOrder(t *testing.T) {
275275
},
276276
},
277277
}
278-
_, err := creator.add(ts.URL+apiAddExpressOrder, "mock-access-token")
278+
_, err := creator.create(ts.URL+apiAddExpressOrder, "mock-access-token")
279279
if err != nil {
280280
t.Fatal(err)
281281
}
282282
}
283283

284-
func TestCancelOrder(t *testing.T) {
284+
func TestCancelExpressOrder(t *testing.T) {
285285
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
286286

287287
if r.Method != "POST" {
@@ -691,11 +691,11 @@ func TestOnPathUpdate(t *testing.T) {
691691
if mix.CreateTime == 0 {
692692
t.Error("CreateTime can not be zero")
693693
}
694-
if mix.MsgType != MsgEvent {
694+
if mix.MsgType != "event" {
695695
t.Error("Unexpected message type")
696696
}
697697

698-
if mix.Event != EventExpressPathUpdate {
698+
if mix.Event != "add_express_path" {
699699
t.Error("Unexpected message event")
700700
}
701701

@@ -757,7 +757,11 @@ func TestOnPathUpdate(t *testing.T) {
757757
<ActionMsg><![CDATA[运往目的地]]></ActionMsg>
758758
</Actions>
759759
</xml>`
760-
http.Post(ts.URL, "application/xml", strings.NewReader(xmlData))
760+
res, err := http.Post(ts.URL, "application/xml", strings.NewReader(xmlData))
761+
if err != nil {
762+
t.Fatal(err)
763+
}
764+
defer res.Body.Close()
761765

762766
jsonData := `{
763767
"ToUserName": "toUser",
@@ -787,8 +791,11 @@ func TestOnPathUpdate(t *testing.T) {
787791
}
788792
]
789793
}`
790-
791-
http.Post(ts.URL, "application/json", strings.NewReader(jsonData))
794+
res, err = http.Post(ts.URL, "application/json", strings.NewReader(jsonData))
795+
if err != nil {
796+
t.Fatal(err)
797+
}
798+
defer res.Body.Close()
792799
}
793800

794801
func TestTestUpdateOrder(t *testing.T) {

express_provider_test.go

+34-50
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,14 @@ func TestOnAddExpressOrder(t *testing.T) {
210210
}
211211

212212
res := AddExpressOrderReturn{
213-
ToUserName: "oABCD",
214-
FromUserName: "gh_abcdefg",
215-
CreateTime: 1533042556,
216-
MsgType: "event",
217-
Event: "add_waybill",
218-
Token: "1234ABC234523451",
219-
OrderID: "012345678901234567890123456789",
220-
BizID: "xyz",
221-
WayBillID: "123456789",
222-
ResultCode: 0,
223-
ResultMsg: "success",
224-
WaybillData: "##ZTO_bagAddr##广州##ZTO_mark##888-666-666##",
213+
CommonServerReturn: CommonServerReturn{
214+
"oABCD", "gh_abcdefg", 1533042556, "event", "add_waybill", 1, "success",
215+
},
216+
Token: "1234ABC234523451",
217+
OrderID: "012345678901234567890123456789",
218+
BizID: "xyz",
219+
WayBillID: "123456789",
220+
WaybillData: "##ZTO_bagAddr##广州##ZTO_mark##888-666-666##",
225221
}
226222

227223
return &res
@@ -449,15 +445,15 @@ func TestOnAddExpressOrder(t *testing.T) {
449445
}
450446
}
451447

452-
func TestOnCancelExpressOrder(t *testing.T) {
448+
func TestOnExpressOrderCancel(t *testing.T) {
453449
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
454450
aesKey := base64.StdEncoding.EncodeToString([]byte("mock-aes-key"))
455451
srv, err := NewServer("mock-app-id", "mock-access-token", aesKey, "mock-mch-id", "mock-api-key", false)
456452
if err != nil {
457453
t.Fatal(err)
458454
}
459455

460-
srv.HandleCancelExpressOrderRequest(func(result *CancelExpressOrderResult) *CancelExpressOrderReturn {
456+
srv.HandleExpressOrderCancelRequest(func(result *ExpressOrderCancelResult) *ExpressOrderCancelReturn {
461457
if result.ToUserName == "" {
462458
t.Error("ToUserName can not be empty")
463459
}
@@ -490,17 +486,13 @@ func TestOnCancelExpressOrder(t *testing.T) {
490486
t.Error("Result column 'WayBillID' can not be empty")
491487
}
492488

493-
res := CancelExpressOrderReturn{
494-
ToUserName: "oABCD",
495-
FromUserName: "gh_abcdefg",
496-
CreateTime: 1533042556,
497-
MsgType: "event",
498-
Event: "add_waybill",
499-
OrderID: "012345678901234567890123456789",
500-
BizID: "xyz",
501-
WayBillID: "123456789",
502-
ResultCode: 0,
503-
ResultMsg: "success",
489+
res := ExpressOrderCancelReturn{
490+
CommonServerReturn: CommonServerReturn{
491+
"oABCD", "gh_abcdefg", 1533042556, "event", "cancel_waybill", 1, "success",
492+
},
493+
OrderID: "012345678901234567890123456789",
494+
BizID: "xyz",
495+
WayBillID: "123456789",
504496
}
505497

506498
return &res
@@ -529,7 +521,7 @@ func TestOnCancelExpressOrder(t *testing.T) {
529521
t.Error(err)
530522
}
531523
defer xmlResp.Body.Close()
532-
res := new(CancelExpressOrderReturn)
524+
res := new(ExpressOrderCancelReturn)
533525
if err := xml.NewDecoder(xmlResp.Body).Decode(res); err != nil {
534526
t.Error(err)
535527
}
@@ -581,7 +573,7 @@ func TestOnCancelExpressOrder(t *testing.T) {
581573
t.Error(err)
582574
}
583575
defer jsonResp.Body.Close()
584-
res = new(CancelExpressOrderReturn)
576+
res = new(ExpressOrderCancelReturn)
585577
if err := json.NewDecoder(jsonResp.Body).Decode(res); err != nil {
586578
t.Error(err)
587579
}
@@ -668,15 +660,11 @@ func TestOnCheckBusiness(t *testing.T) {
668660
}
669661

670662
res := CheckBusinessReturn{
671-
ToUserName: "oABCD",
672-
FromUserName: "gh_abcdefg",
673-
CreateTime: 1533042556,
674-
MsgType: "event",
675-
Event: "add_waybill",
676-
BizID: "xyz",
677-
Quota: 3.14159265358,
678-
ResultCode: 0,
679-
ResultMsg: "success",
663+
CommonServerReturn: CommonServerReturn{
664+
"oABCD", "gh_abcdefg", 1533042556, "event", "check_biz", 1, "success",
665+
},
666+
BizID: "xyz",
667+
Quota: 3.14159265358,
680668
}
681669
return &res
682670
})
@@ -789,15 +777,15 @@ func TestOnCheckBusiness(t *testing.T) {
789777
}
790778
}
791779

792-
func TestOnGetQuota(t *testing.T) {
780+
func TestOnQuotaGet(t *testing.T) {
793781
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
794782
aesKey := base64.StdEncoding.EncodeToString([]byte("mock-aes-key"))
795783
srv, err := NewServer("mock-app-id", "mock-access-token", aesKey, "mock-mch-id", "mock-api-key", false)
796784
if err != nil {
797785
t.Fatal(err)
798786
}
799787

800-
srv.HandleGetQuotaRequest(func(result *GetQuotaResult) *GetQuotaReturn {
788+
srv.HandleQuotaGetRequest(func(result *QuotaGetResult) *QuotaGetReturn {
801789
if result.ToUserName == "" {
802790
t.Error("ToUserName can not be empty")
803791
}
@@ -824,16 +812,12 @@ func TestOnGetQuota(t *testing.T) {
824812
t.Error("Result column 'ShopAppID' can not be empty")
825813
}
826814

827-
res := GetQuotaReturn{
828-
ToUserName: "oABCD",
829-
FromUserName: "gh_abcdefg",
830-
CreateTime: 1533042556,
831-
MsgType: "event",
832-
Event: "get_quota",
833-
BizID: "xyz",
834-
Quota: 3.14159265358,
835-
ResultCode: 0,
836-
ResultMsg: "success",
815+
res := QuotaGetReturn{
816+
CommonServerReturn: CommonServerReturn{
817+
"oABCD", "gh_abcdefg", 1533042556, "event", "get_quota", 1, "success",
818+
},
819+
BizID: "xyz",
820+
Quota: 3.14159265358,
837821
}
838822
return &res
839823
})
@@ -859,7 +843,7 @@ func TestOnGetQuota(t *testing.T) {
859843
t.Error(err)
860844
}
861845
defer xmlResp.Body.Close()
862-
res := new(GetQuotaReturn)
846+
res := new(QuotaGetReturn)
863847
if err := xml.NewDecoder(xmlResp.Body).Decode(res); err != nil {
864848
t.Error(err)
865849
}
@@ -905,7 +889,7 @@ func TestOnGetQuota(t *testing.T) {
905889
t.Error(err)
906890
}
907891
defer jsonResp.Body.Close()
908-
res = new(GetQuotaReturn)
892+
res = new(QuotaGetReturn)
909893
if err := json.NewDecoder(jsonResp.Body).Decode(res); err != nil {
910894
t.Error(err)
911895
}

0 commit comments

Comments
 (0)