147 lines
4.1 KiB
Go
147 lines
4.1 KiB
Go
package IVYZ
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"errors"
|
|
"tianyuan-api/apps/api/internal/service"
|
|
"tianyuan-api/apps/api/internal/svc"
|
|
"tianyuan-api/apps/api/internal/types"
|
|
"tianyuan-api/apps/api/internal/validator"
|
|
"tianyuan-api/pkg/crypto"
|
|
"tianyuan-api/pkg/errs"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type IVYZ5733Logic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewIVYZ5733Logic(ctx context.Context, svcCtx *svc.ServiceContext) *IVYZ5733Logic {
|
|
return &IVYZ5733Logic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *IVYZ5733Logic) IVYZ5733(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.IVYZ5733Request
|
|
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.IVYZ5733FieldMapping, "data")
|
|
|
|
// westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G09SC02", apiRequest, l.svcCtx.Config.WestConfig.SecretId)
|
|
// if callAPIErr != nil {
|
|
// if callAPIErr.Code == errs.ErrDataSource.Code {
|
|
// encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
|
|
// if aesEncrypt != nil {
|
|
// return "", errs.ErrSystem
|
|
// }
|
|
|
|
// return encryptData, callAPIErr
|
|
// }
|
|
// return "", callAPIErr
|
|
// }
|
|
apiRequest := map[string]interface{}{
|
|
"cardNo": data.IDCard,
|
|
"name": data.Name,
|
|
}
|
|
respData, callAPIErr := l.svcCtx.YushanService.Request("IDV044", apiRequest)
|
|
if callAPIErr != nil {
|
|
if errors.Is(callAPIErr, service.NotFound) {
|
|
return "", errs.ErrNotFound
|
|
}
|
|
return "", errs.ErrSystem
|
|
}
|
|
// 使用gjson判断respData是否有status字段
|
|
statusField := gjson.GetBytes(respData, "status")
|
|
if !statusField.Exists() {
|
|
logx.Errorf("羽山返回数据中缺少status字段: %s", string(respData))
|
|
return "", errs.ErrNotFound
|
|
}
|
|
encryptData, aesEncrypt := crypto.AesEncrypt(respData, key)
|
|
if aesEncrypt != nil {
|
|
return "", errs.ErrSystem
|
|
}
|
|
|
|
// 解析加密后的数据
|
|
// var response map[string]interface{}
|
|
// unmarshalErr := json.Unmarshal([]byte(encryptData), &response)
|
|
// if unmarshalErr != nil {
|
|
// return "", errs.ErrSystem
|
|
// }
|
|
|
|
// // 判断是否包含 "data" 字段
|
|
// responseData, dataOk := response["data"].([]interface{})
|
|
// if !dataOk || len(responseData) == 0 {
|
|
// return "", errs.ErrSystem
|
|
// }
|
|
|
|
// 判断 "data" 中是否包含 "maritalStatus"
|
|
// maritalStatus := response.(map[string]interface{})["maritalStatus"]
|
|
// if maritalStatus == nil {
|
|
// return "", errs.ErrSystem
|
|
// }
|
|
return encryptData, nil
|
|
}
|