代码优化
This commit is contained in:
parent
3913f00b13
commit
7428fb1892
|
@ -12,12 +12,19 @@ import (
|
||||||
var cardService = &ZLTXCardV1Service{}
|
var cardService = &ZLTXCardV1Service{}
|
||||||
|
|
||||||
func config() []byte {
|
func config() []byte {
|
||||||
|
//c := &Config{
|
||||||
|
// AppId: "1",
|
||||||
|
// AppKey: "1e2bf7a04b8b1e6be5dc78d04e8639c9",
|
||||||
|
// BaseUri: "http://test.openapi.1688sup.cn",
|
||||||
|
// NotifyUrl: "https://gateway.dev.cdlsxd.cn/yxh5api/v1/order/direct/notify",
|
||||||
|
// MerchantId: 23329,
|
||||||
|
//}
|
||||||
c := &Config{
|
c := &Config{
|
||||||
AppId: "1",
|
AppId: "101",
|
||||||
AppKey: "1e2bf7a04b8b1e6be5dc78d04e8639c9",
|
AppKey: "95E7EC7D4A394FF8D11788E5E436DE99",
|
||||||
BaseUri: "http://test.openapi.1688sup.cn",
|
BaseUri: "https://openapi.1688sup.com",
|
||||||
NotifyUrl: "https://gateway.dev.cdlsxd.cn/yxh5api/v1/order/direct/notify",
|
NotifyUrl: "https://gateway.dev.cdlsxd.cn/yxh5api/v1/order/direct/notify",
|
||||||
MerchantId: 23329,
|
MerchantId: 25715,
|
||||||
}
|
}
|
||||||
marshal, _ := json.Marshal(c)
|
marshal, _ := json.Marshal(c)
|
||||||
return marshal
|
return marshal
|
||||||
|
|
|
@ -3,10 +3,10 @@ module plugins/zltx_v1
|
||||||
go 1.22.2
|
go 1.22.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
gitea.cdlsxd.cn/sdk/plugin v1.0.19
|
gitea.cdlsxd.cn/sdk/plugin v1.0.20
|
||||||
github.com/go-playground/validator/v10 v10.22.0
|
github.com/go-playground/validator/v10 v10.22.0
|
||||||
github.com/hashicorp/go-plugin v1.6.1
|
github.com/hashicorp/go-plugin v1.6.1
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.10.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
gitea.cdlsxd.cn/sdk/plugin v1.0.18 h1:YgVJmCSSEu8JAniXlT1rI+h0w3EEGDRWLFqjk/5xBQY=
|
gitea.cdlsxd.cn/sdk/plugin v1.0.20 h1:oAcqfHiOHoa+f9w6BPbvY9EZ0pwQ1mo3dD+YJD8OpYo=
|
||||||
gitea.cdlsxd.cn/sdk/plugin v1.0.18/go.mod h1:O/bYQWg1o9g/cBq9qNA3kLIpuPt7VDZqj1bPE6s04NM=
|
gitea.cdlsxd.cn/sdk/plugin v1.0.20/go.mod h1:W2zpoqi70a9X7n3WCLcZ0sjxXTPgYhiJW36FO+qKlb0=
|
||||||
gitea.cdlsxd.cn/sdk/plugin v1.0.19 h1:j0Ifn3q+C7ibxSTfL1KbmnX1k/VO9e0XMDJSuPutixU=
|
|
||||||
gitea.cdlsxd.cn/sdk/plugin v1.0.19/go.mod h1:O/bYQWg1o9g/cBq9qNA3kLIpuPt7VDZqj1bPE6s04NM=
|
|
||||||
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
|
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
|
||||||
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
|
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
@ -52,8 +50,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
||||||
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
|
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
|
||||||
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
|
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
|
||||||
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
|
||||||
|
|
|
@ -21,8 +21,7 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) validate() error {
|
func (c *Config) validate() error {
|
||||||
err := validator.New().Struct(c)
|
if err := validator.New().Struct(c); err != nil {
|
||||||
if err != nil {
|
|
||||||
for _, err = range err.(validator.ValidationErrors) {
|
for _, err = range err.(validator.ValidationErrors) {
|
||||||
return proto.ErrorConfigFail(fmt.Sprintf("配置参数有误:%s", err.Error()))
|
return proto.ErrorConfigFail(fmt.Sprintf("配置参数有误:%s", err.Error()))
|
||||||
}
|
}
|
||||||
|
@ -32,13 +31,15 @@ func (c *Config) validate() error {
|
||||||
|
|
||||||
func transConfig(config []byte) (*Config, error) {
|
func transConfig(config []byte) (*Config, error) {
|
||||||
var c Config
|
var c Config
|
||||||
err := json.Unmarshal(config, &c)
|
|
||||||
if err != nil {
|
if err := json.Unmarshal(config, &c); err != nil {
|
||||||
return nil, proto.ErrorSignFail(fmt.Sprintf("配置参数解析失败: %v", err))
|
return nil, proto.ErrorSignFail(fmt.Sprintf("配置参数解析失败: %v", err))
|
||||||
}
|
}
|
||||||
if err = c.validate(); err != nil {
|
|
||||||
|
if err := c.validate(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &c, nil
|
return &c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +76,7 @@ func (c *Config) orderReq(in *proto.OrderRequest) (*direct.Order, error) {
|
||||||
accountType = 2
|
accountType = 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
d := &direct.Order{
|
d := &direct.Order{
|
||||||
Number: in.Order.Quantity,
|
Number: in.Order.Quantity,
|
||||||
MerchantId: c.MerchantId,
|
MerchantId: c.MerchantId,
|
||||||
|
@ -88,6 +90,7 @@ func (c *Config) orderReq(in *proto.OrderRequest) (*direct.Order, error) {
|
||||||
if in.Order.Extra != nil {
|
if in.Order.Extra != nil {
|
||||||
d.ExtendParameter = string(in.Order.Extra)
|
d.ExtendParameter = string(in.Order.Extra)
|
||||||
}
|
}
|
||||||
|
|
||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,8 +116,12 @@ func (c *Config) queryReq(in *proto.QueryRequest) *direct.Query {
|
||||||
}
|
}
|
||||||
|
|
||||||
func queryResp(request *proto.QueryRequest, resp *direct.QueryResp) (*proto.QueryResponse, error) {
|
func queryResp(request *proto.QueryRequest, resp *direct.QueryResp) (*proto.QueryResponse, error) {
|
||||||
data, _ := json.Marshal(resp)
|
data, err := json.Marshal(resp)
|
||||||
pb := &proto.QueryResponse{
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &proto.QueryResponse{
|
||||||
Result: &proto.Result{
|
Result: &proto.Result{
|
||||||
Status: resp.Status.GetOrderStatus(),
|
Status: resp.Status.GetOrderStatus(),
|
||||||
OrderNo: request.Order.OrderNo,
|
OrderNo: request.Order.OrderNo,
|
||||||
|
@ -123,12 +130,15 @@ func queryResp(request *proto.QueryRequest, resp *direct.QueryResp) (*proto.Quer
|
||||||
Data: data,
|
Data: data,
|
||||||
Extra: nil,
|
Extra: nil,
|
||||||
},
|
},
|
||||||
}
|
}, nil
|
||||||
return pb, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func notifyResp(resp *direct.Notify) (*proto.NotifyResponse, error) {
|
func notifyResp(resp *direct.Notify) (*proto.NotifyResponse, error) {
|
||||||
data, _ := json.Marshal(resp)
|
data, err := json.Marshal(resp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
pb := &proto.NotifyResponse{
|
pb := &proto.NotifyResponse{
|
||||||
Result: &proto.Result{
|
Result: &proto.Result{
|
||||||
Status: resp.Status.GetOrderStatus(),
|
Status: resp.Status.GetOrderStatus(),
|
||||||
|
@ -148,6 +158,7 @@ func notifyResp(resp *direct.Notify) (*proto.NotifyResponse, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pb.Headers = string(responseHeadersBytes)
|
pb.Headers = string(responseHeadersBytes)
|
||||||
|
|
||||||
return pb, nil
|
return pb, nil
|
||||||
|
|
|
@ -43,13 +43,13 @@ func TestOrder(t *testing.T) {
|
||||||
request := &proto.OrderRequest{
|
request := &proto.OrderRequest{
|
||||||
Config: config(),
|
Config: config(),
|
||||||
Order: &proto.OrderRequest_Order{
|
Order: &proto.OrderRequest_Order{
|
||||||
OrderNo: "test_plugin_zltx_v1_direct_2",
|
OrderNo: "test_plugin_zltx_v1_direct_1",
|
||||||
Account: "583989020@qq.com",
|
Account: "18666173766",
|
||||||
Quantity: 1,
|
Quantity: 1,
|
||||||
Extra: nil,
|
Extra: []byte(`{"jdCode":"123456"}`),
|
||||||
},
|
},
|
||||||
Product: &proto.OrderRequest_Product{
|
Product: &proto.OrderRequest_Product{
|
||||||
ProductNo: "101",
|
ProductNo: "2255", // 101 2255
|
||||||
Extra: []byte(`{}`),
|
Extra: []byte(`{}`),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ func TestQuery(t *testing.T) {
|
||||||
request := &proto.QueryRequest{
|
request := &proto.QueryRequest{
|
||||||
Config: config(),
|
Config: config(),
|
||||||
Order: &proto.QueryRequest_Order{
|
Order: &proto.QueryRequest_Order{
|
||||||
OrderNo: "test_plugin_zltx_v1_direct_2",
|
OrderNo: "test_plugin_zltx_v1_direct_1",
|
||||||
TradeNo: "",
|
TradeNo: "",
|
||||||
Account: "",
|
Account: "",
|
||||||
Extra: nil,
|
Extra: nil,
|
||||||
|
|
Loading…
Reference in New Issue