fix certification
This commit is contained in:
@@ -2,15 +2,20 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"tyapi-server/internal/config"
|
||||
"tyapi-server/internal/domains/api/dto"
|
||||
"tyapi-server/internal/domains/api/services/processors"
|
||||
"tyapi-server/internal/domains/api/services/processors/qygl"
|
||||
"tyapi-server/internal/domains/certification/entities"
|
||||
"tyapi-server/internal/domains/certification/entities/value_objects"
|
||||
"tyapi-server/internal/domains/certification/repositories"
|
||||
"tyapi-server/internal/infrastructure/external/tianyancha"
|
||||
"tyapi-server/internal/infrastructure/external/westdex"
|
||||
"tyapi-server/internal/shared/interfaces"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -19,19 +24,30 @@ import (
|
||||
// 领域服务应无状态
|
||||
|
||||
type EnterpriseInfoSubmitRecordService struct {
|
||||
westdexService *westdex.WestDexService
|
||||
repositories repositories.EnterpriseInfoSubmitRecordRepository
|
||||
appConfig config.AppConfig
|
||||
logger *zap.Logger
|
||||
westdexService *westdex.WestDexService
|
||||
tianYanChaService *tianyancha.TianYanChaService
|
||||
validator interfaces.RequestValidator
|
||||
repositories repositories.EnterpriseInfoSubmitRecordRepository
|
||||
appConfig config.AppConfig
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewEnterpriseInfoSubmitRecordService 构造函数
|
||||
func NewEnterpriseInfoSubmitRecordService(westdexService *westdex.WestDexService, repositories repositories.EnterpriseInfoSubmitRecordRepository, appConfig config.AppConfig, logger *zap.Logger) *EnterpriseInfoSubmitRecordService {
|
||||
func NewEnterpriseInfoSubmitRecordService(
|
||||
westdexService *westdex.WestDexService,
|
||||
tianYanChaService *tianyancha.TianYanChaService,
|
||||
validator interfaces.RequestValidator,
|
||||
repositories repositories.EnterpriseInfoSubmitRecordRepository,
|
||||
appConfig config.AppConfig,
|
||||
logger *zap.Logger,
|
||||
) *EnterpriseInfoSubmitRecordService {
|
||||
return &EnterpriseInfoSubmitRecordService{
|
||||
westdexService: westdexService,
|
||||
repositories: repositories,
|
||||
appConfig: appConfig,
|
||||
logger: logger,
|
||||
westdexService: westdexService,
|
||||
tianYanChaService: tianYanChaService,
|
||||
validator: validator,
|
||||
repositories: repositories,
|
||||
appConfig: appConfig,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +63,7 @@ func (s *EnterpriseInfoSubmitRecordService) Save(ctx context.Context, enterprise
|
||||
return s.repositories.Create(ctx, enterpriseInfoSubmitRecord)
|
||||
}
|
||||
|
||||
// ValidateWithWestdex 调用westdexService验证企业信息
|
||||
// ValidateWithWestdex 调用QYGL23T7处理器验证企业信息
|
||||
func (s *EnterpriseInfoSubmitRecordService) ValidateWithWestdex(ctx context.Context, info *value_objects.EnterpriseInfo) error {
|
||||
if info == nil {
|
||||
return errors.New("企业信息不能为空")
|
||||
@@ -58,57 +74,71 @@ func (s *EnterpriseInfoSubmitRecordService) ValidateWithWestdex(ctx context.Cont
|
||||
}
|
||||
|
||||
// 开发环境下跳过外部验证
|
||||
if s.appConfig.IsDevelopment() {
|
||||
s.logger.Info("开发环境:跳过企业信息外部验证",
|
||||
// if s.appConfig.IsDevelopment() {
|
||||
// s.logger.Info("开发环境:跳过企业信息外部验证",
|
||||
// zap.String("company_name", info.CompanyName),
|
||||
// zap.String("legal_person", info.LegalPersonName))
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// 构建QYGL23T7请求参数
|
||||
reqDto := dto.QYGL23T7Req{
|
||||
EntName: info.CompanyName,
|
||||
LegalPerson: info.LegalPersonName,
|
||||
EntCode: info.UnifiedSocialCode,
|
||||
IDCard: info.LegalPersonID,
|
||||
}
|
||||
|
||||
// 序列化请求参数
|
||||
paramsBytes, err := json.Marshal(reqDto)
|
||||
if err != nil {
|
||||
return fmt.Errorf("序列化请求参数失败: %w", err)
|
||||
}
|
||||
|
||||
// 创建处理器依赖
|
||||
deps := &processors.ProcessorDependencies{
|
||||
WestDexService: s.westdexService,
|
||||
TianYanChaService: s.tianYanChaService,
|
||||
Validator: s.validator,
|
||||
}
|
||||
|
||||
// 调用QYGL23T7处理器进行验证
|
||||
responseBytes, err := qygl.ProcessQYGL23T7Request(ctx, paramsBytes, deps)
|
||||
if err != nil {
|
||||
// 检查是否是数据源错误
|
||||
if errors.Is(err, processors.ErrDatasource) {
|
||||
return fmt.Errorf("数据源异常: %w", err)
|
||||
}
|
||||
return fmt.Errorf("企业信息验证失败: %w", err)
|
||||
}
|
||||
|
||||
// 解析响应结果
|
||||
var response map[string]interface{}
|
||||
if err := json.Unmarshal(responseBytes, &response); err != nil {
|
||||
return fmt.Errorf("解析响应结果失败: %w", err)
|
||||
}
|
||||
|
||||
// 检查验证状态
|
||||
status, ok := response["status"].(float64)
|
||||
if !ok {
|
||||
return fmt.Errorf("响应格式错误")
|
||||
}
|
||||
|
||||
// 根据状态码判断验证结果
|
||||
switch int(status) {
|
||||
case 0:
|
||||
// 验证通过
|
||||
s.logger.Info("企业信息验证通过",
|
||||
zap.String("company_name", info.CompanyName),
|
||||
zap.String("legal_person", info.LegalPersonName))
|
||||
return nil
|
||||
}
|
||||
encryptedEntName, err := s.westdexService.Encrypt(info.CompanyName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", "企业四要素验证", err)
|
||||
}
|
||||
|
||||
encryptedLegalPerson, err := s.westdexService.Encrypt(info.LegalPersonName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", "企业四要素验证", err)
|
||||
}
|
||||
|
||||
encryptedEntCode, err := s.westdexService.Encrypt(info.UnifiedSocialCode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", "企业四要素验证", err)
|
||||
}
|
||||
|
||||
encryptedIDCard, err := s.westdexService.Encrypt(info.LegalPersonID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", "企业四要素验证", err)
|
||||
}
|
||||
|
||||
reqParams := map[string]interface{}{
|
||||
"data": map[string]interface{}{
|
||||
"entname": encryptedEntName,
|
||||
"realname": encryptedLegalPerson,
|
||||
"entmark": encryptedEntCode,
|
||||
"idcard": encryptedIDCard,
|
||||
},
|
||||
}
|
||||
var respData []byte
|
||||
resp, err := s.westdexService.CallAPI("WEST00021", reqParams)
|
||||
if err != nil {
|
||||
if errors.Is(err, westdex.ErrDatasource) && resp != nil {
|
||||
respData = resp
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
respData = resp
|
||||
s.logger.Info("验证企业四要素响应", zap.Any("response", respData))
|
||||
resultState := gjson.GetBytes(respData, "result.state")
|
||||
if !resultState.Exists() {
|
||||
case 1:
|
||||
// 企业信息不一致
|
||||
return fmt.Errorf("企业信息不一致")
|
||||
case 2:
|
||||
// 身份证信息不一致
|
||||
return fmt.Errorf("身份证信息不一致")
|
||||
default:
|
||||
return fmt.Errorf("未知的验证状态: %d", int(status))
|
||||
}
|
||||
if resultState.Int() != 1 {
|
||||
return fmt.Errorf("企业信息不一致")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user