tianyuan-api-server/apps/api/internal/logic/QYGL/qygl8271logic.go

133 lines
3.4 KiB
Go
Raw Normal View History

2025-03-18 22:07:42 +08:00
package QYGL
import (
"context"
"encoding/hex"
"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"
)
type QYGL8271Logic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewQYGL8271Logic(ctx context.Context, svcCtx *svc.ServiceContext) *QYGL8271Logic {
return &QYGL8271Logic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *QYGL8271Logic) QYGL8271(req *types.Request) (resp string, err *errs.AppError) {
var status string
var charges bool
var remark = ""
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return "", errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return "", errs.ErrSystem
}
userId, userIdOk := l.ctx.Value("userId").(int64)
if !userIdOk {
return "", errs.ErrSystem
}
productCode, productCodeOk := l.ctx.Value("productCode").(string)
if !productCodeOk || productCode == "" {
return "", errs.ErrSystem
}
defer func() {
if err != nil {
status = "failed"
charges = false
} else {
status = "success"
charges = true
}
sendApiRequestMessageErr := l.svcCtx.ApiRequestMqsService.SendApiRequestMessage(l.ctx, transactionID, userId, productCode, status, charges, remark)
if sendApiRequestMessageErr != nil {
logx.Errorf("发送 API 请求消息失败: %v", err)
}
}()
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return "", errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return "", errs.ErrParamDecryption
}
// 2、校验
var data validator.QYGL8271Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return "", errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, encryptStructFieldsErr := common.EncryptStructFields(data, westConfig.Key)
if encryptStructFieldsErr != nil {
logx.Errorf("西部加密错误:%v", encryptStructFieldsErr)
return "", errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.QYGL8271FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("Q03SC01", apiRequest, l.svcCtx.Config.WestConfig.SecretId)
if callAPIErr != nil {
if callAPIErr.Code == errs.ErrDataSource.Code {
2025-05-21 18:42:39 +08:00
var resultResp []byte
if req.Options.Json {
var parseErr *errs.AppError
resultResp, parseErr = common.ParseJsonResponse(westResp)
if parseErr != nil {
return "", parseErr
}
} else {
resultResp = westResp
}
encryptData, aesEncrypt := crypto.AesEncrypt(resultResp, key)
2025-03-18 22:07:42 +08:00
if aesEncrypt != nil {
return "", errs.ErrSystem
}
return encryptData, callAPIErr
}
return "", callAPIErr
}
2025-05-21 18:42:39 +08:00
var resultResp []byte
if req.Options.Json {
var parseErr *errs.AppError
resultResp, parseErr = common.ParseJsonResponse(westResp)
if parseErr != nil {
return "", parseErr
}
} else {
resultResp = westResp
}
encryptData, aesEncrypt := crypto.AesEncrypt(resultResp, key)
2025-03-18 22:07:42 +08:00
if aesEncrypt != nil {
return "", errs.ErrSystem
}
return encryptData, nil
}