up
This commit is contained in:
@@ -4,13 +4,14 @@ 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/shujubao"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ProcessQYGL6S1BRequest QYGL6S1B API处理方法 - 董监高司法综合信息核验
|
// ProcessQYGL6S1BRequest QYGL6S1B API处理方法 - 董监高司法综合信息核验(使用数据宝服务)
|
||||||
|
|
||||||
func ProcessQYGL6S1BRequest(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
|
func ProcessQYGL6S1BRequest(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
|
||||||
var paramsDto dto.QYGL6S1BReq
|
var paramsDto dto.QYGL6S1BReq
|
||||||
@@ -22,31 +23,106 @@ func ProcessQYGL6S1BRequest(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)
|
// 构建数据宝入参
|
||||||
|
reqParams := map[string]interface{}{
|
||||||
|
"key": "1cce582f0a6f3ca40de80f1bea9b9698",
|
||||||
|
"idcard": paramsDto.IDCard,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用数据宝API
|
||||||
|
apiPath := "/communication/personal/10166"
|
||||||
|
data, err := deps.ShujubaoService.CallAPI(ctx, apiPath, reqParams)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, shujubao.ErrDatasource) {
|
||||||
|
return nil, errors.Join(processors.ErrDatasource, err)
|
||||||
|
}
|
||||||
|
if errors.Is(err, shujubao.ErrQueryEmpty) {
|
||||||
|
return nil, errors.Join(processors.ErrNotFound, err)
|
||||||
|
}
|
||||||
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析响应中的 JSON 字符串(使用 RecursiveParse)
|
||||||
|
parsedResp, err := RecursiveParse(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Join(processors.ErrSystem, err)
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建API调用参数
|
// 提取 resultData 字段
|
||||||
reqData := map[string]interface{}{
|
resultData, ok := parsedResp.(map[string]interface{})
|
||||||
"idCard": encryptedIdCard,
|
if !ok {
|
||||||
"authorized": paramsDto.Authorized,
|
return nil, errors.Join(processors.ErrSystem, errors.New("invalid response format"))
|
||||||
}
|
}
|
||||||
|
|
||||||
respData, err := deps.ZhichaService.CallAPI(ctx, "ZCI043", reqData)
|
resultDataValue, exists := resultData["resultData"]
|
||||||
if err != nil {
|
if !exists {
|
||||||
if errors.Is(err, zhicha.ErrDatasource) {
|
// 如果 resultData 不存在,说明查询为空,返回空的业务数据结构
|
||||||
return nil, errors.Join(processors.ErrDatasource, err)
|
emptyResult := map[string]interface{}{
|
||||||
} else {
|
"caseInfoList": []interface{}{},
|
||||||
return nil, errors.Join(processors.ErrSystem, err)
|
"legRepInfoList": []interface{}{},
|
||||||
|
"lossPromiseList": []interface{}{},
|
||||||
|
"performerList": []interface{}{},
|
||||||
|
"ryPosPerList": []interface{}{},
|
||||||
|
"shareholderList": []interface{}{},
|
||||||
}
|
}
|
||||||
|
return json.Marshal(emptyResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将响应数据转换为JSON字节
|
// 转换数据类型:将数字字段转换为字符串
|
||||||
respBytes, err := json.Marshal(respData)
|
convertedData, err := convertDataTypes(resultDataValue)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
respBytes, err := json.Marshal(convertedData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Join(processors.ErrSystem, err)
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return respBytes, nil
|
return respBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convertDataTypes 递归转换数据类型,将数字字段转换为字符串以保持与原有格式一致
|
||||||
|
func convertDataTypes(data interface{}) (interface{}, error) {
|
||||||
|
switch v := data.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
for key, val := range v {
|
||||||
|
converted, err := convertDataTypes(val)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
v[key] = converted
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
case []interface{}:
|
||||||
|
for i, item := range v {
|
||||||
|
converted, err := convertDataTypes(item)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
v[i] = converted
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
case float64:
|
||||||
|
// 将 float64 类型转换为字符串(JSON 解析后数字默认为 float64)
|
||||||
|
if v == float64(int64(v)) {
|
||||||
|
return strconv.FormatInt(int64(v), 10), nil
|
||||||
|
}
|
||||||
|
return strconv.FormatFloat(v, 'f', -1, 64), nil
|
||||||
|
case int:
|
||||||
|
return strconv.Itoa(v), nil
|
||||||
|
case int32:
|
||||||
|
return strconv.FormatInt(int64(v), 10), nil
|
||||||
|
case int64:
|
||||||
|
return strconv.FormatInt(v, 10), nil
|
||||||
|
case string:
|
||||||
|
// 尝试解析字符串中的 JSON
|
||||||
|
var parsed interface{}
|
||||||
|
if err := json.Unmarshal([]byte(v), &parsed); err == nil {
|
||||||
|
return convertDataTypes(parsed)
|
||||||
|
}
|
||||||
|
return v, nil
|
||||||
|
default:
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user