f
This commit is contained in:
@@ -4,10 +4,13 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"tyapi-server/internal/domains/api/dto"
|
||||
"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处理方法
|
||||
@@ -21,44 +24,48 @@ func ProcessQCXG9P1CRequest(ctx context.Context, params []byte, deps *processors
|
||||
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{}{
|
||||
"idCard": encryptedIDCard,
|
||||
"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
|
||||
"id_card": paramsDto.IDCard,
|
||||
}
|
||||
|
||||
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 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)
|
||||
} else {
|
||||
return nil, errors.Join(processors.ErrSystem, err)
|
||||
}
|
||||
}
|
||||
|
||||
// 将响应数据转换为 JSON 字节
|
||||
respBytes, err := json.Marshal(respData)
|
||||
// 使用 gjson 检查并转换 vehicleCount 字段
|
||||
vehicleCountResult := gjson.GetBytes(respBytes, "vehicleCount")
|
||||
if vehicleCountResult.Exists() && vehicleCountResult.Type == gjson.String {
|
||||
// 如果是字符串类型,转换为整数
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user