- 新增 ConfigAPI 和 ConfigService 实现配置管理 - 新增 SettingsPanel 和 UpdateNotification 组件 - 文件系统模块化重构,提升代码质量 - 提取公共函数,优化代码结构 - 版本号更新至 0.2.0
134 lines
3.6 KiB
Go
134 lines
3.6 KiB
Go
package service
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"log"
|
||
"os"
|
||
"path/filepath"
|
||
"time"
|
||
)
|
||
|
||
// UpdateConfig 更新配置
|
||
type UpdateConfig struct {
|
||
CurrentVersion string `json:"current_version"`
|
||
LastCheckTime time.Time `json:"last_check_time"`
|
||
AutoCheckEnabled bool `json:"auto_check_enabled"`
|
||
CheckIntervalMinutes int `json:"check_interval_minutes"` // 检查间隔(分钟)
|
||
CheckURL string `json:"check_url,omitempty"` // 版本检查接口 URL
|
||
}
|
||
|
||
// GetUpdateConfigPath 获取更新配置文件路径
|
||
func GetUpdateConfigPath() (string, error) {
|
||
homeDir, err := os.UserHomeDir()
|
||
if err != nil {
|
||
return "", fmt.Errorf("获取用户目录失败: %v", err)
|
||
}
|
||
|
||
configDir := filepath.Join(homeDir, ".go-desk")
|
||
if err := os.MkdirAll(configDir, 0755); err != nil {
|
||
return "", fmt.Errorf("创建配置目录失败: %v", err)
|
||
}
|
||
|
||
return filepath.Join(configDir, "update_config.json"), nil
|
||
}
|
||
|
||
// LoadUpdateConfig 加载更新配置
|
||
func LoadUpdateConfig() (*UpdateConfig, error) {
|
||
configPath, err := GetUpdateConfigPath()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 如果文件不存在,返回默认配置
|
||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||
return &UpdateConfig{
|
||
CurrentVersion: GetCurrentVersion(),
|
||
LastCheckTime: time.Time{}, // 启动时会立即检查
|
||
AutoCheckEnabled: true,
|
||
CheckIntervalMinutes: 5, // 5分钟检查一次
|
||
CheckURL: "https://img.1216.top/u-desk/last-version.json",
|
||
}, nil
|
||
}
|
||
|
||
data, err := os.ReadFile(configPath)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("读取配置文件失败: %v", err)
|
||
}
|
||
|
||
var config UpdateConfig
|
||
if err := json.Unmarshal(data, &config); err != nil {
|
||
return nil, fmt.Errorf("解析配置文件失败: %v", err)
|
||
}
|
||
|
||
// 兼容旧配置:如果 check_interval_minutes 为 0,尝试从旧字段转换
|
||
if config.CheckIntervalMinutes == 0 {
|
||
var configMap map[string]interface{}
|
||
if json.Unmarshal(data, &configMap) == nil {
|
||
if days, ok := configMap["check_interval_days"].(float64); ok && days > 0 {
|
||
config.CheckIntervalMinutes = int(days * 24 * 60)
|
||
}
|
||
}
|
||
if config.CheckIntervalMinutes == 0 {
|
||
config.CheckIntervalMinutes = 1
|
||
}
|
||
}
|
||
|
||
// 同步最新版本号
|
||
latestVersion := GetCurrentVersion()
|
||
if config.CurrentVersion == "" || config.CurrentVersion != latestVersion {
|
||
if config.CurrentVersion != "" {
|
||
log.Printf("[配置] 配置中的版本号 (%s) 与最新版本号 (%s) 不一致", config.CurrentVersion, latestVersion)
|
||
}
|
||
config.CurrentVersion = latestVersion
|
||
}
|
||
|
||
// 使用默认检查地址
|
||
if config.CheckURL == "" {
|
||
config.CheckURL = "https://img.1216.top/u-desk/last-version.json"
|
||
}
|
||
|
||
return &config, nil
|
||
}
|
||
|
||
// SaveUpdateConfig 保存更新配置
|
||
func SaveUpdateConfig(config *UpdateConfig) error {
|
||
configPath, err := GetUpdateConfigPath()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
data, err := json.MarshalIndent(config, "", " ")
|
||
if err != nil {
|
||
return fmt.Errorf("序列化配置失败: %v", err)
|
||
}
|
||
|
||
if err := os.WriteFile(configPath, data, 0644); err != nil {
|
||
return fmt.Errorf("写入配置文件失败: %v", err)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// ShouldCheckUpdate 判断是否应该检查更新
|
||
func (c *UpdateConfig) ShouldCheckUpdate() bool {
|
||
if !c.AutoCheckEnabled {
|
||
return false
|
||
}
|
||
|
||
// 如果从未检查过,应该检查
|
||
if c.LastCheckTime.IsZero() {
|
||
return true
|
||
}
|
||
|
||
// 检查是否超过间隔分钟数
|
||
minutesSinceLastCheck := time.Since(c.LastCheckTime).Minutes()
|
||
return minutesSinceLastCheck >= float64(c.CheckIntervalMinutes)
|
||
}
|
||
|
||
// UpdateLastCheckTime 更新最后检查时间
|
||
func (c *UpdateConfig) UpdateLastCheckTime() error {
|
||
c.LastCheckTime = time.Now()
|
||
return SaveUpdateConfig(c)
|
||
}
|