add JRZQ09J8、FLXGDEA8、FLXGDEA9、JRZQ1D09

add external_services log
This commit is contained in:
2025-08-25 15:44:06 +08:00
parent 365a2a8886
commit 267ff92998
80 changed files with 5555 additions and 1254 deletions

View File

@@ -2,8 +2,10 @@ package yushan
import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/hex"
@@ -15,6 +17,8 @@ import (
"strings"
"time"
"tyapi-server/internal/shared/external_logger"
"github.com/tidwall/gjson"
)
@@ -32,21 +36,37 @@ type YushanConfig struct {
type YushanService struct {
config YushanConfig
logger *external_logger.ExternalServiceLogger
}
// NewWestDexService 是一个构造函数,用于初始化 WestDexService
func NewYushanService(url, apiKey, acctID string) *YushanService {
// NewYushanService 是一个构造函数,用于初始化 YushanService
func NewYushanService(url, apiKey, acctID string, logger *external_logger.ExternalServiceLogger) *YushanService {
return &YushanService{
config: YushanConfig{
URL: url,
ApiKey: apiKey,
AcctID: acctID,
},
logger: logger,
}
}
// CallAPI 调用西部数据的 API
func (y *YushanService) CallAPI(code string, params map[string]interface{}) (respBytes []byte, err error) {
// CallAPI 调用羽山数据的 API
func (y *YushanService) CallAPI(ctx context.Context, code string, params map[string]interface{}) (respBytes []byte, err error) {
startTime := time.Now()
requestID := y.generateRequestID()
// 从ctx中获取transactionId
var transactionID string
if ctxTransactionID, ok := ctx.Value("transaction_id").(string); ok {
transactionID = ctxTransactionID
}
// 记录请求日志
if y.logger != nil {
y.logger.LogRequest(requestID, code, y.config.URL, y.buildLogData(params, transactionID))
}
// 获取当前时间戳
unixMilliseconds := time.Now().UnixNano() / int64(time.Millisecond)
@@ -64,13 +84,21 @@ func (y *YushanService) CallAPI(code string, params map[string]interface{}) (res
// 将请求数据转换为 JSON 字节数组
messageBytes, err := json.Marshal(reqData)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
if y.logger != nil {
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
}
return nil, err
}
// 获取 API 密钥
key, err := hex.DecodeString(y.config.ApiKey)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
if y.logger != nil {
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
}
return nil, err
}
// 使用 AES CBC 加密请求数据
@@ -80,10 +108,16 @@ func (y *YushanService) CallAPI(code string, params map[string]interface{}) (res
content := base64.StdEncoding.EncodeToString(cipherText)
// 发起 HTTP 请求
client := &http.Client{}
req, err := http.NewRequest("POST", y.config.URL, strings.NewReader(content))
client := &http.Client{
Timeout: 20 * time.Second,
}
req, err := http.NewRequestWithContext(ctx, "POST", y.config.URL, strings.NewReader(content))
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
if y.logger != nil {
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
}
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("ACCT_ID", y.config.AcctID)
@@ -91,13 +125,20 @@ func (y *YushanService) CallAPI(code string, params map[string]interface{}) (res
// 执行请求
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
if y.logger != nil {
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
}
return nil, err
}
defer resp.Body.Close()
// 读取响应体
body, err := io.ReadAll(resp.Body)
if err != nil {
if y.logger != nil {
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
}
return nil, err
}
@@ -108,12 +149,22 @@ func (y *YushanService) CallAPI(code string, params map[string]interface{}) (res
} else {
sDec, err := base64.StdEncoding.DecodeString(string(body))
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrSystem, err.Error())
err = fmt.Errorf("%w: %s", ErrSystem, err.Error())
if y.logger != nil {
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
}
return nil, err
}
respData = y.AES_CBC_Decrypt(sDec, key)
}
retCode := gjson.GetBytes(respData, "retcode").String()
// 记录响应日志
if y.logger != nil {
duration := time.Since(startTime)
y.logger.LogResponse(requestID, code, resp.StatusCode, respData, duration)
}
if retCode == "100000" {
// retcode 为 100000表示查询为空
return nil, ErrNotFound
@@ -121,13 +172,41 @@ func (y *YushanService) CallAPI(code string, params map[string]interface{}) (res
// retcode 为 000000表示有数据返回 retdata
retData := gjson.GetBytes(respData, "retdata")
if !retData.Exists() {
return nil, fmt.Errorf("%w: %s", ErrDatasource, "羽山请求retdata为空")
err = fmt.Errorf("%w: %s", ErrDatasource, "羽山请求retdata为空")
if y.logger != nil {
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
}
return nil, err
}
return []byte(retData.Raw), nil
} else {
return nil, fmt.Errorf("%w: %s", ErrDatasource, "羽山请求未知的状态码")
err = fmt.Errorf("%w: %s", ErrDatasource, "羽山请求未知的状态码")
if y.logger != nil {
y.logger.LogError(requestID, code, err, y.buildLogData(params, transactionID))
}
return nil, err
}
}
// generateRequestID 生成请求ID
func (y *YushanService) generateRequestID() string {
timestamp := time.Now().UnixNano()
hash := md5.Sum([]byte(fmt.Sprintf("%d_%s", timestamp, y.config.ApiKey)))
return fmt.Sprintf("yushan_%x", hash[:8])
}
// buildLogData 构建包含transactionId的日志数据
func (y *YushanService) buildLogData(data map[string]interface{}, transactionID string) map[string]interface{} {
if transactionID == "" {
return data
}
logData := data
if logData == nil {
logData = make(map[string]interface{})
}
logData["transaction_id"] = transactionID
return logData
}
// GenerateRandomString 生成一个32位的随机字符串订单号