1
This commit is contained in:
8
platformapi/tiktok_shop/sdk-golang/utils/common_util.go
Normal file
8
platformapi/tiktok_shop/sdk-golang/utils/common_util.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package utils
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func MarshalNoErr(object interface{}) string {
|
||||
bs, _ := json.Marshal(object)
|
||||
return string(bs)
|
||||
}
|
||||
95
platformapi/tiktok_shop/sdk-golang/utils/log_id.go
Normal file
95
platformapi/tiktok_shop/sdk-golang/utils/log_id.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"math/rand"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// 目前版本为 02
|
||||
version = "02"
|
||||
length = 53
|
||||
maxRandNum = 1<<24 - 1<<20
|
||||
)
|
||||
|
||||
// LogID represents a logID generator
|
||||
type LogID struct{}
|
||||
|
||||
// NewLogID create a new LogID instance
|
||||
func NewLogID() LogID {
|
||||
return LogID{}
|
||||
}
|
||||
|
||||
// GenLogID return a new logID string
|
||||
func (l LogID) GenLogID() string {
|
||||
rand.Uint32()
|
||||
r := randUint32n(maxRandNum) + 1<<20
|
||||
sb := strings.Builder{}
|
||||
sb.Grow(length)
|
||||
sb.WriteString(version)
|
||||
sb.WriteString(strconv.FormatUint(uint64(getMSTimestamp()), 10))
|
||||
sb.Write(localIP)
|
||||
sb.WriteString(strconv.FormatUint(uint64(r), 16))
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
var defaultLogID LogID
|
||||
|
||||
func init() {
|
||||
defaultLogID = NewLogID()
|
||||
}
|
||||
|
||||
// GenLogID return a new logID
|
||||
func GenLogID() string {
|
||||
return defaultLogID.GenLogID()
|
||||
}
|
||||
|
||||
func randUint32n(n uint32) uint32 {
|
||||
return rand.Uint32() % n
|
||||
}
|
||||
|
||||
const (
|
||||
// IPUnknown represents unknown ip
|
||||
// 32 * 0
|
||||
IPUnknown = "00000000000000000000000000000000"
|
||||
)
|
||||
|
||||
var localIP []byte
|
||||
|
||||
func init() {
|
||||
localIP = formatIP(getLocalIp())
|
||||
}
|
||||
|
||||
// getMSTimestamp return the millisecond timestamp
|
||||
func getMSTimestamp() int64 {
|
||||
return time.Now().UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
|
||||
func getLocalIp() net.IP {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
for _, address := range addrs {
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
return ipnet.IP
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func formatIP(ip net.IP) []byte {
|
||||
if ip == nil {
|
||||
return []byte(IPUnknown)
|
||||
}
|
||||
dst := make([]byte, 32)
|
||||
i := ip.To16()
|
||||
hex.Encode(dst, i)
|
||||
return dst
|
||||
}
|
||||
71
platformapi/tiktok_shop/sdk-golang/utils/sign_util.go
Normal file
71
platformapi/tiktok_shop/sdk-golang/utils/sign_util.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Sign 计算签名
|
||||
func Sign(appKey, appSecret, method string, timestamp int64, paramJson string) string {
|
||||
// 按给定规则拼接参数
|
||||
paramPattern := "app_key" + appKey + "method" + method + "param_json" + paramJson + "timestamp" + strconv.FormatInt(timestamp, 10) + "v2"
|
||||
signPattern := appSecret + paramPattern + appSecret
|
||||
fmt.Println("sign_pattern:" + signPattern)
|
||||
return Hmac(signPattern, appSecret)
|
||||
}
|
||||
|
||||
func SpiSign(appKey, appSecret, timestamp string, paramJson interface{}, signMethod string) string {
|
||||
sortedParamStr := Marshal(paramJson)
|
||||
paramPattern := "app_key" + appKey + "param_json" + sortedParamStr +"timestamp" + timestamp
|
||||
signPattern := appSecret + paramPattern + appSecret
|
||||
if signMethod == "hmac-sha256" {
|
||||
return Hmac(signPattern, appSecret)
|
||||
} else {
|
||||
return Md5(signPattern)
|
||||
}
|
||||
}
|
||||
|
||||
// Hmac 计算hmac
|
||||
func Hmac(s string, appSecret string) string {
|
||||
h := hmac.New(sha256.New, []byte(appSecret))
|
||||
_, _ = h.Write([]byte(s))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
func Md5(s string) string {
|
||||
h := md5.New()
|
||||
_, _ = io.WriteString(h, s)
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Marshal 序列化参数
|
||||
func Marshal(o interface{}) string {
|
||||
// 序列化一次
|
||||
raw, _ := json.Marshal(o)
|
||||
|
||||
// 反序列化为map
|
||||
m := make(map[string]interface{})
|
||||
reader := bytes.NewReader(raw)
|
||||
decode := json.NewDecoder(reader)
|
||||
decode.UseNumber()
|
||||
_ = decode.Decode(&m)
|
||||
|
||||
// 重新做一次序列化,并禁用Html Escape
|
||||
buffer := bytes.NewBufferString("")
|
||||
encoder := json.NewEncoder(buffer)
|
||||
encoder.SetEscapeHTML(false)
|
||||
_ = encoder.Encode(m)
|
||||
|
||||
marshal := strings.TrimSpace(buffer.String()) // Trim掉末尾的换行符
|
||||
return marshal
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user