Files
jx-callback/controllers/jd_callback.go
苏尹岚 ac2bb2bcfe aa
2021-03-30 10:19:36 +08:00

237 lines
6.1 KiB
Go

package controllers
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"github.com/astaxie/beego/server/web/context"
"git.rosy.net.cn/baseapi/platformapi/jdapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/partner/purchase/jd"
"git.rosy.net.cn/jx-callback/globals"
beego "github.com/astaxie/beego/adapter"
)
// Operations about JDOrder
type DjswController struct {
beego.Controller
}
func (c *DjswController) handleMsg(handler func(*jdapi.API, interface{}) *jdapi.CallbackResponse) (callbackResponse *jdapi.CallbackResponse) {
callbackMsg, mapData, callbackResponse := jdapi.GetCallbackMsg(getUsefulRequest(c.Ctx))
globals.SugarLogger.Debug(utils.Format4Output(callbackMsg, true))
if callbackResponse == nil {
if jdAPI := jd.GetAPIbyKey(callbackMsg.AppKey); jdAPI != nil {
if callbackResponse = jdAPI.CheckCallbackValidation(mapData, callbackMsg.Sign); callbackResponse == nil {
callbackResponse = handler(jdAPI, callbackMsg.Param)
}
} else {
callbackResponse = jdapi.Err2CallbackResponse(fmt.Errorf("没有匹配的appKey,非法请求"), "")
}
}
return callbackResponse
}
func (c *DjswController) orderStatus() {
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(func(a *jdapi.API, obj interface{}) (callbackResponse *jdapi.CallbackResponse) {
callbackResponse = jd.OnOrderMsg(obj.(*jdapi.CallbackOrderMsg))
return callbackResponse
})
c.Data["json"] = c.transferResponse("orderStatus", callbackResponse)
c.ServeJSON()
} else {
c.Abort("404")
}
}
func (c *DjswController) NewOrder() {
c.orderStatus()
}
func (c *DjswController) OrderAdjust() {
c.orderStatus()
}
func (c *DjswController) OrderWaitOutStore() {
c.orderStatus()
}
func (c *DjswController) PickFinishOrder() {
c.orderStatus()
}
func (c *DjswController) DeliveryOrder() {
c.orderStatus()
}
func (c *DjswController) FinishOrder() {
c.orderStatus()
}
func (c *DjswController) LockOrder() {
c.orderStatus()
}
func (c *DjswController) UnlockOrder() {
c.orderStatus()
}
func (c *DjswController) UserCancelOrder() {
c.orderStatus()
}
func (c *DjswController) ApplyCancelOrder() {
c.orderStatus()
}
func (c *DjswController) VenderAuditApplyCancelOrder() {
c.orderStatus()
}
func (c *DjswController) PushDeliveryStatus() {
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(func(a *jdapi.API, obj interface{}) (callbackResponse *jdapi.CallbackResponse) {
callbackResponse = jd.OnWaybillMsg(obj.(*jdapi.CallbackDeliveryStatusMsg))
return callbackResponse
})
c.Data["json"] = c.transferResponse("PushDeliveryStatus", callbackResponse)
c.ServeJSON()
} else {
c.Abort("404")
}
}
func (c *DjswController) OrderCommentPush() {
c.orderStatus()
}
func (c *DjswController) Token() {
urlValues, err := utils.HTTPBody2Values(c.Ctx.Input.RequestBody, false)
if err == nil {
globals.SugarLogger.Info(utils.Format4Output(utils.URLValues2Map(urlValues), false))
}
jd.OnTokenChange(urlValues)
c.Data["json"] = c.transferResponse("Token", nil)
c.ServeJSON()
}
func (c *DjswController) StockIsHave() {
// globals.SugarLogger.Info(string(c.Ctx.Input.RequestBody))
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(func(a *jdapi.API, obj interface{}) (callbackResponse *jdapi.CallbackResponse) {
return callbackResponse
})
c.Data["json"] = c.transferResponse("StockIsHave", callbackResponse)
c.ServeJSON()
} else {
c.Abort("404")
}
}
func (c *DjswController) SinglePromoteCreate() {
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(func(a *jdapi.API, obj interface{}) (callbackResponse *jdapi.CallbackResponse) {
callbackResponse = jd.OnActMsg(obj.(*jdapi.CallbackOrderMsg))
return callbackResponse
})
c.Data["json"] = c.transferResponse("SinglePromoteCreate", callbackResponse)
c.ServeJSON()
} else {
c.Abort("404")
}
}
func (c *DjswController) StoreCrud() {
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(func(a *jdapi.API, obj interface{}) (callbackResponse *jdapi.CallbackResponse) {
callbackResponse = jd.OnStoreMsg(obj.(*jdapi.CallbackOrderMsg))
return callbackResponse
})
c.Data["json"] = c.transferResponse("StoreCrud", callbackResponse)
c.ServeJSON()
} else {
c.Abort("404")
}
}
func (c *DjswController) transferResponse(funcName string, inCallbackResponse *jdapi.CallbackResponse) (outCallbackResponse *jdapi.CallbackResponse) {
if globals.IsCallbackAlwaysReturnSuccess() {
return jdapi.SuccessResponse
}
if inCallbackResponse == nil {
return jdapi.SuccessResponse
}
globals.SugarLogger.Debugf("%s callbackResponse:%s", funcName, utils.Format4Output(inCallbackResponse, true))
return inCallbackResponse
}
func (c *DjswController) EndOrderFinance() {
c.nullOperation()
}
func (c *DjswController) FinanceAdjustment() {
c.nullOperation()
}
func (c *DjswController) DeliveryCarrierModify() {
c.nullOperation()
}
func (c *DjswController) NewApplyAfterSaleBill() {
c.orderStatus()
}
func (c *DjswController) UpdateApplyAfterSaleBill() {
c.orderStatus()
}
func (c *DjswController) NewAfterSaleBill() {
c.orderStatus()
}
func (c *DjswController) AfterSaleBillStatus() {
c.orderStatus()
}
func (c *DjswController) OrderAccounting() {
c.orderStatus()
}
func getUsefulRequest(ctx *context.Context) *http.Request {
ctx.Request.Body = ioutil.NopCloser(bytes.NewReader(ctx.Input.RequestBody))
return ctx.Request
}
func (c *DjswController) nullOperation() {
c.Data["json"] = c.transferResponse("nullOperation", nil)
c.ServeJSON()
}
func (c *DjswController) UpdateSku() {
c.nullOperation()
}
func (c *DjswController) OrderAddTips() {
c.orderStatus()
}
func (c *DjswController) OrderInfoChange() {
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(func(a *jdapi.API, obj interface{}) (callbackResponse *jdapi.CallbackResponse) {
callbackResponse = jd.OnOrderInfoChangeMsg(obj.(*jdapi.CallbackOrderInfoChangeMsg))
return callbackResponse
})
c.Data["json"] = c.transferResponse("OrderInfoChange", callbackResponse)
c.ServeJSON()
} else {
c.Abort("404")
}
}
// func (c *DjswController) OrderInfoChange() {
// c.orderStatus()
// }