This commit is contained in:
2025-07-28 01:46:39 +08:00
parent b03129667a
commit 357639462a
219 changed files with 21634 additions and 8138 deletions

View File

@@ -44,8 +44,8 @@ func (h *HTTPClient) UpdateConfig(config *Config) {
func (h *HTTPClient) Request(method, urlPath string, body []byte) ([]byte, error) {
// 生成签名所需参数
timestamp := getCurrentTimestamp()
nonce := generateNonce()
date := getCurrentDate()
// date := getCurrentDate()
date := ""
// 计算Content-MD5
contentMD5 := ""
@@ -53,14 +53,14 @@ func (h *HTTPClient) Request(method, urlPath string, body []byte) ([]byte, error
contentMD5 = getContentMD5(body)
}
// 根据Java示例Headers为空字符串
headers := ""
// 生成签名
// 生成签名用原始urlPath
signature := generateSignature(h.config.AppSecret, method, "*/*", contentMD5, "application/json", date, headers, urlPath)
// 创建HTTP请求
url := h.config.ServerURL + urlPath
// 实际请求url用encode后的urlPath
encodedURLPath := encodeURLQueryParams(urlPath)
url := h.config.ServerURL + encodedURLPath
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return nil, fmt.Errorf("创建HTTP请求失败: %v", err)
@@ -69,12 +69,11 @@ func (h *HTTPClient) Request(method, urlPath string, body []byte) ([]byte, error
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-MD5", contentMD5)
req.Header.Set("Date", date)
// req.Header.Set("Date", date)
req.Header.Set("Accept", "*/*")
req.Header.Set("X-Tsign-Open-App-Id", h.config.AppID)
req.Header.Set("X-Tsign-Open-Auth-Mode", "Signature")
req.Header.Set("X-Tsign-Open-Ca-Timestamp", timestamp)
req.Header.Set("X-Tsign-Open-Nonce", nonce)
req.Header.Set("X-Tsign-Open-Ca-Signature", signature)
// 发送请求
@@ -197,3 +196,24 @@ func sortURLQueryParams(urlPath string) string {
}
return basePath
}
// encodeURLQueryParams 对urlPath中的query参数值进行encode
func encodeURLQueryParams(urlPath string) string {
if !strings.Contains(urlPath, "?") {
return urlPath
}
parts := strings.SplitN(urlPath, "?", 2)
basePath := parts[0]
queryString := parts[1]
values, err := url.ParseQuery(queryString)
if err != nil {
return urlPath
}
var encodedPairs []string
for key, vals := range values {
for _, val := range vals {
encodedPairs = append(encodedPairs, key+"="+url.QueryEscape(val))
}
}
return basePath + "?" + strings.Join(encodedPairs, "&")
}