From 5702df37b03cf9cf258ab950c53ecb30fe684dbe Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 10 Jul 2018 13:28:22 +0800 Subject: [PATCH] - more interface and const added. --- platformapi/dadaapi/callback.go | 7 ++ platformapi/dadaapi/order.go | 2 +- platformapi/elmapi/callback.go | 131 +++++++++++++++++++++++++++++++- platformapi/elmapi/order.go | 23 ++++++ platformapi/jdapi/order.go | 40 ++++++++-- platformapi/mtpsapi/callback.go | 13 +++- platformapi/mtpsapi/mtpsapi.go | 2 +- utils/utils.go | 13 ++++ 8 files changed, 217 insertions(+), 14 deletions(-) diff --git a/platformapi/dadaapi/callback.go b/platformapi/dadaapi/callback.go index cc3a2853..e01a6f48 100644 --- a/platformapi/dadaapi/callback.go +++ b/platformapi/dadaapi/callback.go @@ -40,6 +40,13 @@ var ( FailedResponse = &CallbackResponse{Code: ResponseHttpCodeGeneralErr} ) +func Err2CallbackResponse(err error, data string) *CallbackResponse { + if err == nil { + return SuccessResponse + } + return FailedResponse +} + func (a *API) signCallbackParams(mapData map[string]interface{}) string { values := make([]string, 0) for _, k := range []string{"client_id", "order_id", "update_time"} { diff --git a/platformapi/dadaapi/order.go b/platformapi/dadaapi/order.go index 3b13eff7..0f0e6eb0 100644 --- a/platformapi/dadaapi/order.go +++ b/platformapi/dadaapi/order.go @@ -20,7 +20,7 @@ const ( const ( OrderStatusWaitingForAccept = 1 - OrderStatusPickingup = 2 + OrderStatusAccepted = 2 // 待取货 OrderStatusDeliverying = 3 OrderStatusFinished = 4 OrderStatusCanceled = 5 diff --git a/platformapi/elmapi/callback.go b/platformapi/elmapi/callback.go index fe20d052..dc286f60 100644 --- a/platformapi/elmapi/callback.go +++ b/platformapi/elmapi/callback.go @@ -12,12 +12,72 @@ import ( // https://open.shop.ele.me/openapi/documents/callback const ( MsgTypeOrderValid = 10 - MsgTypeMerchantValid = 12 + MsgTypeOrderAccepted = 12 MsgTypeOrderCanceled = 14 - MsgTypeMerchantInvalid = 15 + MsgTypeOrderInvalid = 15 MsgTypeOrderForceInvalid = 17 MsgTypeOrderFinished = 18 - MsgTypeClientUrgeOrder = 45 + + MsgTypeUserApplyCancel = 20 + MsgTypeUserCancelApplyCancel = 21 + MsgTypeMerchantRejectCancel = 22 + MsgTypeMerchantAcceptCancel = 23 + MsgTypeUserApplyArbitrateCancel = 24 + MsgTypeCustomerServiceAcceptCancel = 25 + MsgTypeCustomerServiceRejectCancel = 26 + + MsgTypeUserApplyRefund = 30 + MsgTypeUserCancelRefund = 31 + MsgTypeMerchantRejectRefund = 32 + MsgTypeMerchantAcceptRefund = 33 + MsgTypeUserApplyArbitrateRefund = 34 + MsgTypeCustomerServiceAcceptRefund = 35 + MsgTypeCustomerServiceRejectRefund = 36 + + MsgTypeUserUrgeOrder = 45 + + MsgTypeWaybillWait4DeliveryVendor = 51 + MsgTypeWaybillWait4Courier = 52 + MsgTypeWaybillPickingUp = 53 + MsgTypeWaybillCourierArrived = 54 + MsgTypeWaybillDelivering = 55 + MsgTypeWaybillDelivered = 56 + + MsgTypeWaybillCanceledByMerchant = 57 + MsgTypeWaybillCanceledByUser = 58 + MsgTypeWaybillCanceledBySystem = 59 + MsgTypeWaybillFailedCallLate = 60 + MsgTypeWaybillFailedStore = 61 + MsgTypeWaybillFailedMerchantInterrupt = 62 + MsgTypeWaybillFailedCannotCantactUser = 63 + MsgTypeWaybillFailedUserApplyRefund = 64 + MsgTypeWaybillFailedWrongAddress = 65 + MsgTypeWaybillFailedOutOfSerivceRange = 66 + MsgTypeWaybillFailedCourierMarkException = 67 + MsgTypeWaybillFailedSystemMarkException = 68 + MsgTypeWaybillFailedOtherException = 69 + MsgTypeWaybillFailedTimeout = 70 + + MsgTypeDeiverBySelf = 71 + MsgTypeDontNeeded = 72 + MsgTypeRejectedOnlySupportPayOnline = 73 + MsgTypeRejectedOutOfServiceRange = 74 + MsgTypeRejectedLateRequest = 75 + MsgTypeRejectedSystemError = 76 +) + +const ( + DeliveryStateToBeAssignedMerchant = "tobeAssignedMerchant" + DeliveryStateToBeAssignedCourier = "tobeAssignedCourier" + DeliveryStateToBeFetched = "tobeFetched" + DeliveryStateDelivering = "delivering" + DeliveryStateCompleted = "completed" + DeliveryStateCancelled = "cancelled" + DeliveryStateException = "exception" + DeliveryStateArrived = "arrived" + DeliveryStateSelfDelivery = "selfDelivery" + DeliveryStateNoMoreDelivery = "noMoreDelivery" + DeliveryStateReject = "reject" ) type CallbackResponse struct { @@ -30,15 +90,78 @@ type CallbackMsg struct { Type int `json:"type"` Message string `json:"message"` ShopID int `json:"shopId"` - Timestamp int64 `json:"timestamp"` + Timestamp int64 `json:"timestamp"` // 毫秒 UserID int64 `json:"userId"` Signature string `json:"signature"` } +type CallbackOrderStatusMsg struct { + OrderID string `json:"orderId"` + State string `json:"state"` + ShopID int `json:"shopId"` + UpdateTime int64 `json:"updateTime"` // 秒,文档上写的是毫秒,看示例数据应该是秒 + Role int `json:"role"` + MsgType int `json:"-"` // 用于传递msg type,不是真正消息的一部分 +} + +type GoodItem struct { + Name string `json:"name"` + Quantity int `json:"quantity"` + Price float64 `json:"price"` + VfoodID int64 `json:"vfoodiId"` + SkuID string `json:"skuId"` +} + +const ( + RefundTypeNormal = "normal" + RefundTypePart = "part" +) + +type CallbackOrderCancelRefundMsg struct { + OrderID string `json:"orderId"` + RefundStatus string `json:"refundStatus"` + Reason string `json:"reason"` + ShopID int `json:"shopId"` + GoodsList []GoodItem `json:"goodsList"` + RefundType string `json:"refundType"` + TotalPrice float64 `json:"totalPrice"` + UpdateTime int64 `json:"updateTime"` // 秒 + MsgType int `json:"-"` // 用于传递msg type,不是真正消息的一部分 +} + +type CallbackOrderUrgeMsg struct { + OrderID string `json:"orderId"` + ShopID int `json:"shopId"` + RemindID int `json:"remindId"` + UserID int `json:"userId"` + UpdateTime int64 `json:"updateTime"` // 秒 + MsgType int `json:"-"` // 用于传递msg type,不是真正消息的一部分 +} + +type CallbackWaybillStatusMsg struct { + OrderID string `json:"orderId"` + ShopID int `json:"shopId"` + State string `json:"state"` + SubState string `json:"subState"` + Name string `json:"name"` + Phone string `json:"phone"` + UpdateAt int64 `json:"updateAt"` // 毫秒 + MsgType int `json:"-"` // 用于传递msg type,不是真正消息的一部分 +} + var ( SuccessResponse = &CallbackResponse{"ok"} ) +func Err2CallbackResponse(err error, data string) *CallbackResponse { + if err == nil { + return SuccessResponse + } + return &CallbackResponse{ + Message: fmt.Sprintf("error:%v, data:%v", data), + } +} + func (a *API) unmarshalData(data []byte, msg interface{}) (callbackResponse *CallbackResponse) { err := utils.UnmarshalUseNumber(data, msg) if err != nil { diff --git a/platformapi/elmapi/order.go b/platformapi/elmapi/order.go index 2fbc7a39..473401f3 100644 --- a/platformapi/elmapi/order.go +++ b/platformapi/elmapi/order.go @@ -1,6 +1,8 @@ package elmapi const ( + OrderStatusFake = "fake" + OrderStatusPending = "pending" OrderStatusUnprocessed = "unprocessed" OrderStatusRefunding = "refunding" @@ -22,6 +24,27 @@ const ( CancelOrderTypeNotSatisfiedDeliveryRequirement = "notSatisfiedDeliveryRequirement" ) +const ( + RefundStatusNoRefund = "noRefund" + RefundStatusApplied = "applied" + RefundStatusRejected = "rejected" + RefundStatusArbitrating = "arbitrating" + RefundStatusFailed = "failed" + RefundStatusSuccessful = "successful" +) + +const ( + RoleOrderUser = 1 + RoleSystem = 2 + RoleMerchert = 3 + RoleCustomerService = 4 + RoleOpenPlatform = 5 + RoleSMS = 6 + RolePrinter = 7 + RoleRiskController = 8 + RoleOrderFinished = 9 +) + func (a *API) GetOrder(orderID string) (map[string]interface{}, error) { result, err := a.AccessAPI("eleme.order.getOrder", map[string]interface{}{ "orderId": orderID, diff --git a/platformapi/jdapi/order.go b/platformapi/jdapi/order.go index ad25766e..5fa8b0d8 100644 --- a/platformapi/jdapi/order.go +++ b/platformapi/jdapi/order.go @@ -8,10 +8,34 @@ const ( OrderStatusAddComment = "12001" OrderStatusModifyComment = "12006" - OrderStatusNew = "32000" - OrderStatusAdjust = "33080" - OrderStatusUserCancel = "20030" - OrderStatusWaitOutStore = "32001" + OrderStatusPurchased = "41000" // 也即待处理,JD的消息很怪,新定单消息发过来是32000,但如果不是自动接单的,去查却是41000?,接单后才变为32000 + OrderStatusNew = "32000" + OrderStatusAdjust = "33080" + OrderStatusWaitOutStore = "32001" + OrderStatusFinishedPickup = "2" + OrderStatusDelivering = "33040" + + OrderStatusDelivered = "33060" + OrderStatusFinished = "90000" + OrderStatusCanceled = "20020" + + OrderStatusUserApplyCancel = "20030" // 这个其实不是一个状态,是一个动作 + OrderStatusLocked = "20010" + OrderStatusUnlocked = "20050" + OrderStatusInfoChanged = "1" +) + +const ( + DeliveryStatusWait4Grap = "10" + DeliveryStatusAccepted = "20" + DeliveryStatusCourierCanceled = "21" + DeliveryStatusCourierArrived = "23" + DeliveryStatusFailedGetGoods = "25" + DeliveryStatusFailedGetGoodsRejected = "26" + DeliveryStatusFailedGetGoodsWaiting = "27" + DeliveryStatusGotGoods = "30" + DeliveryStatusFailedDelivery = "35" + DeliveryStatusFinished = "40" ) func (a API) OrderQuery(jdParams map[string]interface{}) (retVal []interface{}, err error) { @@ -19,10 +43,14 @@ func (a API) OrderQuery(jdParams map[string]interface{}) (retVal []interface{}, return } -func (a API) QuerySingleOrder(orderId string) ([]interface{}, error) { +func (a API) QuerySingleOrder(orderId string) (map[string]interface{}, error) { jdParams := make(map[string]interface{}) jdParams["orderId"] = orderId - return a.AccessAPIHavePage("order/es/query", jdParams, nil, nil, nil) + result, err := a.AccessAPIHavePage("order/es/query", jdParams, nil, nil, nil) + if err == nil { + return nil, err + } + return result[0].(map[string]interface{}), nil } func (a API) LegacyQuerySingleOrder(orderId string) (map[string]interface{}, error) { diff --git a/platformapi/mtpsapi/callback.go b/platformapi/mtpsapi/callback.go index 785a1b40..a66a8a56 100644 --- a/platformapi/mtpsapi/callback.go +++ b/platformapi/mtpsapi/callback.go @@ -39,6 +39,15 @@ var ( SignatureIsNotOk = &CallbackResponse{Code: -1} ) +func Err2CallbackResponse(err error, data string) *CallbackResponse { + if err == nil { + return SuccessResponse + } + return &CallbackResponse{ + Code: -1, + } +} + func (a *API) CheckCallbackValidation(request *http.Request) (callbackResponse *CallbackResponse) { request.ParseForm() sign := a.signParams(request.PostForm) @@ -66,7 +75,7 @@ func (a *API) GetOrderCallbackMsg(request *http.Request) (orderMsg *CallbackOrde OrderInfoCommon: OrderInfoCommon{ DeliveryID: utils.Str2Int64(request.FormValue("delivery_id")), MtPeisongID: request.FormValue("mt_peisong_id"), - OrderId: request.FormValue("order_id"), + OrderID: request.FormValue("order_id"), CourierName: request.FormValue("courier_name"), CourierPhone: request.FormValue("courier_phone"), }, @@ -90,7 +99,7 @@ func (a *API) GetOrderExceptionCallbackMsg(request *http.Request) (orderMsg *Cal OrderInfoCommon: OrderInfoCommon{ DeliveryID: utils.Str2Int64(request.FormValue("delivery_id")), MtPeisongID: request.FormValue("mt_peisong_id"), - OrderId: request.FormValue("order_id"), + OrderID: request.FormValue("order_id"), CourierName: request.FormValue("courier_name"), CourierPhone: request.FormValue("courier_phone"), }, diff --git a/platformapi/mtpsapi/mtpsapi.go b/platformapi/mtpsapi/mtpsapi.go index 2d65ba89..75dc3e42 100644 --- a/platformapi/mtpsapi/mtpsapi.go +++ b/platformapi/mtpsapi/mtpsapi.go @@ -73,7 +73,7 @@ const ( type OrderInfoCommon struct { DeliveryID int64 MtPeisongID string - OrderId string + OrderID string CourierName string CourierPhone string } diff --git a/utils/utils.go b/utils/utils.go index 603c92a6..cea9d0bf 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -163,10 +163,23 @@ func Timestamp2Str(timestamp int64) string { return Time2Str(time.Unix(timestamp, 0)) } +func Timestamp2Time(timestamp int64) time.Time { + return time.Unix(timestamp, 0) +} + func Time2Str(t time.Time) string { return t.Format("2006-01-02 15:04:05") } +func Str2Time(timeStr string) time.Time { + timeStr = strings.Replace(timeStr, "T", " ", 1) + retVal, err := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local) + if err != nil { + baseapi.SugarLogger.Errorf("ParseInLocation failed, timeStr:%v, error:%v", timeStr, err) + } + return retVal +} + func HTTPResponse2Json(response *http.Response) (map[string]interface{}, error) { var jsonResult map[string]interface{} bodyData, err := ioutil.ReadAll(response.Body)