七月迭代短暂完结,还有很多后端的关键东西没有弄,这是一版赶时间做的东西
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m26s

This commit is contained in:
2026-07-25 17:06:58 +08:00
parent ad9f613dd6
commit 73f5125d3d
249 changed files with 17137 additions and 877 deletions

View File

@@ -96,6 +96,44 @@ func (r *Reader) Get(ctx context.Context, key string) (string, error) {
return value, nil
}
// GetStrict 严格读取单个已注册配置;已落库的非法值或数据库读取失败不会回退默认值。
// 该入口用于支付方式等必须失败关闭的关键配置,未落库时仍返回代码注册的首次初始化默认值。
func (r *Reader) GetStrict(ctx context.Context, key string) (string, error) {
definition, registered := r.registry.Get(key)
if !registered {
return "", stderrors.New("系统配置 Key 未注册")
}
cacheKey := constants.RedisSystemConfigKey(key)
if r.cache != nil {
if value, err := r.cache.Get(ctx, cacheKey); err == nil {
if ValidateValue(definition, value) == nil {
return value, nil
}
r.warn(ctx, "SYSTEM_CONFIG_CACHE_INVALID", key, "关键系统配置缓存值非法,已回源 PostgreSQL")
} else if err != redis.Nil {
r.warn(ctx, "SYSTEM_CONFIG_CACHE_READ_FAILED", key, "关键系统配置缓存读取失败,已回源 PostgreSQL")
}
}
var record model.SystemConfig
err := r.db.WithContext(ctx).Where("config_key = ?", key).First(&record).Error
if err == gorm.ErrRecordNotFound {
return definition.DefaultValue, nil
}
if err != nil {
return "", err
}
if ValidateValue(definition, record.ConfigValue) != nil {
r.warn(ctx, "SYSTEM_CONFIG_DATABASE_VALUE_INVALID", key, "关键系统配置数据库值非法,已失败关闭")
return "", stderrors.New("系统配置数据库值非法")
}
if r.cache != nil {
if err := r.cache.Set(ctx, cacheKey, record.ConfigValue, constants.SystemConfigCacheTTL); err != nil {
r.warn(ctx, "SYSTEM_CONFIG_CACHE_WRITE_FAILED", key, "关键系统配置缓存回填失败")
}
}
return record.ConfigValue, nil
}
// ListItem 是查询层组装 DTO 所需的稳定投影。
type ListItem struct {
Definition Definition

View File

@@ -28,6 +28,7 @@ type Definition struct {
EnumValues []string
Min *int64
Max *int64
Validator func(string) error
}
// Registry 保存可写配置的代码权威定义。
@@ -127,12 +128,19 @@ func ValidateValue(definition Definition, value string) error {
return stderrors.New("系统配置类型不受支持")
}
if len(definition.EnumValues) > 0 {
matched := false
for _, allowed := range definition.EnumValues {
if value == allowed {
return nil
matched = true
break
}
}
return stderrors.New("系统配置值不在允许枚举中")
if !matched {
return stderrors.New("系统配置值不在允许枚举中")
}
}
if definition.Validator != nil {
return definition.Validator(value)
}
return nil
}