更新处理器
This commit is contained in:
51
internal/infrastructure/external/tianyuanapi/tianyuanapi_factory.go
vendored
Normal file
51
internal/infrastructure/external/tianyuanapi/tianyuanapi_factory.go
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"hyapi-server/internal/config"
|
||||
"hyapi-server/internal/shared/external_logger"
|
||||
)
|
||||
|
||||
// NewTianyuanapiServiceWithConfig 使用配置创建天远 API 服务
|
||||
func NewTianyuanapiServiceWithConfig(cfg *config.Config) (*TianyuanapiService, error) {
|
||||
loggingConfig := external_logger.ExternalServiceLoggingConfig{
|
||||
Enabled: cfg.Tianyuanapi.Logging.Enabled,
|
||||
LogDir: cfg.Tianyuanapi.Logging.LogDir,
|
||||
ServiceName: "tianyuanapi",
|
||||
UseDaily: cfg.Tianyuanapi.Logging.UseDaily,
|
||||
EnableLevelSeparation: cfg.Tianyuanapi.Logging.EnableLevelSeparation,
|
||||
LevelConfigs: make(map[string]external_logger.ExternalServiceLevelFileConfig),
|
||||
}
|
||||
for k, v := range cfg.Tianyuanapi.Logging.LevelConfigs {
|
||||
loggingConfig.LevelConfigs[k] = external_logger.ExternalServiceLevelFileConfig{
|
||||
MaxSize: v.MaxSize,
|
||||
MaxBackups: v.MaxBackups,
|
||||
MaxAge: v.MaxAge,
|
||||
Compress: v.Compress,
|
||||
}
|
||||
}
|
||||
|
||||
var logger *external_logger.ExternalServiceLogger
|
||||
var err error
|
||||
if loggingConfig.Enabled {
|
||||
logger, err = external_logger.NewExternalServiceLogger(loggingConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
timeout := 30 * time.Second
|
||||
if cfg.Tianyuanapi.Timeout > 0 {
|
||||
timeout = cfg.Tianyuanapi.Timeout
|
||||
}
|
||||
|
||||
serviceCfg := TianyuanapiConfig{
|
||||
BaseURL: cfg.Tianyuanapi.BaseURL,
|
||||
AccessID: cfg.Tianyuanapi.AccessID,
|
||||
EncryptionKey: cfg.Tianyuanapi.EncryptionKey,
|
||||
Timeout: timeout,
|
||||
}
|
||||
|
||||
return NewTianyuanapiService(serviceCfg, logger), nil
|
||||
}
|
||||
285
internal/infrastructure/external/tianyuanapi/tianyuanapi_service.go
vendored
Normal file
285
internal/infrastructure/external/tianyuanapi/tianyuanapi_service.go
vendored
Normal file
@@ -0,0 +1,285 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hyapi-server/internal/shared/crypto"
|
||||
"hyapi-server/internal/shared/external_logger"
|
||||
)
|
||||
|
||||
const (
|
||||
maxLogParamValueLen = 300
|
||||
maxLogResponseBodyLen = 500
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDatasource = errors.New("数据源异常")
|
||||
ErrSystem = errors.New("系统异常")
|
||||
ErrNotFound = errors.New("查询为空")
|
||||
)
|
||||
|
||||
// TianyuanapiConfig 天远 API 服务配置
|
||||
type TianyuanapiConfig struct {
|
||||
BaseURL string
|
||||
AccessID string
|
||||
EncryptionKey string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// apiResponse 天远平台 HTTP 外层响应
|
||||
type apiResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// requestPayload 请求体
|
||||
type requestPayload struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
// TianyuanapiService 天远平台中转服务
|
||||
type TianyuanapiService struct {
|
||||
config TianyuanapiConfig
|
||||
logger *external_logger.ExternalServiceLogger
|
||||
}
|
||||
|
||||
// NewTianyuanapiService 创建天远 API 服务实例
|
||||
func NewTianyuanapiService(cfg TianyuanapiConfig, logger *external_logger.ExternalServiceLogger) *TianyuanapiService {
|
||||
if cfg.Timeout == 0 {
|
||||
cfg.Timeout = 30 * time.Second
|
||||
}
|
||||
return &TianyuanapiService{
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TianyuanapiService) generateRequestID() string {
|
||||
timestamp := time.Now().UnixNano()
|
||||
hash := md5.Sum([]byte(fmt.Sprintf("%d_%s", timestamp, s.config.AccessID)))
|
||||
return fmt.Sprintf("tianyuanapi_%x", hash[:8])
|
||||
}
|
||||
|
||||
func truncateForLog(str string, maxLen int) string {
|
||||
if maxLen <= 0 || len(str) <= maxLen {
|
||||
return str
|
||||
}
|
||||
return str[:maxLen] + "...[truncated, total " + strconv.Itoa(len(str)) + " chars]"
|
||||
}
|
||||
|
||||
func requestParamsForLog(params map[string]interface{}) map[string]interface{} {
|
||||
if params == nil {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]interface{}, len(params))
|
||||
for k, v := range params {
|
||||
if v == nil {
|
||||
out[k] = nil
|
||||
continue
|
||||
}
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
out[k] = truncateForLog(val, maxLogParamValueLen)
|
||||
default:
|
||||
out[k] = truncateForLog(fmt.Sprint(v), maxLogParamValueLen)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// CallAPI 调用天远平台指定产品(产品编号与处理器 ApiCode 一致)
|
||||
func (s *TianyuanapiService) CallAPI(ctx context.Context, productCode string, params map[string]interface{}) ([]byte, error) {
|
||||
startTime := time.Now()
|
||||
requestID := s.generateRequestID()
|
||||
|
||||
var transactionID string
|
||||
if id, ok := ctx.Value("transaction_id").(string); ok {
|
||||
transactionID = id
|
||||
}
|
||||
|
||||
baseURL := strings.TrimSuffix(s.config.BaseURL, "/")
|
||||
reqURL := fmt.Sprintf("%s/api/v1/%s", baseURL, productCode)
|
||||
|
||||
if s.logger != nil {
|
||||
s.logger.LogRequest(requestID, transactionID, productCode, reqURL)
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, err, map[string]interface{}{"request_params": requestParamsForLog(params)})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
encryptedData, err := crypto.AesEncrypt(jsonData, s.config.EncryptionKey)
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, fmt.Errorf("加密请求失败: %w", err))
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, err, map[string]interface{}{"request_params": requestParamsForLog(params)})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bodyBytes, err := json.Marshal(requestPayload{Data: encryptedData})
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, err, map[string]interface{}{"request_params": requestParamsForLog(params)})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewReader(bodyBytes))
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, err, map[string]interface{}{"request_params": requestParamsForLog(params)})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Access-Id", s.config.AccessID)
|
||||
|
||||
client := &http.Client{Timeout: s.config.Timeout}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
isTimeout := ctx.Err() == context.DeadlineExceeded
|
||||
if !isTimeout {
|
||||
if te, ok := err.(interface{ Timeout() bool }); ok && te.Timeout() {
|
||||
isTimeout = true
|
||||
}
|
||||
}
|
||||
if !isTimeout {
|
||||
es := err.Error()
|
||||
if strings.Contains(es, "deadline exceeded") || strings.Contains(es, "timeout") || strings.Contains(es, "canceled") {
|
||||
isTimeout = true
|
||||
}
|
||||
}
|
||||
if isTimeout {
|
||||
err = errors.Join(ErrDatasource, fmt.Errorf("API请求超时: %v", err))
|
||||
} else {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
}
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, err, map[string]interface{}{"request_params": requestParamsForLog(params)})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
duration := time.Since(startTime)
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
err = errors.Join(ErrSystem, err)
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, err, map[string]interface{}{"request_params": requestParamsForLog(params)})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err = errors.Join(ErrDatasource, fmt.Errorf("HTTP %d", resp.StatusCode))
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, err, map[string]interface{}{
|
||||
"request_params": requestParamsForLog(params),
|
||||
"response_body": truncateForLog(string(raw), maxLogResponseBodyLen),
|
||||
})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if s.logger != nil {
|
||||
s.logger.LogResponse(requestID, transactionID, productCode, resp.StatusCode, duration)
|
||||
}
|
||||
|
||||
var outer apiResponse
|
||||
if err := json.Unmarshal(raw, &outer); err != nil {
|
||||
parseErr := errors.Join(ErrSystem, fmt.Errorf("响应解析失败: %w", err))
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, parseErr, map[string]interface{}{
|
||||
"request_params": requestParamsForLog(params),
|
||||
"response_body": truncateForLog(string(raw), maxLogResponseBodyLen),
|
||||
})
|
||||
}
|
||||
return nil, parseErr
|
||||
}
|
||||
|
||||
if outer.Code != 0 {
|
||||
mappedErr := mapBusinessError(outer.Code, outer.Message)
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, mappedErr, map[string]interface{}{
|
||||
"request_params": requestParamsForLog(params),
|
||||
"response_body": truncateForLog(string(raw), maxLogResponseBodyLen),
|
||||
"api_code": outer.Code,
|
||||
})
|
||||
}
|
||||
if errors.Is(mappedErr, ErrNotFound) {
|
||||
return nil, mappedErr
|
||||
}
|
||||
return nil, mappedErr
|
||||
}
|
||||
|
||||
if outer.Data == "" {
|
||||
return []byte("{}"), nil
|
||||
}
|
||||
|
||||
plain, err := crypto.AesDecrypt(outer.Data, s.config.EncryptionKey)
|
||||
if err != nil {
|
||||
decErr := errors.Join(ErrSystem, fmt.Errorf("解密响应失败: %w", err))
|
||||
if s.logger != nil {
|
||||
s.logger.LogError(requestID, transactionID, productCode, decErr, map[string]interface{}{
|
||||
"request_params": requestParamsForLog(params),
|
||||
"response_body": truncateForLog(string(raw), maxLogResponseBodyLen),
|
||||
})
|
||||
}
|
||||
return nil, decErr
|
||||
}
|
||||
|
||||
if len(plain) == 0 {
|
||||
return []byte("{}"), nil
|
||||
}
|
||||
if !json.Valid(plain) {
|
||||
return plain, nil
|
||||
}
|
||||
return plain, nil
|
||||
}
|
||||
|
||||
func mapBusinessError(code int, message string) error {
|
||||
switch code {
|
||||
case 0:
|
||||
return nil
|
||||
case 1000:
|
||||
if message != "" {
|
||||
return errors.Join(ErrNotFound, errors.New(message))
|
||||
}
|
||||
return ErrNotFound
|
||||
case 1003:
|
||||
if message != "" {
|
||||
return fmt.Errorf("%s", message)
|
||||
}
|
||||
return errors.New("请求参数结构不正确")
|
||||
case 2001:
|
||||
if message != "" {
|
||||
return errors.Join(ErrDatasource, errors.New(message))
|
||||
}
|
||||
return ErrDatasource
|
||||
default:
|
||||
if message != "" {
|
||||
return errors.Join(ErrDatasource, errors.New(message))
|
||||
}
|
||||
return ErrDatasource
|
||||
}
|
||||
}
|
||||
18
internal/infrastructure/external/tianyuanapi/tianyuanapi_service_test.go
vendored
Normal file
18
internal/infrastructure/external/tianyuanapi/tianyuanapi_service_test.go
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMapBusinessError(t *testing.T) {
|
||||
if err := mapBusinessError(0, ""); err != nil {
|
||||
t.Fatalf("code 0 should be nil, got %v", err)
|
||||
}
|
||||
if !errors.Is(mapBusinessError(1000, "查无"), ErrNotFound) {
|
||||
t.Fatal("expected ErrNotFound for 1000")
|
||||
}
|
||||
if !errors.Is(mapBusinessError(2001, "业务失败"), ErrDatasource) {
|
||||
t.Fatal("expected ErrDatasource for 2001")
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
api_repositories "hyapi-server/internal/domains/api/repositories"
|
||||
api_services "hyapi-server/internal/domains/api/services"
|
||||
"hyapi-server/internal/domains/api/services/processors"
|
||||
"hyapi-server/internal/domains/api/services/processors/qygl"
|
||||
"hyapi-server/internal/domains/api/services/processors/qygl/reportstore"
|
||||
"hyapi-server/internal/shared/pdf"
|
||||
)
|
||||
|
||||
@@ -55,13 +55,11 @@ func (h *QYGLReportHandler) GetQYGLReportPage(c *gin.Context) {
|
||||
// 读取查询参数
|
||||
entCode := c.Query("ent_code")
|
||||
entName := c.Query("ent_name")
|
||||
entRegNo := c.Query("ent_reg_no")
|
||||
|
||||
// 组装 QYGLUY3S 入参
|
||||
req := dto.QYGLUY3SReq{
|
||||
EntName: entName,
|
||||
EntRegno: entRegNo,
|
||||
EntCode: entCode,
|
||||
// 组装企业全景(QYGL7HBN)入参
|
||||
req := dto.QYGL7HBNReq{
|
||||
EntName: entName,
|
||||
EntCode: entCode,
|
||||
}
|
||||
|
||||
params, err := json.Marshal(req)
|
||||
@@ -71,12 +69,12 @@ func (h *QYGLReportHandler) GetQYGLReportPage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 通过 ApiRequestService 调用 QYGLJ1U9 聚合处理器
|
||||
// 通过 ApiRequestService 调用 QYGL7HBN 聚合处理器
|
||||
options := &commands.ApiCallOptions{}
|
||||
callCtx := &processors.CallContext{}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
respBytes, err := h.apiRequestService.PreprocessRequestApi(ctx, "QYGLJ1U9", params, options, callCtx)
|
||||
respBytes, err := h.apiRequestService.PreprocessRequestApi(ctx, "QYGL7HBN", params, options, callCtx)
|
||||
if err != nil {
|
||||
h.logger.Error("调用企业全景报告处理器失败", zap.Error(err))
|
||||
c.String(http.StatusInternalServerError, "生成企业报告失败,请稍后重试")
|
||||
@@ -127,7 +125,7 @@ func (h *QYGLReportHandler) GetQYGLReportPageByID(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 回退到进程内存缓存(兼容老的访问方式)
|
||||
report, ok := qygl.GetQYGLReport(id)
|
||||
report, ok := reportstore.GetQYGLReport(id)
|
||||
if !ok {
|
||||
c.String(http.StatusNotFound, "报告不存在或已过期")
|
||||
return
|
||||
@@ -156,7 +154,7 @@ func (h *QYGLReportHandler) qyglReportExists(ctx context.Context, id string) boo
|
||||
return true
|
||||
}
|
||||
}
|
||||
_, ok := qygl.GetQYGLReport(id)
|
||||
_, ok := reportstore.GetQYGLReport(id)
|
||||
return ok
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user