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" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"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/shumai"
) )
// ProcessYYSYBE08Request YYSYBE08 API处理方法 - 使用数脉二要素验证 // ProcessYYSYBE08Request YYSYBE08 API处理方法 - 使用阿里云二要素验证
func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) { func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
var paramsDto dto.YYSYBE08Req var paramsDto dto.YYSYBE08Req
if err := json.Unmarshal(params, &paramsDto); err != nil { 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 { 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{}{ reqFormData := map[string]interface{}{
"idcard": paramsDto.IDCard, "idcard": paramsDto.IDCard,
"name": paramsDto.Name, "name": paramsDto.Name,
} }
//走政务接口 - 使用 app_id2 和 app_secret2
// 走政务接口 - 使用 app_id2 和 app_secret2
deps.ShumaiService.UseGovernment() deps.ShumaiService.UseGovernment()
// 以表单方式调用数脉 API参数在 CallAPIForm 内转为 application/x-www-form-urlencoded // 以表单方式调用数脉 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) respBytes, err := deps.ShumaiService.CallAPIForm(ctx, apiPath, reqFormData)
if err != nil { if err != nil {
// 处理错误响应 if errors.Is(err, shumai.ErrNotFound) {
var errorCode int // 查无记录情况
var errorMsg string return nil, errors.Join(processors.ErrNotFound, err)
} else if errors.Is(err, shumai.ErrDatasource) {
// 从错误中提取数脉错误信息 // 数据源错误
if shumaiErr := shumai.GetShumaiError(err); shumaiErr != nil { return nil, errors.Join(processors.ErrDatasource, err)
errorCode = parseCodeToInt(shumaiErr.Code) } else if errors.Is(err, shumai.ErrSystem) {
errorMsg = shumaiErr.Message // 系统错误
return nil, errors.Join(processors.ErrSystem, err)
} else { } else {
// 根据错误类型设置默认错误码 // 其他未知错误
if errors.Is(err, shumai.ErrNotFound) { return nil, errors.Join(processors.ErrSystem, err)
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 = "系统内部错误,请联系服务商"
}
} }
// 构建错误响应
errorResponse := map[string]interface{}{
"msg": errorMsg,
"success": false,
"code": errorCode,
"data": map[string]interface{}{},
}
return json.Marshal(errorResponse)
} }
// 解析数脉响应数据 // 解析数脉响应
var shumaiData struct { var alicloudResponse struct {
Result int `json:"result"` Msg string `json:"msg"`
OrderNo string `json:"order_no"` Success bool `json:"success"`
Desc string `json:"desc"` Code int `json:"code"`
Sex string `json:"sex"` Data struct {
Birthday string `json:"birthday"` Birthday string `json:"birthday"`
Address string `json:"address"` 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 { if err := json.Unmarshal(respBytes, &alicloudResponse); err != nil {
// 解析失败,返回系统错误 return nil, errors.Join(processors.ErrSystem, err)
errorResponse := map[string]interface{}{ }
"msg": "响应解析失败",
"success": false, // 检查响应状态
"code": 500, if alicloudResponse.Code != 200 && alicloudResponse.Code != 400 {
"data": map[string]interface{}{}, 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{}{ response := map[string]interface{}{
"msg": "成功", "ctidRequest": map[string]interface{}{
"success": true, "ctidAuth": map[string]interface{}{
"code": 200, "resultCode": resultCode,
"data": map[string]interface{}{ "resultMsg": resultMsg,
"result": shumaiData.Result, "name": paramsDto.Name,
"order_no": shumaiData.OrderNo, "idCard": paramsDto.IDCard,
"desc": shumaiData.Desc, "verifyResult": verifyResult,
"sex": shumaiData.Sex, },
"birthday": shumaiData.Birthday,
"address": shumaiData.Address,
}, },
} }
// 返回JSON格式的响应
return json.Marshal(response) return json.Marshal(response)
} }