add qygl23t7

This commit is contained in:
2025-07-30 00:51:22 +08:00
parent 83530c0f9b
commit 723c418a1b
38 changed files with 999 additions and 785 deletions

View File

@@ -0,0 +1,140 @@
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"
)
// ProcessQYGL23T7Request QYGL23T7 API处理方法 - 企业三要素验证
func ProcessQYGL23T7Request(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
var paramsDto dto.QYGL23T7Req
if err := json.Unmarshal(params, &paramsDto); err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
if err := deps.Validator.ValidateStruct(paramsDto); err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrInvalidParam, err)
}
// 构建API调用参数
apiParams := map[string]string{
"code": paramsDto.EntCode,
"name": paramsDto.EntName,
"legalPersonName": paramsDto.LegalPerson,
}
// 调用天眼查API - 使用通用的CallAPI方法
response, err := deps.TianYanChaService.CallAPI(ctx, "VerifyThreeElements", apiParams)
if err != nil {
if err.Error() == "数据源异常" { // Specific error handling for data source issues
return nil, fmt.Errorf("%s: %w", processors.ErrDatasource, err)
} else {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
}
// 检查天眼查API调用是否成功
if !response.Success {
// 天眼查API调用失败返回企业信息校验不通过
return createStatusResponse(1), nil
}
// 解析天眼查响应数据
if response.Data == nil {
// 天眼查响应数据为空,返回企业信息校验不通过
return createStatusResponse(1), nil
}
// 将response.Data转换为JSON字符串然后使用gjson解析
dataBytes, err := json.Marshal(response.Data)
if err != nil {
// 数据序列化失败,返回企业信息校验不通过
return createStatusResponse(1), nil
}
// 使用gjson解析嵌套的data.result.data字段
result := gjson.GetBytes(dataBytes, "result")
if !result.Exists() {
// 字段不存在,返回企业信息校验不通过
return createStatusResponse(1), nil
}
// 检查data.result.data是否等于1
if result.Int() != 1 {
// 不等于1返回企业信息校验不通过
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.EntCode)
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().Key,
"timeStamp": fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond)),
},
}
// 调用WestDex身份证二要素验证API
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)
}
}
// 使用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 {
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 firstChar == "0" {
return createStatusResponse(0), nil
} else if firstChar == "5" {
return createStatusResponse(2), nil
} else {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
}
// createStatusResponse 创建状态响应
func createStatusResponse(status int) []byte {
response := map[string]interface{}{
"status": status,
}
respBytes, _ := json.Marshal(response)
return respBytes
}