This commit is contained in:
Mrx
2026-01-23 19:03:01 +08:00
parent 791147e520
commit da1eef618e

View File

@@ -4,14 +4,12 @@ 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"
) )
// 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 {
@@ -21,84 +19,68 @@ 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" // 接口路径,根据数脉文档填写(如 v4/xxx apiPath := "/v4/id_card/check"
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
return nil, errors.Join(processors.ErrNotFound, err) var errorMsg string
} else if errors.Is(err, shumai.ErrDatasource) { // 构建错误响应
// 数据源错误 errorResponse := map[string]interface{}{
return nil, errors.Join(processors.ErrDatasource, err) "msg": errorMsg,
} else if errors.Is(err, shumai.ErrSystem) { "success": false,
// 系统错误 "code": errorCode,
return nil, errors.Join(processors.ErrSystem, err) "data": map[string]interface{}{},
} else {
// 其他未知错误
return nil, errors.Join(processors.ErrSystem, err)
} }
return json.Marshal(errorResponse)
} }
// 解析数脉响应 // 解析数脉响应数据
var alicloudResponse struct { var shumaiData struct {
Msg string `json:"msg"` Result int `json:"result"`
Success bool `json:"success"` OrderNo string `json:"order_no"`
Code int `json:"code"` Desc string `json:"desc"`
Data struct { Sex string `json:"sex"`
Birthday string `json:"birthday"` Birthday string `json:"birthday"`
Result int `json:"result"` Address string `json:"address"`
Address string `json:"address"`
OrderNo string `json:"orderNo"`
Sex string `json:"sex"`
Desc string `json:"desc"`
} `json:"data"`
} }
if err := json.Unmarshal(respBytes, &alicloudResponse); err != nil { if err := json.Unmarshal(respBytes, &shumaiData); err != nil {
return nil, errors.Join(processors.ErrSystem, err) // 解析失败,返回系统错误
} errorResponse := map[string]interface{}{
"msg": "响应解析失败",
// 检查响应状态 "success": false,
if alicloudResponse.Code != 200 && alicloudResponse.Code != 400 { "code": 500,
return nil, fmt.Errorf("%s: %s", processors.ErrDatasource, alicloudResponse.Msg) "data": map[string]interface{}{},
}
// 构建返回结果
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{}{
"ctidRequest": map[string]interface{}{ "msg": "成功",
"ctidAuth": map[string]interface{}{ "success": true,
"resultCode": resultCode, "code": 200,
"resultMsg": resultMsg, "data": map[string]interface{}{
"name": paramsDto.Name, "result": shumaiData.Result,
"idCard": paramsDto.IDCard, "order_no": shumaiData.OrderNo,
"verifyResult": verifyResult, "desc": shumaiData.Desc,
}, "sex": shumaiData.Sex,
"birthday": shumaiData.Birthday,
"address": shumaiData.Address,
}, },
} }
// 返回JSON格式的响应
return json.Marshal(response) return json.Marshal(response)
} }