@@ -0,0 +1,140 @@
package qygl
import (
"context"
"encoding/json"
"errors"
"fmt"
"tyapi-server/internal/domains/api/dto"
"tyapi-server/internal/domains/api/services/processors"
"tyapi-server/internal/infrastructure/external/xingwei"
"github.com/tidwall/gjson"
)
// ProcessQYGL5CMPRequest QYGL5CMP API处理方法 - 企业五要素验证
func ProcessQYGL5CMPRequest ( ctx context . Context , params [ ] byte , deps * processors . ProcessorDependencies ) ( [ ] byte , error ) {
var paramsDto dto . QYGL5CMPReq
if err := json . Unmarshal ( params , & paramsDto ) ; err != nil {
return nil , errors . Join ( processors . ErrSystem , err )
}
if err := deps . Validator . ValidateStruct ( paramsDto ) ; err != nil {
return nil , errors . Join ( 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 {
return nil , convertTianYanChaError ( err )
}
// 检查天眼查API调用是否成功
if ! response . Success {
// 天眼查API调用失败, 返回企业信息校验不通过
return createStatusResponsess ( 1 ) , nil
}
// 解析天眼查响应数据
if response . Data == nil {
// 天眼查响应数据为空,返回企业信息校验不通过
return createStatusResponsess ( 1 ) , nil
}
// 将response.Data转换为JSON字符串, 然后使用gjson解析
dataBytes , err := json . Marshal ( response . Data )
if err != nil {
// 数据序列化失败,返回企业信息校验不通过
return createStatusResponsess ( 1 ) , nil
}
// 使用gjson解析嵌套的data.result.data字段
result := gjson . GetBytes ( dataBytes , "result" )
if ! result . Exists ( ) {
// 字段不存在,返回企业信息校验不通过
return createStatusResponsess ( 1 ) , nil
}
// 检查data.result.data是否等于1
if result . Int ( ) != 1 {
// 不等于1, 返回企业信息校验不通过
return createStatusResponsess ( 1 ) , nil
}
// 天眼查三要素验证通过,继续调用星维身份证三要素验证
if err := json . Unmarshal ( params , & paramsDto ) ; err != nil {
return nil , errors . Join ( processors . ErrSystem , err )
}
if err := deps . Validator . ValidateStruct ( paramsDto ) ; err != nil {
return nil , errors . Join ( processors . ErrInvalidParam , err )
}
// 构建请求数据,将项目规范的字段名转换为 XingweiService 需要的字段名
reqData := map [ string ] interface { } {
"name" : paramsDto . LegalPerson ,
"idCardNum" : paramsDto . IDCard ,
"phoneNumber" : paramsDto . MobileNo ,
}
// 调用行为数据API, 使用指定的project_id
projectID := "CDJ-1100244702166183936"
respBytes , err := deps . XingweiService . CallAPI ( ctx , projectID , reqData )
if err != nil {
if errors . Is ( err , xingwei . ErrNotFound ) {
return nil , errors . Join ( processors . ErrNotFound , err )
} else if errors . Is ( err , xingwei . ErrDatasource ) {
return nil , errors . Join ( processors . ErrDatasource , err )
} else if errors . Is ( err , xingwei . ErrSystem ) {
return nil , errors . Join ( processors . ErrSystem , err )
} else {
return nil , errors . Join ( processors . ErrSystem , err )
}
}
// 解析星维API返回的数据
var xingweiData map [ string ] interface { }
if err := json . Unmarshal ( respBytes , & xingweiData ) ; err != nil {
return nil , errors . Join ( processors . ErrSystem , fmt . Errorf ( "解析星维API响应失败: %w" , err ) )
}
// 构建天眼查API返回的数据结构
tianYanChaData := map [ string ] interface { } {
"success" : response . Success ,
"message" : response . Message ,
"data" : response . Data ,
}
// 合并两个API的返回数据
mergedData := map [ string ] interface { } {
"Personal Information" : xingweiData , // 星维API返回的数据
"Enterprise Information" : tianYanChaData , // 天眼查API返回的数据
}
// 将合并后的数据序列化为JSON
mergedBytes , err := json . Marshal ( mergedData )
if err != nil {
return nil , errors . Join ( processors . ErrSystem , fmt . Errorf ( "合并数据序列化失败: %w" , err ) )
}
return mergedBytes , nil
}
// createStatusResponsess 创建状态响应
func createStatusResponsess ( status int ) [ ] byte {
response := map [ string ] interface { } {
"status" : status ,
"message" : "企业信息不通过" ,
}
respBytes , _ := json . Marshal ( response )
return respBytes
}