京东回调消息根据token动态选择不同的配置

This commit is contained in:
gazebo
2019-12-03 15:07:50 +08:00
parent ea3cd1b833
commit 84dac23be8
4 changed files with 78 additions and 29 deletions

View File

@@ -2,6 +2,7 @@ package partner
type IAPIManager interface { type IAPIManager interface {
GetAPI(vendorID int, appOrgCode string) interface{} GetAPI(vendorID int, appOrgCode string) interface{}
GetAPIList(vendorID int) []interface{}
GetAppOrgCodeList(vendorID int) (appOrgCodeList []string) GetAppOrgCodeList(vendorID int) (appOrgCodeList []string)
} }

View File

@@ -26,6 +26,22 @@ func getAPI(appOrgCode string) (apiObj *jdapi.API) {
return partner.CurAPIManager.GetAPI(model.VendorIDJD, appOrgCode).(*jdapi.API) return partner.CurAPIManager.GetAPI(model.VendorIDJD, appOrgCode).(*jdapi.API)
} }
func GetAPIByToken(token string) (apiObj *jdapi.API) {
if token == "" {
apiObj = getAPI("")
} else {
apiList := partner.CurAPIManager.GetAPIList(model.VendorIDJD)
for _, v := range apiList {
jdAPI := v.(*jdapi.API)
if jdAPI.GetToken() == token {
apiObj = jdAPI
break
}
}
}
return apiObj
}
func (c *PurchaseHandler) GetVendorID() int { func (c *PurchaseHandler) GetVendorID() int {
return model.VendorIDJD return model.VendorIDJD
} }

View File

