Private
Public Access
1
0

新增:版本更新管理功能,优化代码架构

This commit is contained in:
2026-01-25 18:06:16 +08:00
parent 652f5e5d60
commit cc50de0323
11 changed files with 1887 additions and 42 deletions

80
app.go
View File

@@ -9,6 +9,7 @@ import (
"go-desk/internal/storage"
"go-desk/internal/system"
"os"
"strings"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
@@ -20,6 +21,7 @@ type App struct {
connectionAPI *api.ConnectionAPI
sqlAPI *api.SqlAPI
tabAPI *api.TabAPI
updateAPI *api.UpdateAPI
}
// NewApp 创建新的应用实例
@@ -27,8 +29,8 @@ func NewApp() *App {
return &App{}
}
// startup 应用启动时调用
func (a *App) startup(ctx context.Context) {
// Startup 应用启动时调用
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
// 初始化 SQLite 本地存储(核心依赖,必须成功)
@@ -52,17 +54,15 @@ func (a *App) startup(ctx context.Context) {
if err := a.initAPIs(); err != nil {
panic(fmt.Sprintf("API 初始化失败,应用无法启动: %v", err))
}
// 设置 updateAPI 的上下文
if a.updateAPI != nil {
a.updateAPI.SetContext(ctx)
}
}
// QueryUsers 查询用户列表
func (a *App) QueryUsers(keyword string, status int, role int, organid int, page int, pageSize int, sortField string, sortOrder string) (map[string]interface{}, error) {
if a.db == nil {
return map[string]interface{}{
"rows": []interface{}{},
"total": 0,
}, nil
}
return a.db.QueryUsers(keyword, status, role, organid, page, pageSize, sortField, sortOrder)
}
@@ -125,24 +125,13 @@ func (a *App) GetFileInfo(path string) (map[string]interface{}, error) {
func (a *App) GetEnvVars() (map[string]string, error) {
envVars := make(map[string]string)
for _, env := range os.Environ() {
parts := splitEnv(env)
if len(parts) == 2 {
envVars[parts[0]] = parts[1]
if key, value, found := strings.Cut(env, "="); found {
envVars[key] = value
}
}
return envVars, nil
}
// splitEnv 分割环境变量字符串key=value
func splitEnv(env string) []string {
for i := 0; i < len(env); i++ {
if env[i] == '=' {
return []string{env[:i], env[i+1:]}
}
}
return []string{env}
}
// ========== 数据库连接管理接口 ==========
// initAPIs 初始化所有API在startup中调用
@@ -157,6 +146,10 @@ func (a *App) initAPIs() error {
return err
}
a.tabAPI, err = api.NewTabAPI()
if err != nil {
return err
}
a.updateAPI, err = api.NewUpdateAPI("https://img.1216.top/go-desk/last-version.json")
return err
}
@@ -267,3 +260,46 @@ func (a *App) SaveSqlTabs(tabs []map[string]interface{}) error {
func (a *App) ListSqlTabs() ([]map[string]interface{}, error) {
return a.tabAPI.ListSqlTabs()
}
// ========== 版本更新管理接口 ==========
// CheckUpdate 检查更新
func (a *App) CheckUpdate() (map[string]interface{}, error) {
return a.updateAPI.CheckUpdate()
}
// GetCurrentVersion 获取当前版本号
func (a *App) GetCurrentVersion() (map[string]interface{}, error) {
return a.updateAPI.GetCurrentVersion()
}
// GetUpdateConfig 获取更新配置
func (a *App) GetUpdateConfig() (map[string]interface{}, error) {
return a.updateAPI.GetUpdateConfig()
}
// SetUpdateConfig 设置更新配置
func (a *App) SetUpdateConfig(autoCheckEnabled bool, checkIntervalMinutes int, checkURL string) (map[string]interface{}, error) {
return a.updateAPI.SetUpdateConfig(autoCheckEnabled, checkIntervalMinutes, checkURL)
}
// DownloadUpdate 下载更新包
func (a *App) DownloadUpdate(downloadURL string) (map[string]interface{}, error) {
return a.updateAPI.DownloadUpdate(downloadURL)
}
// InstallUpdate 安装更新包
func (a *App) InstallUpdate(installerPath string, autoRestart bool) (map[string]interface{}, error) {
return a.updateAPI.InstallUpdate(installerPath, autoRestart)
}
// InstallUpdateWithHash 安装更新包(带哈希验证)
func (a *App) InstallUpdateWithHash(installerPath string, autoRestart bool, expectedHash string, hashType string) (map[string]interface{}, error) {
return a.updateAPI.InstallUpdateWithHash(installerPath, autoRestart, expectedHash, hashType)
}
// VerifyUpdateFile 验证更新文件哈希值
func (a *App) VerifyUpdateFile(filePath string, expectedHash string, hashType string) (map[string]interface{}, error) {
return a.updateAPI.VerifyUpdateFile(filePath, expectedHash, hashType)
}