From 83a7ee8a8d0f22ecde1f1deb591d157d1e15d14e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AD=90=E9=93=AD?= Date: Wed, 20 Nov 2024 18:03:44 +0800 Subject: [PATCH] =?UTF-8?q?config=E5=BF=85=E5=A1=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/alipay_cpn/internal/transform.go | 22 ++++++++++++++++---- plugins/alipay_redpack/internal/transform.go | 18 ++++++++++++++-- plugins/wechat_cpn/internal/transform.go | 3 +++ plugins/wechat_redpack/internal/transform.go | 3 +++ utils/wechat/client.go | 15 +++++++++++-- 5 files changed, 53 insertions(+), 8 deletions(-) diff --git a/plugins/alipay_cpn/internal/transform.go b/plugins/alipay_cpn/internal/transform.go index b63c29b..819e89e 100644 --- a/plugins/alipay_cpn/internal/transform.go +++ b/plugins/alipay_cpn/internal/transform.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "gitea.cdlsxd.cn/sdk/plugin/proto" + "github.com/go-playground/validator/v10" "html" "plugins/alipay_cpn/internal/po" "plugins/alipay_cpn/internal/vo" @@ -11,9 +12,19 @@ import ( ) type Config struct { - AppId string `json:"app_id"` - Prk string `json:"prk"` // 私钥 - Npk string `json:"npk"` // 回调公钥 + AppId string `validate:"required" json:"app_id"` + Prk string `validate:"required" json:"prk"` // 私钥 + Npk string `validate:"required" json:"npk"` // 回调公钥 +} + +func (c *Config) Validate() error { + err := validator.New().Struct(c) + if err != nil { + for _, err = range err.(validator.ValidationErrors) { + return fmt.Errorf("配置有误:" + err.Error()) + } + } + return nil } func transConfig(config []byte) (*Config, error) { @@ -22,6 +33,9 @@ func transConfig(config []byte) (*Config, error) { if err != nil { return nil, err } + if err = c.Validate(); err != nil { + return nil, err + } return &c, nil } @@ -30,7 +44,7 @@ func (c *Config) paramReq(req po.Req, method string) (*po.Param, error) { return nil, err } return &po.Param{ - Timestamp: time.Now().Format("2006-01-02 15:04:05"), + Timestamp: time.Now().Format(time.DateTime), Version: vo.Version, AppId: c.AppId, Method: method, diff --git a/plugins/alipay_redpack/internal/transform.go b/plugins/alipay_redpack/internal/transform.go index 05916f5..37539db 100644 --- a/plugins/alipay_redpack/internal/transform.go +++ b/plugins/alipay_redpack/internal/transform.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "gitea.cdlsxd.cn/sdk/plugin/proto" + "github.com/go-playground/validator/v10" "html" "plugins/alipay_redpack/internal/po" "plugins/alipay_redpack/internal/vo" @@ -13,8 +14,18 @@ import ( ) type Config struct { - AppId string `json:"app_id"` - Prk string `json:"prk"` // 私钥 + AppId string `validate:"required" json:"app_id"` + Prk string `validate:"required" json:"prk"` +} + +func (c *Config) Validate() error { + err := validator.New().Struct(c) + if err != nil { + for _, err = range err.(validator.ValidationErrors) { + return fmt.Errorf("配置有误:" + err.Error()) + } + } + return nil } func transConfig(config []byte) (*Config, error) { @@ -23,6 +34,9 @@ func transConfig(config []byte) (*Config, error) { if err != nil { return nil, err } + if err = c.Validate(); err != nil { + return nil, err + } return &c, nil } diff --git a/plugins/wechat_cpn/internal/transform.go b/plugins/wechat_cpn/internal/transform.go index 245b2f0..b5323ec 100644 --- a/plugins/wechat_cpn/internal/transform.go +++ b/plugins/wechat_cpn/internal/transform.go @@ -17,6 +17,9 @@ func transConfig(config []byte) (*wechat.Server, error) { if err != nil { return nil, err } + if err = c.Validate(); err != nil { + return nil, err + } return &c, nil } diff --git a/plugins/wechat_redpack/internal/transform.go b/plugins/wechat_redpack/internal/transform.go index aa403f2..8c9408f 100644 --- a/plugins/wechat_redpack/internal/transform.go +++ b/plugins/wechat_redpack/internal/transform.go @@ -16,6 +16,9 @@ func transConfig(config []byte) (*wechat.Server, error) { if err != nil { return nil, err } + if err = c.Validate(); err != nil { + return nil, err + } return &c, nil } diff --git a/utils/wechat/client.go b/utils/wechat/client.go index 60880e4..51bf2ec 100644 --- a/utils/wechat/client.go +++ b/utils/wechat/client.go @@ -4,6 +4,7 @@ import ( "context" "crypto/x509" "fmt" + "github.com/go-playground/validator/v10" "github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/core/option" "github.com/wechatpay-apiv3/wechatpay-go/utils" @@ -12,8 +13,18 @@ import ( ) type Server struct { - MchID string `json:"mch_id"` - MchCertificateSerialNumber string `json:"mch_certificate_serial_number"` + MchID string `validate:"required" json:"mch_id"` + MchCertificateSerialNumber string `validate:"required" json:"mch_certificate_serial_number"` +} + +func (c *Server) Validate() error { + err := validator.New().Struct(c) + if err != nil { + for _, err = range err.(validator.ValidationErrors) { + return fmt.Errorf("配置有误:" + err.Error()) + } + } + return nil } func newClient(ctx context.Context, c *Server) (*core.Client, error) {