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},