125 lines
3.4 KiB
Go
125 lines
3.4 KiB
Go
package QYGL
|
||
|
||
import (
|
||
"context"
|
||
"encoding/hex"
|
||
"encoding/json"
|
||
"tianyuan-api/apps/api/internal/common"
|
||
"tianyuan-api/apps/api/internal/validator"
|
||
"tianyuan-api/apps/api/internal/westmodel"
|
||
"tianyuan-api/pkg/crypto"
|
||
"tianyuan-api/pkg/errs"
|
||
|
||
"tianyuan-api/apps/api/internal/svc"
|
||
"tianyuan-api/apps/api/internal/types"
|
||
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
)
|
||
|
||
type QYGLB4C0Logic struct {
|
||
logx.Logger
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
func NewQYGLB4C0Logic(ctx context.Context, svcCtx *svc.ServiceContext) *QYGLB4C0Logic {
|
||
return &QYGLB4C0Logic{
|
||
Logger: logx.WithContext(ctx),
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
}
|
||
}
|
||
|
||
func (l *QYGLB4C0Logic) QYGLB4C0(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.QYGLB4C0Request
|
||
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
|
||
//}
|
||
IDCard := crypto.Md5Encrypt(data.IDCard)
|
||
encryptedFields := map[string]interface{}{
|
||
"IDCard": IDCard,
|
||
}
|
||
|
||
// 4、发送请求到西部
|
||
logx.Infof("交易号:%s", transactionID)
|
||
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.QYGLB4C0FieldMapping, "")
|
||
|
||
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPISecond("G05HZ01", apiRequest, l.svcCtx.Config.WestConfig.SecretSecondId)
|
||
if callAPIErr != nil {
|
||
if callAPIErr.Code == errs.ErrDataSource.Code {
|
||
// 检查是否是1404错误(库中无此记录)
|
||
var westData map[string]interface{}
|
||
if err := json.Unmarshal(westResp, &westData); err == nil {
|
||
// 日志显示错误码在顶层的"code"字段
|
||
if code, ok := westData["code"].(string); ok && code == "1404" {
|
||
return "", errs.ErrNotFound
|
||
}
|
||
}
|
||
|
||
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
|
||
if aesEncrypt != nil {
|
||
return "", errs.ErrSystem
|
||
}
|
||
return encryptData, callAPIErr
|
||
}
|
||
return "", callAPIErr
|
||
}
|
||
|
||
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
|
||
if aesEncrypt != nil {
|
||
return "", errs.ErrSystem
|
||
}
|
||
return encryptData, nil
|
||
}
|