- refactor platform apis.

This commit is contained in:
gazebo
2018-06-18 11:47:20 +08:00
parent db287fb025
commit 03574f0f9d
7 changed files with 220 additions and 42 deletions

View File

@@ -32,6 +32,11 @@ var (
ErrBusinessCode = errors.New("Business code is not ok")
)
var (
CBErrMsgUnescape = "can not unescape data:%v, data:%v"
CBErrMsgUnmarshal = "can not unmarshal data:%v, data:%v"
)
func AccessPlatformAPIWithRetry(params *AccessPlatformAPIWithRetryParams, handleResponse func(response *http.Response) (int, error)) error {
exceedLimitRetryCount := 0
recoverableErrorRetryCount := 0

View File

@@ -1,5 +1,12 @@
package elmapi
import (
"encoding/json"
"fmt"
"git.rosy.net.cn/baseapi/platform/common"
)
const (
OrderValid = 10
MerchantValid = 12
@@ -25,5 +32,24 @@ type ELMCallbackMsg struct {
}
var (
ELMResponseOK = &ELMCallbackResponse{"ok"}
SuccessResponse = &ELMCallbackResponse{"ok"}
)
func (e *ELMAPI) unmarshalData(data []byte, msg interface{}) (callbackResponse *ELMCallbackResponse) {
err := json.Unmarshal(data, msg)
if err != nil {
return &ELMCallbackResponse{
Message: fmt.Sprintf(common.CBErrMsgUnmarshal, data, err),
}
}
return nil
}
func (e *ELMAPI) GetMsgFromData(data []byte) (msg *ELMCallbackMsg, callbackResponse *ELMCallbackResponse) {
msg = new(ELMCallbackMsg)
callbackResponse = e.unmarshalData(data, msg)
if callbackResponse != nil {
return nil, callbackResponse
}
return msg, nil
}

View File

@@ -0,0 +1,86 @@
package jdapi
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"git.rosy.net.cn/baseapi/platform/common"
)
type JDCallbackResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
}
type JDOrderMsg struct {
Id int `json:"-"` // 用于传递Jdorder的主键值减少一次读库操作
BillId string `json:"billId"`
StatusId string `json:"statusId"`
Timestamp string `json:"timestamp"`
}
type JDDeliveryStatusMsg struct {
OrderId string
DeliveryStatusTime string
DeliveryManNo string
DeliveryManName string
DeliveryManPhone string
DeliveryCarrierNo string
DeliveryCarrierName string
DeliveryStatus int
Remark string
FailType string
CreatePin string
OpTime int64
InputTime string
}
var (
SuccessResponse = &JDCallbackResponse{Code: "0", Msg: "success", Data: ""}
)
func (j *JDAPI) unmarshalData(strData string, msg interface{}) (callbackResponse *JDCallbackResponse) {
err := json.Unmarshal([]byte(strData), msg)
if err != nil {
return &JDCallbackResponse{
Code: JDerrorCodeAbnormalParam,
Msg: fmt.Sprintf(common.CBErrMsgUnmarshal, strData, err),
Data: strData,
}
}
return nil
}
func (j *JDAPI) GetOrderMsg(request *http.Request) (msg *JDOrderMsg, callbackResponse *JDCallbackResponse) {
msg = new(JDOrderMsg)
jdParamJSON := request.FormValue(JD_PARAM_JSON)
callbackResponse = j.unmarshalData(jdParamJSON, msg)
if callbackResponse != nil {
return nil, callbackResponse
}
return msg, nil
}
func (j *JDAPI) GetOrderDeliveryMsg(request *http.Request) (msg *JDDeliveryStatusMsg, callbackResponse *JDCallbackResponse) {
msg = new(JDDeliveryStatusMsg)
jdParamJSON := request.FormValue(JD_PARAM_JSON)
jdParamJSON2, err := url.QueryUnescape(jdParamJSON)
if err != nil {
return nil, &JDCallbackResponse{
Code: JDerrorCodeAbnormalParam,
Msg: fmt.Sprintf(common.CBErrMsgUnescape, jdParamJSON, err),
Data: jdParamJSON,
}
}
jdParamJSON = jdParamJSON2
callbackResponse = j.unmarshalData(jdParamJSON, msg)
if callbackResponse != nil {
return nil, callbackResponse
}
return msg, nil
}

View File

