接入阿里云二要素

This commit is contained in:
2025-08-04 17:16:38 +08:00
parent bce55a3bb2
commit f482f0a6e8
15 changed files with 1012 additions and 100 deletions

View File

@@ -3,13 +3,10 @@ package qygl
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"tyapi-server/internal/domains/api/dto"
"tyapi-server/internal/domains/api/services/processors"
"tyapi-server/internal/infrastructure/external/westdex"
"github.com/tidwall/gjson"
)
@@ -74,58 +71,55 @@ func ProcessQYGL23T7Request(ctx context.Context, params []byte, deps *processors
return createStatusResponse(1), nil
}
// 天眼查三要素验证通过,继续调用WestDex身份证二要素验证
// 加密姓名和身份证号
encryptedName, err := deps.WestDexService.Encrypt(paramsDto.LegalPerson)
if err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
encryptedIDCard, err := deps.WestDexService.Encrypt(paramsDto.IDCard)
if err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
// 构建WestDex身份证二要素验证请求参数参考yysybe08_processor.go
// 天眼查三要素验证通过,继续调用阿里云身份证二要素验证
// 构建阿里云二要素验证请求参数
reqData := map[string]interface{}{
"data": map[string]interface{}{
"xM": encryptedName,
"gMSFZHM": encryptedIDCard,
"customerNumber": deps.WestDexService.GetConfig().SecretId,
"timeStamp": fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond)),
},
"name": paramsDto.LegalPerson,
"idcard": paramsDto.IDCard,
}
// 调用WestDex身份证二要素验证API
respBytes, err := deps.WestDexService.CallAPI("layoutIdcard", reqData)
// 调用阿里云二要素验证API
respBytes, err := deps.AlicloudService.CallAPI("api-mall/api/id_card/check", reqData)
if err != nil {
if !errors.Is(err, westdex.ErrDatasource) {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
}
// 使用gjson获取resultCode
resultCode := gjson.GetBytes(respBytes, "ctidRequest.ctidAuth.resultCode")
if !resultCode.Exists() {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
// 获取resultCode的第一个字符
resultCodeStr := resultCode.String()
if len(resultCodeStr) == 0 {
// 解析阿里云响应
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, &alicloudResponse); err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
firstChar := string(resultCodeStr[0])
if firstChar != "0" && firstChar != "5" {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
// 检查响应状态
if alicloudResponse.Code != 200 && alicloudResponse.Code != 400 {
return nil, fmt.Errorf("%s: %s", processors.ErrDatasource, alicloudResponse.Msg)
}
if firstChar == "0" {
return createStatusResponse(0), nil
} else if firstChar == "5" {
// 根据阿里云响应结果返回状态
if alicloudResponse.Code == 400 {
// 身份证号格式错误返回状态2
return createStatusResponse(2), nil
} else {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
if alicloudResponse.Data.Result == 0 {
// 验证通过返回状态0
return createStatusResponse(0), nil
} else {
// 验证失败返回状态2
return createStatusResponse(2), nil
}
}
}