This commit is contained in:
Mrx
2026-06-12 14:45:09 +08:00
parent 97f17e0cb7
commit 2fff537bfc

View File

@@ -2,7 +2,6 @@ package shujubao
import ( import (
"context" "context"
"crypto/md5"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -67,6 +66,7 @@ type ShujubaoResp struct {
Message string `json:"message"` Message string `json:"message"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
Success bool `json:"success"` Success bool `json:"success"`
SeqNo string `json:"seqNo"`
} }
// ShujubaoConfig 数据宝服务配置 // ShujubaoConfig 数据宝服务配置
@@ -102,13 +102,6 @@ func NewShujubaoService(url, appSecret string, signMethod SignMethod, timeout ti
} }
} }
// generateRequestID 生成请求 ID
func (s *ShujubaoService) generateRequestID() string {
timestamp := time.Now().UnixNano()
hash := md5.Sum([]byte(fmt.Sprintf("%d_%s", timestamp, s.config.AppSecret)))
return fmt.Sprintf("shujubao_%x", hash[:8])
}
// buildSortedParamStr 将入参按 key 的 ASCII 排序组合为 key1=value1&key2=value2&... // buildSortedParamStr 将入参按 key 的 ASCII 排序组合为 key1=value1&key2=value2&...
func buildSortedParamStr(params map[string]interface{}) string { func buildSortedParamStr(params map[string]interface{}) string {
if len(params) == 0 { if len(params) == 0 {
@@ -203,7 +196,6 @@ func (s *ShujubaoService) buildRequestURL(apiPath string) string {
// CallAPI 调用数据宝 APIPOST。最终请求地址 = url + 拼接接口地址值body 为业务参数sign、timestamp 按原样传 header。 // CallAPI 调用数据宝 APIPOST。最终请求地址 = url + 拼接接口地址值body 为业务参数sign、timestamp 按原样传 header。
func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params map[string]interface{}) (data interface{}, err error) { func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params map[string]interface{}) (data interface{}, err error) {
startTime := time.Now() startTime := time.Now()
requestID := s.generateRequestID()
timestamp := strconv.FormatInt(time.Now().Unix(), 10) timestamp := strconv.FormatInt(time.Now().Unix(), 10)
// 最终请求 URL = https://api.chinadatapay.com/communication + 拼接接口地址值,如 /personal/197 // 最终请求 URL = https://api.chinadatapay.com/communication + 拼接接口地址值,如 /personal/197
@@ -215,7 +207,7 @@ func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params ma
} }
if s.logger != nil { if s.logger != nil {
s.logger.LogRequest(requestID, transactionID, apiPath, requestURL) s.logger.LogRequest("", transactionID, apiPath, requestURL)
} }
// 使用 application/x-www-form-urlencoded贵司接口暂不支持 JSON 入参 // 使用 application/x-www-form-urlencoded贵司接口暂不支持 JSON 入参
@@ -225,7 +217,7 @@ func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params ma
if err != nil { if err != nil {
err = errors.Join(ErrSystem, err) err = errors.Join(ErrSystem, err)
if s.logger != nil { if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, paramsForLog(params)) s.logger.LogError("", transactionID, apiPath, err, paramsForLog(params))
} }
return nil, err return nil, err
} }
@@ -254,7 +246,7 @@ func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params ma
err = errors.Join(ErrSystem, err) err = errors.Join(ErrSystem, err)
} }
if s.logger != nil { if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, paramsForLog(params)) s.logger.LogError("", transactionID, apiPath, err, paramsForLog(params))
} }
return nil, err return nil, err
} }
@@ -264,20 +256,18 @@ func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params ma
if err != nil { if err != nil {
err = errors.Join(ErrSystem, err) err = errors.Join(ErrSystem, err)
if s.logger != nil { if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, paramsForLog(params)) s.logger.LogError("", transactionID, apiPath, err, paramsForLog(params))
} }
return nil, err return nil, err
} }
if s.logger != nil {
duration := time.Since(startTime) duration := time.Since(startTime)
s.logger.LogResponse(requestID, transactionID, apiPath, response.StatusCode, duration)
}
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
err = errors.Join(ErrDatasource, fmt.Errorf("HTTP状态码 %d", response.StatusCode)) err = errors.Join(ErrDatasource, fmt.Errorf("HTTP状态码 %d", response.StatusCode))
if s.logger != nil { if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, paramsForLog(params)) s.logger.LogResponse("", transactionID, apiPath, response.StatusCode, duration)
s.logger.LogError("", transactionID, apiPath, err, paramsForLog(params))
} }
return nil, err return nil, err
} }
@@ -286,11 +276,17 @@ func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params ma
if err := json.Unmarshal(respBody, &shujubaoResp); err != nil { if err := json.Unmarshal(respBody, &shujubaoResp); err != nil {
err = errors.Join(ErrSystem, fmt.Errorf("响应解析失败: %w", err)) err = errors.Join(ErrSystem, fmt.Errorf("响应解析失败: %w", err))
if s.logger != nil { if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, paramsForLog(params)) s.logger.LogResponse("", transactionID, apiPath, response.StatusCode, duration)
s.logger.LogError("", transactionID, apiPath, err, paramsForLog(params))
} }
return nil, err return nil, err
} }
seqNo := shujubaoResp.SeqNo
if s.logger != nil {
s.logger.LogResponse(seqNo, transactionID, apiPath, response.StatusCode, duration)
}
code := shujubaoResp.Code code := shujubaoResp.Code
// 成功码只有这三类:其它 code 都走统一错误映射返回 // 成功码只有这三类:其它 code 都走统一错误映射返回
@@ -299,12 +295,12 @@ func (s *ShujubaoService) CallAPI(ctx context.Context, apiPath string, params ma
if queryEmptyErr := GetQueryEmptyErrByCode(code); queryEmptyErr != nil { if queryEmptyErr := GetQueryEmptyErrByCode(code); queryEmptyErr != nil {
err = errors.Join(queryEmptyErr, shujubaoErr) err = errors.Join(queryEmptyErr, shujubaoErr)
if s.logger != nil { if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, err, paramsForLog(params)) s.logger.LogError(seqNo, transactionID, apiPath, err, paramsForLog(params))
} }
return nil, err return nil, err
} }
if s.logger != nil { if s.logger != nil {
s.logger.LogError(requestID, transactionID, apiPath, shujubaoErr, paramsForLog(params)) s.logger.LogError(seqNo, transactionID, apiPath, shujubaoErr, paramsForLog(params))
} }
return nil, errors.Join(ErrDatasource, shujubaoErr) return nil, errors.Join(ErrDatasource, shujubaoErr)
} }