From adc97df22a51fadbf108d63f622856c5a95ce76a Mon Sep 17 00:00:00 2001 From: shenghui Date: Fri, 19 Jan 2024 10:37:24 +0800 Subject: [PATCH] Update import statements in Go code --- alipay/client.go | 32 ++++++++++++------------ alipay/client_v3.go | 38 ++++++++++++++--------------- antchain/client.go | 24 +++++++++--------- esign/client.go | 16 ++++++------ lib/hash/hmac.go | 9 +++++++ lib/hash/hmac_test.go | 4 +++ lib/http/client.go | 26 ++++++++++---------- lib/http/util.go | 34 +++++++++++++------------- lib/logger.go | 8 +++--- sandpay/client.go | 20 +++++++-------- sandpay/util.go | 4 +-- wechat/corp.go | 16 ++++++------ wechat/event.go | 8 +++--- wechat/miniprogram.go | 36 +++++++++++++-------------- wechat/official_account.go | 18 +++++++------- wechat/pay.go | 20 +++++++-------- wechat/pay_v3.go | 50 +++++++++++++++++++------------------- ysepay/client.go | 26 ++++++++++---------- 18 files changed, 201 insertions(+), 188 deletions(-) diff --git a/alipay/client.go b/alipay/client.go index 65355a3..3233532 100644 --- a/alipay/client.go +++ b/alipay/client.go @@ -14,8 +14,8 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_http "github.com/shenghui0779/sdk-go/lib/http" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -24,9 +24,9 @@ type Client struct { gateway string appid string aesKey string - prvKey *libCrypto.PrivateKey - pubKey *libCrypto.PublicKey - httpCli libHttp.HTTPClient + prvKey *lib_crypto.PrivateKey + pubKey *lib_crypto.PublicKey + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -52,8 +52,8 @@ func (c *Client) Do(ctx context.Context, method string, options ...ActionOption) log.SetReqBody(body) resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, []byte(body), - libHttp.WithHeader(libHttp.HeaderAccept, "application/json"), - libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentForm), + lib_http.WithHeader(lib_http.HeaderAccept, "application/json"), + lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentForm), ) if err != nil { return lib.Fail(err) @@ -100,7 +100,7 @@ func (c *Client) Do(ctx context.Context, method string, options ...ActionOption) } // Upload 文件上传,参考:https://opendocs.alipay.com/apis/api_4/alipay.merchant.item.file.upload -func (c *Client) Upload(ctx context.Context, method string, form libHttp.UploadForm, options ...ActionOption) (gjson.Result, error) { +func (c *Client) Upload(ctx context.Context, method string, form lib_http.UploadForm, options ...ActionOption) (gjson.Result, error) { log := lib.NewReqLog(http.MethodPost, c.gateway) defer log.Do(ctx, c.logger) @@ -113,7 +113,7 @@ func (c *Client) Upload(ctx context.Context, method string, form libHttp.UploadF log.Set("query", query) - resp, err := c.httpCli.Upload(ctx, c.gateway+"?"+query, form, libHttp.WithHeader(libHttp.HeaderAccept, "application/json")) + resp, err := c.httpCli.Upload(ctx, c.gateway+"?"+query, form, lib_http.WithHeader(lib_http.HeaderAccept, "application/json")) if err != nil { return lib.Fail(err) } @@ -215,7 +215,7 @@ func (c *Client) Encrypt(data string) (string, error) { return "", fmt.Errorf("aes_key base64.decode error: %w", err) } - ct, err := libCrypto.AESEncryptCBC(key, make([]byte, 16), []byte(data)) + ct, err := lib_crypto.AESEncryptCBC(key, make([]byte, 16), []byte(data)) if err != nil { return "", err } @@ -235,7 +235,7 @@ func (c *Client) Decrypt(encryptData string) ([]byte, error) { return nil, fmt.Errorf("encrypt_data base64.decode error: %w", err) } - return libCrypto.AESDecryptCBC(key, make([]byte, 16), data) + return lib_crypto.AESDecryptCBC(key, make([]byte, 16), data) } // DecodeEncryptData 解析加密数据,如:授权的用户信息和手机号 @@ -297,19 +297,19 @@ type Option func(c *Client) // WithHttpCli 设置自定义 HTTP Client func WithHttpCli(cli *http.Client) Option { return func(c *Client) { - c.httpCli = libHttp.NewHTTPClient(cli) + c.httpCli = lib_http.NewHTTPClient(cli) } } // WithPrivateKey 设置商户RSA私钥 -func WithPrivateKey(key *libCrypto.PrivateKey) Option { +func WithPrivateKey(key *lib_crypto.PrivateKey) Option { return func(c *Client) { c.prvKey = key } } // WithPublicKey 设置平台RSA公钥 -func WithPublicKey(key *libCrypto.PublicKey) Option { +func WithPublicKey(key *lib_crypto.PublicKey) Option { return func(c *Client) { c.pubKey = key } @@ -328,7 +328,7 @@ func NewClient(appid, aesKey string, options ...Option) *Client { appid: appid, aesKey: aesKey, gateway: "https://openapi.alipay.com/gateway.do", - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { @@ -344,7 +344,7 @@ func NewSandbox(appid, aesKey string, options ...Option) *Client { appid: appid, aesKey: aesKey, gateway: "https://openapi-sandbox.dl.alipaydev.com/gateway.do", - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/alipay/client_v3.go b/alipay/client_v3.go index ca593ab..5db3f0a 100644 --- a/alipay/client_v3.go +++ b/alipay/client_v3.go @@ -18,8 +18,8 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_http "github.com/shenghui0779/sdk-go/lib/http" ) // ClientV3 支付宝V3客户端(仅支持v3版本的接口可用) @@ -27,9 +27,9 @@ type ClientV3 struct { host string appid string aesKey string - prvKey *libCrypto.PrivateKey - pubKey *libCrypto.PublicKey - httpCli libHttp.HTTPClient + prvKey *lib_crypto.PrivateKey + pubKey *lib_crypto.PublicKey + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -89,7 +89,7 @@ func (c *ClientV3) do(ctx context.Context, method, path string, query url.Values if err != nil { return nil, err } - header.Set(libHttp.HeaderAuthorization, authStr) + header.Set(lib_http.HeaderAuthorization, authStr) log.SetReqHeader(header) @@ -137,7 +137,7 @@ func (c *ClientV3) do(ctx context.Context, method, path string, query url.Values func (c *ClientV3) GetJSON(ctx context.Context, path string, query url.Values, options ...V3HeaderOption) (*APIResult, error) { header := http.Header{} - header.Set(libHttp.HeaderAccept, "application/json") + header.Set(lib_http.HeaderAccept, "application/json") header.Set(HeaderRequestID, uuid.NewString()) for _, f := range options { @@ -151,9 +151,9 @@ func (c *ClientV3) GetJSON(ctx context.Context, path string, query url.Values, o func (c *ClientV3) PostJSON(ctx context.Context, path string, params lib.X, options ...V3HeaderOption) (*APIResult, error) { header := http.Header{} - header.Set(libHttp.HeaderAccept, "application/json") + header.Set(lib_http.HeaderAccept, "application/json") header.Set(HeaderRequestID, uuid.NewString()) - header.Set(libHttp.HeaderContentType, libHttp.ContentJSON) + header.Set(lib_http.HeaderContentType, lib_http.ContentJSON) for _, f := range options { f(header) @@ -168,7 +168,7 @@ func (c *ClientV3) PostEncrypt(ctx context.Context, path string, params lib.X, o header.Set(HeaderRequestID, uuid.NewString()) header.Set(HeaderEncryptType, "AES") - header.Set(libHttp.HeaderContentType, libHttp.ContentText) + header.Set(lib_http.HeaderContentType, lib_http.ContentText) for _, f := range options { f(header) @@ -178,7 +178,7 @@ func (c *ClientV3) PostEncrypt(ctx context.Context, path string, params lib.X, o } // Upload 文件上传,参考:https://opendocs.alipay.com/open-v3/054oog?pathHash=7834d743 -func (c *ClientV3) Upload(ctx context.Context, path string, form libHttp.UploadForm, options ...V3HeaderOption) (*APIResult, error) { +func (c *ClientV3) Upload(ctx context.Context, path string, form lib_http.UploadForm, options ...V3HeaderOption) (*APIResult, error) { reqID := uuid.NewString() reqURL := c.url(path, nil) @@ -196,7 +196,7 @@ func (c *ClientV3) Upload(ctx context.Context, path string, form libHttp.UploadF if err != nil { return nil, err } - reqHeader.Set(libHttp.HeaderAuthorization, authStr) + reqHeader.Set(lib_http.HeaderAuthorization, authStr) log.SetReqHeader(reqHeader) @@ -307,7 +307,7 @@ func (c *ClientV3) Encrypt(data string) (string, error) { return "", err } - ct, err := libCrypto.AESEncryptCBC(key, make([]byte, 16), []byte(data)) + ct, err := lib_crypto.AESEncryptCBC(key, make([]byte, 16), []byte(data)) if err != nil { return "", err } @@ -327,7 +327,7 @@ func (c *ClientV3) Decrypt(encryptData string) ([]byte, error) { return nil, err } - return libCrypto.AESDecryptCBC(key, make([]byte, 16), data) + return lib_crypto.AESDecryptCBC(key, make([]byte, 16), data) } // V3Option 自定义设置项 @@ -336,19 +336,19 @@ type V3Option func(c *ClientV3) // WithV3Client 设置自定义 HTTP Client func WithV3Client(cli *http.Client) V3Option { return func(c *ClientV3) { - c.httpCli = libHttp.NewHTTPClient(cli) + c.httpCli = lib_http.NewHTTPClient(cli) } } // WithV3PrivateKey 设置商户RSA私钥 -func WithV3PrivateKey(key *libCrypto.PrivateKey) V3Option { +func WithV3PrivateKey(key *lib_crypto.PrivateKey) V3Option { return func(c *ClientV3) { c.prvKey = key } } // WithV3PublicKey 设置平台RSA公钥 -func WithV3PublicKey(key *libCrypto.PublicKey) V3Option { +func WithV3PublicKey(key *lib_crypto.PublicKey) V3Option { return func(c *ClientV3) { c.pubKey = key } @@ -367,7 +367,7 @@ func NewClientV3(appid, aesKey string, options ...V3Option) *ClientV3 { host: "https://openapi.alipay.com", appid: appid, aesKey: aesKey, - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { @@ -383,7 +383,7 @@ func NewSandboxV3(appid, aesKey string, options ...V3Option) *ClientV3 { host: "http://openapi.sandbox.dl.alipaydev.com", appid: appid, aesKey: aesKey, - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/antchain/client.go b/antchain/client.go index 6c762ba..60db875 100644 --- a/antchain/client.go +++ b/antchain/client.go @@ -15,18 +15,18 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_http "github.com/shenghui0779/sdk-go/lib/http" ) // Config 客户端配置 type Config struct { - BizID string `json:"biz_id"` // 链ID (a00e36c5) - TenantID string `json:"tenant_id"` // 租户ID - AccessID string `json:"access_id"` // AccessID - AccessKey *libCrypto.PrivateKey `json:"access_key"` // AccessKey - Account string `json:"account"` // 链账户 - MyKmsKeyID string `json:"mykmskey_id"` // 托管标识 + BizID string `json:"biz_id"` // 链ID (a00e36c5) + TenantID string `json:"tenant_id"` // 租户ID + AccessID string `json:"access_id"` // AccessID + AccessKey *lib_crypto.PrivateKey `json:"access_key"` // AccessKey + Account string `json:"account"` // 链账户 + MyKmsKeyID string `json:"mykmskey_id"` // 托管标识 } // Client 发送请求使用的客户端 @@ -74,7 +74,7 @@ func WithParam(key string, value any) ChainCallOption { type client struct { endpoint string config *Config - httpCli libHttp.HTTPClient + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -150,7 +150,7 @@ func (c *client) do(ctx context.Context, reqURL string, params lib.X) (string, e log.SetReqBody(string(body)) - resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, body, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, body, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return "", err } @@ -184,7 +184,7 @@ type Option func(c *client) // WithHttpCli 设置自定义 HTTP Client func WithHttpCli(httpCli *http.Client) Option { return func(c *client) { - c.httpCli = libHttp.NewHTTPClient(httpCli) + c.httpCli = lib_http.NewHTTPClient(httpCli) } } @@ -200,7 +200,7 @@ func NewClient(cfg *Config, options ...Option) Client { c := &client{ endpoint: "https://rest.baas.alipay.com", config: cfg, - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/esign/client.go b/esign/client.go index b81ca54..4602694 100644 --- a/esign/client.go +++ b/esign/client.go @@ -21,7 +21,7 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_http "github.com/shenghui0779/sdk-go/lib/http" ) // Client E签宝客户端 @@ -29,7 +29,7 @@ type Client struct { host string appid string secret string - httpCli libHttp.HTTPClient + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -58,7 +58,7 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values, header := http.Header{} - header.Set(libHttp.HeaderAccept, AcceptAll) + header.Set(lib_http.HeaderAccept, AcceptAll) header.Set(HeaderTSignOpenAppID, c.appid) header.Set(HeaderTSignOpenAuthMode, AuthModeSign) header.Set(HeaderTSignOpenCaTimestamp, strconv.FormatInt(time.Now().UnixMilli(), 10)) @@ -83,7 +83,7 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values, contentMD5 := ContentMD5(body) - header.Set(libHttp.HeaderContentType, "application/json; charset=UTF-8") + header.Set(lib_http.HeaderContentType, "application/json; charset=UTF-8") header.Set(HeaderContentMD5, contentMD5) options = append(options, WithSignContMD5(contentMD5), WithSignContType("application/json; charset=UTF-8")) @@ -132,7 +132,7 @@ func (c *Client) doStream(ctx context.Context, uploadURL string, reader io.ReadS header := http.Header{} - header.Set(libHttp.HeaderContentType, libHttp.ContentStream) + header.Set(lib_http.HeaderContentType, lib_http.ContentStream) header.Set(HeaderContentMD5, base64.StdEncoding.EncodeToString(h.Sum(nil))) log.SetReqHeader(header) @@ -227,7 +227,7 @@ type Option func(c *Client) // WithHttpCli 设置自定义 HTTP Client func WithHttpCli(cli *http.Client) Option { return func(c *Client) { - c.httpCli = libHttp.NewHTTPClient(cli) + c.httpCli = lib_http.NewHTTPClient(cli) } } @@ -244,7 +244,7 @@ func NewClient(appid, secret string, options ...Option) *Client { host: "https://openapi.esign.cn", appid: appid, secret: secret, - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { @@ -260,7 +260,7 @@ func NewSandbox(appid, secret string, options ...Option) *Client { host: "https://smlopenapi.esign.cn", appid: appid, secret: secret, - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/lib/hash/hmac.go b/lib/hash/hmac.go index 8b3e0ff..c222ab4 100644 --- a/lib/hash/hmac.go +++ b/lib/hash/hmac.go @@ -3,11 +3,20 @@ package hash import ( "crypto" "crypto/hmac" + "crypto/sha1" "crypto/sha256" "encoding/hex" "fmt" ) +// HMacSHA1 计算hmac-sha1值 +func HMacSHA1(key, str string) string { + h := hmac.New(sha1.New, []byte(key)) + h.Write([]byte(str)) + + return hex.EncodeToString(h.Sum(nil)) +} + // HMacSHA256 计算hmac-sha256值 func HMacSHA256(key, str string) string { h := hmac.New(sha256.New, []byte(key)) diff --git a/lib/hash/hmac_test.go b/lib/hash/hmac_test.go index dd4651b..6d807f0 100644 --- a/lib/hash/hmac_test.go +++ b/lib/hash/hmac_test.go @@ -7,6 +7,10 @@ import ( "github.com/stretchr/testify/assert" ) +func TestHMacSHA1(t *testing.T) { + assert.Equal(t, "30c0c496355c2bb9308c63159cc4b726f1205dfc", HMacSHA1("iiinsomnia", "ILoveYiigo")) +} + func TestHMacSHA256(t *testing.T) { assert.Equal(t, "a458409cd884140c1ca36ef3013a5c7289c3e057049e3563401094d3f929b93b", HMacSHA256("iiinsomnia", "ILoveYiigo")) } diff --git a/lib/http/client.go b/lib/http/client.go index 0e60e77..71994af 100644 --- a/lib/http/client.go +++ b/lib/http/client.go @@ -10,8 +10,8 @@ import ( "time" ) -// HTTPClient HTTP客户端 -type HTTPClient interface { +// Client HTTP客户端 +type Client interface { // Do 发送HTTP请求 // 注意:应该使用Context设置请求超时时间 Do(ctx context.Context, method, reqURL string, body []byte, opts ...Option) (*http.Response, error) @@ -20,11 +20,11 @@ type HTTPClient interface { Upload(ctx context.Context, reqURL string, form UploadForm, opts ...Option) (*http.Response, error) } -type httpCli struct { - client *http.Client +type client struct { + cli *http.Client } -func (c *httpCli) Do(ctx context.Context, method, reqURL string, body []byte, opts ...Option) (*http.Response, error) { +func (c *client) Do(ctx context.Context, method, reqURL string, body []byte, opts ...Option) (*http.Response, error) { req, err := http.NewRequestWithContext(ctx, method, reqURL, bytes.NewReader(body)) if err != nil { return nil, err @@ -53,7 +53,7 @@ func (c *httpCli) Do(ctx context.Context, method, reqURL string, body []byte, op req.Close = true } - resp, err := c.client.Do(req) + resp, err := c.cli.Do(req) if err != nil { // If the context has been canceled, the context's error is probably more useful. select { @@ -68,7 +68,7 @@ func (c *httpCli) Do(ctx context.Context, method, reqURL string, body []byte, op return resp, nil } -func (c *httpCli) Upload(ctx context.Context, reqURL string, form UploadForm, options ...Option) (*http.Response, error) { +func (c *client) Upload(ctx context.Context, reqURL string, form UploadForm, options ...Option) (*http.Response, error) { buf := bytes.NewBuffer(make([]byte, 0, 20<<10)) // 20kb w := multipart.NewWriter(buf) if err := form.Write(w); err != nil { @@ -87,9 +87,9 @@ func (c *httpCli) Upload(ctx context.Context, reqURL string, form UploadForm, op } // NewDefaultClient 生成一个默认的HTTP客户端 -func NewDefaultClient(certs ...tls.Certificate) HTTPClient { - return &httpCli{ - client: &http.Client{ +func NewDefaultClient(certs ...tls.Certificate) Client { + return &client{ + cli: &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{ @@ -112,8 +112,8 @@ func NewDefaultClient(certs ...tls.Certificate) HTTPClient { } // NewHTTPClient 通过官方 `http.Client` 生成一个HTTP客户端 -func NewHTTPClient(client *http.Client) HTTPClient { - return &httpCli{ - client: client, +func NewHTTPClient(c *http.Client) Client { + return &client{ + cli: c, } } diff --git a/lib/http/util.go b/lib/http/util.go index 1e69405..97d7ebf 100644 --- a/lib/http/util.go +++ b/lib/http/util.go @@ -15,43 +15,43 @@ const ( ) const ( - ContentText = "text/plain;charset=utf-8" - ContentJSON = "application/json;charset=utf-8" - ContentForm = "application/x-www-form-urlencoded" - ContentStream = "application/octet-stream" - ContentFormData = "multipart/form-data" + ContentText = "text/plain;charset=utf-8" + ContentJSON = "application/json;charset=utf-8" + ContentForm = "application/x-www-form-urlencoded" + ContentStream = "application/octet-stream" + ContentFormMultipart = "multipart/form-data" ) var defaultCli = NewDefaultClient() -// HTTPGet 发送GET请求 -func HTTPGet(ctx context.Context, reqURL string, options ...Option) (*http.Response, error) { +// Get 发送GET请求 +func Get(ctx context.Context, reqURL string, options ...Option) (*http.Response, error) { return defaultCli.Do(ctx, http.MethodGet, reqURL, nil, options...) } -// HTTPPost 发送POST请求 -func HTTPPost(ctx context.Context, reqURL string, body []byte, options ...Option) (*http.Response, error) { +// Post 发送POST请求 +func Post(ctx context.Context, reqURL string, body []byte, options ...Option) (*http.Response, error) { return defaultCli.Do(ctx, http.MethodPost, reqURL, body, options...) } -// HTTPPostJSON 发送POST请求(json数据) -func HTTPPostJSON(ctx context.Context, reqURL string, body []byte, options ...Option) (*http.Response, error) { +// PostJSON 发送POST请求(json数据) +func PostJSON(ctx context.Context, reqURL string, body []byte, options ...Option) (*http.Response, error) { options = append(options, WithHeader(HeaderContentType, ContentJSON)) return defaultCli.Do(ctx, http.MethodPost, reqURL, body, options...) } -// HTTPPostForm 发送POST表单请求 -func HTTPPostForm(ctx context.Context, reqURL string, data url.Values, options ...Option) (*http.Response, error) { +// PostForm 发送POST表单请求 +func PostForm(ctx context.Context, reqURL string, data url.Values, options ...Option) (*http.Response, error) { options = append(options, WithHeader(HeaderContentType, ContentForm)) return defaultCli.Do(ctx, http.MethodPost, reqURL, []byte(data.Encode()), options...) } -// HTTPUpload 文件上传 -func HTTPUpload(ctx context.Context, reqURL string, form UploadForm, options ...Option) (*http.Response, error) { +// Upload 文件上传 +func Upload(ctx context.Context, reqURL string, form UploadForm, options ...Option) (*http.Response, error) { return defaultCli.Upload(ctx, reqURL, form, options...) } -// HTTPDo 发送HTTP请求 -func HTTPDo(ctx context.Context, method, reqURL string, body []byte, options ...Option) (*http.Response, error) { +// Do 发送HTTP请求 +func Do(ctx context.Context, method, reqURL string, body []byte, options ...Option) (*http.Response, error) { return defaultCli.Do(ctx, method, reqURL, body, options...) } diff --git a/lib/logger.go b/lib/logger.go index b67f66c..98ded13 100644 --- a/lib/logger.go +++ b/lib/logger.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_http "github.com/shenghui0779/sdk-go/lib/http" ) // ReqLog 请求日志 @@ -81,11 +81,11 @@ func HeaderEncode(h http.Header) string { return buf.String() } -func HeaderToHttpOption(h http.Header) []libHttp.Option { - options := make([]libHttp.Option, 0, len(h)) +func HeaderToHttpOption(h http.Header) []lib_http.Option { + options := make([]lib_http.Option, 0, len(h)) for k, vals := range h { - options = append(options, libHttp.WithHeader(k, vals...)) + options = append(options, lib_http.WithHeader(k, vals...)) } return options diff --git a/sandpay/client.go b/sandpay/client.go index a2cb649..b6dc0b9 100755 --- a/sandpay/client.go +++ b/sandpay/client.go @@ -13,16 +13,16 @@ import ( "strings" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_http "github.com/shenghui0779/sdk-go/lib/http" ) // Client 杉德支付客户端 type Client struct { mchID string - prvKey *libCrypto.PrivateKey - pubKey *libCrypto.PublicKey - httpCli libHttp.HTTPClient + prvKey *lib_crypto.PrivateKey + pubKey *lib_crypto.PublicKey + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -43,7 +43,7 @@ func (c *Client) Do(ctx context.Context, reqURL string, form *Form) (*Form, erro log.SetReqBody(body) - resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, []byte(body), libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentForm)) + resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, []byte(body), lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentForm)) if err != nil { return nil, err } @@ -105,19 +105,19 @@ type Option func(c *Client) // WithHttpCli 设置自定义 HTTP Client func WithHttpCli(cli *http.Client) Option { return func(c *Client) { - c.httpCli = libHttp.NewHTTPClient(cli) + c.httpCli = lib_http.NewHTTPClient(cli) } } // WithPrivateKey 设置商户RSA私钥 -func WithPrivateKey(key *libCrypto.PrivateKey) Option { +func WithPrivateKey(key *lib_crypto.PrivateKey) Option { return func(c *Client) { c.prvKey = key } } // WithPublicKey 设置平台RSA公钥 -func WithPublicKey(key *libCrypto.PublicKey) Option { +func WithPublicKey(key *lib_crypto.PublicKey) Option { return func(c *Client) { c.pubKey = key } @@ -134,7 +134,7 @@ func WithLogger(f func(ctx context.Context, data map[string]string)) Option { func NewClient(mchID string, options ...Option) *Client { c := &Client{ mchID: mchID, - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/sandpay/util.go b/sandpay/util.go index b9d7991..4d3d4bc 100644 --- a/sandpay/util.go +++ b/sandpay/util.go @@ -8,7 +8,7 @@ import ( "time" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -21,7 +21,7 @@ type Form struct { } // URLEncode 数据表单格式化为POST表单 -func (f *Form) URLEncode(mid string, key *libCrypto.PrivateKey) (string, error) { +func (f *Form) URLEncode(mid string, key *lib_crypto.PrivateKey) (string, error) { if key == nil { return "", errors.New("private key is nil (forgotten configure?)") } diff --git a/wechat/corp.go b/wechat/corp.go index a2ac1d1..6e8b1b6 100644 --- a/wechat/corp.go +++ b/wechat/corp.go @@ -13,7 +13,7 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_http "github.com/shenghui0779/sdk-go/lib/http" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -23,7 +23,7 @@ type Corp struct { corpid string secret string srvCfg *ServerConfig - httpCli libHttp.HTTPClient + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -54,7 +54,7 @@ func (c *Corp) url(path string, query url.Values) string { return builder.String() } -func (c *Corp) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...libHttp.Option) ([]byte, error) { +func (c *Corp) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...lib_http.Option) ([]byte, error) { reqURL := c.url(path, query) log := lib.NewReqLog(method, reqURL) @@ -157,7 +157,7 @@ func (c *Corp) PostJSON(ctx context.Context, accessToken, path string, params li query := url.Values{} query.Set(AccessToken, accessToken) - b, err := c.do(ctx, http.MethodPost, path, query, params, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + b, err := c.do(ctx, http.MethodPost, path, query, params, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return lib.Fail(err) } @@ -195,7 +195,7 @@ func (c *Corp) PostBuffer(ctx context.Context, accessToken, path string, params query := url.Values{} query.Set(AccessToken, accessToken) - b, err := c.do(ctx, http.MethodPost, path, query, params, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + b, err := c.do(ctx, http.MethodPost, path, query, params, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return nil, err } @@ -209,7 +209,7 @@ func (c *Corp) PostBuffer(ctx context.Context, accessToken, path string, params } // Upload 上传媒体资源 -func (c *Corp) Upload(ctx context.Context, accessToken, path string, form libHttp.UploadForm) (gjson.Result, error) { +func (c *Corp) Upload(ctx context.Context, accessToken, path string, form lib_http.UploadForm) (gjson.Result, error) { query := url.Values{} query.Set(AccessToken, accessToken) @@ -296,7 +296,7 @@ func WithCorpSrvCfg(token, aeskey string) CorpOption { // WithCorpHttpCli 设置企业微信请求的 HTTP Client func WithCorpHttpCli(cli *http.Client) CorpOption { return func(c *Corp) { - c.httpCli = libHttp.NewHTTPClient(cli) + c.httpCli = lib_http.NewHTTPClient(cli) } } @@ -314,7 +314,7 @@ func NewCorp(corpid, secret string, options ...CorpOption) *Corp { corpid: corpid, secret: secret, srvCfg: new(ServerConfig), - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/wechat/event.go b/wechat/event.go index 70c4037..c98c2ac 100644 --- a/wechat/event.go +++ b/wechat/event.go @@ -11,7 +11,7 @@ import ( "time" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -31,7 +31,7 @@ func SignWithSHA1(token string, items ...string) string { // EventEncrypt 时间消息加密 // [参考](https://developer.work.weixin.qq.com/document/path/90968) -func EventEncrypt(receiveID, encodingAESKey, nonce string, plainText []byte) (*libCrypto.CipherText, error) { +func EventEncrypt(receiveID, encodingAESKey, nonce string, plainText []byte) (*lib_crypto.CipherText, error) { key, err := base64.StdEncoding.DecodeString(encodingAESKey + "=") if err != nil { return nil, err @@ -47,7 +47,7 @@ func EventEncrypt(receiveID, encodingAESKey, nonce string, plainText []byte) (*l copy(encryptData[20:], plainText) copy(encryptData[appidOffset:], receiveID) - return libCrypto.AESEncryptCBC(key, key[:aes.BlockSize], encryptData) + return lib_crypto.AESEncryptCBC(key, key[:aes.BlockSize], encryptData) } // EventDecrypt 事件消息解密 @@ -63,7 +63,7 @@ func EventDecrypt(receiveID, encodingAESKey, cipherText string) ([]byte, error) return nil, err } - plainText, err := libCrypto.AESDecryptCBC(key, key[:aes.BlockSize], decryptData) + plainText, err := lib_crypto.AESDecryptCBC(key, key[:aes.BlockSize], decryptData) if err != nil { return nil, err } diff --git a/wechat/miniprogram.go b/wechat/miniprogram.go index 09f2a44..423241a 100644 --- a/wechat/miniprogram.go +++ b/wechat/miniprogram.go @@ -17,8 +17,8 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_http "github.com/shenghui0779/sdk-go/lib/http" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -26,9 +26,9 @@ import ( type SafeMode struct { aesSN string aeskey string - prvKey *libCrypto.PrivateKey + prvKey *lib_crypto.PrivateKey pubSN string - pubKey *libCrypto.PublicKey + pubKey *lib_crypto.PublicKey } // MiniProgram 小程序 @@ -38,7 +38,7 @@ type MiniProgram struct { secret string srvCfg *ServerConfig sfMode *SafeMode - httpCli libHttp.HTTPClient + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -69,7 +69,7 @@ func (mp *MiniProgram) url(path string, query url.Values) string { return builder.String() } -func (mp *MiniProgram) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...libHttp.Option) ([]byte, error) { +func (mp *MiniProgram) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...lib_http.Option) ([]byte, error) { reqURL := mp.url(path, query) log := lib.NewReqLog(method, reqURL) @@ -141,7 +141,7 @@ func (mp *MiniProgram) doSafe(ctx context.Context, method, path string, query ur reqHeader := http.Header{} - reqHeader.Set(libHttp.HeaderContentType, libHttp.ContentJSON) + reqHeader.Set(lib_http.HeaderContentType, lib_http.ContentJSON) reqHeader.Set(HeaderMPAppID, mp.appid) reqHeader.Set(HeaderMPTimestamp, strconv.FormatInt(now, 10)) reqHeader.Set(HeaderMPSignature, sign) @@ -218,7 +218,7 @@ func (mp *MiniProgram) encrypt(log *lib.ReqLog, path string, query url.Values, p iv := lib.NonceByte(12) aad := fmt.Sprintf("%s|%s|%d|%s", mp.url(path, nil), mp.appid, timestamp, mp.sfMode.aesSN) - ct, err := libCrypto.AESEncryptGCM(key, iv, data, []byte(aad), nil) + ct, err := lib_crypto.AESEncryptGCM(key, iv, data, []byte(aad), nil) if err != nil { return nil, err } @@ -325,7 +325,7 @@ func (mp *MiniProgram) decrypt(path string, header http.Header, body []byte) ([] aad := fmt.Sprintf("%s|%s|%s|%s", mp.url(path, nil), mp.appid, header.Get(HeaderMPTimestamp), mp.sfMode.aesSN) - return libCrypto.AESDecryptGCM(key, iv, append(data, tag...), []byte(aad), nil) + return lib_crypto.AESDecryptGCM(key, iv, append(data, tag...), []byte(aad), nil) } // Code2Session 通过临时登录凭证code完成登录流程 @@ -382,7 +382,7 @@ func (mp *MiniProgram) StableAccessToken(ctx context.Context, forceRefresh bool) "force_refresh": forceRefresh, } - b, err := mp.do(ctx, http.MethodPost, "/cgi-bin/stable_token", nil, params, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + b, err := mp.do(ctx, http.MethodPost, "/cgi-bin/stable_token", nil, params, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return lib.Fail(err) } @@ -440,7 +440,7 @@ func (mp *MiniProgram) PostJSON(ctx context.Context, accessToken, path string, p query := url.Values{} query.Set(AccessToken, accessToken) - b, err := mp.do(ctx, http.MethodPost, path, query, params, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + b, err := mp.do(ctx, http.MethodPost, path, query, params, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return lib.Fail(err) } @@ -458,7 +458,7 @@ func (mp *MiniProgram) PostBuffer(ctx context.Context, accessToken, path string, query := url.Values{} query.Set(AccessToken, accessToken) - b, err := mp.do(ctx, http.MethodPost, path, query, params, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + b, err := mp.do(ctx, http.MethodPost, path, query, params, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return nil, err } @@ -512,7 +512,7 @@ func (mp *MiniProgram) SafePostBuffer(ctx context.Context, accessToken, path str } // Upload 上传媒体资源 -func (mp *MiniProgram) Upload(ctx context.Context, accessToken, path string, form libHttp.UploadForm) (gjson.Result, error) { +func (mp *MiniProgram) Upload(ctx context.Context, accessToken, path string, form lib_http.UploadForm) (gjson.Result, error) { query := url.Values{} query.Set(AccessToken, accessToken) @@ -577,7 +577,7 @@ func (mp *MiniProgram) DecodeEncryptData(sessionKey, iv, encryptData string) ([] return nil, fmt.Errorf("encrypt_data base64.decode error: %w", err) } - ct, err := libCrypto.AESEncryptCBC(keyBlock, ivBlock, data) + ct, err := lib_crypto.AESEncryptCBC(keyBlock, ivBlock, data) if err != nil { return nil, err } @@ -620,7 +620,7 @@ func WithMPSrvCfg(token, aeskey string) MPOption { // WithMPHttpCli 设置小程序请求的 HTTP Client func WithMPHttpCli(c *http.Client) MPOption { return func(mp *MiniProgram) { - mp.httpCli = libHttp.NewHTTPClient(c) + mp.httpCli = lib_http.NewHTTPClient(c) } } @@ -640,14 +640,14 @@ func WithMPAesKey(serialNO, key string) MPOption { } // WithMPPrivateKey 设置小程序RSA私钥 -func WithMPPrivateKey(key *libCrypto.PrivateKey) MPOption { +func WithMPPrivateKey(key *lib_crypto.PrivateKey) MPOption { return func(mp *MiniProgram) { mp.sfMode.prvKey = key } } // WithMPPublicKey 设置小程序平台RSA公钥 -func WithMPPublicKey(serialNO string, key *libCrypto.PublicKey) MPOption { +func WithMPPublicKey(serialNO string, key *lib_crypto.PublicKey) MPOption { return func(mp *MiniProgram) { mp.sfMode.pubSN = serialNO mp.sfMode.pubKey = key @@ -661,7 +661,7 @@ func NewMiniProgram(appid, secret string, options ...MPOption) *MiniProgram { appid: appid, secret: secret, srvCfg: new(ServerConfig), - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/wechat/official_account.go b/wechat/official_account.go index b782d98..7ec85ee 100644 --- a/wechat/official_account.go +++ b/wechat/official_account.go @@ -13,7 +13,7 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_http "github.com/shenghui0779/sdk-go/lib/http" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -29,7 +29,7 @@ type OfficialAccount struct { appid string secret string srvCfg *ServerConfig - httpCli libHttp.HTTPClient + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -61,7 +61,7 @@ func (oa *OfficialAccount) url(path string, query url.Values) string { return builder.String() } -func (oa *OfficialAccount) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...libHttp.Option) ([]byte, error) { +func (oa *OfficialAccount) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...lib_http.Option) ([]byte, error) { reqURL := oa.url(path, query) log := lib.NewReqLog(method, reqURL) @@ -207,7 +207,7 @@ func (oa *OfficialAccount) StableAccessToken(ctx context.Context, forceRefresh b "force_refresh": forceRefresh, } - b, err := oa.do(ctx, http.MethodPost, "/cgi-bin/stable_token", nil, params, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + b, err := oa.do(ctx, http.MethodPost, "/cgi-bin/stable_token", nil, params, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return lib.Fail(err) } @@ -245,7 +245,7 @@ func (oa *OfficialAccount) PostJSON(ctx context.Context, accessToken, path strin query := url.Values{} query.Set(AccessToken, accessToken) - b, err := oa.do(ctx, http.MethodPost, path, query, params, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + b, err := oa.do(ctx, http.MethodPost, path, query, params, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return lib.Fail(err) } @@ -283,7 +283,7 @@ func (oa *OfficialAccount) PostBuffer(ctx context.Context, accessToken, path str query := url.Values{} query.Set(AccessToken, accessToken) - b, err := oa.do(ctx, http.MethodPost, path, query, params, libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON)) + b, err := oa.do(ctx, http.MethodPost, path, query, params, lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON)) if err != nil { return nil, err } @@ -297,7 +297,7 @@ func (oa *OfficialAccount) PostBuffer(ctx context.Context, accessToken, path str } // Upload 上传媒体资源 -func (oa *OfficialAccount) Upload(ctx context.Context, accessToken, path string, form libHttp.UploadForm) (gjson.Result, error) { +func (oa *OfficialAccount) Upload(ctx context.Context, accessToken, path string, form lib_http.UploadForm) (gjson.Result, error) { query := url.Values{} query.Set(AccessToken, accessToken) @@ -379,7 +379,7 @@ func WithOASrvCfg(token, aeskey string) OAOption { // WithOAHttpCli 设置公众号请求的 HTTP Client func WithOAHttpCli(c *http.Client) OAOption { return func(oa *OfficialAccount) { - oa.httpCli = libHttp.NewHTTPClient(c) + oa.httpCli = lib_http.NewHTTPClient(c) } } @@ -397,7 +397,7 @@ func NewOfficialAccount(appid, secret string, options ...OAOption) *OfficialAcco appid: appid, secret: secret, srvCfg: new(ServerConfig), - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/wechat/pay.go b/wechat/pay.go index b7478a4..862c0c8 100644 --- a/wechat/pay.go +++ b/wechat/pay.go @@ -13,9 +13,9 @@ import ( "time" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" "github.com/shenghui0779/sdk-go/lib/hash" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_http "github.com/shenghui0779/sdk-go/lib/http" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -24,8 +24,8 @@ type Pay struct { host string mchid string apikey string - httpCli libHttp.HTTPClient - tlsCli libHttp.HTTPClient + httpCli lib_http.Client + tlsCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -265,7 +265,7 @@ func (p *Pay) DecryptRefund(encrypt string) (value.V, error) { return nil, err } - plainText, err := libCrypto.AESDecryptECB([]byte(hash.MD5(p.apikey)), cipherText) + plainText, err := lib_crypto.AESDecryptECB([]byte(hash.MD5(p.apikey)), cipherText) if err != nil { return nil, err } @@ -327,21 +327,21 @@ type PayOption func(p *Pay) // WithPayTLSCert 设置支付TLS证书 func WithPayTLSCert(cert tls.Certificate) PayOption { return func(p *Pay) { - p.tlsCli = libHttp.NewDefaultClient(cert) + p.tlsCli = lib_http.NewDefaultClient(cert) } } // WithPayHttpCli 设置支付无证书 HTTP Client func WithPayHttpCli(c *http.Client) PayOption { return func(p *Pay) { - p.httpCli = libHttp.NewHTTPClient(c) + p.httpCli = lib_http.NewHTTPClient(c) } } // WithPayTLSCli 设置支付带证书 HTTP Client func WithPayTLSCli(c *http.Client) PayOption { return func(p *Pay) { - p.tlsCli = libHttp.NewHTTPClient(c) + p.tlsCli = lib_http.NewHTTPClient(c) } } @@ -358,8 +358,8 @@ func NewPay(mchid, apikey string, options ...PayOption) *Pay { host: "https://api.mch.weixin.qq.com", mchid: mchid, apikey: apikey, - httpCli: libHttp.NewDefaultClient(), - tlsCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), + tlsCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/wechat/pay_v3.go b/wechat/pay_v3.go index b0bf615..854285c 100644 --- a/wechat/pay_v3.go +++ b/wechat/pay_v3.go @@ -18,8 +18,8 @@ import ( "golang.org/x/sync/singleflight" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_http "github.com/shenghui0779/sdk-go/lib/http" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -29,10 +29,10 @@ type PayV3 struct { mchid string apikey string prvSN string - prvKey *libCrypto.PrivateKey - pubKeyM map[string]*libCrypto.PublicKey + prvKey *lib_crypto.PrivateKey + pubKeyM map[string]*lib_crypto.PublicKey mutex singleflight.Group - httpCli libHttp.HTTPClient + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -63,7 +63,7 @@ func (p *PayV3) url(path string, query url.Values) string { return builder.String() } -func (p *PayV3) publicKey(ctx context.Context, serialNO string) (*libCrypto.PublicKey, error) { +func (p *PayV3) publicKey(ctx context.Context, serialNO string) (*lib_crypto.PublicKey, error) { pubKey, ok := p.pubKeyM[serialNO] if ok { return pubKey, nil @@ -86,12 +86,12 @@ func (p *PayV3) publicKey(ctx context.Context, serialNO string) (*libCrypto.Publ data := cert.Get("ciphertext").String() aad := cert.Get("associated_data").String() - block, err := libCrypto.AESDecryptGCM([]byte(p.apikey), []byte(nonce), []byte(data), []byte(aad), nil) + block, err := lib_crypto.AESDecryptGCM([]byte(p.apikey), []byte(nonce), []byte(data), []byte(aad), nil) if err != nil { return nil, err } - key, err := libCrypto.NewPublicKeyFromDerBlock(block) + key, err := lib_crypto.NewPublicKeyFromDerBlock(block) if err != nil { return nil, err } @@ -123,7 +123,7 @@ func (p *PayV3) publicKey(ctx context.Context, serialNO string) (*libCrypto.Publ return nil, v.Err } - return v.Val.(*libCrypto.PublicKey), nil + return v.Val.(*lib_crypto.PublicKey), nil } } @@ -138,9 +138,9 @@ func (p *PayV3) httpCerts(ctx context.Context) (gjson.Result, error) { return lib.Fail(err) } - log.Set(libHttp.HeaderAuthorization, authStr) + log.Set(lib_http.HeaderAuthorization, authStr) - resp, err := p.httpCli.Do(ctx, http.MethodGet, reqURL, nil, libHttp.WithHeader(libHttp.HeaderAccept, "application/json"), libHttp.WithHeader(libHttp.HeaderAuthorization, authStr)) + resp, err := p.httpCli.Do(ctx, http.MethodGet, reqURL, nil, lib_http.WithHeader(lib_http.HeaderAccept, "application/json"), lib_http.WithHeader(lib_http.HeaderAuthorization, authStr)) if err != nil { return lib.Fail(err) } @@ -173,12 +173,12 @@ func (p *PayV3) httpCerts(ctx context.Context) (gjson.Result, error) { data := cert.Get("ciphertext").String() aad := cert.Get("associated_data").String() - block, err := libCrypto.AESDecryptGCM([]byte(p.apikey), []byte(nonce), []byte(data), []byte(aad), nil) + block, err := lib_crypto.AESDecryptGCM([]byte(p.apikey), []byte(nonce), []byte(data), []byte(aad), nil) if err != nil { return lib.Fail(err) } - key, err := libCrypto.NewPublicKeyFromDerBlock(block) + key, err := lib_crypto.NewPublicKeyFromDerBlock(block) if err != nil { return lib.Fail(err) } @@ -233,12 +233,12 @@ func (p *PayV3) do(ctx context.Context, method, path string, query url.Values, p return nil, err } - log.Set(libHttp.HeaderAuthorization, authStr) + log.Set(lib_http.HeaderAuthorization, authStr) resp, err := p.httpCli.Do(ctx, method, reqURL, body, - libHttp.WithHeader(libHttp.HeaderAccept, "application/json"), - libHttp.WithHeader(libHttp.HeaderAuthorization, authStr), - libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentJSON), + lib_http.WithHeader(lib_http.HeaderAccept, "application/json"), + lib_http.WithHeader(lib_http.HeaderAuthorization, authStr), + lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentJSON), ) if err != nil { return nil, err @@ -279,7 +279,7 @@ func (p *PayV3) PostJSON(ctx context.Context, path string, params lib.X) (*APIRe } // Upload 上传资源 -func (p *PayV3) Upload(ctx context.Context, path string, form libHttp.UploadForm) (*APIResult, error) { +func (p *PayV3) Upload(ctx context.Context, path string, form lib_http.UploadForm) (*APIResult, error) { reqURL := p.url(path, nil) log := lib.NewReqLog(http.MethodPost, reqURL) @@ -290,9 +290,9 @@ func (p *PayV3) Upload(ctx context.Context, path string, form libHttp.UploadForm return nil, err } - log.Set(libHttp.HeaderAuthorization, authStr) + log.Set(lib_http.HeaderAuthorization, authStr) - resp, err := p.httpCli.Upload(ctx, reqURL, form, libHttp.WithHeader(libHttp.HeaderAuthorization, authStr)) + resp, err := p.httpCli.Upload(ctx, reqURL, form, lib_http.WithHeader(lib_http.HeaderAuthorization, authStr)) if err != nil { return nil, err } @@ -332,9 +332,9 @@ func (p *PayV3) Download(ctx context.Context, downloadURL string, w io.Writer) e return err } - log.Set(libHttp.HeaderAuthorization, authStr) + log.Set(lib_http.HeaderAuthorization, authStr) - resp, err := p.httpCli.Do(ctx, http.MethodGet, downloadURL, nil, libHttp.WithHeader(libHttp.HeaderAuthorization, authStr)) + resp, err := p.httpCli.Do(ctx, http.MethodGet, downloadURL, nil, lib_http.WithHeader(lib_http.HeaderAuthorization, authStr)) if err != nil { return err } @@ -488,12 +488,12 @@ type PayV3Option func(p *PayV3) // WithPayV3HttpCli 设置支付(v3)请求的 HTTP Client func WithPayV3HttpCli(c *http.Client) PayV3Option { return func(p *PayV3) { - p.httpCli = libHttp.NewHTTPClient(c) + p.httpCli = lib_http.NewHTTPClient(c) } } // WithPayV3PrivateKey 设置支付(v3)商户RSA私钥 -func WithPayV3PrivateKey(serialNO string, key *libCrypto.PrivateKey) PayV3Option { +func WithPayV3PrivateKey(serialNO string, key *lib_crypto.PrivateKey) PayV3Option { return func(p *PayV3) { p.prvSN = serialNO p.prvKey = key @@ -513,7 +513,7 @@ func NewPayV3(mchid, apikey string, options ...PayV3Option) *PayV3 { host: "https://api.mch.weixin.qq.com", mchid: mchid, apikey: apikey, - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options { diff --git a/ysepay/client.go b/ysepay/client.go index cacf588..d97547e 100644 --- a/ysepay/client.go +++ b/ysepay/client.go @@ -16,8 +16,8 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - libCrypto "github.com/shenghui0779/sdk-go/lib/crypto" - libHttp "github.com/shenghui0779/sdk-go/lib/http" + lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" + lib_http "github.com/shenghui0779/sdk-go/lib/http" "github.com/shenghui0779/sdk-go/lib/value" ) @@ -26,9 +26,9 @@ type Client struct { host string mchNO string desKey string - prvKey *libCrypto.PrivateKey - pubKey *libCrypto.PublicKey - httpCli libHttp.HTTPClient + prvKey *lib_crypto.PrivateKey + pubKey *lib_crypto.PublicKey + httpCli lib_http.Client logger func(ctx context.Context, data map[string]string) } @@ -49,7 +49,7 @@ func (c *Client) url(api string) string { // Encrypt 敏感数据DES加密 func (c *Client) Encrypt(plain string) (string, error) { - b, err := libCrypto.DESEncryptECB([]byte(c.desKey), []byte(plain)) + b, err := lib_crypto.DESEncryptECB([]byte(c.desKey), []byte(plain)) if err != nil { return "", err } @@ -59,7 +59,7 @@ func (c *Client) Encrypt(plain string) (string, error) { // MustEncrypt 敏感数据DES加密;若发生错误,则Panic func (c *Client) MustEncrypt(plain string) string { - b, err := libCrypto.DESEncryptECB([]byte(c.desKey), []byte(plain)) + b, err := lib_crypto.DESEncryptECB([]byte(c.desKey), []byte(plain)) if err != nil { panic(err) } @@ -74,7 +74,7 @@ func (c *Client) Decrypt(cipher string) (string, error) { return "", err } - plain, err := libCrypto.DESEncryptECB([]byte(c.desKey), b) + plain, err := lib_crypto.DESEncryptECB([]byte(c.desKey), b) if err != nil { return "", err } @@ -96,7 +96,7 @@ func (c *Client) PostForm(ctx context.Context, api, serviceNO string, bizData va log.SetReqBody(form) - resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, []byte(form), libHttp.WithHeader(libHttp.HeaderContentType, libHttp.ContentForm)) + resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, []byte(form), lib_http.WithHeader(lib_http.HeaderContentType, lib_http.ContentForm)) if err != nil { return lib.Fail(err) } @@ -222,19 +222,19 @@ type Option func(c *Client) // WithHttpCli 设置自定义 HTTP Client func WithHttpCli(cli *http.Client) Option { return func(c *Client) { - c.httpCli = libHttp.NewHTTPClient(cli) + c.httpCli = lib_http.NewHTTPClient(cli) } } // WithPrivateKey 设置商户RSA私钥 -func WithPrivateKey(key *libCrypto.PrivateKey) Option { +func WithPrivateKey(key *lib_crypto.PrivateKey) Option { return func(c *Client) { c.prvKey = key } } // WithPublicKey 设置平台RSA公钥 -func WithPublicKey(key *libCrypto.PublicKey) Option { +func WithPublicKey(key *lib_crypto.PublicKey) Option { return func(c *Client) { c.pubKey = key } @@ -253,7 +253,7 @@ func NewClient(mchNO, desKey string, options ...Option) *Client { host: "https://eqt.ysepay.com", mchNO: mchNO, desKey: desKey, - httpCli: libHttp.NewDefaultClient(), + httpCli: lib_http.NewDefaultClient(), } for _, f := range options {