From 23b3691a131c6cb5c7787246d88c2f39ffe58cbe Mon Sep 17 00:00:00 2001 From: AoXuan Date: Mon, 4 Aug 2025 00:12:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(version):=20=E4=BC=98=E5=8C=96=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=AF=94=E8=BE=83=E5=92=8C=E6=98=BE=E7=A4=BA=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改版本比较逻辑,优先比较主版本号,然后是次版本号和修订号 - 增加对 beta版本的特殊处理,正式版比 beta 版更新 - 优化版本显示格式,移除不必要的 "v" 前缀 - 在主界面和日志中使用优化后的版本显示格式 --- Go_Updater/api/client.go | 36 ++++++++++++++++++++++++++++------- Go_Updater/main.go | 18 ++++++++++++++++-- Go_Updater/version/manager.go | 17 ++++++++++++++--- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/Go_Updater/api/client.go b/Go_Updater/api/client.go index ef28e82..5cee79a 100644 --- a/Go_Updater/api/client.go +++ b/Go_Updater/api/client.go @@ -176,13 +176,8 @@ func compareVersions(v1, v2 string) int { parts1 := parseVersionParts(v1) parts2 := parseVersionParts(v2) - // 比较每个组件 - maxLen := len(parts1) - if len(parts2) > maxLen { - maxLen = len(parts2) - } - - for i := 0; i < maxLen; i++ { + // 比较前三个组件 (major.minor.patch) + for i := 0; i < 3; i++ { var p1, p2 int if i < len(parts1) { p1 = parts1[i] @@ -198,6 +193,32 @@ func compareVersions(v1, v2 string) int { } } + // 如果前三个组件相同,比较beta版本号 + var beta1, beta2 int + if len(parts1) > 3 { + beta1 = parts1[3] + } + if len(parts2) > 3 { + beta2 = parts2[3] + } + + // 特殊处理beta版本比较: + // - 如果一个是正式版(beta=0),另一个是beta版(beta>0),正式版更新 + // - 如果都是beta版,比较beta版本号 + if beta1 == 0 && beta2 > 0 { + return 1 // 正式版比beta版更新 + } + if beta1 > 0 && beta2 == 0 { + return -1 // beta版比正式版旧 + } + + // 都是正式版或都是beta版,直接比较 + if beta1 < beta2 { + return -1 + } else if beta1 > beta2 { + return 1 + } + return 0 } @@ -265,6 +286,7 @@ func parseVersionParts(version string) []int { break } } + // Beta版本号保持正数,但在比较时会特殊处理 parts = append(parts, betaNum) } else { // 非beta版本,添加0作为beta版本号 diff --git a/Go_Updater/main.go b/Go_Updater/main.go index 45459d6..6a20703 100644 --- a/Go_Updater/main.go +++ b/Go_Updater/main.go @@ -563,7 +563,7 @@ func (app *Application) executeCheckingState() (UpdateState, error) { default: fmt.Printf("检查更新类别:%v\n", updateChannel) } - fmt.Printf("当前版本:%v\n", currentVer) + fmt.Printf("当前版本:%s\n", app.formatVersionForDisplay(currentVer)) app.logger.Info("当前更新类别:" + updateChannel + ";当前版本:" + currentVer) if err != nil { app.logger.Error("检查更新失败: %v", err) @@ -599,7 +599,7 @@ func (app *Application) executeCheckingState() (UpdateState, error) { } app.logger.Info("有可用更新: %s -> %s", currentVer, response.Data.VersionName) - fmt.Printf("发现新版本: %s -> %s\n", currentVer, response.Data.VersionName) + fmt.Printf("发现新版本: %s -> %s\n", app.formatVersionForDisplay(currentVer), response.Data.VersionName) return StateUpdateAvailable, nil } @@ -813,6 +813,20 @@ func (app *Application) formatBytes(bytes int64) string { return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp]) } +// formatVersionForDisplay 将版本格式转换为用户友好的显示格式 +// 例如: "4.4.1.3" -> "4.4.1-beta3", "4.4.1.0" -> "4.4.1" +func (app *Application) formatVersionForDisplay(version string) string { + // 尝试解析版本 + parsedVersion, err := appversion.ParseVersion(version) + if err != nil { + // 如果解析失败,返回原始版本 + return version + } + + // 使用 ToDisplayVersion 方法转换为显示格式 + return parsedVersion.ToDisplayVersion() +} + // handleUserInteraction 处理 GUI 模式的用户交互 func (app *Application) handleUserInteraction(action string) { switch action { diff --git a/Go_Updater/version/manager.go b/Go_Updater/version/manager.go index 9f708d9..99b25a2 100644 --- a/Go_Updater/version/manager.go +++ b/Go_Updater/version/manager.go @@ -147,12 +147,12 @@ func (pv *ParsedVersion) ToVersionString() string { return fmt.Sprintf("%d.%d.%d.%d", pv.Major, pv.Minor, pv.Patch, pv.Beta) } -// ToDisplayVersion 将版本转换为显示格式 (v4.4.0 或 v4.4.1-beta3) +// ToDisplayVersion 将版本转换为显示格式 (4.4.0 或 4.4.1-beta3) func (pv *ParsedVersion) ToDisplayVersion() string { if pv.Beta == 0 { - return fmt.Sprintf("v%d.%d.%d", pv.Major, pv.Minor, pv.Patch) + return fmt.Sprintf("%d.%d.%d", pv.Major, pv.Minor, pv.Patch) } - return fmt.Sprintf("v%d.%d.%d-beta%d", pv.Major, pv.Minor, pv.Patch, pv.Beta) + return fmt.Sprintf("%d.%d.%d-beta%d", pv.Major, pv.Minor, pv.Patch, pv.Beta) } // GetChannel 根据版本返回渠道 (stable 或 beta) @@ -174,5 +174,16 @@ func (pv *ParsedVersion) IsNewer(other *ParsedVersion) bool { if pv.Patch != other.Patch { return pv.Patch > other.Patch } + + // 对于相同的主版本号,正式版本(Beta=0)比beta版本(Beta>0)更新 + // 例如:4.4.1.0(正式版)> 4.4.1.3(beta3) + if pv.Beta == 0 && other.Beta > 0 { + return true // 正式版比beta版更新 + } + if pv.Beta > 0 && other.Beta == 0 { + return false // beta版比正式版旧 + } + + // 如果都是beta版本,比较beta版本号 return pv.Beta > other.Beta }