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
|
||||
}
|
||||
|
||||
bizJsonStr := string(bizJsonBytes)
|
||||
|
||||
request, err := v.CmbMixRepo.GetRequestData(ctx, "updateCodeStatus.json", bizJsonStr)
|
||||
request, err := v.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
||||
FuncName: "updateCodeStatus.json",
|
||||
BizContent: string(bizJsonBytes),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,10 +3,12 @@ package mixrepos
|
|||
import (
|
||||
"context"
|
||||
v1 "voucher/api/v1"
|
||||
"voucher/internal/biz/bo"
|
||||
)
|
||||
|
||||
type CmbMixRepo interface {
|
||||
OrderVerify(ctx context.Context, req *v1.CmbRequest, funcName string) (*v1.CmbOrderRequest, 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"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
v1 "voucher/api/v1"
|
||||
"voucher/internal/biz/bo"
|
||||
"voucher/internal/biz/mixrepos"
|
||||
"voucher/internal/conf"
|
||||
"voucher/internal/pkg/cmb"
|
||||
"voucher/internal/pkg/helper"
|
||||
)
|
||||
|
||||
type CmbMixRepoImpl struct {
|
||||
|
|
@ -50,11 +48,8 @@ func (s *CmbMixRepoImpl) ProductQueryVerify(ctx context.Context, req *v1.CmbRequ
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *CmbMixRepoImpl) Verify(ctx context.Context, req *v1.CmbRequest, funcName string) (string, error) {
|
||||
str, err := s.getSignStr(ctx, req, funcName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
func (s *CmbMixRepoImpl) Verify(_ context.Context, req *v1.CmbRequest, funcName string) (string, error) {
|
||||
str := cmb.SortStructStr(req, funcName)
|
||||
|
||||
b, err := cmb.Verify(s.bc.Cmb.CmbSm2Puk, str, req.Sign)
|
||||
if err != nil {
|
||||
|
|
@ -73,26 +68,9 @@ func (s *CmbMixRepoImpl) Verify(ctx context.Context, req *v1.CmbRequest, funcNam
|
|||
return bizStr, nil
|
||||
}
|
||||
|
||||
func (s *CmbMixRepoImpl) getSignStr(_ context.Context, req *v1.CmbRequest, funcName string) (string, error) {
|
||||
kvRows := helper.SortStructFieldsByKey(req)
|
||||
func (s *CmbMixRepoImpl) GetRequest(_ context.Context, reqBo *bo.CmbRequestBo) (*v1.CmbRequest, error) {
|
||||
|
||||
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(), "&")), nil
|
||||
}
|
||||
|
||||
func (s *CmbMixRepoImpl) GetRequestData(ctx context.Context, funcName, bizJsonStr string) (*v1.CmbRequest, error) {
|
||||
|
||||
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, bizJsonStr)
|
||||
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, reqBo.BizContent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -110,10 +88,7 @@ func (s *CmbMixRepoImpl) GetRequestData(ctx context.Context, funcName, bizJsonSt
|
|||
Sign: "",
|
||||
}
|
||||
|
||||
str, err := s.getSignStr(ctx, req, funcName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
str := cmb.SortStructStr(req, reqBo.FuncName)
|
||||
|
||||
sing, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
|
||||
if err != nil {
|
||||
|
|
@ -124,46 +99,35 @@ func (s *CmbMixRepoImpl) GetRequestData(ctx context.Context, funcName, bizJsonSt
|
|||
return req, nil
|
||||
}
|
||||
|
||||
func (s *CmbMixRepoImpl) GetResponseData(ctx context.Context, funcName, bizJsonStr string) (*v1.CmbRequest, error) {
|
||||
|
||||
encryptBody, err := cmb.Encrypt(s.bc.Cmb.Sm2Puk, bizJsonStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func (s *CmbMixRepoImpl) GetResponse(_ context.Context, reqBo *bo.CmbResponseBo) (*v1.CmbReply, error) {
|
||||
|
||||
date := time.Now().Format("20060102150405")
|
||||
|
||||
req := &v1.CmbRequest{
|
||||
Mid: s.bc.Cmb.Mid,
|
||||
Aid: s.bc.Cmb.Aid,
|
||||
reply := &v1.CmbReply{
|
||||
RespCode: reqBo.RespCode,
|
||||
RespMsg: reqBo.RespMsg,
|
||||
Date: date,
|
||||
Random: string(cmb.RandomBytes(16)),
|
||||
KeyAlias: s.bc.Cmb.KeyAlias,
|
||||
CmbKeyAlias: s.bc.Cmb.CmbKeyAlias,
|
||||
EncryptBody: encryptBody,
|
||||
EncryptBody: "",
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
reply.Sign = sign
|
||||
|
||||
sing, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
|
||||
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
|
||||
return reply, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ package cmb
|
|||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
"voucher/internal/pkg/helper"
|
||||
)
|
||||
|
||||
func GenerateSM4Key() []byte {
|
||||
|
|
@ -60,3 +63,20 @@ func AssemblingByteArray(key, iv []byte) []byte {
|
|||
os = append(os, iv...)
|
||||
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
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/go-kratos/kratos/v2/transport/http"
|
||||
v1 "voucher/api/v1"
|
||||
"voucher/internal/biz/bo"
|
||||
|
|
@ -14,9 +15,26 @@ const (
|
|||
|
||||
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 {
|
||||
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)
|
||||
|
|
@ -56,10 +74,26 @@ func (s *VoucherService) cmbOrder(ctx http.Context) (*v1.CmbOrderReply, error) {
|
|||
}
|
||||
|
||||
func (s *VoucherService) CmbProductQuery(ctx http.Context) error {
|
||||
|
||||
reply, err := s.cmbProductQuery(ctx)
|
||||
var (
|
||||
reply *v1.CmbReply
|
||||
err error
|
||||
)
|
||||
bizReply, err := s.cmbProductQuery(ctx)
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"github.com/go-kratos/kratos/v2/transport/http"
|
||||
v1 "voucher/api/v1"
|
||||
"voucher/internal/biz/bo"
|
||||
)
|
||||
|
||||
func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
|
||||
|
|
@ -18,9 +19,10 @@ func (s *VoucherService) CmbOrderMock(ctx http.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
bizJsonStr := string(bizJsonBytes)
|
||||
|
||||
reply, err := s.CmbMixRepo.GetRequestData(ctx, "/voucher/cmb/v1/order", bizJsonStr)
|
||||
reply, err := s.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
||||
FuncName: cmbOrderFuncName,
|
||||
BizContent: string(bizJsonBytes),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -40,9 +42,10 @@ func (s *VoucherService) CmbProductQueryMock(ctx http.Context) error {
|
|||
return err
|
||||
}
|
||||
|
||||
bizJsonStr := string(bizJsonBytes)
|
||||
|
||||
reply, err := s.CmbMixRepo.GetRequestData(ctx, "/voucher/cmb/v1/productQuery", bizJsonStr)
|
||||
reply, err := s.CmbMixRepo.GetRequest(ctx, &bo.CmbRequestBo{
|
||||
FuncName: cmbProductQueryFuncName,
|
||||
BizContent: string(bizJsonBytes),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue