This commit is contained in:
李子铭 2025-03-06 17:17:10 +08:00
parent 9d22d81cce
commit f04ad17edb
5 changed files with 44 additions and 32 deletions

View File

@ -10,6 +10,5 @@ type CmbRequestBo struct {
type CmbResponseBo struct {
RespCode string
RespMsg string
FuncName vo.CmbFuncName
BizContent string
}

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
v1 "voucher/api/v1"
"voucher/internal/biz/bo"
@ -50,7 +51,7 @@ func (s *CmbMixRepoImpl) ProductQueryVerify(ctx context.Context, req *v1.CmbRequ
}
func (s *CmbMixRepoImpl) Verify(_ context.Context, req *v1.CmbRequest, funcName vo.CmbFuncName) (string, error) {
str := cmb.SortStructStr(req, funcName.GetValue())
str := cmb.SortStructStr(req)
b, err := cmb.Verify(s.bc.Cmb.CmbSm2Puk, str, req.Sign)
if err != nil {
@ -87,7 +88,11 @@ func (s *CmbMixRepoImpl) GetRequest(_ context.Context, reqBo *bo.CmbRequestBo) (
Sign: "",
}
str := cmb.SortStructStr(req, reqBo.FuncName.GetValue())
str := cmb.SortStructStr(req)
if len(reqBo.FuncName) > 0 {
str = fmt.Sprintf("%s?%s", reqBo.FuncName, str)
}
sing, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
if err != nil {
@ -119,7 +124,7 @@ func (s *CmbMixRepoImpl) GetResponse(_ context.Context, reqBo *bo.CmbResponseBo)
reply.EncryptBody = encryptBody
}
str := cmb.SortStructStr(reply, reqBo.FuncName.GetValue())
str := cmb.SortStructStr(reply)
sign, err := cmb.Sign(s.bc.Cmb.CmbSm2Pik, str)
if err != nil {

View File

@ -64,7 +64,7 @@ func AssemblingByteArray(key, iv []byte) []byte {
return os
}
func SortStructStr(req interface{}, funcName string) string {
func SortStructStr(req interface{}) string {
kvRows := helper.SortStructFieldsByKey(req)
var strToBeSigned strings.Builder
@ -78,5 +78,5 @@ func SortStructStr(req interface{}, funcName string) string {
strToBeSigned.WriteString(fmt.Sprintf("%s=%s&", kv.Key, kv.Value))
}
return fmt.Sprintf("%s?%s", funcName, strings.TrimRight(strToBeSigned.String(), "&"))
return strings.TrimRight(strToBeSigned.String(), "&")
}

View File

@ -28,11 +28,12 @@ func NewHTTPServer(
//构建 server
srv := buildHTTPServer(c, accessLogger, log)
srv.Route("/").GET("/ping", func(ctx http.Context) error {
r := srv.Route("/voucher")
r.GET("/ping", func(ctx http.Context) error {
return ctx.String(http2.StatusOK, "pong")
})
r := srv.Route("/voucher")
r.POST("/v1/wechat/notify", voucherService.WechatNotify)
cmb := r.Group("/cmb")

View File

@ -17,20 +17,24 @@ func (s *VoucherService) CmbOrder(ctx http.Context) error {
bizReply, err := s.cmbOrder(ctx)
if err != nil {
reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: err.Error(),
FuncName: vo.CmbOrderFuncName,
BizContent: "",
})
reply, err = s.CmbMixRepo.GetResponse(
ctx,
&bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: err.Error(),
BizContent: "",
},
)
} else {
replyBizContent, _ := json.Marshal(bizReply)
reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
FuncName: vo.CmbOrderFuncName,
BizContent: string(replyBizContent),
})
reply, err = s.CmbMixRepo.GetResponse(
ctx,
&bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
BizContent: string(replyBizContent),
},
)
}
return ctx.JSON(200, reply)
@ -79,20 +83,23 @@ func (s *VoucherService) CmbProductQuery(ctx http.Context) error {
bizReply, err := s.cmbProductQuery(ctx)
if err != nil {
reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: err.Error(),
FuncName: vo.CmbOrderFuncName,
BizContent: "",
})
reply, err = s.CmbMixRepo.GetResponse(
ctx,
&bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: err.Error(),
BizContent: "",
},
)
} else {
replyBizContent, _ := json.Marshal(bizReply)
reply, err = s.CmbMixRepo.GetResponse(ctx, &bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
FuncName: vo.CmbOrderFuncName,
BizContent: string(replyBizContent),
})
reply, err = s.CmbMixRepo.GetResponse(ctx,
&bo.CmbResponseBo{
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
RespMsg: "成功",
BizContent: string(replyBizContent),
},
)
}
return ctx.JSON(200, reply)