新增部分接口合同授权码

This commit is contained in:
liangzai
2025-08-04 22:02:09 +08:00
parent f482f0a6e8
commit 3f4c3086f3
6 changed files with 57 additions and 21 deletions

View File

@@ -45,6 +45,7 @@ type ApiApplicationServiceImpl struct {
apiRequestService *services.ApiRequestService apiRequestService *services.ApiRequestService
apiCallRepository repositories.ApiCallRepository apiCallRepository repositories.ApiCallRepository
walletService finance_services.WalletAggregateService walletService finance_services.WalletAggregateService
contractInfoService user_repositories.ContractInfoRepository
productManagementService *product_services.ProductManagementService productManagementService *product_services.ProductManagementService
productSubscriptionService *product_services.ProductSubscriptionService productSubscriptionService *product_services.ProductSubscriptionService
userRepo user_repositories.UserRepository userRepo user_repositories.UserRepository
@@ -53,8 +54,8 @@ type ApiApplicationServiceImpl struct {
logger *zap.Logger logger *zap.Logger
} }
func NewApiApplicationService(apiCallService services.ApiCallAggregateService, apiUserService services.ApiUserAggregateService, apiRequestService *services.ApiRequestService, apiCallRepository repositories.ApiCallRepository, walletService finance_services.WalletAggregateService, productManagementService *product_services.ProductManagementService, productSubscriptionService *product_services.ProductSubscriptionService, userRepo user_repositories.UserRepository, txManager *database.TransactionManager, config *config.Config, logger *zap.Logger) ApiApplicationService { func NewApiApplicationService(apiCallService services.ApiCallAggregateService, apiUserService services.ApiUserAggregateService, apiRequestService *services.ApiRequestService, apiCallRepository repositories.ApiCallRepository, walletService finance_services.WalletAggregateService, productManagementService *product_services.ProductManagementService, productSubscriptionService *product_services.ProductSubscriptionService, userRepo user_repositories.UserRepository, txManager *database.TransactionManager, config *config.Config, logger *zap.Logger, contractInfoService user_repositories.ContractInfoRepository) ApiApplicationService {
return &ApiApplicationServiceImpl{apiCallService: apiCallService, apiUserService: apiUserService, apiRequestService: apiRequestService, apiCallRepository: apiCallRepository, walletService: walletService, productManagementService: productManagementService, productSubscriptionService: productSubscriptionService, userRepo: userRepo, txManager: txManager, config: config, logger: logger} return &ApiApplicationServiceImpl{apiCallService: apiCallService, apiUserService: apiUserService, apiRequestService: apiRequestService, apiCallRepository: apiCallRepository, walletService: walletService, productManagementService: productManagementService, productSubscriptionService: productSubscriptionService, userRepo: userRepo, txManager: txManager, config: config, logger: logger, contractInfoService: contractInfoService}
} }
// CallApi 应用服务层统一入口 // CallApi 应用服务层统一入口
@@ -149,7 +150,22 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A
} }
// 6. 调用API // 6. 调用API
response, err := s.apiRequestService.PreprocessRequestApi(txCtx, cmd.ApiName, requestParams, &cmd.Options) // 查询用户的合同信息获取合同编号
contractCode := "" // 默认空字符串
contractInfo, err := s.contractInfoService.FindByUserID(txCtx, apiUser.UserId)
if err == nil && len(contractInfo) > 0 {
contractCode = contractInfo[0].ContractCode
} else {
s.logger.Error("查合同信息失败", zap.Error(err))
businessError = ErrSystem
return ErrSystem
}
// 创建CallContext传递合同编号
callContext := &processors.CallContext{
ContractCode: contractCode,
}
response, err := s.apiRequestService.PreprocessRequestApi(txCtx, cmd.ApiName, requestParams, &cmd.Options, callContext)
if err != nil { if err != nil {
if errors.Is(err, processors.ErrDatasource) { if errors.Is(err, processors.ErrDatasource) {
s.logger.Error("调用API失败", zap.Error(err)) s.logger.Error("调用API失败", zap.Error(err))

View File

@@ -147,10 +147,10 @@ func registerAllProcessors(combService *comb.CombService) {
var RequestProcessors map[string]processors.ProcessorFunc var RequestProcessors map[string]processors.ProcessorFunc
// PreprocessRequestApi 调用指定的请求处理函数 // PreprocessRequestApi 调用指定的请求处理函数
func (a *ApiRequestService) PreprocessRequestApi(ctx context.Context, apiCode string, params []byte, options *commands.ApiCallOptions) ([]byte, error) { func (a *ApiRequestService) PreprocessRequestApi(ctx context.Context, apiCode string, params []byte, options *commands.ApiCallOptions, callContext *processors.CallContext) ([]byte, error) {
if processor, exists := RequestProcessors[apiCode]; exists { if processor, exists := RequestProcessors[apiCode]; exists {
// 设置Options到依赖容器 // 设置Options和CallContext到依赖容器
deps := a.processorDeps.WithOptions(options) deps := a.processorDeps.WithOptions(options).WithCallContext(callContext)
return processor(ctx, params, deps) return processor(ctx, params, deps)
} }
return nil, fmt.Errorf("%s: 未找到处理器: %s", ErrSystem, apiCode) return nil, fmt.Errorf("%s: 未找到处理器: %s", ErrSystem, apiCode)

View File

@@ -15,6 +15,11 @@ type CombServiceInterface interface {
ProcessCombRequest(ctx context.Context, params []byte, deps *ProcessorDependencies, packageCode string) (*CombinedResult, error) ProcessCombRequest(ctx context.Context, params []byte, deps *ProcessorDependencies, packageCode string) (*CombinedResult, error)
} }
// CallContext CallApi调用上下文包含调用相关的数据
type CallContext struct {
ContractCode string // 合同编号
}
// ProcessorDependencies 处理器依赖容器 // ProcessorDependencies 处理器依赖容器
type ProcessorDependencies struct { type ProcessorDependencies struct {
WestDexService *westdex.WestDexService WestDexService *westdex.WestDexService
@@ -24,6 +29,7 @@ type ProcessorDependencies struct {
Validator interfaces.RequestValidator Validator interfaces.RequestValidator
CombService CombServiceInterface // Changed to interface to break import cycle CombService CombServiceInterface // Changed to interface to break import cycle
Options *commands.ApiCallOptions // 添加Options支持 Options *commands.ApiCallOptions // 添加Options支持
CallContext *CallContext // 添加CallApi调用上下文
} }
// NewProcessorDependencies 创建处理器依赖容器 // NewProcessorDependencies 创建处理器依赖容器
@@ -43,6 +49,7 @@ func NewProcessorDependencies(
Validator: validator, Validator: validator,
CombService: combService, CombService: combService,
Options: nil, // 初始化为nil在调用时设置 Options: nil, // 初始化为nil在调用时设置
CallContext: nil, // 初始化为nil在调用时设置
} }
} }
@@ -52,6 +59,12 @@ func (deps *ProcessorDependencies) WithOptions(options *commands.ApiCallOptions)
return deps return deps
} }
// WithCallContext 设置CallContext的便捷方法
func (deps *ProcessorDependencies) WithCallContext(callContext *CallContext) *ProcessorDependencies {
deps.CallContext = callContext
return deps
}
// ProcessorFunc 处理器函数类型定义 // ProcessorFunc 处理器函数类型定义
type ProcessorFunc func(ctx context.Context, params []byte, deps *ProcessorDependencies) ([]byte, error) type ProcessorFunc func(ctx context.Context, params []byte, deps *ProcessorDependencies) ([]byte, error)

View File

@@ -33,13 +33,16 @@ func ProcessFLXG0V4BRequest(ctx context.Context, params []byte, deps *processors
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err) return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
} }
encryptedAuthAuthorizeFileCode, err := deps.WestDexService.Encrypt(deps.CallContext.ContractCode)
if err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
reqData := map[string]interface{}{ reqData := map[string]interface{}{
"data": map[string]interface{}{ "data": map[string]interface{}{
"name": encryptedName, "name": encryptedName,
"idcard": encryptedIDCard, "idcard": encryptedIDCard,
"auth_authorizeFileCode": encryptedAuthAuthorizeFileCode,
"inquired_auth": paramsDto.AuthDate, "inquired_auth": paramsDto.AuthDate,
// "auth_authorizeFileCode": paramsDto.AuthAuthorizeFileCode,
}, },
} }
respBytes, err := deps.WestDexService.CallAPI("G22SC01", reqData) respBytes, err := deps.WestDexService.CallAPI("G22SC01", reqData)

