110 lines
2.9 KiB
Go
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://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
|
||
}
|
||
}
|
||
|
||
// 使用默认检查地址
|
||
if config.CheckURL == "" {
|
||
config.CheckURL = "https://img.1216.top/u-desk/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)
|
||
}
|