f
This commit is contained in:
@@ -4,10 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"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/shumai"
|
"tyapi-server/internal/infrastructure/external/alicloud"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ProcessIVYZN2P8Request IVYZN2P8 身份证实名认证政务版 API处理方法
|
// ProcessIVYZN2P8Request IVYZN2P8 身份证实名认证政务版 API处理方法
|
||||||
@@ -20,36 +22,80 @@ func ProcessIVYZN2P8Request(ctx context.Context, params []byte, deps *processors
|
|||||||
if err := deps.Validator.ValidateStruct(paramsDto); err != nil {
|
if err := deps.Validator.ValidateStruct(paramsDto); err != nil {
|
||||||
return nil, errors.Join(processors.ErrInvalidParam, err)
|
return nil, errors.Join(processors.ErrInvalidParam, err)
|
||||||
}
|
}
|
||||||
reqFormData := map[string]interface{}{
|
|
||||||
"idcard": paramsDto.IDCard,
|
reqData := map[string]interface{}{
|
||||||
"name": paramsDto.Name,
|
"name": paramsDto.Name,
|
||||||
|
"idcard": paramsDto.IDCard,
|
||||||
|
}
|
||||||
|
respBytes, err := deps.AlicloudService.CallAPI("api-mall/api/id_card/check", reqData)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, alicloud.ErrDatasource) {
|
||||||
|
return nil, errors.Join(processors.ErrDatasource, err)
|
||||||
|
}
|
||||||
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 以表单方式调用数脉 API;参数在 CallAPIForm 内转为 application/x-www-form-urlencoded
|
// 对齐 yysybe08test 的原始响应结构,取 data 字段映射为 ivyzn2p8 返回
|
||||||
apiPath := "/v4/id_card/check" // 接口路径,根据数脉文档填写(如 v4/xxx)
|
var aliyunData struct {
|
||||||
|
Code int `json:"code"`
|
||||||
// 先尝试使用政务接口(app_id2 和 app_secret2)
|
Data struct {
|
||||||
respBytes, err := deps.ShumaiService.CallAPIForm(ctx, apiPath, reqFormData, true)
|
Birthday string `json:"birthday"`
|
||||||
if err != nil {
|
Result interface{} `json:"result"`
|
||||||
// 使用实时接口(app_id 和 app_secret)重试
|
Address string `json:"address"`
|
||||||
respBytes, err = deps.ShumaiService.CallAPIForm(ctx, apiPath, reqFormData, false)
|
OrderNo string `json:"orderNo"`
|
||||||
// 如果重试后仍然失败,返回错误
|
Sex string `json:"sex"`
|
||||||
if err != nil {
|
Desc string `json:"desc"`
|
||||||
if errors.Is(err, shumai.ErrNotFound) {
|
} `json:"data"`
|
||||||
// 查无记录情况
|
Result interface{} `json:"result"`
|
||||||
return nil, errors.Join(processors.ErrNotFound, err)
|
Desc string `json:"desc"`
|
||||||
} else if errors.Is(err, shumai.ErrDatasource) {
|
}
|
||||||
// 数据源错误
|
if err := json.Unmarshal(respBytes, &aliyunData); err != nil {
|
||||||
return nil, errors.Join(processors.ErrDatasource, err)
|
return nil, errors.Join(processors.ErrSystem, err)
|
||||||
} else if errors.Is(err, shumai.ErrSystem) {
|
}
|
||||||
// 系统错误
|
|
||||||
return nil, errors.Join(processors.ErrSystem, err)
|
rawResult := aliyunData.Result
|
||||||
} else {
|
rawDesc := aliyunData.Desc
|
||||||
// 其他未知错误
|
if aliyunData.Code == 200 {
|
||||||
return nil, errors.Join(processors.ErrSystem, err)
|
rawResult = aliyunData.Data.Result
|
||||||
}
|
rawDesc = aliyunData.Data.Desc
|
||||||
|
}
|
||||||
|
|
||||||
|
response := map[string]interface{}{
|
||||||
|
"result": normalizeResult(rawResult),
|
||||||
|
"order_no": aliyunData.Data.OrderNo,
|
||||||
|
"desc": rawDesc,
|
||||||
|
"sex": aliyunData.Data.Sex,
|
||||||
|
"birthday": aliyunData.Data.Birthday,
|
||||||
|
"address": aliyunData.Data.Address,
|
||||||
|
}
|
||||||
|
return json.Marshal(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeResult(v interface{}) int {
|
||||||
|
switch r := v.(type) {
|
||||||
|
case float64:
|
||||||
|
return int(r)
|
||||||
|
case int:
|
||||||
|
return r
|
||||||
|
case int32:
|
||||||
|
return int(r)
|
||||||
|
case int64:
|
||||||
|
return int(r)
|
||||||
|
case json.Number:
|
||||||
|
n, err := r.Int64()
|
||||||
|
if err == nil {
|
||||||
|
return int(n)
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
s := strings.TrimSpace(r)
|
||||||
|
if s == "" {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
n, err := strconv.Atoi(s)
|
||||||
|
if err == nil {
|
||||||
|
return n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 默认按不一致处理
|
||||||
return respBytes, nil
|
return 1
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user