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, "") }