f
This commit is contained in:
@@ -4,14 +4,13 @@ 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, ¶msDto); err != nil {
|
||||
@@ -21,50 +20,57 @@ 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
|
||||
deps.ShumaiService.UseGovernment()
|
||||
|
||||
// 以表单方式调用数脉 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)
|
||||
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)
|
||||
// 处理错误响应
|
||||
var errorCode int
|
||||
var errorMsg string
|
||||
|
||||
// 从错误中提取数脉错误信息
|
||||
if shumaiErr := shumai.GetShumaiError(err); shumaiErr != nil {
|
||||
errorCode = parseCodeToInt(shumaiErr.Code)
|
||||
errorMsg = shumaiErr.Message
|
||||
} else {
|
||||
// 其他未知错误
|
||||
return nil, errors.Join(processors.ErrSystem, err)
|
||||
// 根据错误类型设置默认错误码
|
||||
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 = "系统内部错误,请联系服务商"
|
||||
}
|
||||
}
|
||||
|
||||
// 解析数脉响应 - 兼容完整响应格式或仅data格式
|
||||
var shumaiResponse struct {
|
||||
Msg string `json:"msg"`
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
Data 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"`
|
||||
} `json:"data"`
|
||||
// 构建错误响应
|
||||
errorResponse := map[string]interface{}{
|
||||
"msg": errorMsg,
|
||||
"success": false,
|
||||
"code": errorCode,
|
||||
"data": map[string]interface{}{},
|
||||
}
|
||||
|
||||
// 尝试解析完整响应格式
|
||||
if err := json.Unmarshal(respBytes, &shumaiResponse); err != nil {
|
||||
// 如果解析失败,可能是只返回了data部分,尝试直接解析为data结构
|
||||
var dataOnly struct {
|
||||
return json.Marshal(errorResponse)
|
||||
}
|
||||
|
||||
// 解析数脉响应数据
|
||||
var shumaiData struct {
|
||||
Result int `json:"result"`
|
||||
OrderNo string `json:"order_no"`
|
||||
Desc string `json:"desc"`
|
||||
@@ -72,81 +78,32 @@ func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors
|
||||
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))
|
||||
|
||||
if err := json.Unmarshal(respBytes, &shumaiData); err != nil {
|
||||
// 解析失败,返回系统错误
|
||||
errorResponse := map[string]interface{}{
|
||||
"msg": "响应解析失败",
|
||||
"success": false,
|
||||
"code": 500,
|
||||
"data": map[string]interface{}{},
|
||||
}
|
||||
// 将dataOnly赋值给shumaiResponse.Data
|
||||
shumaiResponse.Data = dataOnly
|
||||
shumaiResponse.Code = 200 // 默认成功
|
||||
shumaiResponse.Success = true
|
||||
return json.Marshal(errorResponse)
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
if shumaiResponse.Code != 200 {
|
||||
// code != 200 表示请求失败,返回错误
|
||||
msg := shumaiResponse.Msg
|
||||
if msg == "" {
|
||||
msg = "数据源异常"
|
||||
}
|
||||
return nil, fmt.Errorf("%s: %s", processors.ErrDatasource, msg)
|
||||
}
|
||||
|
||||
// code == 200 时,根据 data.result 判断验证结果
|
||||
// result: 0 一致(收费),1 不一致(收费),2 无记录(预留)
|
||||
resultCode := "0XXX" // 默认成功
|
||||
resultMsg := "验证通过"
|
||||
verifyResult := "一致"
|
||||
|
||||
switch shumaiResponse.Data.Result {
|
||||
case 0:
|
||||
// 一致(验证通过)
|
||||
resultCode = "0XXX"
|
||||
resultMsg = "验证通过"
|
||||
verifyResult = "一致"
|
||||
// 如果desc字段有值,使用desc作为resultMsg
|
||||
if shumaiResponse.Data.Desc != "" {
|
||||
resultMsg = shumaiResponse.Data.Desc
|
||||
}
|
||||
case 1:
|
||||
// 不一致(验证失败)
|
||||
resultCode = "5XXX"
|
||||
resultMsg = "身份证号不匹配"
|
||||
verifyResult = "不一致"
|
||||
// 如果desc字段有值,使用desc作为resultMsg
|
||||
if shumaiResponse.Data.Desc != "" {
|
||||
resultMsg = shumaiResponse.Data.Desc
|
||||
}
|
||||
case 2:
|
||||
// 无记录
|
||||
resultCode = "5XXX"
|
||||
resultMsg = "无记录"
|
||||
verifyResult = "不一致"
|
||||
// 如果desc字段有值,使用desc作为resultMsg
|
||||
if shumaiResponse.Data.Desc != "" {
|
||||
resultMsg = shumaiResponse.Data.Desc
|
||||
}
|
||||
default:
|
||||
// 其他未知状态,按不一致处理
|
||||
resultCode = "5XXX"
|
||||
resultMsg = "验证失败"
|
||||
verifyResult = "不一致"
|
||||
if shumaiResponse.Data.Desc != "" {
|
||||
resultMsg = shumaiResponse.Data.Desc
|
||||
}
|
||||
}
|
||||
// 构建最终响应结构
|
||||
// 构建成功响应
|
||||
response := map[string]interface{}{
|
||||
"ctidRequest": map[string]interface{}{
|
||||
"ctidAuth": map[string]interface{}{
|
||||
"resultCode": resultCode,
|
||||
"resultMsg": resultMsg,
|
||||
"name": paramsDto.Name,
|
||||
"idCard": paramsDto.IDCard,
|
||||
"verifyResult": verifyResult,
|
||||
},
|
||||
"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,
|
||||
},
|
||||
}
|
||||
|
||||
// 返回JSON格式的响应
|
||||
return json.Marshal(response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user