You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
749 B
Go

8 months ago
package utils
import (
"context"
"gold-shop/global"
"math/rand"
"strconv"
"strings"
"time"
)
func GenerateNo(noType string) string {
now := time.Now()
key := "no_" + noType + ":" + now.Format("200601021504")
cxt := context.Background()
val := global.Redis.Incr(cxt, key).Val()
global.Redis.ExpireAt(cxt, key, now.Add(time.Minute))
valStr := strconv.Itoa(int(val))
l := len(valStr)
valStr = strings.Repeat("0", l) + valStr
return now.Format("20060102150405") + valStr
}
func GenerateRandomString(length int) string {
charSet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
result := make([]byte, length)
for i := 0; i < length; i++ {
result[i] = charSet[rand.Intn(len(charSet))]
}
return string(result)
}