feat(publish): 德穆兰选项支持后台开关并默认打开下赛季通行证

This commit is contained in:
yml2213
2026-10-31 18:26:05 +08:00
parent ecdffb3f2b
commit 9c379b7e0c
9 changed files with 86 additions and 5 deletions
@@ -121,7 +121,9 @@ type HomeBannerHotspot struct {
type PublishOptionsDTO struct {
// SchemaVersion 发布选项结构版本;缺省 0 代表未版本化的历史配置。
// UnlockDemulanEnabled 发布页是否展示「是否解锁德穆兰」选项。
SchemaVersion int `json:"schema_version,omitempty"`
UnlockDemulanEnabled bool `json:"unlock_demulan_enabled"`
NextSeasonPassEnabled bool `json:"next_season_pass_enabled"`
ServerOptions []string `json:"server_options"`
FaceOptions []string `json:"face_options"`
@@ -3,8 +3,10 @@ package systemconfig
import "encoding/json"
const (
// publishOptionsSchemaVersion 发布选项结构版本;v2 新增「是否解锁德穆兰」开关(默认关闭),
// 并把「下赛季是否购买通行证」默认打开。
publishOptionsSchemaVersion = 2
publishOptionsConfigKey = "listing.publish_options"
publishOptionsSchemaVersion = 1
)
func defaultPublishOptionsConfigValue() string {
@@ -18,7 +20,8 @@ func defaultPublishOptionsConfigValue() string {
func DefaultPublishOptions() PublishOptionsDTO {
return PublishOptionsDTO{
SchemaVersion: publishOptionsSchemaVersion,
NextSeasonPassEnabled: false,
UnlockDemulanEnabled: false,
NextSeasonPassEnabled: true,
ServerOptions: []string{"QQ", "微信", "Steam"},
FaceOptions: []string{"是", "否"},
RankOptions: []string{"青铜", "白银", "黄金", "铂金", "钻石", "黑鹰", "三角洲巅峰"},
@@ -106,6 +106,53 @@ func TestNormalizePublishOptionsValuePreservesCurrentSchema(t *testing.T) {
}
}
func TestNormalizePublishOptionsValueMigratesSchemaV1Switches(t *testing.T) {
legacy := `{"schema_version":1,"next_season_pass_enabled":false,"server_options":["QQ"]}`
normalized, err := normalizePublishOptionsValue(legacy)
if err != nil {
t.Fatalf("规范化失败: %v", err)
}
var got PublishOptionsDTO
if err := json.Unmarshal([]byte(normalized), &got); err != nil {
t.Fatalf("解析规范化结果失败: %v", err)
}
if got.SchemaVersion != publishOptionsSchemaVersion {
t.Fatalf("schema_version = %d, want %d", got.SchemaVersion, publishOptionsSchemaVersion)
}
if got.UnlockDemulanEnabled {
t.Fatal("v1 升级后「是否解锁德穆兰」应默认关闭")
}
if !got.NextSeasonPassEnabled {
t.Fatal("v1 升级后「下赛季是否购买通行证」应默认打开")
}
}
func TestNormalizePublishOptionsValueKeepsSchemaV2Switches(t *testing.T) {
options := DefaultPublishOptions()
options.UnlockDemulanEnabled = true
options.NextSeasonPassEnabled = false
raw, err := json.Marshal(options)
if err != nil {
t.Fatalf("构造配置失败: %v", err)
}
normalized, err := normalizePublishOptionsValue(string(raw))
if err != nil {
t.Fatalf("规范化失败: %v", err)
}
var got PublishOptionsDTO
if err := json.Unmarshal([]byte(normalized), &got); err != nil {
t.Fatalf("解析规范化结果失败: %v", err)
}
if !got.UnlockDemulanEnabled {
t.Fatal("v2 配置中后台手动开启的德穆兰开关不应被覆盖")
}
if got.NextSeasonPassEnabled {
t.Fatal("v2 配置中后台手动关闭的通行证开关不应被覆盖")
}
}
func TestNormalizePublishOptionsValueRepairsMojibake(t *testing.T) {
options := DefaultPublishOptions()
raw, err := json.Marshal(options)
@@ -330,6 +330,7 @@ func normalizePublishOptionsValue(value string) (string, error) {
if err := json.Unmarshal([]byte(value), &fields); err != nil {
return "", err
}
originalVersion := options.SchemaVersion
if options.SchemaVersion < publishOptionsSchemaVersion {
if hasLegacyPublishQuantityItem(options.QuantityItems) {
options = DefaultPublishOptions()
@@ -342,6 +343,12 @@ func normalizePublishOptionsValue(value string) (string, error) {
} else {
normalizePublishOptions(&options)
}
// v2 一次性迁移:新增「是否解锁德穆兰」开关并默认关闭,同时打开「下赛季是否购买通行证」。
// 已经是 v2 的配置不再覆盖,保留后台手动调整的结果。
if originalVersion < 2 {
options.UnlockDemulanEnabled = false
options.NextSeasonPassEnabled = true
}
options.SchemaVersion = publishOptionsSchemaVersion
raw, err := json.Marshal(options)
if err != nil {