This commit is contained in:
邹宗楠
2022-08-30 16:02:32 +08:00
parent 2d9e28ea60
commit 9ecef6ff3a
358 changed files with 18529 additions and 1 deletions

View File

@@ -0,0 +1,8 @@
package utils
import "encoding/json"
func MarshalNoErr(object interface{}) string {
bs, _ := json.Marshal(object)
return string(bs)
}

View 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
}

View 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
}