first commit
This commit is contained in:
156
pkg/helper/Helper.go
Normal file
156
pkg/helper/Helper.go
Normal file
@ -0,0 +1,156 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// 生成header
|
||||
func GenerateApiHeader(jsonBody string) map[string]string {
|
||||
|
||||
sign := GenerateSign(jsonBody)
|
||||
|
||||
headerMap := getCommonHeaderMap()
|
||||
|
||||
headerMap["Signature"] = sign
|
||||
|
||||
return headerMap
|
||||
}
|
||||
|
||||
// 根据body生成签名
|
||||
func GenerateSign(jsonBody string) string {
|
||||
|
||||
var mapRequest map[string]string
|
||||
|
||||
err := json.Unmarshal([]byte(jsonBody), &mapRequest)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
commonHeaderMap := getCommonHeaderMap()
|
||||
|
||||
for k, v := range commonHeaderMap {
|
||||
mapRequest[k] = v
|
||||
}
|
||||
sign := createSign(mapRequest)
|
||||
return sign
|
||||
}
|
||||
|
||||
// 获取公共的header字段
|
||||
func getCommonHeaderMap() map[string]string {
|
||||
|
||||
headerMap := make(map[string]string, 0)
|
||||
headerMap["ClientVersion"] = "1.0.1"
|
||||
headerMap["DeviceId"] = "MWEB"
|
||||
headerMap["Platform"] = "H5"
|
||||
headerMap["MarketChannel"] = ""
|
||||
headerMap["DeviceModel"] = "MWEB"
|
||||
headerMap["ApiVersionNum"] = viper.GetString("api-num")
|
||||
headerMap["TimeStamp"] = strconv.Itoa(time.Now().Second())
|
||||
headerMap["BundleId"] = "meetalkH5"
|
||||
headerMap["OsVersion"] = "10.01"
|
||||
headerMap["INNER-TOKEN"] = "b028c52286c95de48a1c773d7ed02d04"
|
||||
|
||||
return headerMap
|
||||
}
|
||||
|
||||
func createSign(params map[string]string) string {
|
||||
|
||||
keys := make([]string, 20)
|
||||
|
||||
for key, _ := range params {
|
||||
if key == "Signature" {
|
||||
continue
|
||||
}
|
||||
keys = append(keys, key)
|
||||
}
|
||||
//按key升序
|
||||
sort.Strings(keys)
|
||||
|
||||
//把KEY值合并为字符串
|
||||
waitSignString := ""
|
||||
for _, value := range keys {
|
||||
|
||||
waitSignString += base64.StdEncoding.EncodeToString([]byte(params[value]))
|
||||
}
|
||||
|
||||
//加上密钥
|
||||
appSecret := "x63363eacf804b4394a120aea240fd9a"
|
||||
waitSignString += appSecret
|
||||
|
||||
//sha1加密
|
||||
t := sha1.New()
|
||||
io.WriteString(t, waitSignString)
|
||||
sign := t.Sum(nil)
|
||||
|
||||
return hex.EncodeToString(sign)
|
||||
|
||||
}
|
||||
|
||||
// 复杂http请求
|
||||
func HttpDo(httpMethod string, url string, headerMap map[string]string, rawBody string) (remoteResponse string, err error) {
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
req, err0 := http.NewRequest(httpMethod, url, strings.NewReader(rawBody))
|
||||
|
||||
if err0 != nil {
|
||||
|
||||
err = err0
|
||||
return
|
||||
}
|
||||
|
||||
if len(headerMap) > 0 {
|
||||
for k, v := range headerMap {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
resp, err1 := client.Do(req)
|
||||
|
||||
if err1 != nil {
|
||||
err = err1
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err2 := ioutil.ReadAll(resp.Body)
|
||||
if err2 != nil {
|
||||
err = err2
|
||||
return
|
||||
}
|
||||
|
||||
remoteResponse = string(body)
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func StringToMD5(waitMD5string string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(waitMD5string))
|
||||
cipherStr := h.Sum(nil)
|
||||
result := hex.EncodeToString(cipherStr)
|
||||
return result
|
||||
}
|
||||
|
||||
// SHA1加密
|
||||
func StringToSHA1(waitMD5string string) string {
|
||||
h := sha1.New()
|
||||
h.Write([]byte(waitMD5string))
|
||||
cipherStr := h.Sum(nil)
|
||||
result := hex.EncodeToString(cipherStr)
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user