feat(home): 首页品牌条文案支持后台配置

This commit is contained in:
2026-11-13 11:06:27 +08:00
parent 5d7fedd915
commit d65b8d8089
13 changed files with 442 additions and 8 deletions
@@ -36,6 +36,8 @@ func validateConfigValue(key, value string) error {
return ErrInvalidConfig
}
return nil
case homeBrandBarConfigKey:
return validateHomeBrandBar(value)
default:
return nil
}
@@ -37,6 +37,15 @@ type HomeConfigDTO struct {
CooperationFeedback CooperationFeedbackConfig `json:"cooperation_feedback"`
// HomeEntriesEnabled 首页业务入口总开关:关闭后 PC/移动端同时隐藏回收与合作入口。
HomeEntriesEnabled bool `json:"home_entries_enabled"`
// BrandBar 首页品牌条三行文案,后台可配置;留空时回退默认文案。
BrandBar HomeBrandBarConfig `json:"brand_bar"`
}
// HomeBrandBarConfig 首页品牌条文案:小标签 / 主标题 / 服务承诺。
type HomeBrandBarConfig struct {
Eyebrow string `json:"eyebrow"`
Title string `json:"title"`
Subtitle string `json:"subtitle"`
}
// AWRecycleConfig 首页「AW炫彩回收」入口:点击展示可配置图片。
@@ -0,0 +1,81 @@
package systemconfig
import (
"context"
"encoding/json"
"strings"
"unicode/utf8"
)
const homeBrandBarConfigKey = "mobile.home_brand_bar"
// 首页品牌条单行文案上限:防止后台输入超长文本撑破 PC/移动端布局。
const (
homeBrandBarEyebrowMaxRunes = 60
homeBrandBarTitleMaxRunes = 20
homeBrandBarSubtitleMaxRunes = 80
)
func defaultHomeBrandBar() HomeBrandBarConfig {
return HomeBrandBarConfig{
Eyebrow: "JIEYOU TRADING HOUSE · 账号租赁",
Title: "解忧柜选",
Subtitle: "实名审核 · 流程留痕 · 账单透明 · 争议可申诉",
}
}
func defaultHomeBrandBarConfigValue() string {
raw, err := json.Marshal(defaultHomeBrandBar())
if err != nil {
return `{"eyebrow":"JIEYOU TRADING HOUSE · 账号租赁","title":"解忧柜选","subtitle":"实名审核 · 流程留痕 · 账单透明 · 争议可申诉"}`
}
return string(raw)
}
// normalizeHomeBrandBar 去除首尾空白;空字段回退默认文案,避免前台出现空白品牌条。
func normalizeHomeBrandBar(item HomeBrandBarConfig) HomeBrandBarConfig {
def := defaultHomeBrandBar()
eyebrow := strings.TrimSpace(item.Eyebrow)
if eyebrow == "" {
eyebrow = def.Eyebrow
}
title := strings.TrimSpace(item.Title)
if title == "" {
title = def.Title
}
subtitle := strings.TrimSpace(item.Subtitle)
if subtitle == "" {
subtitle = def.Subtitle
}
return HomeBrandBarConfig{
Eyebrow: eyebrow,
Title: title,
Subtitle: subtitle,
}
}
func validateHomeBrandBar(value string) error {
var item HomeBrandBarConfig
if err := json.Unmarshal([]byte(value), &item); err != nil {
return ErrInvalidConfig
}
item = normalizeHomeBrandBar(item)
if utf8.RuneCountInString(item.Eyebrow) > homeBrandBarEyebrowMaxRunes ||
utf8.RuneCountInString(item.Title) > homeBrandBarTitleMaxRunes ||
utf8.RuneCountInString(item.Subtitle) > homeBrandBarSubtitleMaxRunes {
return ErrInvalidConfig
}
return nil
}
func (s *Service) homeBrandBar(ctx context.Context) (HomeBrandBarConfig, error) {
value, err := s.repo.FindValue(ctx, homeBrandBarConfigKey)
if err != nil {
return defaultHomeBrandBar(), err
}
item := defaultHomeBrandBar()
if err := json.Unmarshal([]byte(value), &item); err != nil {
item = defaultHomeBrandBar()
}
return normalizeHomeBrandBar(item), nil
}
@@ -0,0 +1,83 @@
package systemconfig
import (
"encoding/json"
"strings"
"testing"
)
func TestHomeBrandBarConfigRegistered(t *testing.T) {
value := ""
for _, item := range defaultConfigs {
if item.Key == homeBrandBarConfigKey {
value = item.Value
break
}
}
if value != defaultHomeBrandBarConfigValue() {
t.Fatalf("defaultConfigs 中 %s 的默认值 = %q, want %q", homeBrandBarConfigKey, value, defaultHomeBrandBarConfigValue())
}
visible := false
for _, key := range adminVisibleConfigKeys {
if key == homeBrandBarConfigKey {
visible = true
break
}
}
if !visible {
t.Fatalf("adminVisibleConfigKeys 缺少 %s", homeBrandBarConfigKey)
}
var parsed HomeBrandBarConfig
if err := json.Unmarshal([]byte(value), &parsed); err != nil {
t.Fatalf("默认值不是合法 JSON: %v", err)
}
if parsed != defaultHomeBrandBar() {
t.Fatalf("默认值解析结果 = %+v, want %+v", parsed, defaultHomeBrandBar())
}
}
func TestNormalizeHomeBrandBar(t *testing.T) {
got := normalizeHomeBrandBar(HomeBrandBarConfig{
Eyebrow: " 测试标签 ",
Title: " ",
Subtitle: "",
})
def := defaultHomeBrandBar()
if got.Eyebrow != "测试标签" {
t.Fatalf("Eyebrow = %q, want %q", got.Eyebrow, "测试标签")
}
if got.Title != def.Title {
t.Fatalf("空标题应回退默认值, got %q want %q", got.Title, def.Title)
}
if got.Subtitle != def.Subtitle {
t.Fatalf("空副标题应回退默认值, got %q want %q", got.Subtitle, def.Subtitle)
}
}
func TestValidateHomeBrandBar(t *testing.T) {
valid := []string{
`{"eyebrow":"JIEYOU TRADING HOUSE · 账号租赁","title":"解忧柜选","subtitle":"实名审核 · 流程留痕 · 账单透明 · 争议可申诉"}`,
`{}`,
`{"eyebrow":"","title":"","subtitle":""}`,
`{"eyebrow":"A","title":"柜选","subtitle":"透明"}`,
}
for _, value := range valid {
if err := validateConfigValue(homeBrandBarConfigKey, value); err != nil {
t.Fatalf("validateConfigValue(%q) = %v, want nil", value, err)
}
}
invalid := []string{
``,
`not-json`,
`[]`,
`{"title":"` + strings.Repeat("超", homeBrandBarTitleMaxRunes+1) + `"}`,
`{"eyebrow":"` + strings.Repeat("长", homeBrandBarEyebrowMaxRunes+1) + `"}`,
`{"subtitle":"` + strings.Repeat("长", homeBrandBarSubtitleMaxRunes+1) + `"}`,
}
for _, value := range invalid {
if err := validateConfigValue(homeBrandBarConfigKey, value); err != ErrInvalidConfig {
t.Fatalf("validateConfigValue(%q) = %v, want ErrInvalidConfig", value, err)
}
}
}
@@ -57,6 +57,7 @@ var defaultConfigs = []defaultConfig{
{Key: awRecycleConfigKey, Value: defaultAWRecycleConfigValue(), Description: "首页 AW炫彩回收入口配置 JSON(标题/副标题/展示图片)"},
{Key: cooperationFeedbackConfigKey, Value: defaultCooperationFeedbackConfigValue(), Description: "首页 合作与反馈入口配置 JSON(标题/副标题/展示图片)"},
{Key: homeEntriesEnabledConfigKey, Value: defaultHomeEntriesEnabledConfigValue(), Description: "首页 AW炫彩回收/合作与反馈入口总开关(关闭后 PC/移动端均隐藏)"},
{Key: homeBrandBarConfigKey, Value: defaultHomeBrandBarConfigValue(), Description: "首页品牌条文案 JSON(小标签/主标题/服务承诺)"},
{Key: publishOptionsConfigKey, Value: defaultPublishOptionsConfigValue(), Description: "发布页选项配置 JSON"},
{Key: salePriceConfigKey, Value: defaultSalePriceConfigValue(), Description: "内部出售定价规则 JSON"},
{Key: DistributionSettlementHoldDaysKey, Value: defaultDistributionSettlementHoldDaysValue(), Description: "新建分销站点默认结算冷却天数(0=立即释放,最大 365)"},
@@ -87,6 +88,7 @@ var adminVisibleConfigKeys = []string{
"mobile.aw_recycle",
"mobile.cooperation_feedback",
"mobile.home_entries_enabled",
homeBrandBarConfigKey,
"order.agreements",
"order.pending_payment_timeout_minutes",
"order.return_overdue_grace_minutes",
@@ -55,6 +55,10 @@ func (s *Service) HomeConfig(ctx context.Context, siteCode string) (*HomeConfigD
if err != nil {
return nil, err
}
brandBar, err := s.homeBrandBar(ctx)
if err != nil {
return nil, err
}
config := &HomeConfigDTO{
Announcements: announcements,
Banners: banners,
@@ -62,6 +66,7 @@ func (s *Service) HomeConfig(ctx context.Context, siteCode string) (*HomeConfigD
AWRecycle: awRecycle,
CooperationFeedback: cooperationFeedback,
HomeEntriesEnabled: homeEntriesEnabled,
BrandBar: brandBar,
}
s.applySiteHomeOverrides(ctx, siteCode, config)
return config, nil