From ad016ee1acccc361c5b005153f4635fdaaf0a160 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 23 Sep 2026 22:10:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=93=E9=A2=98=E5=92=8C=E7=9F=AD=E5=89=A7?= =?UTF-8?q?=20slug=20=E6=94=B9=E4=B8=BA=208=20=E4=BD=8D=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E5=B0=8F=E5=86=99=E5=AD=97=E6=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后台新建/修改(未填 slug)和媒资入库时生成随机 slug,不再按标题拼音生成;标签、分类不变。 Co-Authored-By: Claude Opus 5.5 (1M context) --- pub/common/utils/strings.go | 13 +++++++++++ web/service/srv_mediaresource/resource.go | 3 +-- web/service/srv_playlet/slug.go | 28 ++++------------------- 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/pub/common/utils/strings.go b/pub/common/utils/strings.go index 5c3976c..9811f64 100644 --- a/pub/common/utils/strings.go +++ b/pub/common/utils/strings.go @@ -113,6 +113,19 @@ func IsSlugAllDigits(s string) bool { 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 表示已被占用。生成结果不能全是数字。 func MakeUniqueSlug(source string, exists func(string) bool) string { pinyin := GetSlugInfo(source) diff --git a/web/service/srv_mediaresource/resource.go b/web/service/srv_mediaresource/resource.go index 4f73c22..2ce395b 100644 --- a/web/service/srv_mediaresource/resource.go +++ b/web/service/srv_mediaresource/resource.go @@ -1276,8 +1276,7 @@ func ensureImportedPlayletSlug(ctx context.Context, playlet *playletmod.Playlet) if playlet == nil { return srvdef.New(srvdef.ErrParamError, "短剧数据为空", "") } - source := utils.SlugSourceFromKeywordsOrTitle(playlet.SeoInfo.Keyworld, playlet.Title) - playlet.Slug = utils.MakeUniqueSlug(source, func(slug string) bool { + playlet.Slug = utils.MakeRandomSlug(func(slug string) bool { exist, _ := playletmod.FindOnePlaylet(ctx, bson.M{ playletmod.FN.Playlet.Slug: slug, }) diff --git a/web/service/srv_playlet/slug.go b/web/service/srv_playlet/slug.go index da16dc4..9da07a5 100644 --- a/web/service/srv_playlet/slug.go +++ b/web/service/srv_playlet/slug.go @@ -48,8 +48,7 @@ func ensureThemeSlug(ctx context.Context, ac *playletthememod.PlayletTheme, excl } return nil } - source := utils.SlugSourceFromKeywordsOrTitle(ac.SeoInfo.Keyworld, ac.Name) - ac.Slug = utils.MakeUniqueSlug(source, func(slug string) bool { + ac.Slug = utils.MakeRandomSlug(func(slug string) bool { filter := bson.M{playletthememod.FN.PlayletTheme.Slug: slug} if excludeId > 0 { filter[playletthememod.FN.PlayletTheme.Id] = bson.M{"$ne": excludeId} @@ -81,16 +80,7 @@ func prepareThemeSlugOnUpdate(ctx context.Context, id int64, info *playletthemem } return nil } - name := info.Name - 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 { + newSlug := utils.MakeRandomSlug(func(s string) bool { exist, _ := playletthememod.FindOnePlayletTheme(ctx, bson.M{ playletthememod.FN.PlayletTheme.Slug: s, playletthememod.FN.PlayletTheme.Id: bson.M{"$ne": id}, @@ -186,8 +176,7 @@ func ensurePlayletSlug(ctx context.Context, ac *playletmod.Playlet, excludeId in } return nil } - source := utils.SlugSourceFromKeywordsOrTitle(ac.SeoInfo.Keyworld, ac.Title) - ac.Slug = utils.MakeUniqueSlug(source, func(slug string) bool { + ac.Slug = utils.MakeRandomSlug(func(slug string) bool { filter := bson.M{playletmod.FN.Playlet.Slug: slug} if excludeId > 0 { filter[playletmod.FN.Playlet.Id] = bson.M{"$ne": excludeId} @@ -219,16 +208,7 @@ func preparePlayletSlugOnUpdate(ctx context.Context, id int64, info *playletmod. } return nil } - title := info.Title - 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 { + newSlug := utils.MakeRandomSlug(func(s string) bool { exist, _ := playletmod.FindOnePlaylet(ctx, bson.M{ playletmod.FN.Playlet.Slug: s, playletmod.FN.Playlet.Id: bson.M{"$ne": id},