Skip to content

Commit

Permalink
重命名lib包,避免与标准库冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
shenghui0779 committed Jun 16, 2024
1 parent 1f649a0 commit dc13108
Show file tree
Hide file tree
Showing 33 changed files with 207 additions and 210 deletions.
2 changes: 1 addition & 1 deletion alipay/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 16 additions & 16 deletions alipay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ 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 支付宝客户端
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)
}

Expand All @@ -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())
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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 解析加密数据,如:授权的用户信息和手机号
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
38 changes: 19 additions & 19 deletions alipay/client_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,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"
)

// ClientV3 支付宝V3客户端(仅支持v3版本的接口可用)
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)
}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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
}
Expand All @@ -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 自定义设置项
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions alipay/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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())
}
24 changes: 12 additions & 12 deletions antchain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 发送请求使用的客户端
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
Expand Down
Loading

0 comments on commit dc13108

Please sign in to comment.