From 7f5c14b3b10708b10869544163f3b3ee659341d8 Mon Sep 17 00:00:00 2001 From: gazebo Date: Sun, 20 Jan 2019 10:09:25 +0800 Subject: [PATCH] - weimob callback msg format --- platformapi/weimobapi/callback.go | 39 ++++++++++++++++++++------ platformapi/weimobapi/callback_test.go | 30 ++++++++++++++++++++ platformapi/weimobapi/goods.go | 21 +++++++------- platformapi/weimobapi/goods_test.go | 2 +- platformapi/weimobapi/order.go | 8 ++++++ platformapi/weimobapi/weimobapi.go | 9 ++++++ 6 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 platformapi/weimobapi/callback_test.go diff --git a/platformapi/weimobapi/callback.go b/platformapi/weimobapi/callback.go index d992cebf..155ad9fd 100644 --- a/platformapi/weimobapi/callback.go +++ b/platformapi/weimobapi/callback.go @@ -1,6 +1,8 @@ package weimobapi import ( + "crypto/md5" + "fmt" "strings" "time" @@ -24,12 +26,15 @@ const ( ) type CallbackMsg struct { - ID string `json:"id"` - MsgEvent string `json:"msgEvent"` - PublicAccountID string `json:"public_account_id"` - OrderNo int64 `json:"orderNo"` - StatusTime time.Time `json:"statusTime"` - ChannelType int `json:"channelType"` + IsFake bool `json:"isFake"` // 是否自己生成的假消息 + ID string `json:"id"` + MsgEvent string `json:"msgEvent"` + PublicAccountID string `json:"public_account_id"` + BusinessID string `json:"business_id"` + + OrderNo int64 `json:"orderNo"` + StatusTime time.Time `json:"statusTime"` + ChannelType int `json:"channelType"` } type ErrorInfo struct { @@ -63,6 +68,17 @@ func Err2CallbackResponse(err error, data string) *CallbackResponse { } } +func (a *API) CheckCallbackValidation(sign, msgSignature, id, msgBody string) bool { + localSign := fmt.Sprintf("%x", md5.Sum([]byte(a.appID+id+a.appSecret))) + // baseapi.SugarLogger.Debug(sign, ":", localSign) + if localSign == sign { + localSign := fmt.Sprintf("%x", md5.Sum([]byte(a.appID+id+msgBody+a.appSecret))) + // baseapi.SugarLogger.Debug(msgSignature, ":", localSign) + return localSign == msgSignature + } + return false +} + func (a *API) GetCallbackMsg(body []byte) (msg *CallbackMsg, callbackResponse *CallbackResponse) { var ( mapMsg, msgBody map[string]interface{} @@ -71,7 +87,8 @@ func (a *API) GetCallbackMsg(body []byte) (msg *CallbackMsg, callbackResponse *C if err != nil { return nil, Err2CallbackResponse(err, "") } - err = utils.UnmarshalUseNumber([]byte(mapMsg["msg_body"].(string)), &msgBody) + msgBodyStr := mapMsg["msg_body"].(string) + err = utils.UnmarshalUseNumber([]byte(msgBodyStr), &msgBody) if err != nil { return nil, Err2CallbackResponse(err, "") } @@ -79,13 +96,19 @@ func (a *API) GetCallbackMsg(body []byte) (msg *CallbackMsg, callbackResponse *C ID: utils.Interface2String(mapMsg["id"]), MsgEvent: utils.Interface2String(mapMsg["event"]), PublicAccountID: utils.Interface2String(mapMsg["public_account_id"]), + BusinessID: utils.Interface2String(mapMsg["business_id"]), OrderNo: utils.MustInterface2Int64(msgBody["orderNo"]), } + sign := utils.Interface2String(mapMsg["sign"]) + msgSignature := utils.Interface2String(mapMsg["msgSignature"]) + if !a.CheckCallbackValidation(sign, msgSignature, msg.ID, msgBodyStr) { + return nil, Err2CallbackResponse(fmt.Errorf("sign is not match"), "") + } if msg.MsgEvent == MsgEventCreateOrder { msg.StatusTime = utils.Str2Time(strings.Replace(msgBody["createTime"].(string), ": ", ":", -1)) msg.ChannelType = int(utils.MustInterface2Int64(msgBody["channelType"])) } else { msg.StatusTime = time.Now() } - return nil, SuccessResponse + return msg, nil } diff --git a/platformapi/weimobapi/callback_test.go b/platformapi/weimobapi/callback_test.go new file mode 100644 index 00000000..43c3cc46 --- /dev/null +++ b/platformapi/weimobapi/callback_test.go @@ -0,0 +1,30 @@ +package weimobapi + +import ( + "testing" + + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" +) + +func TestGetCallbackMsg(t *testing.T) { + rawMsg := ` + { + "id": "91bae15f-2f8f-4eca-9f4d-793de40c74cf", + "topic": "ec_order", + "event": "createOrder", + "version": 1, + "sign": "e46b607913c93bd4c31a6e483785ef52", + "msgSignature": "b7a975d07a7b0b507ab01f862c4f7fec", + "test": false, + "business_id": "1224609670", + "public_account_id": "100000386048", + "msg_body": "{\"createTime\":\"2019-01-19 15:23:59\",\"channelType\":1,\"orderNo\":5330003015048}" + } + ` + msg, response := api.GetCallbackMsg([]byte(rawMsg)) + if response != nil { + t.Fatal("GetCallbackMsg failed") + } + baseapi.SugarLogger.Debug(utils.Format4Output(msg, false)) +} diff --git a/platformapi/weimobapi/goods.go b/platformapi/weimobapi/goods.go index 195cf656..abe06c66 100644 --- a/platformapi/weimobapi/goods.go +++ b/platformapi/weimobapi/goods.go @@ -19,14 +19,6 @@ const ( GoodsTypeOversea = 1 ) -const ( - OrderStatusWait4Pay = 0 // 待支付 - OrderStatusPayed = 1 // 已付款,待发货 - OrderStatusDelivering = 2 // 已发货 - OrderStatusFinished = 3 // 已完成 - OrderStatusCanceled = 4 // 已取消 -) - type PendingSaveB2CGoodsVo struct { FreightTemplateId int64 `json:"freightTemplateId"` DeliveryTypeIdList []int64 `json:"deliveryTypeIdList"` @@ -70,11 +62,18 @@ type DeliveryType struct { Selected bool `json:"selected"` } -func (a *API) QueryGoodsList(pageNum, pageSize int) (retVal []map[string]interface{}, totalCount int, err error) { - result, err := a.AccessAPI("goods/queryGoodsList", map[string]interface{}{ +func (a *API) QueryGoodsList(pageNum, pageSize int, orderBy []map[string]interface{}, queryParameter map[string]interface{}) (retVal []map[string]interface{}, totalCount int, err error) { + apiParams := map[string]interface{}{ "pageNum": pageNum, "pageSize": pageSize, - }) + } + if orderBy != nil { + apiParams["orderBy"] = orderBy + } + if queryParameter != nil { + apiParams["queryParameter"] = queryParameter + } + result, err := a.AccessAPI("goods/queryGoodsList", apiParams) if err == nil { if pageList, ok := result.(map[string]interface{})["pageList"].([]interface{}); ok { retVal = utils.Slice2MapSlice(pageList) diff --git a/platformapi/weimobapi/goods_test.go b/platformapi/weimobapi/goods_test.go index b0cc9d9a..9b067970 100644 --- a/platformapi/weimobapi/goods_test.go +++ b/platformapi/weimobapi/goods_test.go @@ -8,7 +8,7 @@ import ( ) func TestQueryGoodsList(t *testing.T) { - result, totalCount, err := api.QueryGoodsList(1, 20) + result, totalCount, err := api.QueryGoodsList(1, 20, nil, nil) if err != nil { t.Fatal(err) } diff --git a/platformapi/weimobapi/order.go b/platformapi/weimobapi/order.go index f9a620f4..90ac18bf 100644 --- a/platformapi/weimobapi/order.go +++ b/platformapi/weimobapi/order.go @@ -6,6 +6,14 @@ import ( "github.com/fatih/structs" ) +const ( + OrderStatusWait4Pay = 0 // 待支付 + OrderStatusPayed = 1 // 已付款,待发货 + OrderStatusDelivering = 2 // 已发货 + OrderStatusFinished = 3 // 已完成 + OrderStatusCanceled = 4 // 已取消 +) + type DeliveryOrderItem struct { ItemId int64 `json:"itemId"` SkuId int64 `json:"skuId"` diff --git a/platformapi/weimobapi/weimobapi.go b/platformapi/weimobapi/weimobapi.go index d2453dea..9a32236a 100644 --- a/platformapi/weimobapi/weimobapi.go +++ b/platformapi/weimobapi/weimobapi.go @@ -38,6 +38,15 @@ const ( KeyEditStockNum = "editStockNum" KeyB2cSku = "b2cSku" KeySkuAttrMap = "skuAttrMap" + + KeyField = "field" + KeySort = "sort" + + KeyGoodsClassifyId = "goodsClassifyId" + KeySearch = "search" + KeyGoodsStatus = "goodsStatus" + KeyUpdateStartTime = "updateStartTime" + KeyUpdateEndTime = "updateEndTime" ) type TokenInfo struct {