@@ -2,14 +2,15 @@ package controllers
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"git.rosy.net.cn/baseapi/platformapi/jdapi" "git.rosy.net.cn/baseapi/platformapi/jdapi"
"git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/partner/purchase/jd" "git.rosy.net.cn/jx-callback/business/partner/purchase/jd"
"git.rosy.net.cn/jx-callback/globals" "git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/astaxie/beego/context" "github.com/astaxie/beego/context"
) )
@@ -19,20 +20,27 @@ type DjswController struct {
beego.Controller beego.Controller
} }
func (c *DjswController) handleMsg(isNeedDecode bool, handler func(*jdapi.API, url.Values, string) *jdapi.CallbackResponse) (callbackResponse *jdapi.CallbackResponse) {
values, token, msgURL, callbackResponse := jdapi.GetCallbackMsg(getUsefulRequest(c.Ctx), isNeedDecode)
if callbackResponse == nil {
if jdAPI := jd.GetAPIByToken(token); jdAPI != nil {
callbackResponse = handler(jdAPI, values, msgURL)
} else {
callbackResponse = jdapi.Err2CallbackResponse(fmt.Errorf("没有匹配的token,非法请求"), "")
}
}
return callbackResponse
}
func (c *DjswController) orderStatus(isCancelOrder bool) { func (c *DjswController) orderStatus(isCancelOrder bool) {
if c.Ctx.Input.Method() == http.MethodPost { if c.Ctx.Input.Method() == http.MethodPost {
var obj *jdapi.CallbackOrderMsg callbackResponse := c.handleMsg(isCancelOrder, func(a *jdapi.API, values url.Values, msgURL string) (callbackResponse *jdapi.CallbackResponse) {
var callbackResponse *jdapi.CallbackResponse obj, callbackResponse := a.GetOrderCallbackMsg(values, msgURL)
if callbackResponse == nil {
if isCancelOrder { callbackResponse = jd.OnOrderMsg(obj)
obj, callbackResponse = api.JdAPI.GetOrderApplyCancelCallbackMsg(getUsefulRequest(c.Ctx)) }
} else { return callbackResponse
obj, callbackResponse = api.JdAPI.GetOrderCallbackMsg(getUsefulRequest(c.Ctx)) })
}
globals.SugarLogger.Debug(utils.Format4Output(obj, true))
if callbackResponse == nil {
callbackResponse = jd.OnOrderMsg(obj)
}
c.Data["json"] = c.transferResponse("orderStatus", callbackResponse) c.Data["json"] = c.transferResponse("orderStatus", callbackResponse)
c.ServeJSON() c.ServeJSON()
} else { } else {
@@ -82,10 +90,13 @@ func (c *DjswController) ApplyCancelOrder() {
func (c *DjswController) PushDeliveryStatus() { func (c *DjswController) PushDeliveryStatus() {
if c.Ctx.Input.Method() == http.MethodPost { if c.Ctx.Input.Method() == http.MethodPost {
obj, callbackResponse := api.JdAPI.GetOrderDeliveryCallbackMsg(getUsefulRequest(c.Ctx)) callbackResponse := c.handleMsg(true, func(a *jdapi.API, values url.Values, msgURL string) (callbackResponse *jdapi.CallbackResponse) {
if callbackResponse == nil { obj, callbackResponse := a.GetOrderDeliveryCallbackMsg(values, msgURL)
callbackResponse = jd.OnWaybillMsg(obj) if callbackResponse == nil {
} callbackResponse = jd.OnWaybillMsg(obj)
}
return callbackResponse
})
c.Data["json"] = c.transferResponse("PushDeliveryStatus", callbackResponse) c.Data["json"] = c.transferResponse("PushDeliveryStatus", callbackResponse)
c.ServeJSON() c.ServeJSON()
} else { } else {
@@ -109,10 +120,13 @@ func (c *DjswController) Token() {
func (c *DjswController) StockIsHave() { func (c *DjswController) StockIsHave() {
// globals.SugarLogger.Info(string(c.Ctx.Input.RequestBody)) // globals.SugarLogger.Info(string(c.Ctx.Input.RequestBody))
if c.Ctx.Input.Method() == http.MethodPost { if c.Ctx.Input.Method() == http.MethodPost {
_, callbackResponse := api.JdAPI.GetStoreStockCallbackMsg(getUsefulRequest(c.Ctx)) callbackResponse := c.handleMsg(true, func(a *jdapi.API, values url.Values, msgURL string) (callbackResponse *jdapi.CallbackResponse) {
if callbackResponse == nil { _, callbackResponse = a.GetStoreStockCallbackMsg(values, msgURL)
// globals.SugarLogger.Debugf("StockIsHave, obj:%s", utils.Format4Output(obj, false)) if callbackResponse == nil {
} // globals.SugarLogger.Debugf("StockIsHave, obj:%s", utils.Format4Output(obj, false))
}
return callbackResponse
})
c.Data["json"] = c.transferResponse("StockIsHave", callbackResponse) c.Data["json"] = c.transferResponse("StockIsHave", callbackResponse)
c.ServeJSON() c.ServeJSON()
} else { } else {
@@ -122,10 +136,13 @@ func (c *DjswController) StockIsHave() {
func (c *DjswController) SinglePromoteCreate() { func (c *DjswController) SinglePromoteCreate() {
if c.Ctx.Input.Method() == http.MethodPost { if c.Ctx.Input.Method() == http.MethodPost {
obj, callbackResponse := api.JdAPI.GetOrderCallbackMsg(getUsefulRequest(c.Ctx)) callbackResponse := c.handleMsg(false, func(a *jdapi.API, values url.Values, msgURL string) (callbackResponse *jdapi.CallbackResponse) {
if callbackResponse == nil { obj, callbackResponse := a.GetOrderCallbackMsg(values, msgURL)
callbackResponse = jd.OnActMsg(obj) if callbackResponse == nil {
} callbackResponse = jd.OnActMsg(obj)
}
return callbackResponse
})
c.Data["json"] = c.transferResponse("SinglePromoteCreate", callbackResponse) c.Data["json"] = c.transferResponse("SinglePromoteCreate", callbackResponse)
c.ServeJSON() c.ServeJSON()
} else { } else {
@@ -135,10 +152,13 @@ func (c *DjswController) SinglePromoteCreate() {
func (c *DjswController) StoreCrud() { func (c *DjswController) StoreCrud() {
if c.Ctx.Input.Method() == http.MethodPost { if c.Ctx.Input.Method() == http.MethodPost {
obj, callbackResponse := api.JdAPI.GetOrderCallbackMsg(getUsefulRequest(c.Ctx)) callbackResponse := c.handleMsg(false, func(a *jdapi.API, values url.Values, msgURL string) (callbackResponse *jdapi.CallbackResponse) {
if callbackResponse == nil { obj, callbackResponse := a.GetOrderCallbackMsg(values, msgURL)
callbackResponse = jd.OnStoreMsg(obj) if callbackResponse == nil {
} callbackResponse = jd.OnStoreMsg(obj)
}
return callbackResponse
})
c.Data["json"] = c.transferResponse("StoreCrud", callbackResponse) c.Data["json"] = c.transferResponse("StoreCrud", callbackResponse)
c.ServeJSON() c.ServeJSON()
} else { } else {

View File

@@ -38,6 +38,18 @@ func (a *APIManager) GetAPI(vendorID int, appOrgCode string) (pfAPI interface{})
return pfAPI return pfAPI
} }
func (a *APIManager) GetAPIList(vendorID int) (pfAPIList []interface{}) {
switch vendorID {
case model.VendorIDJD:
pfAPIList = []interface{}{api.JdAPI}
case model.VendorIDMTWM:
pfAPIList = []interface{}{api.MtwmAPI}
case model.VendorIDEBAI:
pfAPIList = []interface{}{api.EbaiAPI}
}
return pfAPIList
}
func (a *APIManager) GetAppOrgCodeList(vendorID int) (appOrgCodeList []string) { func (a *APIManager) GetAppOrgCodeList(vendorID int) (appOrgCodeList []string) {
switch vendorID { switch vendorID {
case model.VendorIDJD: case model.VendorIDJD: