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

@@ -1,195 +0,0 @@
# Options 使用指南
## 概述
Options 机制允许在 API 调用时传递额外的配置选项,这些选项会传递给所有处理器(包括组合包处理器和子处理器),实现灵活的配置控制。
## 架构设计
```
ApiCallCommand.Options → api_application_service → api_request_service → 所有处理器
组合包处理器 → 子处理器
```
## Options 字段说明
```go
type ApiCallOptions struct {
Json bool `json:"json,omitempty"` // 是否返回JSON格式
Debug bool `json:"debug,omitempty"` // 调试模式
Timeout int `json:"timeout,omitempty"` // 超时时间(秒)
RetryCount int `json:"retry_count,omitempty"` // 重试次数
Async bool `json:"async,omitempty"` // 异步处理
Priority int `json:"priority,omitempty"` // 优先级(1-10)
Cache bool `json:"cache,omitempty"` // 是否使用缓存
Compress bool `json:"compress,omitempty"` // 是否压缩响应
Encrypt bool `json:"encrypt,omitempty"` // 是否加密响应
Validate bool `json:"validate,omitempty"` // 是否严格验证
LogLevel string `json:"log_level,omitempty"` // 日志级别
}
```
## 使用示例
### 1. 普通处理器中使用 Options
```go
func ProcessYYSY4B37Request(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
// ... 参数验证 ...
reqData := map[string]interface{}{
"mobile": paramsDto.Mobile,
}
// 使用 Options 调整请求参数
if deps.Options != nil {
if deps.Options.Timeout > 0 {
reqData["timeout"] = deps.Options.Timeout
}
if deps.Options.RetryCount > 0 {
reqData["retry_count"] = deps.Options.RetryCount
}
if deps.Options.Debug {
reqData["debug"] = true
}
}
return deps.YushanService.CallAPI("YYSY4B37", reqData)
}
```
### 2. 组合包处理器中使用 Options
```go
func ProcessCOMB298YRequest(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error) {
// ... 参数验证 ...
// 根据 Options 调整组合策略
if deps.Options != nil {
if deps.Options.Priority > 0 {
// 高优先级时调整处理策略
fmt.Printf("组合包处理优先级: %d\n", deps.Options.Priority)
}
if deps.Options.Debug {
fmt.Printf("组合包调试模式开启\n")
}
}
// Options 会自动传递给所有子处理器
return deps.CombService.ProcessCombRequest(ctx, params, deps, "COMB298Y")
}
```
### 3. 客户端调用示例
```json
{
"data": "加密的请求数据",
"options": {
"debug": true,
"timeout": 30,
"retry_count": 3,
"priority": 5,
"cache": true,
"log_level": "debug"
}
}
```
## 最佳实践
### 1. 空值检查
始终检查 `deps.Options` 是否为 `nil`,避免空指针异常:
```go
if deps.Options != nil {
// 使用 Options
}
```
### 2. 默认值处理
为 Options 字段提供合理的默认值:
```go
timeout := 30 // 默认30秒
if deps.Options != nil && deps.Options.Timeout > 0 {
timeout = deps.Options.Timeout
}
```
### 3. 验证选项值
对 Options 中的值进行验证:
```go
if deps.Options != nil {
if deps.Options.Priority < 1 || deps.Options.Priority > 10 {
return nil, fmt.Errorf("优先级必须在1-10之间")
}
}
```
### 4. 日志记录
在调试模式下记录 Options 信息:
```go
if deps.Options != nil && deps.Options.Debug {
log.Printf("处理器选项: %+v", deps.Options)
}
```
## 扩展性
### 1. 添加新的 Options 字段
`ApiCallOptions` 结构体中添加新字段:
```go
type ApiCallOptions struct {
// ... 现有字段 ...
CustomField string `json:"custom_field,omitempty"` // 自定义字段
}
```
### 2. 处理器特定选项
可以为特定处理器创建专门的选项结构:
```go
type YYSYOptions struct {
ApiCallOptions
YYSYSpecific bool `json:"yysy_specific,omitempty"`
}
```
## 注意事项
1. **性能影响**: Options 传递会增加少量性能开销,但影响微乎其微
2. **向后兼容**: 新增的 Options 字段应该使用 `omitempty` 标签
3. **安全性**: 敏感配置不应该通过 Options 传递
4. **文档化**: 新增 Options 字段时应该更新文档
## 调试技巧
### 1. 启用调试模式
```json
{
"options": {
"debug": true,
"log_level": "debug"
}
}
```
### 2. 查看 Options 传递
在处理器中添加日志:
```go
if deps.Options != nil {
log.Printf("处理器 %s 收到选项: %+v", "YYSY4B37", deps.Options)
}
```
### 3. 组合包调试
组合包处理器会自动将 Options 传递给所有子处理器,无需额外配置。

