This commit is contained in:
2025-12-31 17:46:03 +08:00
parent fe44b452e3
commit 1d4411a940
7 changed files with 70 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"strings"
)
// SignMethod 签名方法类型
@@ -38,7 +39,7 @@ func GenerateSign(timestamp string, appSecret string, signMethod SignMethod) (st
}
// 将二进制转化为大写的十六进制正确签名应该为32大写字符串
return hex.EncodeToString(hashBytes), nil
return strings.ToUpper(hex.EncodeToString(hashBytes)), nil
}
// GenerateSignWithDefault 使用默认的 HMAC-MD5 方法生成签名

View File

@@ -10,6 +10,7 @@ import (
"io"
"net/http"
"strconv"
"strings"
"time"
"tyapi-server/internal/shared/external_logger"
@@ -76,9 +77,10 @@ func (j *JiguangService) generateRequestID() string {
}
// CallAPI 调用极光API
// apiCode: API服务编码如 marriage-single-v2
// apiCode: API服务编码如 marriage-single-v2,用于请求头
// apiPath: API路径如 marriage/single-v2用于URL路径
// params: 请求参数会作为JSON body发送
func (j *JiguangService) CallAPI(ctx context.Context, apiCode string, params map[string]interface{}) (resp []byte, err error) {
func (j *JiguangService) CallAPI(ctx context.Context, apiCode string, apiPath string, params map[string]interface{}) (resp []byte, err error) {
startTime := time.Now()
requestID := j.generateRequestID()
@@ -101,9 +103,12 @@ func (j *JiguangService) CallAPI(ctx context.Context, apiCode string, params map
return nil, err
}
// 构建完整的请求URL使用apiPath作为路径
requestURL := strings.TrimSuffix(j.config.URL, "/") + "/" + strings.TrimPrefix(apiPath, "/")
// 记录请求日志
if j.logger != nil {
j.logger.LogRequest(requestID, transactionID, apiCode, j.config.URL, params)
j.logger.LogRequest(requestID, transactionID, apiCode, requestURL, params)
}
// 将请求参数转换为JSON
@@ -117,7 +122,7 @@ func (j *JiguangService) CallAPI(ctx context.Context, apiCode string, params map
}
// 创建HTTP POST请求
req, newRequestErr := http.NewRequestWithContext(ctx, "POST", j.config.URL, bytes.NewBuffer(jsonData))
req, newRequestErr := http.NewRequestWithContext(ctx, "POST", requestURL, bytes.NewBuffer(jsonData))
if newRequestErr != nil {
err = errors.Join(ErrSystem, newRequestErr)
if j.logger != nil {