diff --git a/cmd/alipay_redpack.go b/cmd/alipay_redpack.go index 3d6b577..8b92dc0 100644 --- a/cmd/alipay_redpack.go +++ b/cmd/alipay_redpack.go @@ -48,7 +48,7 @@ func alipayOrderRedPack() { Config: getAlpayRedConf(), Order: &proto.OrderRequest_Order{ OrderNo: "lsxd202406071545141534", - Account: "", + Account: "123", Quantity: 1, Amount: 0.01, Extra: []byte(`{"name":"李1子铭"}`), diff --git a/plugins/alipay_cpn/internal/alipay_cpn.go b/plugins/alipay_cpn/internal/alipay_cpn.go index 7512a45..d357722 100644 --- a/plugins/alipay_cpn/internal/alipay_cpn.go +++ b/plugins/alipay_cpn/internal/alipay_cpn.go @@ -2,12 +2,10 @@ package internal import ( "context" - "encoding/json" "fmt" "gitea.cdlsxd.cn/sdk/plugin/proto" "github.com/carlmjohnson/requests" "plugins/alipay_cpn/internal/po" - "plugins/utils/helper" ) // 插件通信信息,若不对应则会报错panic @@ -45,15 +43,9 @@ func (s *AlipayCpnService) Order(ctx context.Context, request *proto.OrderReques return nil, err } - var sp string - err = requests.URL(baseUri).Post().BodyForm(uv).ToString(&sp).Fetch(ctx) - if err != nil { - return nil, fmt.Errorf("请求异常,msg:" + err.Error()) - } var response *po.OrderResp - sp = helper.ToChinese(sp) - if err = json.Unmarshal([]byte(sp), &response); err != nil { - return nil, fmt.Errorf("请求解析异常,msg:" + err.Error()) + if err = requests.URL(baseUri).Post().Params(uv).ToJSON(&response).Fetch(ctx); err != nil { + return nil, err } return orderResp(request, response), nil @@ -77,16 +69,9 @@ func (s *AlipayCpnService) Query(ctx context.Context, request *proto.QueryReques return nil, err } - var sp string - err = requests.URL(baseUri).Post().BodyForm(uv).ToString(&sp).Fetch(ctx) - if err != nil { - return nil, fmt.Errorf("请求异常,msg:" + err.Error()) - } - var response po.QueryResp - sp = helper.ToChinese(sp) - if err = json.Unmarshal([]byte(sp), &response); err != nil { - return nil, fmt.Errorf("请求解析异常,msg:" + err.Error()) + if err = requests.URL(baseUri).Post().BodyForm(uv).ToJSON(&response).Fetch(ctx); err != nil { + return nil, fmt.Errorf("请求异常,msg:" + err.Error()) } return queryResp(request, response), nil diff --git a/plugins/alipay_redpack/internal/alipay_redpack.go b/plugins/alipay_redpack/internal/alipay_redpack.go index 6a872d1..bda536f 100644 --- a/plugins/alipay_redpack/internal/alipay_redpack.go +++ b/plugins/alipay_redpack/internal/alipay_redpack.go @@ -2,13 +2,11 @@ package internal import ( "context" - "encoding/json" "fmt" "gitea.cdlsxd.cn/sdk/plugin/proto" "github.com/carlmjohnson/requests" "plugins/alipay_redpack/internal/po" "plugins/utils/alipay" - "plugins/utils/helper" ) // 插件通信信息,若不对应则会报错panic @@ -47,13 +45,9 @@ func (s *AlipayRedPackService) Order(ctx context.Context, request *proto.OrderRe return nil, err } - var sp string - err = requests.URL(baseUri).Post().Params(uv).ToString(&sp).Fetch(ctx) - var response *po.OrderResp - sp = helper.ToChinese(sp) - if err = json.Unmarshal([]byte(sp), &response); err != nil { - return nil, fmt.Errorf("请求解析异常,msg:" + err.Error()) + if err = requests.URL(baseUri).Post().Params(uv).ToJSON(&response).Fetch(ctx); err != nil { + return nil, err } return orderResp(request, response), nil @@ -77,16 +71,9 @@ func (s *AlipayRedPackService) Query(ctx context.Context, request *proto.QueryRe return nil, err } - var sp string - err = requests.URL(baseUri).Post().BodyForm(uv).ToString(&sp).Fetch(ctx) - if err != nil { - return nil, fmt.Errorf("请求异常,msg:" + err.Error()) - } - var response po.QueryResp - sp = helper.ToChinese(sp) - if err = json.Unmarshal([]byte(sp), &response); err != nil { - return nil, fmt.Errorf("请求解析异常,msg:" + err.Error()) + if err = requests.URL(baseUri).Post().BodyForm(uv).ToJSON(&response).Fetch(ctx); err != nil { + return nil, fmt.Errorf("请求异常,msg:" + err.Error()) } return queryResp(request, response), nil diff --git a/utils/request/request.go b/utils/request/request.go new file mode 100644 index 0000000..5eb8ea2 --- /dev/null +++ b/utils/request/request.go @@ -0,0 +1,38 @@ +package request + +import ( + "bytes" + "context" + "fmt" + "io" + "net/http" + "time" +) + +func Request(_ context.Context, method, url string, body []byte) ([]byte, http.Header, error) { + req, err := http.NewRequest(method, url, bytes.NewBuffer(body)) + if err != nil { + return nil, nil, err + } + + httpClient := &http.Client{ + Timeout: 15 * time.Second, + } + + resp, err := httpClient.Do(req) + if err != nil { + return nil, nil, err + } + defer resp.Body.Close() + + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return nil, nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, nil, fmt.Errorf("请求异常:%s", resp.Status) + } + + return bodyBytes, resp.Header, nil +}