154 lines
2.8 KiB
Go
154 lines
2.8 KiB
Go
package lib
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"strconv"
|
|
"time"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
"legu.airobot/pb"
|
|
)
|
|
|
|
func Md5(content string) (md string) {
|
|
h := md5.New()
|
|
_, _ = io.WriteString(h, content)
|
|
md = fmt.Sprintf("%x", h.Sum(nil))
|
|
return
|
|
}
|
|
|
|
func Base64Encode(data []byte) string {
|
|
return base64.StdEncoding.EncodeToString(data)
|
|
}
|
|
|
|
func Base64Decode(data string) string {
|
|
b, err := base64.StdEncoding.DecodeString(data)
|
|
if err != nil {
|
|
fmt.Errorf("base64 decode", err)
|
|
return ""
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func BuildSecStr(sid, account string) string {
|
|
jsonByte, _ := jsoniter.Marshal(&LoginParam{
|
|
Account: account,
|
|
ServerId: sid,
|
|
TimeStamp: time.Now().Unix(),
|
|
})
|
|
jsonBase64 := Base64Encode(jsonByte)
|
|
// log.Printf("client base64:%s", jsonBase64)
|
|
clientMd5key := Md5(jsonBase64)
|
|
// log.Printf("client md5:%s", clientMd5key)
|
|
return fmt.Sprintf("CE:%s%s", clientMd5key, jsonBase64)
|
|
}
|
|
|
|
func ProtoMarshal(rsp proto.Message, msg *pb.UserMessage) (ok bool) {
|
|
any, err := anypb.New(rsp)
|
|
if err != nil {
|
|
fmt.Printf("Any New %s.%s %v", msg.MainType, msg.SubType, err)
|
|
return
|
|
}
|
|
msg.Data = any
|
|
return true
|
|
}
|
|
|
|
func ProtoUnmarshal(msg *pb.UserMessage, req proto.Message) (ok bool) {
|
|
err := msg.Data.UnmarshalTo(req)
|
|
if err != nil {
|
|
fmt.Printf("UnmarshalTo %s.%s %v\n", msg.MainType, msg.SubType, err)
|
|
return
|
|
}
|
|
return true
|
|
}
|
|
|
|
func SubStr(str string, start int, length int) (result string) {
|
|
s := []rune(str)
|
|
total := len(s)
|
|
if total == 0 {
|
|
return
|
|
}
|
|
// 允许从尾部开始计算
|
|
if start < 0 {
|
|
start = total + start
|
|
if start < 0 {
|
|
return
|
|
}
|
|
}
|
|
if start > total {
|
|
return
|
|
}
|
|
// 到末尾
|
|
if length < 0 {
|
|
length = total
|
|
}
|
|
|
|
end := start + length
|
|
if end > total {
|
|
result = string(s[start:])
|
|
} else {
|
|
result = string(s[start:end])
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func DeleteString(list []string, ele string) []string {
|
|
result := make([]string, 0)
|
|
for _, v := range list {
|
|
if v != ele {
|
|
result = append(result, v)
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
func ConvertFileSize(size int64) string {
|
|
if size == 0 {
|
|
return "0"
|
|
}
|
|
|
|
kb := float64(size) / float64(1024)
|
|
|
|
if kb < 1024 {
|
|
f := math.Ceil(kb)
|
|
return strconv.Itoa(int(f)) + " KB"
|
|
}
|
|
|
|
mb := kb / float64(1024)
|
|
if mb < 1024 {
|
|
f := math.Ceil(mb)
|
|
return strconv.Itoa(int(f)) + " MB"
|
|
}
|
|
|
|
gb := mb / float64(1024)
|
|
if gb < 1024 {
|
|
f := math.Ceil(mb)
|
|
return strconv.Itoa(int(f)) + " G"
|
|
}
|
|
|
|
t := gb / float64(1024)
|
|
if t < 1024 {
|
|
f := math.Ceil(mb)
|
|
return strconv.Itoa(int(f)) + " T"
|
|
}
|
|
|
|
if t >= 1024 {
|
|
return "VeryBig"
|
|
}
|
|
|
|
return "0"
|
|
}
|
|
|
|
// 保留两位小数 通用
|
|
func FormatFloatCommon(num float64) float64 {
|
|
value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", num), 64)
|
|
return value
|
|
}
|