This commit is contained in:
Mrx
2026-01-23 18:58:40 +08:00
parent 82959f74a8
commit 791147e520

View File

@@ -4,13 +4,14 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"tyapi-server/internal/domains/api/dto"
"tyapi-server/internal/domains/api/services/processors"
"tyapi-server/internal/infrastructure/external/shumai"
)
// ProcessYYSYBE08Request YYSYBE08 API处理方法 - 使用数脉二要素验证
// ProcessYYSYBE08Request YYSYBE08 API处理方法 - 使用阿里云二要素验证
func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
var paramsDto dto.YYSYBE08Req
if err := json.Unmarshal(params, &paramsDto); err != nil {
@@ -20,90 +21,84 @@ func ProcessYYSYBE08Request(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,
"name": paramsDto.Name,
}
// 走政务接口 - 使用 app_id2 和 app_secret2
//走政务接口 - 使用 app_id2 和 app_secret2
deps.ShumaiService.UseGovernment()
// 以表单方式调用数脉 API参数在 CallAPIForm 内转为 application/x-www-form-urlencoded
apiPath := "/v4/id_card/check"
apiPath := "/v4/id_card/check" // 接口路径,根据数脉文档填写(如 v4/xxx
respBytes, err := deps.ShumaiService.CallAPIForm(ctx, apiPath, reqFormData)
if err != nil {
// 处理错误响应
var errorCode int
var errorMsg string
// 从错误中提取数脉错误信息
if shumaiErr := shumai.GetShumaiError(err); shumaiErr != nil {
errorCode = parseCodeToInt(shumaiErr.Code)
errorMsg = shumaiErr.Message
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 {
// 根据错误类型设置默认错误码
if errors.Is(err, shumai.ErrNotFound) {
errorCode = 404
errorMsg = "请求资源不存在"
} else if errors.Is(err, shumai.ErrDatasource) {
errorCode = 501
errorMsg = "第三方服务异常"
} else if errors.Is(err, shumai.ErrSystem) {
errorCode = 500
errorMsg = "系统内部错误,请联系服务商"
} else {
errorCode = 500
errorMsg = "系统内部错误,请联系服务商"
}
// 其他未知错误
return nil, errors.Join(processors.ErrSystem, err)
}
// 构建错误响应
errorResponse := map[string]interface{}{
"msg": errorMsg,
"success": false,
"code": errorCode,
"data": map[string]interface{}{},
}
return json.Marshal(errorResponse)
}
// 解析数脉响应数据
var shumaiData 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"`
// 解析数脉响应
var alicloudResponse struct {
Msg string `json:"msg"`
Success bool `json:"success"`
Code int `json:"code"`
Data struct {
Birthday string `json:"birthday"`
Result int `json:"result"`
Address string `json:"address"`
OrderNo string `json:"orderNo"`
Sex string `json:"sex"`
Desc string `json:"desc"`
} `json:"data"`
}
if err := json.Unmarshal(respBytes, &shumaiData); err != nil {
// 解析失败,返回系统错误
errorResponse := map[string]interface{}{
"msg": "响应解析失败",
"success": false,
"code": 500,
"data": map[string]interface{}{},
if err := json.Unmarshal(respBytes, &alicloudResponse); err != nil {
return nil, errors.Join(processors.ErrSystem, err)
}
// 检查响应状态
if alicloudResponse.Code != 200 && alicloudResponse.Code != 400 {
return nil, fmt.Errorf("%s: %s", processors.ErrDatasource, alicloudResponse.Msg)
}
// 构建返回结果
resultCode := "0XXX" // 默认成功
resultMsg := "验证通过"
verifyResult := "一致"
if alicloudResponse.Code == 400 {
resultCode = "5XXX"
resultMsg = "请输入有效的身份证号码"
verifyResult = "不一致"
} else {
if alicloudResponse.Data.Result != 0 {
// 验证失败
resultCode = "5XXX"
resultMsg = "身份证号不匹配"
verifyResult = "不一致"
}
return json.Marshal(errorResponse)
}
// 构建成功响应
// 构建最终响应结构
response := map[string]interface{}{
"msg": "成功",
"success": true,
"code": 200,
"data": map[string]interface{}{
"result": shumaiData.Result,
"order_no": shumaiData.OrderNo,
"desc": shumaiData.Desc,
"sex": shumaiData.Sex,
"birthday": shumaiData.Birthday,
"address": shumaiData.Address,
"ctidRequest": map[string]interface{}{
"ctidAuth": map[string]interface{}{
"resultCode": resultCode,
"resultMsg": resultMsg,
"name": paramsDto.Name,
"idCard": paramsDto.IDCard,
"verifyResult": verifyResult,
},
},
}
// 返回JSON格式的响应
return json.Marshal(response)
}