专题和短剧 slug 改为 8 位随机小写字母
后台新建/修改(未填 slug)和媒资入库时生成随机 slug,不再按标题拼音生成;标签、分类不变。 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -113,6 +113,19 @@ func IsSlugAllDigits(s string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RandomSlugLength 随机 slug 长度
|
||||||
|
const RandomSlugLength = 8
|
||||||
|
|
||||||
|
// MakeRandomSlug 生成唯一的随机 slug(8 位小写字母);exists 返回 true 表示已被占用。
|
||||||
|
func MakeRandomSlug(exists func(string) bool) string {
|
||||||
|
for {
|
||||||
|
s := strings.ToLower(GetRandomLetters(RandomSlugLength))
|
||||||
|
if !exists(s) {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MakeUniqueSlug 生成唯一 slug;exists 返回 true 表示已被占用。生成结果不能全是数字。
|
// MakeUniqueSlug 生成唯一 slug;exists 返回 true 表示已被占用。生成结果不能全是数字。
|
||||||
func MakeUniqueSlug(source string, exists func(string) bool) string {
|
func MakeUniqueSlug(source string, exists func(string) bool) string {
|
||||||
pinyin := GetSlugInfo(source)
|
pinyin := GetSlugInfo(source)
|
||||||
|
|||||||
@@ -1276,8 +1276,7 @@ func ensureImportedPlayletSlug(ctx context.Context, playlet *playletmod.Playlet)
|
|||||||
if playlet == nil {
|
if playlet == nil {
|
||||||
return srvdef.New(srvdef.ErrParamError, "短剧数据为空", "")
|
return srvdef.New(srvdef.ErrParamError, "短剧数据为空", "")
|
||||||
}
|
}
|
||||||
source := utils.SlugSourceFromKeywordsOrTitle(playlet.SeoInfo.Keyworld, playlet.Title)
|
playlet.Slug = utils.MakeRandomSlug(func(slug string) bool {
|
||||||
playlet.Slug = utils.MakeUniqueSlug(source, func(slug string) bool {
|
|
||||||
exist, _ := playletmod.FindOnePlaylet(ctx, bson.M{
|
exist, _ := playletmod.FindOnePlaylet(ctx, bson.M{
|
||||||
playletmod.FN.Playlet.Slug: slug,
|
playletmod.FN.Playlet.Slug: slug,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -48,8 +48,7 @@ func ensureThemeSlug(ctx context.Context, ac *playletthememod.PlayletTheme, excl
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
source := utils.SlugSourceFromKeywordsOrTitle(ac.SeoInfo.Keyworld, ac.Name)
|
ac.Slug = utils.MakeRandomSlug(func(slug string) bool {
|
||||||
ac.Slug = utils.MakeUniqueSlug(source, func(slug string) bool {
|
|
||||||
filter := bson.M{playletthememod.FN.PlayletTheme.Slug: slug}
|
filter := bson.M{playletthememod.FN.PlayletTheme.Slug: slug}
|
||||||
if excludeId > 0 {
|
if excludeId > 0 {
|
||||||
filter[playletthememod.FN.PlayletTheme.Id] = bson.M{"$ne": excludeId}
|
filter[playletthememod.FN.PlayletTheme.Id] = bson.M{"$ne": excludeId}
|
||||||
@@ -81,16 +80,7 @@ func prepareThemeSlugOnUpdate(ctx context.Context, id int64, info *playletthemem
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
name := info.Name
|
newSlug := utils.MakeRandomSlug(func(s string) bool {
|
||||||
if req.Name != nil {
|
|
||||||
name = strings.TrimSpace(*req.Name)
|
|
||||||
}
|
|
||||||
keywords := info.SeoInfo.Keyworld
|
|
||||||
if req.SeoInfo != nil {
|
|
||||||
keywords = normalizeSeoValue(*req.SeoInfo).Keyworld
|
|
||||||
}
|
|
||||||
source := utils.SlugSourceFromKeywordsOrTitle(keywords, name)
|
|
||||||
newSlug := utils.MakeUniqueSlug(source, func(s string) bool {
|
|
||||||
exist, _ := playletthememod.FindOnePlayletTheme(ctx, bson.M{
|
exist, _ := playletthememod.FindOnePlayletTheme(ctx, bson.M{
|
||||||
playletthememod.FN.PlayletTheme.Slug: s,
|
playletthememod.FN.PlayletTheme.Slug: s,
|
||||||
playletthememod.FN.PlayletTheme.Id: bson.M{"$ne": id},
|
playletthememod.FN.PlayletTheme.Id: bson.M{"$ne": id},
|
||||||
@@ -186,8 +176,7 @@ func ensurePlayletSlug(ctx context.Context, ac *playletmod.Playlet, excludeId in
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
source := utils.SlugSourceFromKeywordsOrTitle(ac.SeoInfo.Keyworld, ac.Title)
|
ac.Slug = utils.MakeRandomSlug(func(slug string) bool {
|
||||||
ac.Slug = utils.MakeUniqueSlug(source, func(slug string) bool {
|
|
||||||
filter := bson.M{playletmod.FN.Playlet.Slug: slug}
|
filter := bson.M{playletmod.FN.Playlet.Slug: slug}
|
||||||
if excludeId > 0 {
|
if excludeId > 0 {
|
||||||
filter[playletmod.FN.Playlet.Id] = bson.M{"$ne": excludeId}
|
filter[playletmod.FN.Playlet.Id] = bson.M{"$ne": excludeId}
|
||||||
@@ -219,16 +208,7 @@ func preparePlayletSlugOnUpdate(ctx context.Context, id int64, info *playletmod.
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
title := info.Title
|
newSlug := utils.MakeRandomSlug(func(s string) bool {
|
||||||
if req.Title != nil {
|
|
||||||
title = strings.TrimSpace(*req.Title)
|
|
||||||
}
|
|
||||||
keywords := info.SeoInfo.Keyworld
|
|
||||||
if req.SeoInfo != nil {
|
|
||||||
keywords = normalizeSeoValue(*req.SeoInfo).Keyworld
|
|
||||||
}
|
|
||||||
source := utils.SlugSourceFromKeywordsOrTitle(keywords, title)
|
|
||||||
newSlug := utils.MakeUniqueSlug(source, func(s string) bool {
|
|
||||||
exist, _ := playletmod.FindOnePlaylet(ctx, bson.M{
|
exist, _ := playletmod.FindOnePlaylet(ctx, bson.M{
|
||||||
playletmod.FN.Playlet.Slug: s,
|
playletmod.FN.Playlet.Slug: s,
|
||||||
playletmod.FN.Playlet.Id: bson.M{"$ne": id},
|
playletmod.FN.Playlet.Id: bson.M{"$ne": id},
|
||||||
|
|||||||
Reference in New Issue
Block a user