接入阿里云二要素

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,6 +3,7 @@ package processors
import (
"context"
"tyapi-server/internal/application/api/commands"
"tyapi-server/internal/infrastructure/external/alicloud"
"tyapi-server/internal/infrastructure/external/tianyancha"
"tyapi-server/internal/infrastructure/external/westdex"
"tyapi-server/internal/infrastructure/external/yushan"
@@ -19,6 +20,7 @@ type ProcessorDependencies struct {
WestDexService *westdex.WestDexService
YushanService *yushan.YushanService
TianYanChaService *tianyancha.TianYanChaService
AlicloudService *alicloud.AlicloudService
Validator interfaces.RequestValidator
CombService CombServiceInterface // Changed to interface to break import cycle
Options *commands.ApiCallOptions // 添加Options支持
@@ -29,6 +31,7 @@ func NewProcessorDependencies(
westDexService *westdex.WestDexService,
yushanService *yushan.YushanService,
tianYanChaService *tianyancha.TianYanChaService,
alicloudService *alicloud.AlicloudService,
validator interfaces.RequestValidator,
combService CombServiceInterface, // Changed to interface
) *ProcessorDependencies {
@@ -36,6 +39,7 @@ func NewProcessorDependencies(
WestDexService: westDexService,
YushanService: yushanService,
TianYanChaService: tianYanChaService,
AlicloudService: alicloudService,
Validator: validator,
CombService: combService,
Options: nil, // 初始化为nil在调用时设置

View File

@@ -36,9 +36,10 @@ func ProcessFLXG0V4BRequest(ctx context.Context, params []byte, deps *processors
reqData := map[string]interface{}{
"data": map[string]interface{}{
"name": encryptedName,
"idcard": encryptedIDCard,
"inquired_auth": paramsDto.AuthDate,
"name": encryptedName,
"idcard": encryptedIDCard,
"inquired_auth": paramsDto.AuthDate,
// "auth_authorizeFileCode": paramsDto.AuthAuthorizeFileCode,
},
}
respBytes, err := deps.WestDexService.CallAPI("G22SC01", reqData)

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
}
}
}

View File

@@ -3,16 +3,13 @@ package yysy
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"
)
// 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 {
@@ -23,31 +20,70 @@ func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors
return nil, fmt.Errorf("%s: %w", processors.ErrInvalidParam, err)
}
encryptedName, err := deps.WestDexService.Encrypt(paramsDto.Name)
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)
}
// 调用阿里云二要素验证API
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.Name,
"idcard": paramsDto.IDCard,
}
respBytes, err := deps.AlicloudService.CallAPI("api-mall/api/id_card/check", reqData)
if err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
// 解析阿里云响应
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)
}
// 检查响应状态
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 = "不一致"
}
}
// 构建最终响应结构
response := map[string]interface{}{
"ctidRequest": map[string]interface{}{
"ctidAuth": map[string]interface{}{
"resultCode": resultCode,
"resultMsg": resultMsg,
"name": paramsDto.Name,
"idCard": paramsDto.IDCard,
"verifyResult": verifyResult,
},
},
}
respBytes, err := deps.WestDexService.CallAPI("layoutIdcard", reqData)
if err != nil {
if !errors.Is(err, westdex.ErrDatasource) {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
}
return respBytes, nil
// 返回JSON格式的响应
return json.Marshal(response)
}

View File

@@ -0,0 +1,158 @@
package yysy
import (
"encoding/json"
"testing"
)
func TestYYSYBE08ResponseStructure(t *testing.T) {
// 测试响应结构构建逻辑
resultCode := "1001"
resultMsg := "验证通过"
verifyResult := "一致"
// 模拟阿里云返回result=0一致的情况
alicloudResult := 0
if alicloudResult == 0 {
// 验证成功
resultCode = "1001"
resultMsg = "验证通过"
verifyResult = "一致"
} else {
// 验证失败
resultCode = "1002"
resultMsg = "身份证号不匹配"
verifyResult = "不一致"
}
// 构建响应结构
response := map[string]interface{}{
"ctidRequest": map[string]interface{}{
"ctidAuth": map[string]interface{}{
"resultCode": resultCode,
"resultMsg": resultMsg,
"name": "张荣宏",
"idCard": "45212220000827423X",
"verifyResult": verifyResult,
},
},
}
// 序列化为JSON
jsonData, err := json.Marshal(response)
if err != nil {
t.Fatalf("JSON序列化失败: %v", err)
}
// 验证JSON结构
var parsedResponse map[string]interface{}
if err := json.Unmarshal(jsonData, &parsedResponse); err != nil {
t.Fatalf("JSON反序列化失败: %v", err)
}
// 验证字段存在
ctidRequest, exists := parsedResponse["ctidRequest"]
if !exists {
t.Fatal("响应中缺少ctidRequest字段")
}
ctidAuth, exists := ctidRequest.(map[string]interface{})["ctidAuth"]
if !exists {
t.Fatal("响应中缺少ctidAuth字段")
}
authData := ctidAuth.(map[string]interface{})
// 验证字段值
expectedFields := map[string]string{
"resultCode": "1001",
"resultMsg": "验证通过",
"name": "张荣宏",
"idCard": "45212220000827423X",
"verifyResult": "一致",
}
for field, expectedValue := range expectedFields {
if authData[field] != expectedValue {
t.Errorf("字段%s期望值为%s实际为%s", field, expectedValue, authData[field])
}
}
t.Logf("测试成功,响应结构: %s", string(jsonData))
}
func TestYYSYBE08ResponseStructure_Failure(t *testing.T) {
// 测试验证失败的情况
resultCode := "1002"
resultMsg := "身份证号不匹配"
verifyResult := "不一致"
// 模拟阿里云返回result=1不一致的情况
alicloudResult := 1
if alicloudResult == 0 {
// 验证成功
resultCode = "1001"
resultMsg = "验证通过"
verifyResult = "一致"
} else {
// 验证失败
resultCode = "1002"
resultMsg = "身份证号不匹配"
verifyResult = "不一致"
}
// 构建响应结构
response := map[string]interface{}{
"ctidRequest": map[string]interface{}{
"ctidAuth": map[string]interface{}{
"resultCode": resultCode,
"resultMsg": resultMsg,
"name": "张三",
"idCard": "110101199001011235",
"verifyResult": verifyResult,
},
},
}
// 序列化为JSON
jsonData, err := json.Marshal(response)
if err != nil {
t.Fatalf("JSON序列化失败: %v", err)
}
// 验证JSON结构
var parsedResponse map[string]interface{}
if err := json.Unmarshal(jsonData, &parsedResponse); err != nil {
t.Fatalf("JSON反序列化失败: %v", err)
}
// 验证字段存在
ctidRequest, exists := parsedResponse["ctidRequest"]
if !exists {
t.Fatal("响应中缺少ctidRequest字段")
}
ctidAuth, exists := ctidRequest.(map[string]interface{})["ctidAuth"]
if !exists {
t.Fatal("响应中缺少ctidAuth字段")
}
authData := ctidAuth.(map[string]interface{})
// 验证字段值
expectedFields := map[string]string{
"resultCode": "1002",
"resultMsg": "身份证号不匹配",
"name": "张三",
"idCard": "110101199001011235",
"verifyResult": "不一致",
}
for field, expectedValue := range expectedFields {
if authData[field] != expectedValue {
t.Errorf("字段%s期望值为%s实际为%s", field, expectedValue, authData[field])
}
}
t.Logf("测试成功,失败响应结构: %s", string(jsonData))
}