service.xpcool.com/internal/controller/wallpaper/controller.go
夏犀麟 d0a084b80e
Some checks failed
Build and Deploy (service.xpcool.com) / build-and-deploy (push) Failing after 28s
feat(wallpaper): 平台配置脱敏回显 + 图库元字段补齐
后台做完界面才发现的两个接口缺口:

1) 平台配置不回显 → 用户只改一个开关,defaultQuery / purity / baseUrl
   会被一起清空(SaveSource 只对 apiKey/apiSecret 做了"留空即保留")。
   现在 Sources() 返回脱敏后的 config(凭据一律置空,是否已设置由
   HasApiKey 表达),前台接口不下发该字段 —— toAPISource 加 withConfig
   开关,公开接口没必要泄露自建反代地址等内部信息。

2) Item 缺 enabled / sort / remark → 后台列表无法显示启用状态,
   编辑抽屉也拿不到当前值。补齐字段并在 library.toItem 中填充,
   开源平台条目保持默认值(这三个字段对前台无意义)。

另抽出 sanitizeSourceConfig 统一处理凭据抹除,避免以后再加配置项时漏抹。
2026-09-14 01:48:23 +08:00

84 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package wallpaper 壁纸模块控制器。
//
// 分两个控制器:
// - Controlleradmin后台图库管理与平台配置走 RBAC 权限;
// - OpenControlleropen前台读取免鉴权。
//
// 控制器只做「参数搬运 + 结构转换」,业务逻辑一律在 internal/service/wallpaper。
package wallpaper
import (
wallpaperv1 "service.xpcool.com/api/wallpaper"
"service.xpcool.com/internal/model/dto"
)
// toAPIItem 把领域 DTO 转成接口输出结构。
// 两者字段一一对应,单独转换是为了让 API 契约与内部结构可以各自演进。
func toAPIItem(v *dto.WallpaperItem) *wallpaperv1.Item {
if v == nil {
return nil
}
return &wallpaperv1.Item{
Id: v.Id,
Title: v.Title,
Width: v.Width,
Height: v.Height,
Orientation: v.Orientation,
ThumbUrl: v.ThumbUrl,
PreviewUrl: v.PreviewUrl,
FullUrl: v.FullUrl,
Source: v.Source,
SourceName: v.SourceName,
FromOpen: v.FromOpen,
Author: v.Author,
AuthorUrl: v.AuthorUrl,
PageUrl: v.PageUrl,
License: v.License,
Tags: v.Tags,
Category: v.Category,
Filesize: v.Filesize,
Enabled: v.Enabled,
Sort: v.Sort,
Remark: v.Remark,
CreatedAt: v.CreatedAt,
}
}
// toAPIItems 批量转换。
func toAPIItems(list []dto.WallpaperItem) []*wallpaperv1.Item {
out := make([]*wallpaperv1.Item, 0, len(list))
for i := range list {
out = append(out, toAPIItem(&list[i]))
}
return out
}
// toAPISource 把平台信息转成接口输出结构。
//
// withConfig 控制是否下发脱敏后的配置:后台需要它回显,前台不需要
// (自建反代地址之类属于内部信息,没必要给公网浏览器)。
func toAPISource(v *dto.WallpaperSourceInfo, withConfig bool) *wallpaperv1.SourceInfo {
out := &wallpaperv1.SourceInfo{
Code: v.Code,
Name: v.Name,
Enabled: v.Enabled,
Sort: v.Sort,
Configured: v.Configured,
Available: v.Available,
Hint: v.Hint,
Remark: v.Remark,
HasApiKey: v.HasApiKey,
}
if withConfig && v.Config != nil {
out.Config = &wallpaperv1.SourceConfig{
ApiKey: v.Config.ApiKey,
ApiSecret: v.Config.ApiSecret,
DefaultQuery: v.Config.DefaultQuery,
Purity: v.Config.Purity,
Categories: v.Config.Categories,
BaseUrl: v.Config.BaseUrl,
}
}
return out
}