// Package api 配置相关接口。 package api import ( "net/http" "strconv" "github.com/gin-gonic/gin" "kenaito-media-picker/internal/store" ) // 配置键名(存储在 AppConfig 表中) const ( ConfigKeyNsfwType = "nsfw.type" // 检测类型:"" / "human" / "anime" ConfigKeyNsfwThreshold = "nsfw.threshold" // 人类检测阈值 ConfigKeyAnimeNsfwThreshold = "anime.threshold" // 二次元检测阈值 ) // ConfigController 配置相关接口。 type ConfigController struct { store *store.Store } // NewConfigController 创建配置控制器。 func NewConfigController(st *store.Store) *ConfigController { return &ConfigController{store: st} } // NsfwConfig NSFW 配置响应。 type NsfwConfig struct { Type string `json:"type"` // 检测类型:"" / "human" / "anime" HumanThreshold float64 `json:"humanThreshold"` // 人类检测阈值 AnimeThreshold float64 `json:"animeThreshold"` // 二次元检测阈值 } // GetNsfwConfig 处理 GET /api/config/nsfw:获取 NSFW 配置。 func (c *ConfigController) GetNsfwConfig(ctx *gin.Context) { nsfwType := c.store.GetConfig(ConfigKeyNsfwType) humanThreshold := 0.5 if v := c.store.GetConfig(ConfigKeyNsfwThreshold); v != "" { if t, err := strconv.ParseFloat(v, 64); err == nil && t > 0 && t < 1 { humanThreshold = t } } animeThreshold := 0.5 if v := c.store.GetConfig(ConfigKeyAnimeNsfwThreshold); v != "" { if t, err := strconv.ParseFloat(v, 64); err == nil && t > 0 && t < 1 { animeThreshold = t } } Success(ctx, NsfwConfig{ Type: nsfwType, HumanThreshold: humanThreshold, AnimeThreshold: animeThreshold, }) } // UpdateNsfwConfigReq 更新 NSFW 配置请求。 type UpdateNsfwConfigReq struct { Type string `json:"type"` // 检测类型:"" / "human" / "anime" HumanThreshold float64 `json:"humanThreshold"` // 人类检测阈值 AnimeThreshold float64 `json:"animeThreshold"` // 二次元检测阈值 } // UpdateNsfwConfig 处理 PUT /api/config/nsfw:更新 NSFW 配置。 func (c *ConfigController) UpdateNsfwConfig(ctx *gin.Context) { var req UpdateNsfwConfigReq if err := ctx.ShouldBindJSON(&req); err != nil { Error(ctx, http.StatusBadRequest, "请求参数错误:"+err.Error()) return } // 校验检测类型 if req.Type != "" && req.Type != "human" && req.Type != "anime" { Error(ctx, http.StatusBadRequest, "检测类型必须是空、human 或 anime") return } // 校验阈值范围 if req.HumanThreshold <= 0 || req.HumanThreshold >= 1 { req.HumanThreshold = 0.5 } if req.AnimeThreshold <= 0 || req.AnimeThreshold >= 1 { req.AnimeThreshold = 0.5 } // 保存配置 if err := c.store.SaveConfig(ConfigKeyNsfwType, req.Type); err != nil { Error(ctx, http.StatusInternalServerError, "保存配置失败:"+err.Error()) return } if err := c.store.SaveConfig(ConfigKeyNsfwThreshold, strconv.FormatFloat(req.HumanThreshold, 'f', 2, 64)); err != nil { Error(ctx, http.StatusInternalServerError, "保存配置失败:"+err.Error()) return } if err := c.store.SaveConfig(ConfigKeyAnimeNsfwThreshold, strconv.FormatFloat(req.AnimeThreshold, 'f', 2, 64)); err != nil { Error(ctx, http.StatusInternalServerError, "保存配置失败:"+err.Error()) return } Success(ctx, nil) }