From 4a65fa66c0bc2ad27473c95f6e3079ec4e3a36a8 Mon Sep 17 00:00:00 2001 From: ziming Date: Mon, 11 Aug 2025 11:27:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=AC=94=E7=AB=8B=E5=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/wechatrepoimpl/bank_multi_activity.go | 13 +++++++++++++ internal/pkg/wechat/srv/marketing/model.go | 14 ++++++++++++++ internal/pkg/wechat/utils/wxpay_utility.go | 4 ++-- test/bank_multi_activity.go | 15 ++++++++++++++- 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/internal/data/wechatrepoimpl/bank_multi_activity.go b/internal/data/wechatrepoimpl/bank_multi_activity.go index 9410918..648740e 100644 --- a/internal/data/wechatrepoimpl/bank_multi_activity.go +++ b/internal/data/wechatrepoimpl/bank_multi_activity.go @@ -1,12 +1,16 @@ package wechatrepoimpl import ( + "errors" + "fmt" "github.com/wechatpay-apiv3/wechatpay-go/core" + err2 "voucher/api/err" "voucher/internal/biz/bo" "voucher/internal/biz/wechatrepo" "voucher/internal/conf" "voucher/internal/data" "voucher/internal/pkg/wechat/srv/marketing" + "voucher/internal/pkg/wechat/utils" ) type BankMultiActivityImpl struct { @@ -37,6 +41,15 @@ func (w *BankMultiActivityImpl) Order(order *bo.OrderBo) (couponId string, err e if err != nil { + var e *utils.ApiException + if errors.As(err, &e) { + apiErr, err3 := marketing.BuildErr(e.Body()) + if err3 != nil { + return "", fmt.Errorf("ApiException analysis err: %+v", err3) + } + return "", err2.ErrorWechatFAIL("%s-%s", apiErr.Code, apiErr.Message) + } + return "", err } diff --git a/internal/pkg/wechat/srv/marketing/model.go b/internal/pkg/wechat/srv/marketing/model.go index 71eab28..d1af0c8 100644 --- a/internal/pkg/wechat/srv/marketing/model.go +++ b/internal/pkg/wechat/srv/marketing/model.go @@ -1,5 +1,7 @@ package marketing +import "encoding/json" + type SendReq struct { ActivityId *string `json:"activity_id"` StockId *string `json:"stock_id"` @@ -11,3 +13,15 @@ type SendReq struct { type SendResp struct { CouponId *string `json:"coupon_id"` } + +// Err {"code":"INVALID_REQUEST","message":"对应单号已超出重试期;请查单确认后决定是否换单请求"} +type Err struct { + Code string `json:"code,omitempty"` + Message string `json:"message,omitempty"` +} + +func BuildErr(body []byte) (*Err, error) { + ret := &Err{} + err := json.Unmarshal(body, &ret) + return ret, err +} diff --git a/internal/pkg/wechat/utils/wxpay_utility.go b/internal/pkg/wechat/utils/wxpay_utility.go index 8369952..9df405c 100644 --- a/internal/pkg/wechat/utils/wxpay_utility.go +++ b/internal/pkg/wechat/utils/wxpay_utility.go @@ -515,8 +515,8 @@ func (srv *MchConfig) Request2(host, method, path string, reqBody []byte) (respo httpRequest.Header.Set("Authorization", authorization) - hs, _ := json.Marshal(httpRequest.Header) - fmt.Printf("\npath=%s\nreqBody=%s\nheaders=%s\n", path, string(reqBody), string(hs)) + //hs, _ := json.Marshal(httpRequest.Header) + //fmt.Printf("\npath=%s\nreqBody=%s\nheaders=%s\n", path, string(reqBody), string(hs)) client := &http.Client{} httpResponse, err := client.Do(httpRequest) diff --git a/test/bank_multi_activity.go b/test/bank_multi_activity.go index 6697abf..2ad515c 100644 --- a/test/bank_multi_activity.go +++ b/test/bank_multi_activity.go @@ -1,6 +1,7 @@ package test import ( + "errors" "fmt" "os" "path/filepath" @@ -57,7 +58,19 @@ func MarketingSend() { response, err := marketing().Send(openId, request) if err != nil { - fmt.Printf("请求失败: %+v\n", err) + + var e *utils.ApiException + if errors.As(err, &e) { + + apiErr, err3 := marketing2.BuildErr(e.Body()) + if err3 != nil { + fmt.Printf("请求失败: %+v\n", err3) + } else { + fmt.Printf("请求失败: %s-%s\n", apiErr.Code, apiErr.Message) + } + + } + return }