Files
baseapi/platformapi/jdapi/callback.go
2018-11-08 11:03:58 +08:00

163 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package jdapi
import (
"fmt"
"net/url"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
type CallbackResponse struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data string `json:"data"`
}
type CallbackOrderMsg struct {
ID int `json:"-"` // 用于传递Jdorder的主键值减少一次读库操作
BillID string `json:"billId"`
OutBillID string `json:"outBillId"`
StatusID string `json:"statusId"`
Timestamp string `json:"timestamp"`
Remark string `json:"remark"`
}
type CallbackDeliveryStatusMsg struct {
OrderID string `json:"orderId"`
DeliveryStatusTime string `json:"deliveryStatusTime"`
DeliveryManNo string `json:"deliveryManNo"`
DeliveryManName string `json:"deliveryManName"`
DeliveryManPhone string `json:"deliveryManPhone"`
DeliveryCarrierNo string `json:"deliveryCarrierNo"`
DeliveryCarrierName string `json:"deliveryCarrierName"`
DeliveryStatus string `json:"deliveryStatus"`
Remark string `json:"remark"`
FailType string `json:"failType"`
CreatePin string `json:"createPin"`
OpTime string `json:"opTime"`
InputTime string `json:"inputTime"`
}
const (
OpenSourceJDLSP = 1
OpenSourceJDMedicineCity = 2
OpenSourceJDMerchantDirect = 3
OpenSourceJDOne = 4
OpenSourceStockCenter = 5
OpenSourceOrderCenter = 6
OpenSourceGoodsSystem = 7
OpenSourcePrepositionWH = 8
OpenSourceStoreSystem = 9
OpenSourceMerchantCenter = 10
OpenSourceOpenPlatform = 11
OpenSourcePickupSystem = 13
OpenSourceBatchTask = 14
)
type CallbackStoreStockMsg struct {
StationNo string `json:"stationNo"`
SkuId int64 `json:"skuId"`
Have bool `json:"have"`
Vendibility int `json:"vendibility"`
OperPin string `json:"operPin"`
OperTime int64 `json:"operTime"`
OperSource int `json:"operSource"`
}
var (
SuccessResponse = &CallbackResponse{Code: "0", Msg: "success", Data: ""}
FormatErrorResponse = &CallbackResponse{Code: "-1", Msg: "failed", Data: ""}
)
func Err2CallbackResponse(err error, data string) *CallbackResponse {
if err == nil {
return SuccessResponse
}
return &CallbackResponse{
Code: ResponseCodeAccessFailed,
Msg: err.Error(),
Data: data,
}
}
func (a *API) unmarshalData(strData string, msg interface{}) (callbackResponse *CallbackResponse) {
err := utils.UnmarshalUseNumber([]byte(strData), msg)
if err != nil {
return &CallbackResponse{
Code: ResponseCodeAbnormalParam,
Msg: fmt.Sprintf(platformapi.ErrStrUnmarshalError, strData, err),
Data: strData,
}
}
return nil
}
func (a *API) CheckCallbackValidation(values url.Values) (callbackResponse *CallbackResponse) {
mapData := utils.URLValues2Map(values)
mapData["app_secret"] = a.appSecret
sign := a.signParams(mapData)
if sign != values.Get(signKey) {
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, values.Get(signKey))
return FormatErrorResponse
}
return nil
}
func (a *API) getCommonOrderCallbackMsg(data []byte, msg interface{}, needDecode bool) (callbackResponse *CallbackResponse) {
result, err := utils.HTTPBody2Values(data, needDecode)
if err != nil {
return FormatErrorResponse
}
if callbackResponse = a.CheckCallbackValidation(result); callbackResponse != nil {
return callbackResponse
}
jdParamJSON := result.Get(paramJson)
// baseapi.SugarLogger.Debug(jdParamJSON)
if callbackResponse = a.unmarshalData(jdParamJSON, msg); callbackResponse != nil {
return callbackResponse
}
return nil
}
func (a *API) GetOrderCallbackMsg(data []byte) (msg *CallbackOrderMsg, callbackResponse *CallbackResponse) {
msg = new(CallbackOrderMsg)
callbackResponse = a.getCommonOrderCallbackMsg(data, msg, false)
return msg, callbackResponse
}
func (a *API) GetOrderApplyCancelCallbackMsg(data []byte) (msg *CallbackOrderMsg, callbackResponse *CallbackResponse) {
msg = new(CallbackOrderMsg)
callbackResponse = a.getCommonOrderCallbackMsg(data, msg, true)
return msg, callbackResponse
}
func (a *API) GetOrderDeliveryCallbackMsg(data []byte) (msg *CallbackDeliveryStatusMsg, callbackResponse *CallbackResponse) {
msg = new(CallbackDeliveryStatusMsg)
callbackResponse = a.getCommonOrderCallbackMsg(data, msg, true)
return msg, callbackResponse
}
func (a *API) GetStoreStockCallbackMsg(data []byte) (msg *CallbackStoreStockMsg, callbackResponse *CallbackResponse) {
msg = new(CallbackStoreStockMsg)
var tmpMsg map[string]interface{}
callbackResponse = a.getCommonOrderCallbackMsg(data, &tmpMsg, true)
if callbackResponse == nil {
msg.StationNo = utils.Interface2String(tmpMsg["stationNo"])
msg.SkuId = utils.Str2Int64(utils.Interface2String(tmpMsg["skuId"]))
msg.Vendibility = int(utils.Str2Int64(utils.Interface2String(tmpMsg["vendibility"])))
msg.OperPin = utils.Interface2String(tmpMsg["operPin"])
msg.OperTime = utils.Str2Int64(utils.Interface2String(tmpMsg["operTime"]))
msg.OperSource = int(utils.Str2Int64((utils.Interface2String(tmpMsg["operSource"]))))
have := utils.Interface2String(tmpMsg["have"])
if have == "true" {
msg.Have = true
} else {
msg.Have = false
}
}
return msg, callbackResponse
}