package controllers import ( "crypto/md5" "fmt" "git.rosy.net.cn/baseapi/platformapi/tao_vegetable" "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/tiktok_api" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxstore/common" "git.rosy.net.cn/jx-callback/business/model" taoVegetable "git.rosy.net.cn/jx-callback/business/partner/purchase/tao_vegetable" "git.rosy.net.cn/jx-callback/globals" "git.rosy.net.cn/jx-callback/globals/api" "github.com/astaxie/beego/server/web" "io/ioutil" "net/http" "net/url" "sort" "strings" ) type TaoBaoVegetableController struct { web.Controller } // GetCode 淘菜菜获取商户授权code,这个code和token是门店层次的 func (c *TaoBaoVegetableController) GetCode() { codeData := "" if c.Ctx.Input.Method() == http.MethodPost { body, err := ioutil.ReadAll(c.Ctx.Request.Body) if err != nil { globals.SugarLogger.Debugf("商户收取code获取:%s,%s", string(body), err.Error()) c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } globals.SugarLogger.Debugf("商户收取code获取:%s", string(body)) codeData = string(body) } else { codeData = c.Ctx.Input.Query("code") } globals.SugarLogger.Debugf("code:%s", utils.Format4Output(codeData, false)) tokenInfo, err := api.TaoVegetableApi.GetStoreToken(codeData, "") if err != nil { globals.SugarLogger.Debugf("获取门店token错误:%s", err.Error()) c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } globals.SugarLogger.Debugf("token:%s", utils.Format4Output(tokenInfo, false)) // 创建或者更新账号token param := &model.VendorOrgCode{ VendorID: model.VendorIDTaoVegetable, VendorOrgCode: api.TaoVegetableApi.GetVendorOrgCode(), Comment: tokenInfo.UserNick, VendorType: "platform", IsJxCat: 1, IsOpen: 1, EmpowerURL: "http://oauth.hemaos.com/authorize?\nresponse_type=code&sp=hema&op=topApp&client_id=" + api.TaoVegetableApi.GetVendorOrgCode(), StoreBrandName: "自动更新(京西菜市)", Token: utils.Format4Output(tokenInfo, false), AppKey: api.TaoVegetableApi.GetVendorOrgCode(), AppSecret: api.TaoVegetableApi.GetAppSecret(), } if err := common.AddVendorOrgCode(nil, param); err != nil { c.Data["json"] = tiktok_api.CallbackResponse{Code: tiktok_api.CallbackFailCode, Msg: tiktok_api.CallbackFail} c.ServeJSON() return } c.Data["json"] = tao_vegetable.CallBackResultInfo(nil) c.ServeJSON() return } // OrderStatus 订单状态变化 [post] func (c *TaoBaoVegetableController) OrderStatus() { urlParam := c.Ctx.Request.URL.RawQuery // 获取url参数 values, err := url.ParseQuery(urlParam) globals.SugarLogger.Debugf("ReaderOrderInfo := %s", utils.Format4Output(values, false)) globals.SugarLogger.Debugf("ReaderOrderInfo err := %s", utils.Format4Output(err, false)) if err != nil { c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } // 获取body参数 order, err := api.TaoVegetableApi.ReaderOrderInfo(c.Ctx.Request) globals.SugarLogger.Debugf("order_status ReaderOrderInfo:= %s", utils.Format4Output(order, false)) globals.SugarLogger.Debugf("order_status ReaderOrderInfo:= %s", utils.Format4Output(order, false)) if err != nil { c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } // 验签 sign := Sign(values, utils.Format4Output(order, false), api.TaoVegetableApi.GetAppSecret()) if sign != values.Get("sign") { c.Data["json"] = tao_vegetable.CallBackResultSign(fmt.Errorf("非法签名")) c.ServeJSON() return } /* else { c.Data["json"] = tao_vegetable.CallBackResultSign(nil) c.ServeJSON() return }*/ callbackResponse := taoVegetable.OnCallbackMsg(tao_vegetable.OrderStatusChange, utils.Int64ToStr(order.BizOrderId), order) c.Data["json"] = callbackResponse c.ServeJSON() return } // ApplyCancelOrder 用户发起售后申请 func (c *TaoBaoVegetableController) ApplyCancelOrder() { urlParam := c.Ctx.Request.URL.RawQuery // 获取url参数 values, err := url.ParseQuery(urlParam) globals.SugarLogger.Debugf("ApplyCancelOrder := %s", utils.Format4Output(values, false)) globals.SugarLogger.Debugf("ApplyCancelOrder err := %s", utils.Format4Output(err, false)) if err != nil { c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } afsOrder, err := api.TaoVegetableApi.UserApplyRefund(c.Ctx.Request) globals.SugarLogger.Debugf("ApplyCancelOrder := %s", utils.Format4Output(afsOrder, false)) globals.SugarLogger.Debugf("ApplyCancelOrder err := %s", utils.Format4Output(err, false)) if err != nil { globals.SugarLogger.Debugf("用户发起售后:%s", err.Error()) c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } // 验签 sign := Sign(values, utils.Format4Output(afsOrder, false), api.TaoVegetableApi.GetAppSecret()) if sign != values.Get("sign") { c.Data["json"] = tao_vegetable.CallBackResultSign(fmt.Errorf("非法签名")) c.ServeJSON() return } /* else { c.Data["json"] = tao_vegetable.CallBackResultSign(nil) c.ServeJSON() return }*/ callbackResponse := taoVegetable.OnCallbackMsg(tao_vegetable.OrderStatusApplyAfs, afsOrder.OutOrderId, afsOrder) c.Data["json"] = callbackResponse c.ServeJSON() return } // UserCancelRefund 用户取消售后 func (c *TaoBaoVegetableController) UserCancelRefund() { urlParam := c.Ctx.Request.URL.RawQuery // 获取url参数 values, err := url.ParseQuery(urlParam) globals.SugarLogger.Debugf("UserCancelRefund := %s", utils.Format4Output(values, false)) globals.SugarLogger.Debugf("UserCancelRefund err := %s", utils.Format4Output(err, false)) if err != nil { c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } afsOrder, err := api.TaoVegetableApi.UserCancelRefundApply(c.Ctx.Request) globals.SugarLogger.Debugf("UserCancelRefund := %s", utils.Format4Output(afsOrder, false)) globals.SugarLogger.Debugf("UserCancelRefund err := %s", utils.Format4Output(err, false)) if err != nil { globals.SugarLogger.Debugf("用户取消售后:%s", err.Error()) c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } // 验签 sign := Sign(values, utils.Format4Output(afsOrder, false), api.TaoVegetableApi.GetAppSecret()) if sign != values.Get("sign") { c.Data["json"] = tao_vegetable.CallBackResultSign(fmt.Errorf("非法签名")) c.ServeJSON() return } /*else { c.Data["json"] = tao_vegetable.CallBackResultSign(nil) c.ServeJSON() return }*/ callbackResponse := taoVegetable.OnCallbackMsg(tao_vegetable.OrderStatusCancelAfs, afsOrder.OutOrderId, afsOrder) c.Data["json"] = callbackResponse c.ServeJSON() return } // CancelOnSaleRefundOrder 用户售中取消(走订单取消流程) func (c *TaoBaoVegetableController) CancelOnSaleRefundOrder() { urlParam := c.Ctx.Request.URL.RawQuery // 获取url参数 values, err := url.ParseQuery(urlParam) globals.SugarLogger.Debugf("CancelOnSaleRefundOrder := %s", utils.Format4Output(values, false)) globals.SugarLogger.Debugf("CancelOnSaleRefundOrder err := %s", utils.Format4Output(err, false)) if err != nil { c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } afsOrder, err := api.TaoVegetableApi.OnSaleRefundOrder(c.Ctx.Request) globals.SugarLogger.Debugf("CancelOnSaleRefundOrder := %s", utils.Format4Output(afsOrder, false)) globals.SugarLogger.Debugf("CancelOnSaleRefundOrder err := %s", utils.Format4Output(err, false)) if err != nil { globals.SugarLogger.Debugf("用户售中取消:%s", err.Error()) c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } // 验签 sign := Sign(values, utils.Format4Output(afsOrder, false), api.TaoVegetableApi.GetAppSecret()) if sign != values.Get("sign") { c.Data["json"] = tao_vegetable.CallBackResultSign(fmt.Errorf("非法签名")) c.ServeJSON() return } /* else { c.Data["json"] = tao_vegetable.CallBackResultSign(nil) c.ServeJSON() return }*/ callbackResponse := taoVegetable.OnCallbackMsg(tao_vegetable.OrderStatusOnSaleCancel, utils.Int64ToStr(afsOrder.PartCancelRequest.BizOrderId), afsOrder) c.Data["json"] = callbackResponse c.ServeJSON() return } // RefundOrderSuccess 用户售后成功通知,只有退款成功了才会通知(商户拒绝退款,不会通知) func (c *TaoBaoVegetableController) RefundOrderSuccess() { urlParam := c.Ctx.Request.URL.RawQuery // 获取url参数 values, err := url.ParseQuery(urlParam) globals.SugarLogger.Debugf("RefundOrderSuccess := %s", utils.Format4Output(values, false)) globals.SugarLogger.Debugf("RefundOrderSuccess err := %s", utils.Format4Output(err, false)) if err != nil { c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } refundSuccess, err := api.TaoVegetableApi.RefundOrderFinish(c.Ctx.Request) globals.SugarLogger.Debugf("RefundOrderSuccess := %s", utils.Format4Output(refundSuccess, false)) globals.SugarLogger.Debugf("RefundOrderSuccess err := %s", utils.Format4Output(err, false)) if err != nil { globals.SugarLogger.Debugf("用户售后成功消息通知:%s", err.Error()) c.Data["json"] = tao_vegetable.CallBackResultInfo(err) c.ServeJSON() return } // 验签 sign := Sign(values, utils.Format4Output(refundSuccess, false), api.TaoVegetableApi.GetAppSecret()) if sign != values.Get("sign") { c.Data["json"] = tao_vegetable.CallBackResultSign(fmt.Errorf("非法签名")) c.ServeJSON() return } /*else { c.Data["json"] = tao_vegetable.CallBackResultSign(nil) c.ServeJSON() return }*/ callbackResponse := taoVegetable.OnCallbackMsg(tao_vegetable.OrderStatusRefundSuccess, refundSuccess.OutSubOrderId, refundSuccess) c.Data["json"] = callbackResponse c.ServeJSON() return } func Sign(param url.Values, data, secret string) string { var publicParam = make([]string, 0, 0) for k, v := range param { if k == "sign" { continue } publicParam = append(publicParam, fmt.Sprintf("%s%s", k, v[0])) } sort.Strings(publicParam) cc := secret + strings.Join(publicParam, "") + strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(data, "\n", ""), "\t", ""), " ", "") + secret return fmt.Sprintf("%X", md5.Sum([]byte(cc))) }