enterprise/base/util/util.go

45 lines
973 B
Go

package util
import (
"encoding/base64"
"fmt"
qrcode "github.com/skip2/go-qrcode"
"github.com/spf13/cast"
"math/rand"
"time"
)
func RandomStr(length int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
func CutTail(str string, length int) string {
if len(str) <= length {
return str
}
return str[0:length]
}
func FloatCut(f float64) float64 {
return cast.ToFloat64(fmt.Sprintf("%.2f", f))
}
func CreateQrcodeBase64V2(content string, level qrcode.RecoveryLevel, size int) (string, error) {
var png []byte
//固定方法
png, err := qrcode.Encode(content, level, size)
if err != nil {
return "", err
}
//文件流需要使用base64编码后才可使用
return base64.StdEncoding.EncodeToString(png), nil
}