cmb
This commit is contained in:
parent
9851c1f61b
commit
5d639570a2
|
|
@ -0,0 +1,13 @@
|
||||||
|
package bo
|
||||||
|
|
||||||
|
type CmbRequestBo struct {
|
||||||
|
FuncName string
|
||||||
|
BizContent string
|
||||||
|
}
|
||||||
|
|
||||||
|
type CmbResponseBo struct {
|
||||||
|
RespCode string
|
||||||
|
RespMsg string
|
||||||
|
FuncName string
|
||||||
|
BizContent string
|
||||||
|
}
|
||||||
|
|
@ -107,9 +107,10 @@ func (v *Cmb) NotifyConsume(ctx context.Context, order *bo.OrderBo, orderOutRequ
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
bizJsonStr := string(bizJsonBytes)
|
request, err := v.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
||||||
|
FuncName: "updateCodeStatus.json",
|
||||||
request, err := v.CmbMixRepo.GetRequestData(ctx, "updateCodeStatus.json", bizJsonStr)
|
BizContent: string(bizJsonBytes),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,12 @@ package mixrepos
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
v1 "voucher/api/v1"
|
v1 "voucher/api/v1"
|
||||||
|
"voucher/internal/biz/bo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CmbMixRepo interface {
|
type CmbMixRepo interface {
|
||||||
OrderVerify(ctx context.Context, req *v1.CmbRequest, funcName string) (*v1.CmbOrderRequest, error)
|
OrderVerify(ctx context.Context, req *v1.CmbRequest, funcName string) (*v1.CmbOrderRequest, error)
|
||||||
ProductQueryVerify(ctx context.Context, req *v1.CmbRequest, funcName string) (*v1.CmbQueryProductRequest, error)
|
ProductQueryVerify(ctx context.Context, req *v1.CmbRequest, funcName string) (*v1.CmbQueryProductRequest, error)
|
||||||
GetRequestData(ctx context.Context, funcName, bizJsonStr string) (*v1.CmbRequest, error)
|
GetRequest(ctx context.Context, reqBo *bo.CmbRequestBo) (*v1.CmbRequest, error)
|
||||||
|
GetResponse(ctx context.Context, reqBo *bo.CmbResponseBo) (*v1.CmbReply, error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,12 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
v1 "voucher/api/v1"
|
v1 "voucher/api/v1"
|
||||||
|
"voucher/internal/biz/bo"
|
||||||
"voucher/internal/biz/mixrepos"
|
"voucher/internal/biz/mixrepos"
|
||||||
"voucher/internal/conf"
|
"voucher/internal/conf"
|
||||||
"voucher/internal/pkg/cmb"
|
"voucher/internal/pkg/cmb"
|
||||||
"voucher/internal/pkg/helper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CmbMixRepoImpl struct {
|
type CmbMixRepoImpl struct {
|
||||||
|
|
@ -50,11 +48,8 @@ func (s *CmbMixRepoImpl) ProductQueryVerify(ctx context.Context, req *v1.CmbRequ
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CmbMixRepoImpl) Verify(ctx context.Context, req *v1.CmbRequest, funcName string) (string, error) {
|
func (s *CmbMixRepoImpl) Verify(_ context.Context, req *v1.CmbRequest, funcName string) (string, error) {
|
||||||
str, err := s.getSignStr(ctx, req, funcName)
|
str := cmb.SortStructStr(req, funcName)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := cmb.Verify(s.bc.Cmb.CmbSm2Puk, str, req.Sign)
|
b, err := cmb.Verify(s.bc.Cmb.CmbSm2Puk, str, req.Sign)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -73,26 +68,9 @@ func (s *CmbMixRepoImpl) Verify(ctx context.Context, req *v1.CmbRequest, funcNam
|
||||||
return bizStr, nil
|
return bizStr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CmbMixRepoImpl) getSignStr(_ context.Context, req *v1.CmbRequest, funcName string) (string, error) {
|
func (s *CmbMixRepoImpl) GetRequest(_ context.Context, reqBo *bo.CmbRequestBo) (*v1.CmbRequest, error) {
|
||||||
kvRows := helper.SortStructFieldsByKey(req)
|
|
||||||
|
|
||||||
var strToBeSigned strings.Builder
|
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, reqBo.BizContent)
|
||||||
for _, kv := range kvRows {
|
|
||||||
if kv.Key == "sign" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if kv.Value == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
strToBeSigned.WriteString(fmt.Sprintf("%s=%s&", kv.Key, kv.Value))
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s?%s", funcName, strings.TrimRight(strToBeSigned.String(), "&")), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *CmbMixRepoImpl) GetRequestData(ctx context.Context, funcName, bizJsonStr string) (*v1.CmbRequest, error) {
|
|
||||||
|
|
||||||
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, bizJsonStr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -110,10 +88,7 @@ func (s *CmbMixRepoImpl) GetRequestData(ctx context.Context, funcName, bizJsonSt
|
||||||
Sign: "",
|
Sign: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
str, err := s.getSignStr(ctx, req, funcName)
|
str := cmb.SortStructStr(req, reqBo.FuncName)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sing, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
|
sing, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -124,46 +99,35 @@ func (s *CmbMixRepoImpl) GetRequestData(ctx context.Context, funcName, bizJsonSt
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CmbMixRepoImpl) GetResponseData(ctx context.Context, funcName, bizJsonStr string) (*v1.CmbRequest, error) {
|
func (s *CmbMixRepoImpl) GetResponse(_ context.Context, reqBo *bo.CmbResponseBo) (*v1.CmbReply, error) {
|
||||||
|
|
||||||
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, bizJsonStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
date := time.Now().Format("20060102150405")
|
date := time.Now().Format("20060102150405")
|
||||||
|
|
||||||
req := &v1.CmbRequest{
|
reply := &v1.CmbReply{
|
||||||
Mid: s.bc.Cmb.Mid,
|
RespCode: reqBo.RespCode,
|
||||||
Aid: s.bc.Cmb.Aid,
|
RespMsg: reqBo.RespMsg,
|
||||||
Date: date,
|
Date: date,
|
||||||
Random: string(cmb.RandomBytes(16)),
|
|
||||||
KeyAlias: s.bc.Cmb.KeyAlias,
|
KeyAlias: s.bc.Cmb.KeyAlias,
|
||||||
CmbKeyAlias: s.bc.Cmb.CmbKeyAlias,
|
CmbKeyAlias: s.bc.Cmb.CmbKeyAlias,
|
||||||
EncryptBody: encryptBody,
|
EncryptBody: "",
|
||||||
Sign: "",
|
Sign: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
str, err := s.getSignStr(ctx, req, funcName)
|
if len(reqBo.BizContent) > 0 {
|
||||||
|
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, reqBo.BizContent)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
reply.EncryptBody = encryptBody
|
||||||
|
}
|
||||||
|
|
||||||
|
str := cmb.SortStructStr(reply, reqBo.FuncName)
|
||||||
|
|
||||||
|
sign, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
reply.Sign = sign
|
||||||
|
|
||||||
sing, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
|
return reply, nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Sign = sing
|
|
||||||
|
|
||||||
return req, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *CmbMixRepoImpl) Request(ctx context.Context, funcName, bizJsonStr string) (*v1.CmbRequest, error) {
|
|
||||||
data, err := s.GetRequestData(ctx, funcName, bizJsonStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
fmt.Print(data)
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,10 @@ package cmb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
"voucher/internal/pkg/helper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GenerateSM4Key() []byte {
|
func GenerateSM4Key() []byte {
|
||||||
|
|
@ -60,3 +63,20 @@ func AssemblingByteArray(key, iv []byte) []byte {
|
||||||
os = append(os, iv...)
|
os = append(os, iv...)
|
||||||
return os
|
return os
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SortStructStr(req interface{}, funcName string) string {
|
||||||
|
kvRows := helper.SortStructFieldsByKey(req)
|
||||||
|
|
||||||
|
var strToBeSigned strings.Builder
|
||||||
|
for _, kv := range kvRows {
|
||||||
|
if kv.Key == "sign" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if kv.Value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
strToBeSigned.WriteString(fmt.Sprintf("%s=%s&", kv.Key, kv.Value))
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s?%s", funcName, strings.TrimRight(strToBeSigned.String(), "&"))
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/go-kratos/kratos/v2/transport/http"
|
"github.com/go-kratos/kratos/v2/transport/http"
|
||||||
v1 "voucher/api/v1"
|
v1 "voucher/api/v1"
|
||||||
"voucher/internal/biz/bo"
|
"voucher/internal/biz/bo"
|
||||||
|
|
@ -14,9 +15,26 @@ const (
|
||||||
|
|
||||||
func (s *VoucherService) CmbOrder(ctx http.Context) error {
|
func (s *VoucherService) CmbOrder(ctx http.Context) error {
|
||||||
|
|
||||||
reply, err := s.cmbOrder(ctx)
|
var (
|
||||||
|
reply *v1.CmbReply
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
bizReply, err := s.cmbOrder(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
|
||||||
|
RespCode: "1001",
|
||||||
|
RespMsg: err.Error(),
|
||||||
|
FuncName: cmbOrderFuncName,
|
||||||
|
BizContent: "",
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
replyBizContent, _ := json.Marshal(bizReply)
|
||||||
|
reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
|
||||||
|
RespCode: "1000",
|
||||||
|
RespMsg: "成功",
|
||||||
|
FuncName: cmbOrderFuncName,
|
||||||
|
BizContent: string(replyBizContent),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.JSON(200, reply)
|
return ctx.JSON(200, reply)
|
||||||
|
|
@ -56,10 +74,26 @@ func (s *VoucherService) cmbOrder(ctx http.Context) (*v1.CmbOrderReply, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *VoucherService) CmbProductQuery(ctx http.Context) error {
|
func (s *VoucherService) CmbProductQuery(ctx http.Context) error {
|
||||||
|
var (
|
||||||
reply, err := s.cmbProductQuery(ctx)
|
reply *v1.CmbReply
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
bizReply, err := s.cmbProductQuery(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
|
||||||
|
RespCode: "1001",
|
||||||
|
RespMsg: err.Error(),
|
||||||
|
FuncName: cmbOrderFuncName,
|
||||||
|
BizContent: "",
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
replyBizContent, _ := json.Marshal(bizReply)
|
||||||
|
reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
|
||||||
|
RespCode: "1000",
|
||||||
|
RespMsg: "成功",
|
||||||
|
FuncName: cmbOrderFuncName,
|
||||||
|
BizContent: string(replyBizContent),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.JSON(200, reply)
|
return ctx.JSON(200, reply)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/go-kratos/kratos/v2/transport/http"
|
"github.com/go-kratos/kratos/v2/transport/http"
|
||||||
v1 "voucher/api/v1"
|
v1 "voucher/api/v1"
|
||||||
|
"voucher/internal/biz/bo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
|
func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
|
||||||
|
|
@ -18,9 +19,10 @@ func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
bizJsonStr := string(bizJsonBytes)
|
reply, err := s.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
||||||
|
FuncName: cmbOrderFuncName,
|
||||||
reply, err := s.CmbMixRepo.GetRequestData(ctx, "/voucher/cmb/v1/order", bizJsonStr)
|
BizContent: string(bizJsonBytes),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -40,9 +42,10 @@ func (s *VoucherService) CmbProductQueryMock(ctx http.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
bizJsonStr := string(bizJsonBytes)
|
reply, err := s.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
||||||
|
FuncName: cmbProductQueryFuncName,
|
||||||
reply, err := s.CmbMixRepo.GetRequestData(ctx, "/voucher/cmb/v1/productQuery", bizJsonStr)
|
BizContent: string(bizJsonBytes),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue