Files
u-desktop/config.go
绝尘 f5a473c4b8 新增: 自动升级体系 + WorkerW重建检测 + 知识卡片优化
自动升级:
- 版本变量注入 (-ldflags -X main.version)
- 远程 version.json 检查 + 弹窗提示(非强制右下角/强制居中)
- updater.exe 嵌入主程序, 运行时释放到临时目录
- 下载+SHA256校验+备份+替换+回滚 全流程
- 托盘菜单"检查更新"

稳定性:
- WorkerW 重建自动检测(每10s)并重新嵌入
- 左键托盘点击防抖
- 设置窗口已打开时正确前置

优化:
- 知识卡片prompt改为面向开发者的硬核内容
- 配置目录统一到 ~/.u-desktop/
- 设置窗口WebView2数据目录改为configDir下
2026-05-28 19:06:26 +08:00

154 lines
3.9 KiB
Go

package main
import (
_ "embed"
"encoding/json"
"log"
"os"
"path/filepath"
)
type WallpaperType string
const (
WPTheme WallpaperType = "theme"
WPImage WallpaperType = "image"
WPBing WallpaperType = "bing"
WPColor WallpaperType = "color"
)
type Layout string
const (
LayoutSingle Layout = "single"
LayoutMulti Layout = "multi"
)
type ThemeName string
const (
ThemeAurora ThemeName = "aurora"
ThemeStar ThemeName = "starfield"
ThemeGradient ThemeName = "gradient"
ThemeParticle ThemeName = "particles"
ThemeFractal ThemeName = "fractal"
ThemeText ThemeName = "text"
)
type SavedColor struct {
Color1 string `json:"color1"`
Color2 string `json:"color2"`
Gradient bool `json:"gradient"`
}
type Config struct {
Zodiac string `json:"zodiac"`
City string `json:"city"`
WallpaperType WallpaperType `json:"wallpaperType"`
Theme ThemeName `json:"theme"`
ImagePath string `json:"imagePath"`
Color1 string `json:"color1"`
Color2 string `json:"color2"`
ColorGradient bool `json:"colorGradient"`
WallpaperText string `json:"wallpaperText"`
Layout Layout `json:"layout"`
HideWallpaper bool `json:"hideWallpaper"`
HideTime bool `json:"hideTime"`
HideWeather bool `json:"hideWeather"`
HideZodiac bool `json:"hideZodiac"`
HideAINews bool `json:"hideAINews"`
ShowSeconds bool `json:"showSeconds"`
PhotoDir string `json:"photoDir"`
PhotoInterval int `json:"photoInterval"`
HidePhoto bool `json:"hidePhoto"`
PhotoFrameMode bool `json:"photoFrameMode"`
KnowledgeKeyword string `json:"knowledgeKeyword"`
KnowledgePrompt string `json:"knowledgePrompt"`
HideKnowledge bool `json:"hideKnowledge"`
SavedColors []SavedColor `json:"savedColors"`
BingAutoRefresh bool `json:"bingAutoRefresh"`
AutoStart bool `json:"autoStart"`
QWeatherKey string `json:"qweatherKey,omitempty"`
TianapiKey string `json:"tianapiKey,omitempty"`
CPAKey string `json:"cpaKey,omitempty"`
UpdateURL string `json:"updateUrl,omitempty"`
AutoCheckUpdate bool `json:"autoCheckUpdate"`
SkipVersion string `json:"skipVersion,omitempty"`
}
const defaultZodiac = "射手座"
var configPath string
//go:embed assets/icons/tray.ico
var trayIcon []byte
func configDir() string {
return filepath.Dir(configPath)
}
func loadConfig() *Config {
data, err := os.ReadFile(configPath)
if err != nil {
return defaultConfig()
}
var cfg Config
if json.Unmarshal(data, &cfg) != nil {
return defaultConfig()
}
if cfg.Zodiac == "" {
cfg.Zodiac = defaultZodiac
}
if cfg.WallpaperType == "" {
cfg.WallpaperType = WPTheme
}
if cfg.Theme == "" {
cfg.Theme = ThemeAurora
}
if cfg.Color1 == "" {
cfg.Color1 = "#1a1a2e"
}
if cfg.Layout == "" {
cfg.Layout = LayoutSingle
}
return &cfg
}
func defaultConfig() *Config {
return &Config{
Zodiac: defaultZodiac,
WallpaperType: WPTheme,
Theme: ThemeAurora,
Color1: "#1a1a2e",
Color2: "#16213e",
}
}
func saveConfig(cfg *Config) error {
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
log.Println("配置序列化失败:", err)
return err
}
return os.WriteFile(configPath, data, 0644)
}
func getSecret(envName, cfgValue string) string {
if v := os.Getenv(envName); v != "" {
return v
}
return cfgValue
}
func (c *Config) qweatherKey() string {
return getSecret("U_DESKTOP_QWEATHER_KEY", c.QWeatherKey)
}
func (c *Config) tianapiKey() string {
return getSecret("U_DESKTOP_TIANAPI_KEY", c.TianapiKey)
}
func (c *Config) cpaKey() string {
return getSecret("U_DESKTOP_CPA_KEY", c.CPAKey)
}