View File

@@ -1,155 +0,0 @@
# API处理器架构说明
## 概述
本目录实现了基于依赖注入容器的API处理器架构支持灵活的依赖管理和组合调用模式。
## 架构模式
### 1. 依赖注入容器模式
#### ProcessorDependencies
```go
type ProcessorDependencies struct {
WestDexService *westdex.WestDexService
YushanService *yushan.YushanService
Validator interfaces.RequestValidator
}
```
**优势:**
- 统一的依赖管理
- 类型安全的依赖注入
- 易于测试和mock
- 支持未来扩展新的服务
#### 处理器函数签名
```go
type ProcessorFunc func(ctx context.Context, params []byte, deps *ProcessorDependencies) ([]byte, error)
```
### 2. 组合处理器模式
#### CompositeProcessor
专门用于处理组合包COMB系列的处理器支持
- 动态注册其他处理器
- 批量调用多个处理器
- 结果组合和格式化
```go
type CompositeProcessor struct {
processors map[string]ProcessorFunc
deps *ProcessorDependencies
}
```
## 目录结构
```
processors/
├── dependencies.go # 依赖容器定义
├── comb/
│ ├── comb_processor.go # 组合处理器基类
│ └── comb298y_processor.go # COMB298Y组合处理器
├── flxg/ # FLXG系列处理器
├── jrzq/ # JRZQ系列处理器
├── qygl/ # QYGL系列处理器
├── yysy/ # YYSY系列处理器
└── ivyz/ # IVYZ系列处理器
```
## 服务分配策略
### WestDexService
- FLXG系列使用WestDexService
- JRZQ系列使用WestDexService
- IVYZ系列使用WestDexService
### YushanService
- QYGL系列使用YushanService
- YYSY系列使用YushanService
### 组合包COMB
- 调用多个其他处理器
- 组合结果并返回统一格式
## 使用示例
### 普通处理器
```go
func ProcessFLXG0V3Bequest(ctx context.Context, params []byte, deps *ProcessorDependencies) ([]byte, error) {
// 参数验证
var paramsDto dto.FLXG0V3BequestReq
if err := json.Unmarshal(params, &paramsDto); err != nil {
return nil, fmt.Errorf("参数校验不正确: 解密后的数据格式错误")
}
if err := deps.Validator.ValidateStruct(paramsDto); err != nil {
return nil, fmt.Errorf("参数校验不正确: %s", err.Error())
}
// 调用服务
reqData := map[string]interface{}{
"name": paramsDto.Name,
"idCard": paramsDto.IDCard,
"mobile": paramsDto.Mobile,
}
respBytes, err := deps.WestDexService.CallAPI("FLXG0V3B", reqData)
if err != nil {
return nil, fmt.Errorf("调用外部服务失败: %s", err.Error())
}
return respBytes, nil
}
```
### 组合处理器
```go
func ProcessCOMB298YRequest(ctx context.Context, params []byte, deps *ProcessorDependencies) ([]byte, error) {
// 创建组合处理器
compositeProcessor := NewCompositeProcessor(deps)
// 注册需要调用的处理器
compositeProcessor.RegisterProcessor("FLXG0V3B", flxg.ProcessFLXG0V3Bequest)
compositeProcessor.RegisterProcessor("JRZQ8203", jrzq.ProcessJRZQ8203Request)
// 调用并组合结果
results := make(map[string]interface{})
flxgResult, err := compositeProcessor.CallProcessor(ctx, "FLXG0V3B", params)
if err != nil {
return nil, fmt.Errorf("调用FLXG0V3B处理器失败: %s", err.Error())
}
results["flxg0v3b"] = string(flxgResult)
// 返回组合结果
return compositeProcessor.CombineResults(results)
}
```
## 扩展指南
### 添加新的服务依赖
1.`ProcessorDependencies` 中添加新字段
2. 更新 `NewProcessorDependencies` 构造函数
3.`ApiRequestService` 中注入新服务
### 添加新的处理器
1. 在对应目录下创建新的处理器文件
2. 实现 `ProcessorFunc` 接口
3.`RequestProcessors` 映射中注册
### 添加新的组合包
1.`comb/` 目录下创建新的组合处理器
2. 使用 `CompositeProcessor` 基类
3. 注册需要调用的处理器并组合结果
## 优势
1. **解耦**:处理器与具体服务实现解耦
2. **可测试**:易于进行单元测试和集成测试
3. **可扩展**:支持添加新的服务和处理器
4. **类型安全**:编译时检查依赖关系
5. **组合支持**:灵活的组合调用模式
6. **维护性**:清晰的代码结构和职责分离

