From ce9fc21598385a58bdd13c3617d09e0cd5030f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Tue, 10 Dec 2024 15:46:23 +0800 Subject: [PATCH] ysf --- Makefile | 14 +++++++++++++- utils/request/request.go | 37 ++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 8eb2d30..9f715cc 100644 --- a/Makefile +++ b/Makefile @@ -65,5 +65,17 @@ wechat_redpack: make build-linux name=wechat_redpack && \ make build-win name=wechat_redpack +.PHONY: union_pay_cpn +union_pay_cpn: + make build-mac name=union_pay_cpn && \ + make build-linux name=union_pay_cpn && \ + make build-win name=union_pay_cpn + +.PHONY: union_pay_redpack +union_pay_redpack: + make build-mac name=union_pay_redpack && \ + make build-linux name=union_pay_redpack && \ + make build-win name=union_pay_redpack + .PHONY: all -all: zltx_v1 zltx_card_v1 alipay_cpn alipay_redpack wechat_cpn wechat_redpack \ No newline at end of file +all: zltx_v1 zltx_card_v1 alipay_cpn alipay_redpack wechat_cpn wechat_redpack union_pay_cpn union_pay_redpack \ No newline at end of file diff --git a/utils/request/request.go b/utils/request/request.go index 988e7f9..874d6ee 100644 --- a/utils/request/request.go +++ b/utils/request/request.go @@ -9,57 +9,57 @@ import ( "time" ) -// RequestOptions 用于配置请求的各种选项 -type RequestOptions struct { +// Options 用于配置请求的各种选项 +type Options struct { Headers http.Header - Timeout time.Duration StatusCodeFunc func(int) bool + + Timeout time.Duration } -// RequestOption 是一个函数类型,用于设置RequestOptions的各个字段 -type RequestOption func(*RequestOptions) +// Option 是一个函数类型,用于设置RequestOptions的各个字段 +type Option func(*Options) // WithTimeout 设置请求超时时间的选项函数 -func WithTimeout(timeout time.Duration) RequestOption { - return func(options *RequestOptions) { +func WithTimeout(timeout time.Duration) Option { + return func(options *Options) { options.Timeout = timeout } } // WithHeaders 设置请求头的选项函数 -func WithHeaders(headers http.Header) RequestOption { - return func(options *RequestOptions) { +func WithHeaders(headers http.Header) Option { + return func(options *Options) { options.Headers = headers } } // WithStatusCodeFunc 设置自定义状态码处理函数的选项函数 -func WithStatusCodeFunc(statusCodeFunc func(int) bool) RequestOption { - return func(options *RequestOptions) { +func WithStatusCodeFunc(statusCodeFunc func(int) bool) Option { + return func(options *Options) { options.StatusCodeFunc = statusCodeFunc } } -func Post(ctx context.Context, url string, body []byte, options ...RequestOption) (http.Header, []byte, error) { +func Post(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) { return Request(ctx, http.MethodPost, url, body, options...) } -func Get(ctx context.Context, url string, options ...RequestOption) (http.Header, []byte, error) { +func Get(ctx context.Context, url string, options ...Option) (http.Header, []byte, error) { return Request(ctx, http.MethodGet, url, nil, options...) } -func Put(ctx context.Context, url string, body []byte, options ...RequestOption) (http.Header, []byte, error) { +func Put(ctx context.Context, url string, body []byte, options ...Option) (http.Header, []byte, error) { return Request(ctx, http.MethodPut, url, body, options...) } -// Request 封装的HTTP请求函数,使用选项模式进行配置 -func Request(_ context.Context, method, url string, body []byte, options ...RequestOption) (http.Header, []byte, error) { - // 设置默认选项 - o := &RequestOptions{ +func Request(_ context.Context, method, url string, body []byte, options ...Option) (http.Header, []byte, error) { + o := &Options{ Headers: http.Header{ "Content-Type": []string{"application/json"}, }, + Timeout: 15 * time.Second, StatusCodeFunc: func(code int) bool { @@ -67,7 +67,6 @@ func Request(_ context.Context, method, url string, body []byte, options ...Requ }, } - // 根据传入的选项更新默认选项 for _, option := range options { option(o) }