@@ -24,6 +24,10 @@ const (
maxRetryCountWhenReachLimited = 10
)
const (
JD_PARAM_JSON = "jd_param_json"
)
const (
// JDErrorCodeSuccess 操作成功
JDErrorCodeSuccess = "0"

View File

@@ -4,35 +4,6 @@ import (
"git.rosy.net.cn/baseapi/utils"
)
type JDOrderMsgResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
}
type JDOrderMsg struct {
Id int `json:"-"` // 用于传递Jdorder的主键值减少一次读库操作
BillId string `json:"billId"`
StatusId string `json:"statusId"`
Timestamp string `json:"timestamp"`
}
type JDDeliveryStatusMsg struct {
OrderId string
DeliveryStatusTime string
DeliveryManNo string
DeliveryManName string
DeliveryManPhone string
DeliveryCarrierNo string
DeliveryCarrierName string
DeliveryStatus int
Remark string
FailType string
CreatePin string
OpTime int64
InputTime string
}
const (
JdOrderStatusNew = "32000"
JdOrderStatusAdjust = "33080"

View File

@@ -0,0 +1,82 @@
package mtpsapi
import (
"net/http"
"git.rosy.net.cn/baseapi/utils"
)
type MtpsCallbackResponse struct {
Code string `json:"code"`
}
type MtpsCallbackCommon struct {
AppKey string
Timestamp int64
Sign string
}
type MtpsCallbackOrderMsg struct {
MtpsOrderInfoCommon
MtpsCallbackCommon
Status int
CancelReasonId int
CancelReason string
}
type MtpsCallbackOrderExceptionMsg struct {
MtpsOrderInfoCommon
MtpsCallbackCommon
ExceptionId int64
ExceptionCode int
ExceptionDescr string
ExceptionTime int64
}
var (
SuccessResponse = &MtpsCallbackResponse{Code: "0"}
)
func (m *MTPSAPI) GetOrderCallbackMsg(request *http.Request) (orderMsg *MtpsCallbackOrderMsg, callbackResponse *MtpsCallbackResponse) {
orderMsg = &MtpsCallbackOrderMsg{
MtpsOrderInfoCommon: MtpsOrderInfoCommon{
DeliveryId: utils.Str2Int64(request.FormValue("delivery_id")),
MtPeisongId: request.FormValue("mt_peisong_id"),
OrderId: request.FormValue("order_id"),
CourierName: request.FormValue("courier_name"),
CourierPhone: request.FormValue("courier_phone"),
},
MtpsCallbackCommon: MtpsCallbackCommon{
AppKey: request.FormValue("appkey"),
Timestamp: utils.Str2Int64(request.FormValue("timestamp")),
Sign: request.FormValue("sign"),
},
Status: int(utils.Str2Int64(request.FormValue("status"))),
CancelReasonId: int(utils.Str2Int64(request.FormValue("cancel_reason_id"))),
CancelReason: request.FormValue("cancel_reason"),
}
return orderMsg, nil
}
func (m *MTPSAPI) GetOrderExceptionCallbackMsg(request *http.Request) (orderMsg *MtpsCallbackOrderExceptionMsg, callbackResponse *MtpsCallbackResponse) {
orderMsg = &MtpsCallbackOrderExceptionMsg{
MtpsOrderInfoCommon: MtpsOrderInfoCommon{
DeliveryId: utils.Str2Int64(request.FormValue("delivery_id")),
MtPeisongId: request.FormValue("mt_peisong_id"),
OrderId: request.FormValue("order_id"),
CourierName: request.FormValue("courier_name"),
CourierPhone: request.FormValue("courier_phone"),
},
MtpsCallbackCommon: MtpsCallbackCommon{
AppKey: request.FormValue("appkey"),
Timestamp: utils.Str2Int64(request.FormValue("timestamp")),
Sign: request.FormValue("sign"),
},
ExceptionId: utils.Str2Int64(request.FormValue("exception_id")),
ExceptionCode: int(utils.Str2Int64(request.FormValue("exception_code"))),
ExceptionDescr: request.FormValue("exception_descr"),
ExceptionTime: utils.Str2Int64(request.FormValue("exception_time")),
}
return orderMsg, nil
}

View File

@@ -32,6 +32,22 @@ const (
mtpsStatusMissingBusinessParams = 3
)
type MtpsOrderInfoCommon struct {
DeliveryId int64
MtPeisongId string
OrderId string
CourierName string
CourierPhone string
}
type MtpsOrderInfo struct {
MtpsOrderInfoCommon
Status int
OperateTime int
CancelReasonId int
CancelReason string
}
type MTPSResult struct {
Code int `json:"code"`
Message string `json:"message"`
@@ -45,18 +61,6 @@ type MTPSAPI struct {
client *http.Client
}
// type MTPSOderStatus struct {
// DeliveryId int64 `json:"delivery_id"`
// MtPeiSongId string `json:"mt_peisong_id"`
// OrderId string `json:"order_id"`
// Status int `json:"status"`
// OperateTime int `json:"operate_time"`
// CourierName string `json:"courier_name"`
// CourierPhone string `json:"courier_phone"`
// CancelReasonId int `json:"cancel_reason_id"`
// CancelReason string `json:"cancel_reason"`
// }
func NewMTPSAPI(appKey, secret string, sugarLogger *zap.SugaredLogger) *MTPSAPI {
api := &MTPSAPI{
appKey: appKey,