From 27c08160adbba0cc918fc44a16b770f4614dfd0f Mon Sep 17 00:00:00 2001 From: gazebo Date: Wed, 8 May 2019 16:58:04 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BA=AC=E4=B8=9C=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E6=94=B9=E7=94=A8http.Request?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformapi/jdapi/callback.go | 57 ++++++++++++++++++++++++----------- platformapi/jdapi/order.go | 12 ++++++-- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/platformapi/jdapi/callback.go b/platformapi/jdapi/callback.go index 2b75cbbb..7ddd61a6 100644 --- a/platformapi/jdapi/callback.go +++ b/platformapi/jdapi/callback.go @@ -2,13 +2,20 @@ package jdapi import ( "fmt" + "io/ioutil" + "net/http" "net/url" + "strings" "git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi/platformapi" "git.rosy.net.cn/baseapi/utils" ) +const ( + CallbackPrefix = "/djsw/" +) + // 如下的常量其实都是京东回调消息的 const ( OrderStatusAddComment = "12001" @@ -78,7 +85,7 @@ type CallbackResponse struct { } type CallbackOrderMsg struct { - ID int `json:"-"` // 用于传递Jdorder的主键值,减少一次读库操作 + MsgURL string `json:"msgURL"` BillID string `json:"billId"` OutBillID string `json:"outBillId"` StatusID string `json:"statusId"` @@ -87,6 +94,7 @@ type CallbackOrderMsg struct { } type CallbackDeliveryStatusMsg struct { + MsgURL string `json:"msgURL"` OrderID string `json:"orderId"` DeliveryStatusTime string `json:"deliveryStatusTime"` DeliveryManNo string `json:"deliveryManNo"` @@ -119,6 +127,7 @@ const ( ) type CallbackStoreStockMsg struct { + MsgURL string `json:"msgURL"` StationNo string `json:"stationNo"` SkuId int64 `json:"skuId"` Have bool `json:"have"` @@ -166,7 +175,12 @@ func (a *API) CheckCallbackValidation(values url.Values) (callbackResponse *Call return nil } -func (a *API) getCommonOrderCallbackMsg(data []byte, msg interface{}, needDecode bool) (callbackResponse *CallbackResponse) { +func (a *API) getCommonOrderCallbackMsg(request *http.Request, msg interface{}, needDecode bool) (callbackResponse *CallbackResponse) { + data, err := ioutil.ReadAll(request.Body) + if err != nil { + return Err2CallbackResponse(err, "") + } + baseapi.SugarLogger.Debug(string(data)) result, err := utils.HTTPBody2Values(data, needDecode) if err != nil { return FormatErrorResponse @@ -183,28 +197,34 @@ func (a *API) getCommonOrderCallbackMsg(data []byte, msg interface{}, needDecode return nil } -func (a *API) GetOrderCallbackMsg(data []byte) (msg *CallbackOrderMsg, callbackResponse *CallbackResponse) { +func (a *API) GetOrderCallbackMsg(request *http.Request) (msg *CallbackOrderMsg, callbackResponse *CallbackResponse) { msg = new(CallbackOrderMsg) - callbackResponse = a.getCommonOrderCallbackMsg(data, msg, false) + if callbackResponse = a.getCommonOrderCallbackMsg(request, msg, false); callbackResponse == nil { + msg.MsgURL = getMsgURLFromRequest(request) + } return msg, callbackResponse } -func (a *API) GetOrderApplyCancelCallbackMsg(data []byte) (msg *CallbackOrderMsg, callbackResponse *CallbackResponse) { +func (a *API) GetOrderApplyCancelCallbackMsg(request *http.Request) (msg *CallbackOrderMsg, callbackResponse *CallbackResponse) { msg = new(CallbackOrderMsg) - callbackResponse = a.getCommonOrderCallbackMsg(data, msg, true) + if callbackResponse = a.getCommonOrderCallbackMsg(request, msg, true); callbackResponse == nil { + msg.MsgURL = getMsgURLFromRequest(request) + } return msg, callbackResponse } -func (a *API) GetOrderDeliveryCallbackMsg(data []byte) (msg *CallbackDeliveryStatusMsg, callbackResponse *CallbackResponse) { - msg = new(CallbackDeliveryStatusMsg) - callbackResponse = a.getCommonOrderCallbackMsg(data, msg, true) +func (a *API) GetOrderDeliveryCallbackMsg(request *http.Request) (msg *CallbackDeliveryStatusMsg, callbackResponse *CallbackResponse) { + if callbackResponse = a.getCommonOrderCallbackMsg(request, &msg, true); callbackResponse == nil { + msg.MsgURL = getMsgURLFromRequest(request) + } return msg, callbackResponse } -func (a *API) GetStoreStockCallbackMsg(data []byte) (msg *CallbackStoreStockMsg, callbackResponse *CallbackResponse) { +func (a *API) GetStoreStockCallbackMsg(request *http.Request) (msg *CallbackStoreStockMsg, callbackResponse *CallbackResponse) { msg = new(CallbackStoreStockMsg) + msg.MsgURL = getMsgURLFromRequest(request) var tmpMsg map[string]interface{} - callbackResponse = a.getCommonOrderCallbackMsg(data, &tmpMsg, true) + callbackResponse = a.getCommonOrderCallbackMsg(request, &tmpMsg, true) if callbackResponse == nil { msg.StationNo = utils.Interface2String(tmpMsg["stationNo"]) msg.SkuId = utils.Str2Int64(utils.Interface2String(tmpMsg["skuId"])) @@ -212,12 +232,15 @@ func (a *API) GetStoreStockCallbackMsg(data []byte) (msg *CallbackStoreStockMsg, 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 - } + msg.Have = utils.Interface2String(tmpMsg["have"]) == "true" } return msg, callbackResponse } + +func getMsgURLFromRequest(request *http.Request) (msgURL string) { + index := strings.Index(request.URL.Path, CallbackPrefix) + if index >= 0 { + msgURL = request.URL.Path[index+len(CallbackPrefix):] + } + return msgURL +} diff --git a/platformapi/jdapi/order.go b/platformapi/jdapi/order.go index f77727c8..7686141b 100644 --- a/platformapi/jdapi/order.go +++ b/platformapi/jdapi/order.go @@ -78,9 +78,15 @@ type AfsServiceResponse struct { OrderType int `json:"orderType"` PickwareAddress string `json:"pickwareAddress"` - QuestionDesc string `json:"questionDesc"` - QuestionPic string `json:"questionPic"` - QuestionTypeCid int `json:"questionTypeCid"` + QuestionDesc string `json:"questionDesc"` + QuestionPic string `json:"questionPic"` + QuestionTypeCid int `json:"questionTypeCid"` + + ApplyDeal string `json:"applyDeal"` + DeliveryState string `json:"deliveryState"` + DeliveryMan string `json:"deliveryMan"` + DeliveryMobile string `json:"deliveryMobile"` + StationID string `json:"stationId"` StationName string `json:"stationName"` StationNumOutSystem string `json:"stationNumOutSystem"`