http/internal/utils.go

75 lines
1.2 KiB
Go
Raw Normal View History

2021-08-16 18:30:13 +08:00
/**
2022-10-31 17:22:49 +08:00
* @Author: Echo
* @Email: 1711788888@qq.com
2021-08-16 18:30:13 +08:00
* @Date: 2021/8/16 3:47 下午
* @Desc: TODO
*/
package internal
import (
"encoding/json"
"net/url"
"strings"
)
const fileUploadingKey = "@file:"
func BuildParams(params interface{}) string {
switch v := params.(type) {
case string:
return v
case []byte:
return string(v)
case []interface{}:
if len(v) > 0 {
params = v[0]
} else {
params = nil
}
}
2022-10-31 17:22:49 +08:00
2021-08-16 18:30:13 +08:00
m := make(map[string]interface{})
2022-10-31 17:22:49 +08:00
2021-08-16 18:30:13 +08:00
if params != nil {
if b, err := json.Marshal(params); err != nil {
return String(params)
} else if err = json.Unmarshal(b, &m); err != nil {
return String(params)
}
} else {
return ""
}
2022-10-31 17:22:49 +08:00
2021-08-16 18:30:13 +08:00
urlEncode := true
2022-10-31 17:22:49 +08:00
2021-08-16 18:30:13 +08:00
if len(m) == 0 {
return String(params)
}
2022-10-31 17:22:49 +08:00
2021-08-16 18:30:13 +08:00
for k, v := range m {
if strings.Contains(k, fileUploadingKey) || strings.Contains(String(v), fileUploadingKey) {
urlEncode = false
break
}
}
2022-10-31 17:22:49 +08:00
2021-08-16 18:30:13 +08:00
var (
s = ""
str = ""
)
2022-10-31 17:22:49 +08:00
2021-08-16 18:30:13 +08:00
for k, v := range m {
if len(str) > 0 {
str += "&"
}
s = String(v)
if urlEncode && len(s) > len(fileUploadingKey) && strings.Compare(s[0:len(fileUploadingKey)], fileUploadingKey) != 0 {
s = url.QueryEscape(s)
}
str += k + "=" + s
}
2022-10-31 17:22:49 +08:00
2021-08-16 18:30:13 +08:00
return str
}