2024-10-02 00:57:17 +08:00
|
|
|
package IVYZ
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-02 11:27:51 +08:00
|
|
|
"encoding/hex"
|
2025-06-14 14:54:36 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"tianyuan-api/apps/api/internal/common"
|
2024-10-02 00:57:17 +08:00
|
|
|
"tianyuan-api/apps/api/internal/svc"
|
|
|
|
"tianyuan-api/apps/api/internal/types"
|
2024-10-04 23:07:49 +08:00
|
|
|
"tianyuan-api/apps/api/internal/validator"
|
2025-06-14 14:54:36 +08:00
|
|
|
"tianyuan-api/apps/api/internal/westmodel"
|
2024-10-04 23:07:49 +08:00
|
|
|
"tianyuan-api/pkg/crypto"
|
2024-10-12 20:41:55 +08:00
|
|
|
"tianyuan-api/pkg/errs"
|
2024-10-02 00:57:17 +08:00
|
|
|
|
2025-04-02 18:45:29 +08:00
|
|
|
"github.com/tidwall/gjson"
|
2024-10-02 00:57:17 +08:00
|
|
|
"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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-13 22:17:25 +08:00
|
|
|
func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp string, err *errs.AppError) {
|
2024-10-13 20:52:47 +08:00
|
|
|
var status string
|
|
|
|
var charges bool
|
|
|
|
var remark = ""
|
2024-10-02 11:27:51 +08:00
|
|
|
secretKey, ok := l.ctx.Value("secretKey").(string)
|
|
|
|
if !ok {
|
2024-10-13 22:17:25 +08:00
|
|
|
return "", errs.ErrSystem
|
2024-10-12 20:41:55 +08:00
|
|
|
}
|
|
|
|
transactionID, ok := l.ctx.Value("transactionID").(string)
|
|
|
|
if !ok {
|
2024-10-13 22:17:25 +08:00
|
|
|
return "", errs.ErrSystem
|
2024-10-02 11:27:51 +08:00
|
|
|
}
|
2024-10-13 20:52:47 +08:00
|
|
|
userId, userIdOk := l.ctx.Value("userId").(int64)
|
|
|
|
if !userIdOk {
|
2024-10-13 22:17:25 +08:00
|
|
|
return "", errs.ErrSystem
|
2024-10-13 20:52:47 +08:00
|
|
|
}
|
|
|
|
productCode, productCodeOk := l.ctx.Value("productCode").(string)
|
|
|
|
if !productCodeOk || productCode == "" {
|
2024-10-13 22:17:25 +08:00
|
|
|
return "", errs.ErrSystem
|
2024-10-13 20:52:47 +08:00
|
|
|
}
|
2025-06-14 14:54:36 +08:00
|
|
|
|
2024-10-13 20:52:47 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
2024-10-02 11:27:51 +08:00
|
|
|
// 1、解密
|
2024-10-12 20:41:55 +08:00
|
|
|
key, decodeErr := hex.DecodeString(secretKey)
|
|
|
|
if decodeErr != nil {
|
2024-10-13 22:17:25 +08:00
|
|
|
return "", errs.ErrSystem
|
2024-10-12 20:41:55 +08:00
|
|
|
}
|
|
|
|
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
|
|
|
|
if aesDecryptErr != nil || len(decryptData) == 0 {
|
2024-10-13 22:17:25 +08:00
|
|
|
return "", errs.ErrParamDecryption
|
2024-10-02 11:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2、校验
|
|
|
|
var data validator.IVYZ5733Request
|
|
|
|
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
|
2024-10-13 22:17:25 +08:00
|
|
|
return "", errs.ErrParamValidation
|
2024-10-02 11:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3、西部加密
|
2025-06-14 14:54:36 +08:00
|
|
|
westConfig := l.svcCtx.Config.WestConfig
|
|
|
|
encryptedFields, encryptStructFieldsErr := common.EncryptStructFields(data, westConfig.Key)
|
|
|
|
if encryptStructFieldsErr != nil {
|
|
|
|
logx.Errorf("西部加密错误:%v", encryptStructFieldsErr)
|
|
|
|
return "", errs.ErrSystem
|
|
|
|
}
|
2024-10-12 20:41:55 +08:00
|
|
|
|
2024-10-13 20:52:47 +08:00
|
|
|
// 4、发送请求到西部
|
2024-10-12 20:41:55 +08:00
|
|
|
logx.Infof("交易号:%s", transactionID)
|
2025-06-14 14:54:36 +08:00
|
|
|
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.IVYZ5733FieldMapping, "data")
|
2025-02-11 12:10:30 +08:00
|
|
|
|
2025-06-16 15:19:15 +08:00
|
|
|
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G09XM02", apiRequest, l.svcCtx.Config.WestConfig.SecretId)
|
2025-04-02 18:30:48 +08:00
|
|
|
if callAPIErr != nil {
|
2025-06-14 14:54:36 +08:00
|
|
|
if callAPIErr.Code == errs.ErrDataSource.Code {
|
|
|
|
return "", callAPIErr
|
2024-10-15 20:52:51 +08:00
|
|
|
}
|
2025-06-14 14:54:36 +08:00
|
|
|
return "", callAPIErr
|
2025-04-02 18:45:29 +08:00
|
|
|
}
|
2025-06-14 14:54:36 +08:00
|
|
|
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
|
2024-10-12 20:41:55 +08:00
|
|
|
if aesEncrypt != nil {
|
2024-10-13 22:17:25 +08:00
|
|
|
return "", errs.ErrSystem
|
2024-10-12 20:41:55 +08:00
|
|
|
}
|
2025-06-14 14:54:36 +08:00
|
|
|
return encryptData, nil
|
|
|
|
}
|
|
|
|
func handleResponse(resp []byte) ([]byte, error) {
|
|
|
|
result := gjson.GetBytes(resp, "data.data")
|
|
|
|
if !result.Exists() {
|
|
|
|
return nil, fmt.Errorf("婚姻状态查询失败")
|
|
|
|
}
|
2025-04-02 18:45:29 +08:00
|
|
|
|
2025-06-14 14:54:36 +08:00
|
|
|
// 获取原始结果
|
|
|
|
rawResult := result.String()
|
2025-02-11 13:37:34 +08:00
|
|
|
|
2025-06-14 14:54:36 +08:00
|
|
|
// 根据结果转换状态码
|
|
|
|
var statusCode string
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(rawResult, "INR"):
|
|
|
|
statusCode = "0" // 匹配不成功
|
|
|
|
case strings.HasPrefix(rawResult, "IA"):
|
|
|
|
statusCode = "1" // 结婚
|
|
|
|
case strings.HasPrefix(rawResult, "IB"):
|
|
|
|
statusCode = "2" // 离婚
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("婚姻状态查询失败,未知状态码: %s", statusCode)
|
|
|
|
}
|
2025-02-11 13:37:34 +08:00
|
|
|
|
2025-06-14 14:54:36 +08:00
|
|
|
// 构建新的返回结果
|
|
|
|
response := map[string]string{
|
|
|
|
"status": statusCode,
|
|
|
|
}
|
|
|
|
// 序列化为JSON
|
|
|
|
jsonResponse, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("序列化结果失败: %v", err)
|
|
|
|
}
|
|
|
|
return jsonResponse, nil
|
2024-10-02 00:57:17 +08:00
|
|
|
}
|