View File

@@ -1,117 +0,0 @@
# 处理器文件更新总结
## 更新概述
已成功将所有36个API处理器文件更新为使用新的依赖注入容器模式。
## 更新统计
### 已更新的处理器文件总数36个
#### FLXG系列 (12个)
- ✅ flxg0v3b_processor.go
- ✅ flxg0v4b_processor.go
- ✅ flxg162a_processor.go
- ✅ flxg3d56_processor.go
- ✅ flxg54f5_processor.go
- ✅ flxg5876_processor.go
- ✅ flxg75fe_processor.go
- ✅ flxg9687_processor.go
- ✅ flxg970f_processor.go
- ✅ flxgc9d1_processor.go
- ✅ flxgca3d_processor.go
- ✅ flxgdec7_processor.go
#### JRZQ系列 (4个)
- ✅ jrzq8203_processor.go
- ✅ jrzq0a03_processor.go
- ✅ jrzq4aa8_processor.go
- ✅ jrzqdcbe_processor.go
#### QYGL系列 (6个)
- ✅ qygl8261_processor.go
- ✅ qygl2acd_processor.go
- ✅ qygl45bd_processor.go
- ✅ qygl6f2d_processor.go
- ✅ qygl8271_processor.go
- ✅ qyglb4c0_processor.go
#### YYSY系列 (7个)
- ✅ yysyd50f_processor.go
- ✅ yysy09cd_processor.go
- ✅ yysy4b21_processor.go
- ✅ yysy4b37_processor.go
- ✅ yysy6f2e_processor.go
- ✅ yysybe08_processor.go
- ✅ yysyf7db_processor.go
#### IVYZ系列 (7个)
- ✅ ivyz0b03_processor.go
- ✅ ivyz2125_processor.go
- ✅ ivyz385e_processor.go
- ✅ ivyz5733_processor.go
- ✅ ivyz9363_processor.go
- ✅ ivyz9a2b_processor.go
- ✅ ivyzadee_processor.go
#### COMB系列 (1个)
- ✅ comb298y_processor.go (组合处理器)
## 更新内容
### 1. 函数签名更新
所有处理器函数的签名已从:
```go
func ProcessXXXRequest(ctx context.Context, params []byte, validator interfaces.RequestValidator) ([]byte, error)
```
更新为:
```go
func ProcessXXXRequest(ctx context.Context, params []byte, deps *processors.ProcessorDependencies) ([]byte, error)
```
### 2. 导入更新
- 移除了 `"tyapi-server/internal/shared/interfaces"` 导入
- 添加了 `"tyapi-server/internal/domains/api/services/processors"` 导入
### 3. 验证器调用更新
-`validator.ValidateStruct(paramsDto)`
- 更新为 `deps.Validator.ValidateStruct(paramsDto)`
### 4. 服务调用实现
根据API前缀分配不同的服务
#### WestDexService (FLXG, JRZQ, IVYZ系列)
```go
respBytes, err := deps.WestDexService.CallAPI("API_CODE", reqData)
```
#### YushanService (QYGL, YYSY系列)
```go
respBytes, err := deps.WestDexService.CallAPI("API_CODE", reqData)
```
### 5. 组合处理器
COMB298Y处理器实现了组合调用模式
- 使用 `CompositeProcessor` 基类
- 动态注册其他处理器
- 组合多个处理器的结果
## 架构优势
1. **统一依赖管理**:所有处理器通过 `ProcessorDependencies` 容器访问依赖
2. **类型安全**:编译时检查依赖关系
3. **易于测试**可以轻松mock依赖进行单元测试
4. **可扩展性**:新增服务只需在容器中添加
5. **组合支持**COMB系列支持灵活的组合调用
6. **维护性**:清晰的代码结构和职责分离
## 编译验证
✅ 项目编译成功,无语法错误
## 下一步建议
1. **单元测试**:为各个处理器编写单元测试
2. **集成测试**测试实际的API调用流程
3. **性能测试**:验证新架构的性能表现
4. **文档完善**补充API文档和使用说明

