Files
u-desk/internal/service/update_config.go
T
lxy 691e38604f 发布:v0.3.3 版本历史模块 + 域名迁移 + 站点版本信息修正
- 版本号更新至 0.3.3(version.go/wails.json/README.md)
- 更新检查域名迁移 img.1216.top → c.1216.top
- 新增 views/version 版本历史 Tab 页面(时间线 UI)
- 设置面板新增版本历史入口按钮
- CHANGELOG 补全 0.3.3 全部 17 个提交记录
- 站点 HTML 修正(删除错误 v0.4.0,v0.3.3 为最新)
- 生成 last-version.json / versions.json 发布数据
2026-04-14 00:43:21 +08:00

110 lines
2.9 KiB
Go

package service
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"u-desk/internal/common"
)
// 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) {
dataDir := common.GetUserDataDir()
if err := os.MkdirAll(dataDir, 0755); err != nil {
return "", fmt.Errorf("创建配置目录失败: %v", err)
}
return filepath.Join(dataDir, "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://c.1216.top/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
}
}
// 使用默认检查地址
if config.CheckURL == "" {
config.CheckURL = "https://c.1216.top/last-version.json"
}
// 确保版本号不为空(使用缓存的版本号)
if config.CurrentVersion == "" {
config.CurrentVersion = GetCurrentVersion()
}
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
}
// UpdateLastCheckTime 更新最后检查时间
func (c *UpdateConfig) UpdateLastCheckTime() error {
c.LastCheckTime = time.Now()
return SaveUpdateConfig(c)
}