123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package utils
- import (
- "crypto/md5"
- "encoding/base64"
- "encoding/hex"
- "io/ioutil"
- "math/rand"
- "os"
- "strings"
- "time"
- "github.com/google/uuid"
- )
- func Hmac(data string) string {
- h := md5.New()
- h.Write([]byte(data))
- return hex.EncodeToString(h.Sum(nil))
- }
- func IsStringEmpty(str string) bool {
- return strings.Trim(str, " ") == ""
- }
- func GetUUID() string {
- u := uuid.New()
- return strings.ReplaceAll(u.String(), "-", "")
- }
- func PathExists(path string) bool {
- _, err := os.Stat(path)
- if err == nil {
- return true
- }
- if os.IsNotExist(err) {
- return false
- }
- return false
- }
- func Base64ToImage(imageBase64 string) ([]byte, error) {
- image, err := base64.StdEncoding.DecodeString(imageBase64)
- if err != nil {
- return nil, err
- }
- return image, nil
- }
- func GetDirFiles(dir string) ([]string, error) {
- dirList, err := ioutil.ReadDir(dir)
- if err != nil {
- return nil, err
- }
- filesRet := make([]string, 0)
- for _, file := range dirList {
- if file.IsDir() {
- files, err := GetDirFiles(dir + string(os.PathSeparator) + file.Name())
- if err != nil {
- return nil, err
- }
- filesRet = append(filesRet, files...)
- } else {
- filesRet = append(filesRet, dir+string(os.PathSeparator)+file.Name())
- }
- }
- return filesRet, nil
- }
- func GetCurrentTimeStamp() int64 {
- return time.Now().UnixNano() / 1e6
- }
- // slice去重
- func RemoveRepByMap(slc []string) []string {
- result := []string{}
- tempMap := map[string]byte{}
- for _, e := range slc {
- l := len(tempMap)
- tempMap[e] = 0
- if len(tempMap) != l {
- result = append(result, e)
- }
- }
- return result
- }
- // #取得随机字符串:通过打乱slice来操作
- func GetRandString(length int, char string, rand_x int64) string {
- if length < 1 {
- return ""
- }
- if len(char) <= 6 || len(char) <= length {
- char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
- }
- charArr := strings.Split(char, "")
- ran := rand.New(rand.NewSource(time.Now().UnixMilli() + rand_x))
- l := len(charArr)
- for i := l - 1; i > 0; i-- {
- r := ran.Intn(i)
- charArr[r], charArr[i] = charArr[i], charArr[r]
- }
- rchar := charArr[:length]
- return strings.Join(rchar, "")
- }
|