代码提交

This commit is contained in:
2026-10-16 12:28:09 +08:00
parent 75a0d00258
commit 5c5f5908aa
3 changed files with 17 additions and 7 deletions
+13 -3
View File
@@ -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
}