This commit is contained in:
Mrx
2026-01-23 18:18:49 +08:00
parent 78035ca1ab
commit 9b223c996d

View File

@@ -8,6 +8,7 @@ import (
"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"
) )
// ProcessYYSYBE08Request YYSYBE08 API处理方法 - 使用阿里云二要素验证 // ProcessYYSYBE08Request YYSYBE08 API处理方法 - 使用阿里云二要素验证
@@ -20,56 +21,95 @@ func ProcessYYSYBE08Request(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{}{
// 调用阿里云二要素验证API
reqData := map[string]interface{}{
"name": paramsDto.Name,
"idcard": paramsDto.IDCard, "idcard": paramsDto.IDCard,
"name": paramsDto.Name,
} }
//走政务接口 - 使用 app_id2 和 app_secret2
respBytes, err := deps.AlicloudService.CallAPI("api-mall/api/id_card/check", reqData) deps.ShumaiService.UseGovernment()
// 以表单方式调用数脉 API参数在 CallAPIForm 内转为 application/x-www-form-urlencoded
apiPath := "/v4/id_card/check" // 接口路径,根据数脉文档填写(如 v4/xxx
respBytes, err := deps.ShumaiService.CallAPIForm(ctx, apiPath, reqFormData)
if err != nil { if err != nil {
return nil, errors.Join(processors.ErrSystem, err) 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)
}
} }
// 解析阿里云响应 // 解析数脉响应 - 兼容完整响应格式或仅data格式
var alicloudResponse struct { var shumaiResponse struct {
Msg string `json:"msg"` Msg string `json:"msg"`
Success bool `json:"success"` Success bool `json:"success"`
Code int `json:"code"` Code int `json:"code"`
Data struct { Data struct {
Birthday string `json:"birthday"`
Result int `json:"result"` Result int `json:"result"`
Address string `json:"address"` OrderNo string `json:"order_no"`
OrderNo string `json:"orderNo"`
Sex string `json:"sex"`
Desc string `json:"desc"` Desc string `json:"desc"`
Sex string `json:"sex"`
Birthday string `json:"birthday"`
Address string `json:"address"`
} `json:"data"` } `json:"data"`
} }
if err := json.Unmarshal(respBytes, &alicloudResponse); err != nil { // 尝试解析完整响应格式
return nil, errors.Join(processors.ErrSystem, err) if err := json.Unmarshal(respBytes, &shumaiResponse); err != nil {
// 如果解析失败可能是只返回了data部分尝试直接解析为data结构
var dataOnly struct {
Result int `json:"result"`
OrderNo string `json:"order_no"`
Desc string `json:"desc"`
Sex string `json:"sex"`
Birthday string `json:"birthday"`
Address string `json:"address"`
}
if err2 := json.Unmarshal(respBytes, &dataOnly); err2 != nil {
return nil, errors.Join(processors.ErrSystem, fmt.Errorf("响应解析失败: %w", err))
}
// 将dataOnly赋值给shumaiResponse.Data
shumaiResponse.Data = dataOnly
shumaiResponse.Code = 200 // 默认成功
shumaiResponse.Success = true
} }
// 检查响应状态 // 检查响应状态
if alicloudResponse.Code != 200 && alicloudResponse.Code != 400 { if shumaiResponse.Code != 200 && shumaiResponse.Code != 400 {
return nil, fmt.Errorf("%s: %s", processors.ErrDatasource, alicloudResponse.Msg) msg := shumaiResponse.Msg
if msg == "" {
msg = "数据源异常"
}
return nil, fmt.Errorf("%s: %s", processors.ErrDatasource, msg)
} }
// 构建返回结果 // 构建返回结果
resultCode := "0XXX" // 默认成功 resultCode := "0XXX" // 默认成功
resultMsg := "验证通过" resultMsg := "验证通过"
verifyResult := "一致" verifyResult := "一致"
if alicloudResponse.Code == 400 {
if shumaiResponse.Code == 400 {
resultCode = "5XXX" resultCode = "5XXX"
resultMsg = "请输入有效的身份证号码" resultMsg = "请输入有效的身份证号码"
verifyResult = "不一致" verifyResult = "不一致"
} else { } else {
if alicloudResponse.Data.Result != 0 { // result: 0表示一致非0表示不一致
if shumaiResponse.Data.Result != 0 {
// 验证失败 // 验证失败
resultCode = "5XXX" resultCode = "5XXX"
resultMsg = "身份证号不匹配" resultMsg = "身份证号不匹配"
verifyResult = "不一致" verifyResult = "不一致"
// 如果desc字段有值使用desc作为resultMsg
if shumaiResponse.Data.Desc != "" {
resultMsg = shumaiResponse.Data.Desc
}
} }
} }
// 构建最终响应结构 // 构建最终响应结构