View File

@@ -31,11 +31,15 @@ func ProcessQYGL8271Request(ctx context.Context, params []byte, deps *processors
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err) return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
} }
encryptedAuthAuthorizeFileCode, err := deps.WestDexService.Encrypt(deps.CallContext.ContractCode)
if err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
reqData := map[string]interface{}{ reqData := map[string]interface{}{
"data": map[string]interface{}{ "data": map[string]interface{}{
"org_name": encryptedEntName, "org_name": encryptedEntName,
"uscc": encryptedEntCode, "uscc": encryptedEntCode,
"auth_authorizeFileCode": encryptedAuthAuthorizeFileCode,
"inquired_auth": paramsDto.AuthDate, // AuthDate 有 encrypt:"false" 标签,不加密 "inquired_auth": paramsDto.AuthDate, // AuthDate 有 encrypt:"false" 标签,不加密
}, },
} }

View File

@@ -31,7 +31,7 @@ type ContractInfo struct {
UserID string `gorm:"type:varchar(36);not null;index" json:"user_id" comment:"关联用户ID"` UserID string `gorm:"type:varchar(36);not null;index" json:"user_id" comment:"关联用户ID"`
// 合同基本信息 // 合同基本信息
// ContractCode string `gorm:"type:varchar(255);not null" json:"contract_code" comment:"合同编号"` ContractCode string `gorm:"type:varchar(255);not null;uniqueIndex" json:"contract_code" comment:"合同编号"`
ContractName string `gorm:"type:varchar(255);not null" json:"contract_name" comment:"合同名称"` ContractName string `gorm:"type:varchar(255);not null" json:"contract_name" comment:"合同名称"`
ContractType ContractType `gorm:"type:varchar(50);not null;index" json:"contract_type" comment:"合同类型"` ContractType ContractType `gorm:"type:varchar(50);not null;index" json:"contract_type" comment:"合同类型"`
ContractFileID string `gorm:"type:varchar(100);not null" json:"contract_file_id" comment:"合同文件ID"` ContractFileID string `gorm:"type:varchar(100);not null" json:"contract_file_id" comment:"合同文件ID"`
@@ -91,13 +91,13 @@ func NewContractInfo(enterpriseInfoID, userID, contractName string, contractType
} }
// 生成合同编码 // 生成合同编码
// contractCode := GenerateContractCode(contractType) contractCode := GenerateContractCode(contractType)
contractInfo := &ContractInfo{ contractInfo := &ContractInfo{
ID: uuid.New().String(), ID: uuid.New().String(),
EnterpriseInfoID: enterpriseInfoID, EnterpriseInfoID: enterpriseInfoID,
UserID: userID, UserID: userID,
// ContractCode: contractCode, ContractCode: contractCode,
ContractName: contractName, ContractName: contractName,
ContractType: contractType, ContractType: contractType,
ContractFileID: contractFileID, ContractFileID: contractFileID,
@@ -110,7 +110,7 @@ func NewContractInfo(enterpriseInfoID, userID, contractName string, contractType
ContractInfoID: contractInfo.ID, ContractInfoID: contractInfo.ID,
EnterpriseInfoID: enterpriseInfoID, EnterpriseInfoID: enterpriseInfoID,
UserID: userID, UserID: userID,
// ContractCode: contractCode, ContractCode: contractCode,
ContractName: contractName, ContractName: contractName,
ContractType: string(contractType), ContractType: string(contractType),
CreatedAt: time.Now(), CreatedAt: time.Now(),
@@ -198,9 +198,9 @@ func (c *ContractInfo) validateBasicFields() error {
if c.UserID == "" { if c.UserID == "" {
return fmt.Errorf("用户ID不能为空") return fmt.Errorf("用户ID不能为空")
} }
// if c.ContractCode == "" { if c.ContractCode == "" {
// return fmt.Errorf("合同编码不能为空") return fmt.Errorf("合同编码不能为空")
// } }
if c.ContractName == "" { if c.ContractName == "" {
return fmt.Errorf("合同名称不能为空") return fmt.Errorf("合同名称不能为空")
} }
@@ -309,7 +309,7 @@ type ContractInfoCreatedEvent struct {
ContractInfoID string `json:"contract_info_id"` ContractInfoID string `json:"contract_info_id"`
EnterpriseInfoID string `json:"enterprise_info_id"` EnterpriseInfoID string `json:"enterprise_info_id"`
UserID string `json:"user_id"` UserID string `json:"user_id"`
// ContractCode string `json:"contract_code"` ContractCode string `json:"contract_code"`
ContractName string `json:"contract_name"` ContractName string `json:"contract_name"`
ContractType string `json:"contract_type"` ContractType string `json:"contract_type"`
CreatedAt time.Time `json:"created_at"` CreatedAt time.Time `json:"created_at"`
@@ -347,13 +347,13 @@ func GenerateContractCode(contractType ContractType) string {
prefix += "02" prefix += "02"
} }
// 获取当前日期格式为YYMMDD // 获取当前日期,格式为YYYYMMDD
now := time.Now() now := time.Now()
dateStr := now.Format("060102") // YYMMDD格式 dateStr := now.Format("20060102") // YYYYMMDD格式
// 生成一个随机的6位数字 // 生成一个随机的6位数字
randNum := fmt.Sprintf("%06d", rand.Intn(1000000)) randNum := fmt.Sprintf("%06d", rand.Intn(1000000))
// 格式CON + 类型标识 + YYMMDD + 6位随机数 // 格式CON + 类型标识 + YYYYMMDD + 6位随机数
return fmt.Sprintf("%s%s%s", prefix, dateStr, randNum) return fmt.Sprintf("%s%s%s", prefix, dateStr, randNum)
} }