View File

@@ -3,6 +3,7 @@ package processors
import (
"context"
"tyapi-server/internal/application/api/commands"
"tyapi-server/internal/infrastructure/external/tianyancha"
"tyapi-server/internal/infrastructure/external/westdex"
"tyapi-server/internal/infrastructure/external/yushan"
"tyapi-server/internal/shared/interfaces"
@@ -15,26 +16,29 @@ type CombServiceInterface interface {
// ProcessorDependencies 处理器依赖容器
type ProcessorDependencies struct {
WestDexService *westdex.WestDexService
YushanService *yushan.YushanService
Validator interfaces.RequestValidator
CombService CombServiceInterface // Changed to interface to break import cycle
Options *commands.ApiCallOptions // 添加Options支持
WestDexService *westdex.WestDexService
YushanService *yushan.YushanService
TianYanChaService *tianyancha.TianYanChaService
Validator interfaces.RequestValidator
CombService CombServiceInterface // Changed to interface to break import cycle
Options *commands.ApiCallOptions // 添加Options支持
}
// NewProcessorDependencies 创建处理器依赖容器
func NewProcessorDependencies(
westDexService *westdex.WestDexService,
yushanService *yushan.YushanService,
tianYanChaService *tianyancha.TianYanChaService,
validator interfaces.RequestValidator,
combService CombServiceInterface, // Changed to interface
) *ProcessorDependencies {
return &ProcessorDependencies{
WestDexService: westDexService,
YushanService: yushanService,
Validator: validator,
CombService: combService,
Options: nil, // 初始化为nil在调用时设置
WestDexService: westDexService,
YushanService: yushanService,
TianYanChaService: tianYanChaService,
Validator: validator,
CombService: combService,
Options: nil, // 初始化为nil在调用时设置
}
}

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
}

View File

@@ -32,24 +32,22 @@ func ProcessYYSYBE08Request(ctx context.Context, params []byte, deps *processors
if err != nil {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
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)),
"xM": encryptedName,
"gMSFZHM": encryptedIDCard,
"customerNumber": deps.WestDexService.GetConfig().Key,
"timeStamp": fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond)),
},
}
respBytes, err := deps.WestDexService.CallAPI("layoutIdcard", reqData)
if err != nil {
if errors.Is(err, westdex.ErrDatasource) {
return nil, fmt.Errorf("%s: %w", processors.ErrDatasource, err)
} else {
if !errors.Is(err, westdex.ErrDatasource) {
return nil, fmt.Errorf("%s: %w", processors.ErrSystem, err)
}
}
return respBytes, nil
}
}