f
This commit is contained in:
@@ -4,10 +4,12 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"tyapi-server/internal/domains/api/dto"
|
||||
"tyapi-server/internal/domains/api/services/processors"
|
||||
"tyapi-server/internal/infrastructure/external/shumai"
|
||||
"tyapi-server/internal/infrastructure/external/alicloud"
|
||||
)
|
||||
|
||||
// ProcessIVYZN2P8Request IVYZN2P8 身份证实名认证政务版 API处理方法
|
||||
@@ -20,36 +22,80 @@ func ProcessIVYZN2P8Request(ctx context.Context, params []byte, deps *processors
|
||||
if err := deps.Validator.ValidateStruct(paramsDto); err != nil {
|
||||
return nil, errors.Join(processors.ErrInvalidParam, err)
|
||||
}
|
||||
reqFormData := map[string]interface{}{
|
||||
"idcard": paramsDto.IDCard,
|
||||
|
||||
reqData := map[string]interface{}{
|
||||
"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
|
||||
apiPath := "/v4/id_card/check" // 接口路径,根据数脉文档填写(如 v4/xxx)
|
||||
|
||||
// 先尝试使用政务接口(app_id2 和 app_secret2)
|
||||
respBytes, err := deps.ShumaiService.CallAPIForm(ctx, apiPath, reqFormData, true)
|
||||
if err != nil {
|
||||
// 使用实时接口(app_id 和 app_secret)重试
|
||||
respBytes, err = deps.ShumaiService.CallAPIForm(ctx, apiPath, reqFormData, false)
|
||||
// 如果重试后仍然失败,返回错误
|
||||
if err != nil {
|
||||
if errors.Is(err, shumai.ErrNotFound) {
|
||||
// 查无记录情况
|
||||
return nil, errors.Join(processors.ErrNotFound, err)
|
||||
} else if errors.Is(err, shumai.ErrDatasource) {
|
||||
// 数据源错误
|
||||
return nil, errors.Join(processors.ErrDatasource, err)
|
||||
} else if errors.Is(err, shumai.ErrSystem) {
|
||||
// 系统错误
|
||||
return nil, errors.Join(processors.ErrSystem, err)
|
||||
} else {
|
||||
// 其他未知错误
|
||||
return nil, errors.Join(processors.ErrSystem, err)
|
||||
}
|
||||
// 对齐 yysybe08test 的原始响应结构,取 data 字段映射为 ivyzn2p8 返回
|
||||
var aliyunData struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
Birthday string `json:"birthday"`
|
||||
Result interface{} `json:"result"`
|
||||
Address string `json:"address"`
|
||||
OrderNo string `json:"orderNo"`
|
||||
Sex string `json:"sex"`
|
||||
Desc string `json:"desc"`
|
||||
} `json:"data"`
|
||||
Result interface{} `json:"result"`
|
||||
Desc string `json:"desc"`
|
||||
}
|
||||
if err := json.Unmarshal(respBytes, &aliyunData); err != nil {
|
||||
return nil, errors.Join(processors.ErrSystem, err)
|
||||
}
|
||||
|
||||
rawResult := aliyunData.Result
|
||||
rawDesc := aliyunData.Desc
|
||||
if aliyunData.Code == 200 {
|
||||
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