This commit is contained in:
2026-01-06 17:42:11 +08:00

View File

@@ -4,10 +4,13 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"strconv"
"tyapi-server/internal/domains/api/dto" "tyapi-server/internal/domains/api/dto"
"tyapi-server/internal/domains/api/services/processors" "tyapi-server/internal/domains/api/services/processors"
"tyapi-server/internal/infrastructure/external/zhicha" "tyapi-server/internal/infrastructure/external/jiguang"
"github.com/tidwall/gjson"
) )
// ProcessQCXG9P1CRequest QCXG9P1C API处理方法 // ProcessQCXG9P1CRequest QCXG9P1C API处理方法
@@ -21,44 +24,48 @@ func ProcessQCXG9P1CRequest(ctx context.Context, params []byte, deps *processors
return nil, errors.Join(processors.ErrInvalidParam, err) return nil, errors.Join(processors.ErrInvalidParam, err)
} }
encryptedIDCard, err := deps.ZhichaService.Encrypt(paramsDto.IDCard) // 构建请求参数
if err != nil {
return nil, errors.Join(processors.ErrSystem, err)
}
reqData := map[string]interface{}{ reqData := map[string]interface{}{
"idCard": encryptedIDCard, "id_card": paramsDto.IDCard,
"authorized": paramsDto.Authorized,
}
if paramsDto.Name != "" {
encryptedName, err := deps.ZhichaService.Encrypt(paramsDto.Name)
if err != nil {
return nil, errors.Join(processors.ErrSystem, err)
}
reqData["name"] = encryptedName
}
// 如果传了 vehicleType则添加到请求数据中
if paramsDto.VehicleType != "" {
reqData["vehicleType"] = paramsDto.VehicleType
}
if paramsDto.UserType != "" {
reqData["userType"] = paramsDto.UserType
} }
respData, err := deps.ZhichaService.CallAPI(ctx, "ZCI051", reqData) // 调用极光API
// apiCode: vehicle-person-vehicles (用于请求头)
// apiPath: vehicle/person-vehicles (用于URL路径)
respBytes, err := deps.JiguangService.CallAPI(ctx, "vehicle-person-vehicles", "vehicle/person-vehicles", reqData)
if err != nil { if err != nil {
if errors.Is(err, zhicha.ErrDatasource) { // 根据错误类型返回相应的错误
if errors.Is(err, jiguang.ErrNotFound) {
return nil, errors.Join(processors.ErrNotFound, err)
} else if errors.Is(err, jiguang.ErrDatasource) {
return nil, errors.Join(processors.ErrDatasource, err) return nil, errors.Join(processors.ErrDatasource, err)
} else { } else {
return nil, errors.Join(processors.ErrSystem, err) return nil, errors.Join(processors.ErrSystem, err)
} }
} }
// 将响应数据转换为 JSON // 使用 gjson 检查并转换 vehicleCount
respBytes, err := json.Marshal(respData) vehicleCountResult := gjson.GetBytes(respBytes, "vehicleCount")
if err != nil { if vehicleCountResult.Exists() && vehicleCountResult.Type == gjson.String {
return nil, errors.Join(processors.ErrSystem, err) // 如果是字符串类型,转换为整数
vehicleCountInt, err := strconv.Atoi(vehicleCountResult.String())
if err != nil {
return nil, errors.Join(processors.ErrSystem, err)
}
// 解析 JSON 并修改 vehicleCount 字段
var respData map[string]interface{}
if err := json.Unmarshal(respBytes, &respData); err != nil {
return nil, errors.Join(processors.ErrSystem, err)
}
respData["vehicleCount"] = vehicleCountInt
// 重新序列化为JSON并返回
resultBytes, err := json.Marshal(respData)
if err != nil {
return nil, errors.Join(processors.ErrSystem, err)
}
return resultBytes, nil
} }
// 如果 vehicleCount 不存在或不是字符串,直接返回原始响应
return respBytes, nil return respBytes, nil
} }