Files
jx-callback/controllers/jd_callback.go
2019-12-09 09:38:53 +08:00

205 lines
5.4 KiB
Go

package controllers
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"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"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
)
// Operations about JDOrder
type DjswController struct {
beego.Controller
}
func (c *DjswController) handleMsg(isNeedDecode bool, handler func(*jdapi.API, interface{}) *jdapi.CallbackResponse) (callbackResponse *jdapi.CallbackResponse) {
if callbackMsg, mapData, response := jdapi.GetCallbackMsg2(getUsefulRequest(c.Ctx)); response == nil {
if jdAPI := jd.GetAPIByAppKey(callbackMsg.Token); jdAPI != nil {
if response = jdAPI.CheckCallbackValidation2(mapData, callbackMsg.Sign); response == nil {
callbackResponse = handler(jdAPI, callbackMsg.Param)
}
} else {
callbackResponse = jdapi.Err2CallbackResponse(fmt.Errorf("没有匹配的token,非法请求"), "")
globals.SugarLogger.Warnf("handleMsg failed, can not find api for:%s", callbackMsg.Token)
}
}
return callbackResponse
}
func (c *DjswController) orderStatus(isCancelOrder bool) {
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(isCancelOrder, 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(false)
}
func (c *DjswController) OrderAdjust() {
c.orderStatus(false)
}
func (c *DjswController) OrderWaitOutStore() {
c.orderStatus(false)
}
func (c *DjswController) PickFinishOrder() {
c.orderStatus(false)
}
func (c *DjswController) DeliveryOrder() {
c.orderStatus(false)
}
func (c *DjswController) FinishOrder() {
c.orderStatus(false)
}
func (c *DjswController) LockOrder() {
c.orderStatus(false)
}
func (c *DjswController) UnlockOrder() {
c.orderStatus(false)
}
func (c *DjswController) UserCancelOrder() {
c.orderStatus(false)
}
func (c *DjswController) ApplyCancelOrder() {
c.orderStatus(true)
}
func (c *DjswController) PushDeliveryStatus() {
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(true, 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(false)
}
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))
}
c.Data["json"] = c.transferResponse("Token", nil)
c.ServeJSON()
}
func (c *DjswController) StockIsHave() {
if c.Ctx.Input.Method() == http.MethodPost {
callbackResponse := c.handleMsg(true, 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(false, 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(false, 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.nullOperation()
}
func (c *DjswController) UpdateApplyAfterSaleBill() {
c.nullOperation()
}
func (c *DjswController) NewAfterSaleBill() {
c.nullOperation()
}
func (c *DjswController) AfterSaleBillStatus() {
c.orderStatus(false)
}
func (c *DjswController) OrderAccounting() {
c.orderStatus(false)
}
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()
}