config必填

This commit is contained in:
李子铭 2024-11-20 18:03:44 +08:00
parent 09e2355e03
commit 83a7ee8a8d
5 changed files with 53 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gitea.cdlsxd.cn/sdk/plugin/proto" "gitea.cdlsxd.cn/sdk/plugin/proto"
"github.com/go-playground/validator/v10"
"html" "html"
"plugins/alipay_cpn/internal/po" "plugins/alipay_cpn/internal/po"
"plugins/alipay_cpn/internal/vo" "plugins/alipay_cpn/internal/vo"
@ -11,9 +12,19 @@ import (
) )
type Config struct { type Config struct {
AppId string `json:"app_id"` AppId string `validate:"required" json:"app_id"`
Prk string `json:"prk"` // 私钥 Prk string `validate:"required" json:"prk"` // 私钥
Npk string `json:"npk"` // 回调公钥 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) { func transConfig(config []byte) (*Config, error) {
@ -22,6 +33,9 @@ func transConfig(config []byte) (*Config, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err = c.Validate(); err != nil {
return nil, err
}
return &c, nil return &c, nil
} }
@ -30,7 +44,7 @@ func (c *Config) paramReq(req po.Req, method string) (*po.Param, error) {
return nil, err return nil, err
} }
return &po.Param{ return &po.Param{
Timestamp: time.Now().Format("2006-01-02 15:04:05"), Timestamp: time.Now().Format(time.DateTime),
Version: vo.Version, Version: vo.Version,
AppId: c.AppId, AppId: c.AppId,
Method: method, Method: method,

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gitea.cdlsxd.cn/sdk/plugin/proto" "gitea.cdlsxd.cn/sdk/plugin/proto"
"github.com/go-playground/validator/v10"
"html" "html"
"plugins/alipay_redpack/internal/po" "plugins/alipay_redpack/internal/po"
"plugins/alipay_redpack/internal/vo" "plugins/alipay_redpack/internal/vo"
@ -13,8 +14,18 @@ import (
) )
type Config struct { type Config struct {
AppId string `json:"app_id"` AppId string `validate:"required" json:"app_id"`
Prk string `json:"prk"` // 私钥 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) { func transConfig(config []byte) (*Config, error) {
@ -23,6 +34,9 @@ func transConfig(config []byte) (*Config, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err = c.Validate(); err != nil {
return nil, err
}
return &c, nil return &c, nil
} }

View File

@ -17,6 +17,9 @@ func transConfig(config []byte) (*wechat.Server, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err = c.Validate(); err != nil {
return nil, err
}
return &c, nil return &c, nil
} }

View File

@ -16,6 +16,9 @@ func transConfig(config []byte) (*wechat.Server, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err = c.Validate(); err != nil {
return nil, err
}
return &c, nil return &c, nil
} }

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"github.com/go-playground/validator/v10"
"github.com/wechatpay-apiv3/wechatpay-go/core" "github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option" "github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/utils" "github.com/wechatpay-apiv3/wechatpay-go/utils"
@ -12,8 +13,18 @@ import (
) )
type Server struct { type Server struct {
MchID string `json:"mch_id"` MchID string `validate:"required" json:"mch_id"`
MchCertificateSerialNumber string `json:"mch_certificate_serial_number"` 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) { func newClient(ctx context.Context, c *Server) (*core.Client, error) {