This commit is contained in:
2024-10-12 20:41:55 +08:00
parent 8c09120db6
commit 597e4f1b89
75 changed files with 5009 additions and 823 deletions

View File

@@ -3,11 +3,14 @@ package IVYZ
import (
"context"
"encoding/hex"
"errors"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -26,64 +29,66 @@ func NewIVYZ5733Logic(ctx context.Context, svcCtx *svc.ServiceContext) *IVYZ5733
}
}
type CustomResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"` // `omitempty` 使得当 Data 为空时字段不返回
}
func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err error) {
//userId, ok := l.ctx.Value("userId").(int64)
//if !ok {
// return &types.Response{}, errors.New("系统错误,请联系管理员")
//}
func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err *errs.AppError) {
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errors.New("系统错误,请联系管理员")
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, err := hex.DecodeString(secretKey)
decryptData, err := crypto.AesDecrypt(req.Data, key)
if err != nil || len(decryptData) == 0 {
return nil, errors.New("参数解密失败")
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
// 2、校验
var data validator.IVYZ5733Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, validatorErr
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
name, err := crypto.WestDexEncrypt(data.Name, westConfig.Key)
if err != nil {
encryptedFields, encryptStructFieldsErr := common.EncryptStructFields(data, westConfig.Key)
if encryptStructFieldsErr != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
idCard, err := crypto.WestDexEncrypt(data.IDCard, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
// 4、发送请求到西部
westdexRequest := map[string]interface{}{
"id": idCard,
"name": name,
}
westResp, err := l.svcCtx.WestDexService.CallAPI("G09GX01", westdexRequest)
if err != nil {
return nil, err
return nil, errs.ErrSystem
}
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.IVYZ5733FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G09GX01", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G09GX01Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, unmarshalErr
//}
//crypto.AesEncrypt()
var respData westmodel.G09GX01Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Code == 400 || respData.Code == 500 || respData.Code == 999 {
logx.Errorf("西部响应错误%v", respData)
return nil, errs.ErrSystem
}
if respData.Code == 201 || respData.Code == 202 || respData.Code == 203 {
l.ctx = context.WithValue(l.ctx, "Charges", true)
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: string(westResp),
Data: encryptData,
}, nil
}