代码提交
This commit is contained in:
@@ -79,6 +79,16 @@ func GetRandomLetters(n int) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// GetRandomAlphanumeric 生成 n 位大小写字母 + 数字随机串
|
||||
func GetRandomAlphanumeric(n int) string {
|
||||
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = chars[rand.IntN(len(chars))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func GetSlugInfo(text string) string {
|
||||
return SanitizeSlug(slug.Make(text))
|
||||
}
|
||||
@@ -114,12 +124,12 @@ func IsSlugAllDigits(s string) bool {
|
||||
}
|
||||
|
||||
// RandomSlugLength 随机 slug 长度
|
||||
const RandomSlugLength = 8
|
||||
const RandomSlugLength = 10
|
||||
|
||||
// MakeRandomSlug 生成唯一的随机 slug(8 位小写字母);exists 返回 true 表示已被占用。
|
||||
// MakeRandomSlug 生成唯一的随机 slug(10 位大小写字母+数字混合);exists 返回 true 表示已被占用。
|
||||
func MakeRandomSlug(exists func(string) bool) string {
|
||||
for {
|
||||
s := strings.ToLower(GetRandomLetters(RandomSlugLength))
|
||||
s := GetRandomAlphanumeric(RandomSlugLength)
|
||||
if !exists(s) {
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user