diff --git a/alipay/action.go b/alipay/action.go index b3ee608..c2fc57e 100644 --- a/alipay/action.go +++ b/alipay/action.go @@ -38,7 +38,7 @@ func (a *Action) Encode(c *Client) (string, error) { v.Set("format", "JSON") v.Set("charset", "utf-8") v.Set("sign_type", "RSA2") - v.Set("timestamp", time.Now().In(lib.GMT8).Format("2006-01-02 15:04:05")) + v.Set("timestamp", time.Now().In(time.Local).Format("2006-01-02 15:04:05")) v.Set("version", "1.0") for key, val := range a.params { diff --git a/alipay/client.go b/alipay/client.go index a7d494d..1d9edf1 100644 --- a/alipay/client.go +++ b/alipay/client.go @@ -14,9 +14,9 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" - "github.com/shenghui0779/sdk-go/lib/curl" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xcrypto" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // Client 支付宝客户端 @@ -24,9 +24,9 @@ type Client struct { gateway string appid string aesKey string - prvKey *lib_crypto.PrivateKey - pubKey *lib_crypto.PublicKey - httpCli curl.Client + prvKey *xcrypto.PrivateKey + pubKey *xcrypto.PublicKey + httpCli xhttp.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), - curl.WithHeader(curl.HeaderAccept, "application/json"), - curl.WithHeader(curl.HeaderContentType, curl.ContentForm), + xhttp.WithHeader(xhttp.HeaderAccept, "application/json"), + xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentForm), ) if err != nil { log.Set("error", err.Error()) @@ -102,7 +102,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 curl.UploadForm, options ...ActionOption) (gjson.Result, error) { +func (c *Client) Upload(ctx context.Context, method string, form xhttp.UploadForm, options ...ActionOption) (gjson.Result, error) { log := lib.NewReqLog(http.MethodPost, c.gateway) defer log.Do(ctx, c.logger) @@ -116,7 +116,7 @@ func (c *Client) Upload(ctx context.Context, method string, form curl.UploadForm log.Set("query", query) - resp, err := c.httpCli.Upload(ctx, c.gateway+"?"+query, form, curl.WithHeader(curl.HeaderAccept, "application/json")) + resp, err := c.httpCli.Upload(ctx, c.gateway+"?"+query, form, xhttp.WithHeader(xhttp.HeaderAccept, "application/json")) if err != nil { log.Set("error", err.Error()) return lib.Fail(err) @@ -217,7 +217,7 @@ func (c *Client) Encrypt(data string) (string, error) { if err != nil { return "", fmt.Errorf("aes_key base64.decode error: %w", err) } - ct, err := lib_crypto.AESEncryptCBC(key, make([]byte, 16), []byte(data)) + ct, err := xcrypto.AESEncryptCBC(key, make([]byte, 16), []byte(data)) if err != nil { return "", err } @@ -234,7 +234,7 @@ func (c *Client) Decrypt(encryptData string) ([]byte, error) { if err != nil { return nil, fmt.Errorf("encrypt_data base64.decode error: %w", err) } - return lib_crypto.AESDecryptCBC(key, make([]byte, 16), data) + return xcrypto.AESDecryptCBC(key, make([]byte, 16), data) } // DecodeEncryptData 解析加密数据,如:授权的用户信息和手机号 @@ -289,19 +289,19 @@ type Option func(c *Client) // WithHttpCli 设置自定义 HTTP Client func WithHttpCli(cli *http.Client) Option { return func(c *Client) { - c.httpCli = curl.NewHTTPClient(cli) + c.httpCli = xhttp.NewHTTPClient(cli) } } // WithPrivateKey 设置商户RSA私钥 -func WithPrivateKey(key *lib_crypto.PrivateKey) Option { +func WithPrivateKey(key *xcrypto.PrivateKey) Option { return func(c *Client) { c.prvKey = key } } // WithPublicKey 设置平台RSA公钥 -func WithPublicKey(key *lib_crypto.PublicKey) Option { +func WithPublicKey(key *xcrypto.PublicKey) Option { return func(c *Client) { c.pubKey = key } @@ -320,7 +320,7 @@ func NewClient(appid, aesKey string, options ...Option) *Client { appid: appid, aesKey: aesKey, gateway: "https://openapi.alipay.com/gateway.do", - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) @@ -334,7 +334,7 @@ func NewSandbox(appid, aesKey string, options ...Option) *Client { appid: appid, aesKey: aesKey, gateway: "https://openapi-sandbox.dl.alipaydev.com/gateway.do", - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) diff --git a/alipay/client_v3.go b/alipay/client_v3.go index 87c8813..aed8ec6 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" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" - "github.com/shenghui0779/sdk-go/lib/curl" + "github.com/shenghui0779/sdk-go/lib/xcrypto" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // ClientV3 支付宝V3客户端(仅支持v3版本的接口可用) @@ -27,9 +27,9 @@ type ClientV3 struct { host string appid string aesKey string - prvKey *lib_crypto.PrivateKey - pubKey *lib_crypto.PublicKey - httpCli curl.Client + prvKey *xcrypto.PrivateKey + pubKey *xcrypto.PublicKey + httpCli xhttp.Client logger func(ctx context.Context, data map[string]string) } @@ -90,7 +90,7 @@ func (c *ClientV3) do(ctx context.Context, method, path string, query url.Values log.Set("error", err.Error()) return nil, err } - header.Set(curl.HeaderAuthorization, authStr) + header.Set(xhttp.HeaderAuthorization, authStr) log.SetReqHeader(header) @@ -141,7 +141,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(curl.HeaderAccept, "application/json") + header.Set(xhttp.HeaderAccept, "application/json") header.Set(HeaderRequestID, uuid.NewString()) for _, fn := range options { fn(header) @@ -154,9 +154,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(curl.HeaderAccept, "application/json") + header.Set(xhttp.HeaderAccept, "application/json") header.Set(HeaderRequestID, uuid.NewString()) - header.Set(curl.HeaderContentType, curl.ContentJSON) + header.Set(xhttp.HeaderContentType, xhttp.ContentJSON) for _, fn := range options { fn(header) } @@ -170,7 +170,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(curl.HeaderContentType, curl.ContentText) + header.Set(xhttp.HeaderContentType, xhttp.ContentText) for _, fn := range options { fn(header) } @@ -179,7 +179,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 curl.UploadForm, options ...V3HeaderOption) (*APIResult, error) { +func (c *ClientV3) Upload(ctx context.Context, path string, form xhttp.UploadForm, options ...V3HeaderOption) (*APIResult, error) { reqID := uuid.NewString() reqURL := c.url(path, nil) @@ -198,7 +198,7 @@ func (c *ClientV3) Upload(ctx context.Context, path string, form curl.UploadForm log.Set("error", err.Error()) return nil, err } - reqHeader.Set(curl.HeaderAuthorization, authStr) + reqHeader.Set(xhttp.HeaderAuthorization, authStr) log.SetReqHeader(reqHeader) @@ -303,7 +303,7 @@ func (c *ClientV3) Encrypt(data string) (string, error) { if err != nil { return "", err } - ct, err := lib_crypto.AESEncryptCBC(key, make([]byte, 16), []byte(data)) + ct, err := xcrypto.AESEncryptCBC(key, make([]byte, 16), []byte(data)) if err != nil { return "", err } @@ -320,7 +320,7 @@ func (c *ClientV3) Decrypt(encryptData string) ([]byte, error) { if err != nil { return nil, err } - return lib_crypto.AESDecryptCBC(key, make([]byte, 16), data) + return xcrypto.AESDecryptCBC(key, make([]byte, 16), data) } // V3Option 自定义设置项 @@ -329,19 +329,19 @@ type V3Option func(c *ClientV3) // WithV3Client 设置自定义 HTTP Client func WithV3Client(cli *http.Client) V3Option { return func(c *ClientV3) { - c.httpCli = curl.NewHTTPClient(cli) + c.httpCli = xhttp.NewHTTPClient(cli) } } // WithV3PrivateKey 设置商户RSA私钥 -func WithV3PrivateKey(key *lib_crypto.PrivateKey) V3Option { +func WithV3PrivateKey(key *xcrypto.PrivateKey) V3Option { return func(c *ClientV3) { c.prvKey = key } } // WithV3PublicKey 设置平台RSA公钥 -func WithV3PublicKey(key *lib_crypto.PublicKey) V3Option { +func WithV3PublicKey(key *xcrypto.PublicKey) V3Option { return func(c *ClientV3) { c.pubKey = key } @@ -360,7 +360,7 @@ func NewClientV3(appid, aesKey string, options ...V3Option) *ClientV3 { host: "https://openapi.alipay.com", appid: appid, aesKey: aesKey, - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) @@ -374,7 +374,7 @@ func NewSandboxV3(appid, aesKey string, options ...V3Option) *ClientV3 { host: "http://openapi.sandbox.dl.alipaydev.com", appid: appid, aesKey: aesKey, - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) diff --git a/alipay/util.go b/alipay/util.go index e6154cd..829335a 100644 --- a/alipay/util.go +++ b/alipay/util.go @@ -3,7 +3,7 @@ package alipay import ( "strings" - "github.com/shenghui0779/sdk-go/lib/crypto" + "github.com/shenghui0779/sdk-go/lib/xcrypto" "github.com/tidwall/gjson" ) @@ -35,7 +35,7 @@ type APIResult struct { } // FormatPKCS1PrivateKey 格式化支付宝应用私钥(PKCS#1) -func FormatPKCS1PrivateKey(pemStr string) (crypto.RSAPadding, []byte) { +func FormatPKCS1PrivateKey(pemStr string) (xcrypto.RSAPadding, []byte) { rawLen := 64 keyLen := len(pemStr) @@ -65,11 +65,11 @@ func FormatPKCS1PrivateKey(pemStr string) (crypto.RSAPadding, []byte) { } builder.WriteString("-----END RSA PRIVATE KEY-----\n") - return crypto.RSA_PKCS1, []byte(builder.String()) + return xcrypto.RSA_PKCS1, []byte(builder.String()) } // FormatPKCS8PrivateKey 格式化支付宝应用私钥(PKCS#8) -func FormatPKCS8PrivateKey(pemStr string) (crypto.RSAPadding, []byte) { +func FormatPKCS8PrivateKey(pemStr string) (xcrypto.RSAPadding, []byte) { rawLen := 64 keyLen := len(pemStr) @@ -99,11 +99,11 @@ func FormatPKCS8PrivateKey(pemStr string) (crypto.RSAPadding, []byte) { } builder.WriteString("-----END PRIVATE KEY-----\n") - return crypto.RSA_PKCS8, []byte(builder.String()) + return xcrypto.RSA_PKCS8, []byte(builder.String()) } // FormatPKCS1PublicKey 格式化支付宝应用公钥(PKCS#1) -func FormatPKCS1PublicKey(pemStr string) (crypto.RSAPadding, []byte) { +func FormatPKCS1PublicKey(pemStr string) (xcrypto.RSAPadding, []byte) { rawLen := 64 keyLen := len(pemStr) @@ -133,11 +133,11 @@ func FormatPKCS1PublicKey(pemStr string) (crypto.RSAPadding, []byte) { } builder.WriteString("-----END RSA PUBLIC KEY-----\n") - return crypto.RSA_PKCS1, []byte(builder.String()) + return xcrypto.RSA_PKCS1, []byte(builder.String()) } // FormatPKCS8PublicKey 格式化支付宝应用公钥(PKCS#8) -func FormatPKCS8PublicKey(pemStr string) (crypto.RSAPadding, []byte) { +func FormatPKCS8PublicKey(pemStr string) (xcrypto.RSAPadding, []byte) { rawLen := 64 keyLen := len(pemStr) @@ -166,5 +166,5 @@ func FormatPKCS8PublicKey(pemStr string) (crypto.RSAPadding, []byte) { } builder.WriteString("-----END PUBLIC KEY-----\n") - return crypto.RSA_PKCS8, []byte(builder.String()) + return xcrypto.RSA_PKCS8, []byte(builder.String()) } diff --git a/antchain/client.go b/antchain/client.go index bfab822..647a452 100644 --- a/antchain/client.go +++ b/antchain/client.go @@ -15,18 +15,18 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" - "github.com/shenghui0779/sdk-go/lib/curl" + "github.com/shenghui0779/sdk-go/lib/xcrypto" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // Config 客户端配置 type Config struct { - 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"` // 托管标识 + BizID string `json:"biz_id"` // 链ID (a00e36c5) + TenantID string `json:"tenant_id"` // 租户ID + AccessID string `json:"access_id"` // AccessID + AccessKey *xcrypto.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 curl.Client + httpCli xhttp.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, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, body, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { log.Set("error", err.Error()) 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 = curl.NewHTTPClient(httpCli) + c.httpCli = xhttp.NewHTTPClient(httpCli) } } @@ -200,7 +200,7 @@ func NewClient(cfg *Config, options ...Option) Client { c := &client{ endpoint: "https://rest.baas.alipay.com", config: cfg, - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) diff --git a/esign/client.go b/esign/client.go index b2a8b05..6ae2bce 100644 --- a/esign/client.go +++ b/esign/client.go @@ -21,7 +21,7 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - "github.com/shenghui0779/sdk-go/lib/curl" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // Client E签宝客户端 @@ -29,7 +29,7 @@ type Client struct { host string appid string secret string - httpCli curl.Client + httpCli xhttp.Client logger func(ctx context.Context, data map[string]string) } @@ -57,7 +57,7 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values, header := http.Header{} - header.Set(curl.HeaderAccept, AcceptAll) + header.Set(xhttp.HeaderAccept, AcceptAll) header.Set(HeaderTSignOpenAppID, c.appid) header.Set(HeaderTSignOpenAuthMode, AuthModeSign) header.Set(HeaderTSignOpenCaTimestamp, strconv.FormatInt(time.Now().UnixMilli(), 10)) @@ -82,7 +82,7 @@ func (c *Client) do(ctx context.Context, method, path string, query url.Values, contentMD5 := ContentMD5(body) - header.Set(curl.HeaderContentType, "application/json; charset=UTF-8") + header.Set(xhttp.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(curl.HeaderContentType, curl.ContentStream) + header.Set(xhttp.HeaderContentType, xhttp.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 = curl.NewHTTPClient(cli) + c.httpCli = xhttp.NewHTTPClient(cli) } } @@ -244,7 +244,7 @@ func NewClient(appid, secret string, options ...Option) *Client { host: "https://openapi.esign.cn", appid: appid, secret: secret, - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) @@ -258,7 +258,7 @@ func NewSandbox(appid, secret string, options ...Option) *Client { host: "https://smlopenapi.esign.cn", appid: appid, secret: secret, - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) diff --git a/go.mod b/go.mod index 88aa9b6..b7eeb3d 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,9 @@ go 1.20 require ( github.com/google/uuid v1.6.0 - github.com/robfig/cron/v3 v3.0.1 github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.17.1 - golang.org/x/crypto v0.23.0 + golang.org/x/crypto v0.24.0 ) require ( diff --git a/go.sum b/go.sum index 9abcfd7..beefee5 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= -github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= @@ -26,8 +24,8 @@ github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= -golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= -golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= +golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/lib/logger.go b/lib/logger.go index 25bffb6..1f5cc01 100644 --- a/lib/logger.go +++ b/lib/logger.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "github.com/shenghui0779/sdk-go/lib/curl" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // ReqLog 请求日志 @@ -81,10 +81,10 @@ func HeaderEncode(h http.Header) string { return buf.String() } -func HeaderToHttpOption(h http.Header) []curl.Option { - options := make([]curl.Option, 0, len(h)) +func HeaderToHttpOption(h http.Header) []xhttp.Option { + options := make([]xhttp.Option, 0, len(h)) for k, vals := range h { - options = append(options, curl.WithHeader(k, vals...)) + options = append(options, xhttp.WithHeader(k, vals...)) } return options diff --git a/lib/crypto/aes.go b/lib/xcrypto/aes.go similarity index 99% rename from lib/crypto/aes.go rename to lib/xcrypto/aes.go index 63cdfc9..1cba71b 100644 --- a/lib/crypto/aes.go +++ b/lib/xcrypto/aes.go @@ -1,4 +1,4 @@ -package crypto +package xcrypto import ( "crypto/aes" diff --git a/lib/crypto/aes_test.go b/lib/xcrypto/aes_test.go similarity index 99% rename from lib/crypto/aes_test.go rename to lib/xcrypto/aes_test.go index 285d44b..b047be1 100644 --- a/lib/crypto/aes_test.go +++ b/lib/xcrypto/aes_test.go @@ -1,4 +1,4 @@ -package crypto +package xcrypto import ( "encoding/base64" diff --git a/lib/crypto/des.go b/lib/xcrypto/des.go similarity index 98% rename from lib/crypto/des.go rename to lib/xcrypto/des.go index ffe297e..2ce0ebe 100644 --- a/lib/crypto/des.go +++ b/lib/xcrypto/des.go @@ -1,4 +1,4 @@ -package crypto +package xcrypto import ( "crypto/des" diff --git a/lib/crypto/padding.go b/lib/xcrypto/padding.go similarity index 99% rename from lib/crypto/padding.go rename to lib/xcrypto/padding.go index bde0179..423cec3 100644 --- a/lib/crypto/padding.go +++ b/lib/xcrypto/padding.go @@ -1,4 +1,4 @@ -package crypto +package xcrypto import ( "bytes" diff --git a/lib/crypto/rsa.go b/lib/xcrypto/rsa.go similarity index 99% rename from lib/crypto/rsa.go rename to lib/xcrypto/rsa.go index c236a4a..0b1ee57 100644 --- a/lib/crypto/rsa.go +++ b/lib/xcrypto/rsa.go @@ -1,4 +1,4 @@ -package crypto +package xcrypto import ( "crypto" diff --git a/lib/crypto/rsa_test.go b/lib/xcrypto/rsa_test.go similarity index 99% rename from lib/crypto/rsa_test.go rename to lib/xcrypto/rsa_test.go index d4d2588..5c49f47 100644 --- a/lib/crypto/rsa_test.go +++ b/lib/xcrypto/rsa_test.go @@ -1,4 +1,4 @@ -package crypto +package xcrypto import ( "crypto" diff --git a/lib/hash/hash.go b/lib/xhash/hash.go similarity index 98% rename from lib/hash/hash.go rename to lib/xhash/hash.go index 8807d6a..cf4e2c1 100644 --- a/lib/hash/hash.go +++ b/lib/xhash/hash.go @@ -1,4 +1,4 @@ -package hash +package xhash import ( "crypto" diff --git a/lib/hash/hash_test.go b/lib/xhash/hash_test.go similarity index 99% rename from lib/hash/hash_test.go rename to lib/xhash/hash_test.go index 043d189..9d5eb6f 100644 --- a/lib/hash/hash_test.go +++ b/lib/xhash/hash_test.go @@ -1,4 +1,4 @@ -package hash +package xhash import ( "crypto" diff --git a/lib/hash/hmac.go b/lib/xhash/hmac.go similarity index 98% rename from lib/hash/hmac.go rename to lib/xhash/hmac.go index c222ab4..8521eb7 100644 --- a/lib/hash/hmac.go +++ b/lib/xhash/hmac.go @@ -1,4 +1,4 @@ -package hash +package xhash import ( "crypto" diff --git a/lib/hash/hmac_test.go b/lib/xhash/hmac_test.go similarity index 99% rename from lib/hash/hmac_test.go rename to lib/xhash/hmac_test.go index 6d807f0..9423252 100644 --- a/lib/hash/hmac_test.go +++ b/lib/xhash/hmac_test.go @@ -1,4 +1,4 @@ -package hash +package xhash import ( "crypto" diff --git a/lib/curl/client.go b/lib/xhttp/client.go similarity index 99% rename from lib/curl/client.go rename to lib/xhttp/client.go index 0cb1a08..e3348cf 100644 --- a/lib/curl/client.go +++ b/lib/xhttp/client.go @@ -1,4 +1,4 @@ -package curl +package xhttp import ( "bytes" diff --git a/lib/curl/option.go b/lib/xhttp/option.go similarity index 99% rename from lib/curl/option.go rename to lib/xhttp/option.go index 675475d..1fb177a 100644 --- a/lib/curl/option.go +++ b/lib/xhttp/option.go @@ -1,4 +1,4 @@ -package curl +package xhttp import ( "errors" diff --git a/lib/curl/util.go b/lib/xhttp/util.go similarity index 99% rename from lib/curl/util.go rename to lib/xhttp/util.go index 585de27..ccfb629 100644 --- a/lib/curl/util.go +++ b/lib/xhttp/util.go @@ -1,4 +1,4 @@ -package curl +package xhttp import ( "context" diff --git a/sandpay/client.go b/sandpay/client.go index a272303..4fed531 100755 --- a/sandpay/client.go +++ b/sandpay/client.go @@ -13,16 +13,16 @@ import ( "strings" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" - "github.com/shenghui0779/sdk-go/lib/curl" + "github.com/shenghui0779/sdk-go/lib/xcrypto" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // Client 杉德支付客户端 type Client struct { mchID string - prvKey *lib_crypto.PrivateKey - pubKey *lib_crypto.PublicKey - httpCli curl.Client + prvKey *xcrypto.PrivateKey + pubKey *xcrypto.PublicKey + httpCli xhttp.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), curl.WithHeader(curl.HeaderContentType, curl.ContentForm)) + resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, []byte(body), xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentForm)) if err != nil { log.Set("error", err.Error()) return nil, err @@ -104,19 +104,19 @@ type Option func(c *Client) // WithHttpCli 设置自定义 HTTP Client func WithHttpCli(cli *http.Client) Option { return func(c *Client) { - c.httpCli = curl.NewHTTPClient(cli) + c.httpCli = xhttp.NewHTTPClient(cli) } } // WithPrivateKey 设置商户RSA私钥 -func WithPrivateKey(key *lib_crypto.PrivateKey) Option { +func WithPrivateKey(key *xcrypto.PrivateKey) Option { return func(c *Client) { c.prvKey = key } } // WithPublicKey 设置平台RSA公钥 -func WithPublicKey(key *lib_crypto.PublicKey) Option { +func WithPublicKey(key *xcrypto.PublicKey) Option { return func(c *Client) { c.pubKey = key } @@ -133,7 +133,7 @@ func WithLogger(fn func(ctx context.Context, data map[string]string)) Option { func NewClient(mchID string, options ...Option) *Client { c := &Client{ mchID: mchID, - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) diff --git a/sandpay/util.go b/sandpay/util.go index bebc17c..a733e66 100644 --- a/sandpay/util.go +++ b/sandpay/util.go @@ -8,8 +8,8 @@ import ( "time" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xcrypto" ) const OK = "000000" @@ -21,7 +21,7 @@ type Form struct { } // URLEncode 数据表单格式化为POST表单 -func (f *Form) URLEncode(mid string, key *lib_crypto.PrivateKey) (string, error) { +func (f *Form) URLEncode(mid string, key *xcrypto.PrivateKey) (string, error) { if key == nil { return "", errors.New("private key is nil (forgotten configure?)") } @@ -88,7 +88,7 @@ func NewReqForm(method, productID string, body value.V, options ...HeadOption) * "productId": productID, "accessType": "1", "channelType": "07", - "reqTime": time.Now().In(lib.GMT8).Format("20060102150405"), + "reqTime": time.Now().In(time.Local).Format("20060102150405"), }, Body: body, } diff --git a/wechat/corp.go b/wechat/corp.go index 0e77f03..e723138 100644 --- a/wechat/corp.go +++ b/wechat/corp.go @@ -13,8 +13,8 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - "github.com/shenghui0779/sdk-go/lib/curl" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // Corp 企业微信(企业内部开发) @@ -23,7 +23,7 @@ type Corp struct { corpid string secret string srvCfg *ServerConfig - httpCli curl.Client + httpCli xhttp.Client logger func(ctx context.Context, data map[string]string) } @@ -53,7 +53,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 ...curl.Option) ([]byte, error) { +func (c *Corp) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...xhttp.Option) ([]byte, error) { reqURL := c.url(path, query) log := lib.NewReqLog(method, reqURL) @@ -155,7 +155,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, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + b, err := c.do(ctx, http.MethodPost, path, query, params, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { return lib.Fail(err) } @@ -191,7 +191,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, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + b, err := c.do(ctx, http.MethodPost, path, query, params, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { return nil, err } @@ -204,7 +204,7 @@ func (c *Corp) PostBuffer(ctx context.Context, accessToken, path string, params } // Upload 上传媒体资源 -func (c *Corp) Upload(ctx context.Context, accessToken, path string, form curl.UploadForm) (gjson.Result, error) { +func (c *Corp) Upload(ctx context.Context, accessToken, path string, form xhttp.UploadForm) (gjson.Result, error) { query := url.Values{} query.Set(AccessToken, accessToken) @@ -264,7 +264,7 @@ func (c *Corp) DecodeEventMsg(signature, timestamp, nonce, encryptMsg string) (v if err != nil { return nil, err } - return ParseXMLToV(b) + return XMLToValue(b) } // ReplyEventMsg 事件消息回复 @@ -287,7 +287,7 @@ func WithCorpSrvCfg(token, aeskey string) CorpOption { // WithCorpHttpCli 设置企业微信请求的 HTTP Client func WithCorpHttpCli(cli *http.Client) CorpOption { return func(c *Corp) { - c.httpCli = curl.NewHTTPClient(cli) + c.httpCli = xhttp.NewHTTPClient(cli) } } @@ -305,7 +305,7 @@ func NewCorp(corpid, secret string, options ...CorpOption) *Corp { corpid: corpid, secret: secret, srvCfg: new(ServerConfig), - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c) diff --git a/wechat/event.go b/wechat/event.go index c98c2ac..e4a8640 100644 --- a/wechat/event.go +++ b/wechat/event.go @@ -11,8 +11,8 @@ import ( "time" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xcrypto" ) // SignWithSHA1 事件消息sha1签名 @@ -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) (*lib_crypto.CipherText, error) { +func EventEncrypt(receiveID, encodingAESKey, nonce string, plainText []byte) (*xcrypto.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 lib_crypto.AESEncryptCBC(key, key[:aes.BlockSize], encryptData) + return xcrypto.AESEncryptCBC(key, key[:aes.BlockSize], encryptData) } // EventDecrypt 事件消息解密 @@ -63,7 +63,7 @@ func EventDecrypt(receiveID, encodingAESKey, cipherText string) ([]byte, error) return nil, err } - plainText, err := lib_crypto.AESDecryptCBC(key, key[:aes.BlockSize], decryptData) + plainText, err := xcrypto.AESDecryptCBC(key, key[:aes.BlockSize], decryptData) if err != nil { return nil, err } @@ -78,7 +78,7 @@ func EventDecrypt(receiveID, encodingAESKey, cipherText string) ([]byte, error) } func EventReply(receiveID, token, encodingAESKey string, msg value.V) (value.V, error) { - str, err := FormatVToXML(msg) + str, err := ValueToXML(msg) if err != nil { return nil, err } diff --git a/wechat/miniprogram.go b/wechat/miniprogram.go index 8515950..dc718d8 100644 --- a/wechat/miniprogram.go +++ b/wechat/miniprogram.go @@ -19,18 +19,18 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" - "github.com/shenghui0779/sdk-go/lib/curl" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xcrypto" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // SafeMode 安全鉴权模式配置 type SafeMode struct { aesSN string aeskey string - prvKey *lib_crypto.PrivateKey + prvKey *xcrypto.PrivateKey pubSN string - pubKey *lib_crypto.PublicKey + pubKey *xcrypto.PublicKey } // MiniProgram 小程序 @@ -41,7 +41,7 @@ type MiniProgram struct { srvCfg *ServerConfig sfMode *SafeMode token atomic.Value - httpCli curl.Client + httpCli xhttp.Client logger func(ctx context.Context, data map[string]string) } @@ -71,7 +71,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 ...curl.Option) ([]byte, error) { +func (mp *MiniProgram) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...xhttp.Option) ([]byte, error) { reqURL := mp.url(path, query) log := lib.NewReqLog(method, reqURL) @@ -146,7 +146,7 @@ func (mp *MiniProgram) doSafe(ctx context.Context, method, path string, query ur reqHeader := http.Header{} - reqHeader.Set(curl.HeaderContentType, curl.ContentJSON) + reqHeader.Set(xhttp.HeaderContentType, xhttp.ContentJSON) reqHeader.Set(HeaderMPAppID, mp.appid) reqHeader.Set(HeaderMPTimestamp, strconv.FormatInt(now, 10)) reqHeader.Set(HeaderMPSignature, sign) @@ -228,7 +228,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 := lib_crypto.AESEncryptGCM(key, iv, data, []byte(aad), nil) + ct, err := xcrypto.AESEncryptGCM(key, iv, data, []byte(aad), nil) if err != nil { log.Set("error", err.Error()) return nil, err @@ -330,7 +330,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 lib_crypto.AESDecryptGCM(key, iv, append(data, tag...), []byte(aad), nil) + return xcrypto.AESDecryptGCM(key, iv, append(data, tag...), []byte(aad), nil) } // Code2Session 通过临时登录凭证code完成登录流程 @@ -365,7 +365,7 @@ func (mp *MiniProgram) reloadAccessToken() error { "force_refresh": false, } - b, err := mp.do(context.Background(), http.MethodPost, "/cgi-bin/stable_token", nil, params, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + b, err := mp.do(context.Background(), http.MethodPost, "/cgi-bin/stable_token", nil, params, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { return err } @@ -460,7 +460,7 @@ func (mp *MiniProgram) PostJSON(ctx context.Context, path string, params lib.X) query := url.Values{} query.Set(AccessToken, accessToken) - b, err := mp.do(ctx, http.MethodPost, path, query, params, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + b, err := mp.do(ctx, http.MethodPost, path, query, params, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { return lib.Fail(err) } @@ -481,7 +481,7 @@ func (mp *MiniProgram) PostBuffer(ctx context.Context, path string, params lib.X query := url.Values{} query.Set(AccessToken, accessToken) - b, err := mp.do(ctx, http.MethodPost, path, query, params, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + b, err := mp.do(ctx, http.MethodPost, path, query, params, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { return nil, err } @@ -540,7 +540,7 @@ func (mp *MiniProgram) SafePostBuffer(ctx context.Context, path string, params l } // Upload 上传媒体资源 -func (mp *MiniProgram) Upload(ctx context.Context, path string, form curl.UploadForm) (gjson.Result, error) { +func (mp *MiniProgram) Upload(ctx context.Context, path string, form xhttp.UploadForm) (gjson.Result, error) { accessToken, err := mp.getAccessToken() if err != nil { return lib.Fail(err) @@ -608,7 +608,7 @@ func (mp *MiniProgram) DecodeEncryptData(sessionKey, iv, encryptData string) ([] return nil, fmt.Errorf("encrypt_data base64.decode error: %w", err) } - ct, err := lib_crypto.AESEncryptCBC(keyBlock, ivBlock, data) + ct, err := xcrypto.AESEncryptCBC(keyBlock, ivBlock, data) if err != nil { return nil, err } @@ -627,7 +627,7 @@ func (mp *MiniProgram) DecodeEventMsg(signature, timestamp, nonce, encryptMsg st if err != nil { return nil, err } - return ParseXMLToV(b) + return XMLToValue(b) } // ReplyEventMsg 事件消息回复 @@ -650,7 +650,7 @@ func WithMPSrvCfg(token, aeskey string) MPOption { // WithMPHttpCli 设置小程序请求的 HTTP Client func WithMPHttpCli(c *http.Client) MPOption { return func(mp *MiniProgram) { - mp.httpCli = curl.NewHTTPClient(c) + mp.httpCli = xhttp.NewHTTPClient(c) } } @@ -670,14 +670,14 @@ func WithMPAesKey(serialNO, key string) MPOption { } // WithMPPrivateKey 设置小程序RSA私钥 -func WithMPPrivateKey(key *lib_crypto.PrivateKey) MPOption { +func WithMPPrivateKey(key *xcrypto.PrivateKey) MPOption { return func(mp *MiniProgram) { mp.sfMode.prvKey = key } } // WithMPPublicKey 设置小程序平台RSA公钥 -func WithMPPublicKey(serialNO string, key *lib_crypto.PublicKey) MPOption { +func WithMPPublicKey(serialNO string, key *xcrypto.PublicKey) MPOption { return func(mp *MiniProgram) { mp.sfMode.pubSN = serialNO mp.sfMode.pubKey = key @@ -691,7 +691,7 @@ func NewMiniProgram(appid, secret string, options ...MPOption) *MiniProgram { appid: appid, secret: secret, srvCfg: new(ServerConfig), - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(mp) diff --git a/wechat/official_account.go b/wechat/official_account.go index 0c1d9f9..fb56360 100644 --- a/wechat/official_account.go +++ b/wechat/official_account.go @@ -15,8 +15,8 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - "github.com/shenghui0779/sdk-go/lib/curl" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // ServerConfig 服务器配置 @@ -32,7 +32,7 @@ type OfficialAccount struct { secret string srvCfg *ServerConfig token atomic.Value - httpCli curl.Client + httpCli xhttp.Client logger func(ctx context.Context, data map[string]string) } @@ -63,7 +63,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 ...curl.Option) ([]byte, error) { +func (oa *OfficialAccount) do(ctx context.Context, method, path string, query url.Values, params lib.X, options ...xhttp.Option) ([]byte, error) { reqURL := oa.url(path, query) log := lib.NewReqLog(method, reqURL) @@ -187,7 +187,7 @@ func (oa *OfficialAccount) reloadAccessToken() error { "force_refresh": false, } - b, err := oa.do(context.Background(), http.MethodPost, "/cgi-bin/stable_token", nil, params, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + b, err := oa.do(context.Background(), http.MethodPost, "/cgi-bin/stable_token", nil, params, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { return err } @@ -259,7 +259,7 @@ func (oa *OfficialAccount) PostJSON(ctx context.Context, path string, params lib query := url.Values{} query.Set(AccessToken, accessToken) - b, err := oa.do(ctx, http.MethodPost, path, query, params, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + b, err := oa.do(ctx, http.MethodPost, path, query, params, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { return lib.Fail(err) } @@ -303,7 +303,7 @@ func (oa *OfficialAccount) PostBuffer(ctx context.Context, path string, params l query := url.Values{} query.Set(AccessToken, accessToken) - b, err := oa.do(ctx, http.MethodPost, path, query, params, curl.WithHeader(curl.HeaderContentType, curl.ContentJSON)) + b, err := oa.do(ctx, http.MethodPost, path, query, params, xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON)) if err != nil { return nil, err } @@ -316,7 +316,7 @@ func (oa *OfficialAccount) PostBuffer(ctx context.Context, path string, params l } // Upload 上传媒体资源 -func (oa *OfficialAccount) Upload(ctx context.Context, path string, form curl.UploadForm) (gjson.Result, error) { +func (oa *OfficialAccount) Upload(ctx context.Context, path string, form xhttp.UploadForm) (gjson.Result, error) { accessToken, err := oa.getAccessToken() if err != nil { return lib.Fail(err) @@ -377,7 +377,7 @@ func (oa *OfficialAccount) DecodeEventMsg(signature, timestamp, nonce, encryptMs if err != nil { return nil, err } - return ParseXMLToV(b) + return XMLToValue(b) } // ReplyEventMsg 事件消息回复 @@ -400,7 +400,7 @@ func WithOASrvCfg(token, aeskey string) OAOption { // WithOAHttpCli 设置公众号请求的 HTTP Client func WithOAHttpCli(c *http.Client) OAOption { return func(oa *OfficialAccount) { - oa.httpCli = curl.NewHTTPClient(c) + oa.httpCli = xhttp.NewHTTPClient(c) } } @@ -418,7 +418,7 @@ func NewOfficialAccount(appid, secret string, options ...OAOption) *OfficialAcco appid: appid, secret: secret, srvCfg: new(ServerConfig), - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(oa) diff --git a/wechat/pay.go b/wechat/pay.go index d17f73d..ab6d85b 100644 --- a/wechat/pay.go +++ b/wechat/pay.go @@ -13,10 +13,10 @@ import ( "time" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" - "github.com/shenghui0779/sdk-go/lib/curl" - "github.com/shenghui0779/sdk-go/lib/hash" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xcrypto" + "github.com/shenghui0779/sdk-go/lib/xhash" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // Pay 微信支付 @@ -24,8 +24,8 @@ type Pay struct { host string mchid string apikey string - httpCli curl.Client - tlsCli curl.Client + httpCli xhttp.Client + tlsCli xhttp.Client logger func(ctx context.Context, data map[string]string) } @@ -63,7 +63,7 @@ func (p *Pay) do(ctx context.Context, path string, params value.V) ([]byte, erro params.Set("sign", p.Sign(params)) - body, err := FormatVToXML(params) + body, err := ValueToXML(params) if err != nil { log.Set("error", err.Error()) return nil, err @@ -101,7 +101,7 @@ func (p *Pay) doTLS(ctx context.Context, path string, params value.V) ([]byte, e params.Set("sign", p.Sign(params)) - body, err := FormatVToXML(params) + body, err := ValueToXML(params) if err != nil { log.Set("error", err.Error()) return nil, err @@ -138,7 +138,7 @@ func (p *Pay) PostXML(ctx context.Context, path string, params value.V) (value.V return nil, err } - ret, err := ParseXMLToV(b) + ret, err := XMLToValue(b) if err != nil { return nil, err } @@ -158,7 +158,7 @@ func (p *Pay) PostTLSXML(ctx context.Context, path string, params value.V) (valu return nil, err } - ret, err := ParseXMLToV(b) + ret, err := XMLToValue(b) if err != nil { return nil, err } @@ -178,7 +178,7 @@ func (p *Pay) PostBuffer(ctx context.Context, path string, params value.V) ([]by return nil, err } - ret, err := ParseXMLToV(b) + ret, err := XMLToValue(b) if err != nil { return nil, err } @@ -196,7 +196,7 @@ func (p *Pay) PostTLSBuffer(ctx context.Context, path string, params value.V) ([ return nil, err } - ret, err := ParseXMLToV(b) + ret, err := XMLToValue(b) if err != nil { return nil, err } @@ -214,9 +214,9 @@ func (p *Pay) Sign(v value.V) string { signType = v.Get("signType") } if len(signType) != 0 && SignAlgo(strings.ToUpper(signType)) == SignHMacSHA256 { - return strings.ToUpper(hash.HMacSHA256(p.apikey, signStr)) + return strings.ToUpper(xhash.HMacSHA256(p.apikey, signStr)) } - return strings.ToUpper(hash.MD5(signStr)) + return strings.ToUpper(xhash.MD5(signStr)) } func (p *Pay) Verify(v value.V) error { @@ -228,13 +228,13 @@ func (p *Pay) Verify(v value.V) error { signStr := v.Encode("=", "&", value.WithIgnoreKeys("sign"), value.WithEmptyMode(value.EmptyIgnore)) + "&key=" + p.apikey // hmac-sha256 if len(signType) != 0 && SignAlgo(strings.ToUpper(signType)) == SignHMacSHA256 { - if sign := strings.ToUpper(hash.HMacSHA256(p.apikey, signStr)); sign != wxsign { + if sign := strings.ToUpper(xhash.HMacSHA256(p.apikey, signStr)); sign != wxsign { return fmt.Errorf("sign verify failed, expect = %s, actual = %s", sign, wxsign) } return nil } // md5 - if sign := strings.ToUpper(hash.MD5(signStr)); sign != wxsign { + if sign := strings.ToUpper(xhash.MD5(signStr)); sign != wxsign { return fmt.Errorf("sign verify failed, expect = %s, actual = %s", sign, wxsign) } return nil @@ -246,11 +246,11 @@ func (p *Pay) DecryptRefund(encrypt string) (value.V, error) { if err != nil { return nil, err } - plainText, err := lib_crypto.AESDecryptECB([]byte(hash.MD5(p.apikey)), cipherText) + plainText, err := xcrypto.AESDecryptECB([]byte(xhash.MD5(p.apikey)), cipherText) if err != nil { return nil, err } - return ParseXMLToV(plainText) + return XMLToValue(plainText) } // APPAPI 用于APP拉起支付 @@ -296,7 +296,7 @@ func (p *Pay) MinipRedpackJSAPI(appid, pkg string) value.V { signStr := fmt.Sprintf("appId=%s&nonceStr=%s&package=%s&timeStamp=%s&key=%s", appid, v.Get("nonceStr"), v.Get("package"), v.Get("timeStamp"), p.apikey) - v.Set("paySign", hash.MD5(signStr)) + v.Set("paySign", xhash.MD5(signStr)) return v } @@ -307,21 +307,21 @@ type PayOption func(p *Pay) // WithPayTLSCert 设置支付TLS证书 func WithPayTLSCert(cert tls.Certificate) PayOption { return func(p *Pay) { - p.tlsCli = curl.NewDefaultClient(cert) + p.tlsCli = xhttp.NewDefaultClient(cert) } } // WithPayHttpCli 设置支付无证书 HTTP Client func WithPayHttpCli(c *http.Client) PayOption { return func(p *Pay) { - p.httpCli = curl.NewHTTPClient(c) + p.httpCli = xhttp.NewHTTPClient(c) } } // WithPayTLSCli 设置支付带证书 HTTP Client func WithPayTLSCli(c *http.Client) PayOption { return func(p *Pay) { - p.tlsCli = curl.NewHTTPClient(c) + p.tlsCli = xhttp.NewHTTPClient(c) } } @@ -338,8 +338,8 @@ func NewPay(mchid, apikey string, options ...PayOption) *Pay { host: "https://api.mch.weixin.qq.com", mchid: mchid, apikey: apikey, - httpCli: curl.NewDefaultClient(), - tlsCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), + tlsCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(pay) diff --git a/wechat/pay_v3.go b/wechat/pay_v3.go index b954134..2e5bd93 100644 --- a/wechat/pay_v3.go +++ b/wechat/pay_v3.go @@ -18,9 +18,9 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" - "github.com/shenghui0779/sdk-go/lib/curl" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xcrypto" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // PayV3 微信支付V3 @@ -29,9 +29,9 @@ type PayV3 struct { mchid string apikey string prvSN string - prvKey *lib_crypto.PrivateKey - pubKey atomic.Value // map[string]*lib_crypto.PublicKey - httpCli curl.Client + prvKey *xcrypto.PrivateKey + pubKey atomic.Value // map[string]*xcrypto.PublicKey + httpCli xhttp.Client logger func(ctx context.Context, data map[string]string) } @@ -61,12 +61,12 @@ func (p *PayV3) url(path string, query url.Values) string { return builder.String() } -func (p *PayV3) publicKey(serialNO string) (*lib_crypto.PublicKey, error) { +func (p *PayV3) publicKey(serialNO string) (*xcrypto.PublicKey, error) { v := p.pubKey.Load() if v == nil { return nil, errors.New("public key is empty (forgotten auto load?)") } - keyMap, ok := v.(map[string]*lib_crypto.PublicKey) + keyMap, ok := v.(map[string]*xcrypto.PublicKey) if !ok { return nil, errors.New("public key is not map[string]*PublicKey") } @@ -91,9 +91,9 @@ func (p *PayV3) reloadCerts() error { return err } - log.Set(curl.HeaderAuthorization, authStr) + log.Set(xhttp.HeaderAuthorization, authStr) - resp, err := p.httpCli.Do(ctx, http.MethodGet, reqURL, nil, curl.WithHeader(curl.HeaderAccept, "application/json"), curl.WithHeader(curl.HeaderAuthorization, authStr)) + resp, err := p.httpCli.Do(ctx, http.MethodGet, reqURL, nil, xhttp.WithHeader(xhttp.HeaderAccept, "application/json"), xhttp.WithHeader(xhttp.HeaderAuthorization, authStr)) if err != nil { log.Set("error", err.Error()) return err @@ -116,7 +116,7 @@ func (p *PayV3) reloadCerts() error { return errors.New(text) } - keyMap := make(map[string]*lib_crypto.PublicKey) + keyMap := make(map[string]*xcrypto.PublicKey) headSerial := resp.Header.Get(HeaderPaySerial) ret := gjson.GetBytes(b, "data") @@ -128,12 +128,12 @@ func (p *PayV3) reloadCerts() error { data := cert.Get("ciphertext").String() aad := cert.Get("associated_data").String() - block, err := lib_crypto.AESDecryptGCM([]byte(p.apikey), []byte(nonce), []byte(data), []byte(aad), nil) + block, err := xcrypto.AESDecryptGCM([]byte(p.apikey), []byte(nonce), []byte(data), []byte(aad), nil) if err != nil { log.Set("error", err.Error()) return err } - key, err := lib_crypto.NewPublicKeyFromDerBlock(block) + key, err := xcrypto.NewPublicKeyFromDerBlock(block) if err != nil { log.Set("error", err.Error()) return err @@ -189,12 +189,12 @@ func (p *PayV3) do(ctx context.Context, method, path string, query url.Values, p return nil, err } - log.Set(curl.HeaderAuthorization, authStr) + log.Set(xhttp.HeaderAuthorization, authStr) resp, err := p.httpCli.Do(ctx, method, reqURL, body, - curl.WithHeader(curl.HeaderAccept, "application/json"), - curl.WithHeader(curl.HeaderAuthorization, authStr), - curl.WithHeader(curl.HeaderContentType, curl.ContentJSON), + xhttp.WithHeader(xhttp.HeaderAccept, "application/json"), + xhttp.WithHeader(xhttp.HeaderAuthorization, authStr), + xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentJSON), ) if err != nil { log.Set("error", err.Error()) @@ -251,7 +251,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 curl.UploadForm) (*APIResult, error) { +func (p *PayV3) Upload(ctx context.Context, path string, form xhttp.UploadForm) (*APIResult, error) { reqURL := p.url(path, nil) log := lib.NewReqLog(http.MethodPost, reqURL) @@ -263,9 +263,9 @@ func (p *PayV3) Upload(ctx context.Context, path string, form curl.UploadForm) ( return nil, err } - log.Set(curl.HeaderAuthorization, authStr) + log.Set(xhttp.HeaderAuthorization, authStr) - resp, err := p.httpCli.Upload(ctx, reqURL, form, curl.WithHeader(curl.HeaderAuthorization, authStr)) + resp, err := p.httpCli.Upload(ctx, reqURL, form, xhttp.WithHeader(xhttp.HeaderAuthorization, authStr)) if err != nil { log.Set("error", err.Error()) return nil, err @@ -307,9 +307,9 @@ func (p *PayV3) Download(ctx context.Context, downloadURL string, w io.Writer) e return err } - log.Set(curl.HeaderAuthorization, authStr) + log.Set(xhttp.HeaderAuthorization, authStr) - resp, err := p.httpCli.Do(ctx, http.MethodGet, downloadURL, nil, curl.WithHeader(curl.HeaderAuthorization, authStr)) + resp, err := p.httpCli.Do(ctx, http.MethodGet, downloadURL, nil, xhttp.WithHeader(xhttp.HeaderAuthorization, authStr)) if err != nil { log.Set("error", err.Error()) return err @@ -461,12 +461,12 @@ type PayV3Option func(p *PayV3) // WithPayV3HttpCli 设置支付(v3)请求的 HTTP Client func WithPayV3HttpCli(c *http.Client) PayV3Option { return func(p *PayV3) { - p.httpCli = curl.NewHTTPClient(c) + p.httpCli = xhttp.NewHTTPClient(c) } } // WithPayV3PrivateKey 设置支付(v3)商户RSA私钥 -func WithPayV3PrivateKey(serialNO string, key *lib_crypto.PrivateKey) PayV3Option { +func WithPayV3PrivateKey(serialNO string, key *xcrypto.PrivateKey) PayV3Option { return func(p *PayV3) { p.prvSN = serialNO p.prvKey = key @@ -486,7 +486,7 @@ func NewPayV3(mchid, apikey string, options ...PayV3Option) *PayV3 { host: "https://api.mch.weixin.qq.com", mchid: mchid, apikey: apikey, - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(pay) diff --git a/wechat/xml.go b/wechat/xml.go index 09af304..55a486c 100644 --- a/wechat/xml.go +++ b/wechat/xml.go @@ -9,8 +9,8 @@ import ( "github.com/shenghui0779/sdk-go/lib/value" ) -// FormatVToXML format map to xml -func FormatVToXML(vals value.V) ([]byte, error) { +// ValueToXML value to xml +func ValueToXML(vals value.V) ([]byte, error) { var builder strings.Builder builder.WriteString("") @@ -26,8 +26,8 @@ func FormatVToXML(vals value.V) ([]byte, error) { return []byte(builder.String()), nil } -// ParseXMLToV parse xml to map -func ParseXMLToV(b []byte) (value.V, error) { +// XMLToValue xml to value +func XMLToValue(b []byte) (value.V, error) { m := make(value.V) xmlReader := bytes.NewReader(b) diff --git a/wechat/xml_test.go b/wechat/xml_test.go index f967185..bd3da40 100644 --- a/wechat/xml_test.go +++ b/wechat/xml_test.go @@ -16,10 +16,10 @@ func TestXML(t *testing.T) { "noncestr": "5K8264ILTKCH16CQ2502SI8ZNMTM67VS", "timestamp": "1514363815", } - x, err := FormatVToXML(m) + x, err := ValueToXML(m) assert.Nil(t, err) - r, err := ParseXMLToV([]byte(x)) + r, err := XMLToValue([]byte(x)) assert.Nil(t, err) assert.Equal(t, m, r) } diff --git a/ysepay/client.go b/ysepay/client.go index b432e72..3966d32 100644 --- a/ysepay/client.go +++ b/ysepay/client.go @@ -16,9 +16,9 @@ import ( "github.com/tidwall/gjson" "github.com/shenghui0779/sdk-go/lib" - lib_crypto "github.com/shenghui0779/sdk-go/lib/crypto" - "github.com/shenghui0779/sdk-go/lib/curl" "github.com/shenghui0779/sdk-go/lib/value" + "github.com/shenghui0779/sdk-go/lib/xcrypto" + "github.com/shenghui0779/sdk-go/lib/xhttp" ) // Client 银盛支付客户端 @@ -26,9 +26,9 @@ type Client struct { host string mchNO string desKey string - prvKey *lib_crypto.PrivateKey - pubKey *lib_crypto.PublicKey - httpCli curl.Client + prvKey *xcrypto.PrivateKey + pubKey *xcrypto.PublicKey + httpCli xhttp.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 := lib_crypto.DESEncryptECB([]byte(c.desKey), []byte(plain)) + b, err := xcrypto.DESEncryptECB([]byte(c.desKey), []byte(plain)) if err != nil { return "", err } @@ -58,7 +58,7 @@ func (c *Client) Encrypt(plain string) (string, error) { // MustEncrypt 敏感数据DES加密;若发生错误,则Panic func (c *Client) MustEncrypt(plain string) string { - b, err := lib_crypto.DESEncryptECB([]byte(c.desKey), []byte(plain)) + b, err := xcrypto.DESEncryptECB([]byte(c.desKey), []byte(plain)) if err != nil { panic(err) } @@ -71,7 +71,7 @@ func (c *Client) Decrypt(cipher string) (string, error) { if err != nil { return "", err } - plain, err := lib_crypto.DESEncryptECB([]byte(c.desKey), b) + plain, err := xcrypto.DESEncryptECB([]byte(c.desKey), b) if err != nil { return "", err } @@ -92,7 +92,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), curl.WithHeader(curl.HeaderContentType, curl.ContentForm)) + resp, err := c.httpCli.Do(ctx, http.MethodPost, reqURL, []byte(form), xhttp.WithHeader(xhttp.HeaderContentType, xhttp.ContentForm)) if err != nil { log.Set("error", err.Error()) return lib.Fail(err) @@ -215,19 +215,19 @@ type Option func(c *Client) // WithHttpCli 设置自定义 HTTP Client func WithHttpCli(cli *http.Client) Option { return func(c *Client) { - c.httpCli = curl.NewHTTPClient(cli) + c.httpCli = xhttp.NewHTTPClient(cli) } } // WithPrivateKey 设置商户RSA私钥 -func WithPrivateKey(key *lib_crypto.PrivateKey) Option { +func WithPrivateKey(key *xcrypto.PrivateKey) Option { return func(c *Client) { c.prvKey = key } } // WithPublicKey 设置平台RSA公钥 -func WithPublicKey(key *lib_crypto.PublicKey) Option { +func WithPublicKey(key *xcrypto.PublicKey) Option { return func(c *Client) { c.pubKey = key } @@ -246,7 +246,7 @@ func NewClient(mchNO, desKey string, options ...Option) *Client { host: "https://eqt.ysepay.com", mchNO: mchNO, desKey: desKey, - httpCli: curl.NewDefaultClient(), + httpCli: xhttp.NewDefaultClient(), } for _, fn := range options { fn(c)