feat(api): 修复beta版本的更新检查

This commit is contained in:
2025-07-27 14:12:54 +08:00
parent 2aad0c65c2
commit 226d68cb1c
3 changed files with 65 additions and 7 deletions

View File

@@ -137,6 +137,11 @@ func (c *Client) IsUpdateAvailable(response *MirrorResponse, currentVersion stri
currentVersionNormalized := c.normalizeVersionForComparison(currentVersion)
latestVersionNormalized := c.normalizeVersionForComparison(latestVersion)
// 调试输出
// fmt.Printf("Current: %s -> %s\n", currentVersion, currentVersionNormalized)
// fmt.Printf("Latest: %s -> %s\n", latestVersion, latestVersionNormalized)
// fmt.Printf("Compare result: %d\n", compareVersions(currentVersionNormalized, latestVersionNormalized))
// 使用语义版本比较
return compareVersions(currentVersionNormalized, latestVersionNormalized) < 0
}
@@ -204,28 +209,40 @@ func normalizeVersion(version string) string {
return version
}
// parseVersionParts 将版本字符串解析为数字组件
// parseVersionParts 将版本字符串解析为数字组件包括beta版本号
func parseVersionParts(version string) []int {
if version == "" {
return []int{0}
}
parts := make([]int, 0, 3)
parts := make([]int, 0, 4)
current := 0
for _, char := range version {
// 先检查是否包含 -beta
betaIndex := strings.Index(version, "-beta")
var mainVersion, betaVersion string
if betaIndex != -1 {
mainVersion = version[:betaIndex]
betaVersion = version[betaIndex+5:] // 跳过 "-beta"
} else {
mainVersion = version
betaVersion = ""
}
// 解析主版本号 (major.minor.patch)
for _, char := range mainVersion {
if char >= '0' && char <= '9' {
current = current*10 + int(char-'0')
} else if char == '.' {
parts = append(parts, current)
current = 0
} else {
// 非数字非点字符停止解析(如预发布标识符)
// 遇到非数字非点字符停止解析
break
}
}
// 添加最后一个组件
// 添加最后一个主版本组件
parts = append(parts, current)
// 确保至少有 3 个组件 (major.minor.patch)
@@ -233,6 +250,27 @@ func parseVersionParts(version string) []int {
parts = append(parts, 0)
}
// 解析beta版本号
if betaVersion != "" {
// 跳过可能的点号
if strings.HasPrefix(betaVersion, ".") {
betaVersion = betaVersion[1:]
}
betaNum := 0
for _, char := range betaVersion {
if char >= '0' && char <= '9' {
betaNum = betaNum*10 + int(char-'0')
} else {
break
}
}
parts = append(parts, betaNum)
} else {
// 非beta版本添加0作为beta版本号
parts = append(parts, 0)
}
return parts
}

View File

@@ -153,6 +153,26 @@ func TestIsUpdateAvailable(t *testing.T) {
currentVersion: "4.4.0.0",
expected: false,
},
{
name: "beta版本有更新",
response: &MirrorResponse{
Code: 0,
Data: struct {
VersionName string `json:"version_name"`
VersionNumber int `json:"version_number"`
URL string `json:"url,omitempty"`
SHA256 string `json:"sha256,omitempty"`
Channel string `json:"channel"`
OS string `json:"os"`
Arch string `json:"arch"`
UpdateType string `json:"update_type,omitempty"`
ReleaseNote string `json:"release_note"`
FileSize int64 `json:"filesize,omitempty"`
}{VersionName: "v4.4.1-beta.4"},
},
currentVersion: "4.4.1.3",
expected: true,
},
}
for _, test := range tests {

View File

@@ -563,7 +563,7 @@ func (app *Application) executeCheckingState() (UpdateState, error) {
default:
fmt.Printf("检查更新类别:%v\n", updateChannel)
}
fmt.Printf("当前版本:%v", currentVer)
fmt.Printf("当前版本:%v\n", currentVer)
app.logger.Info("当前更新类别:" + updateChannel + ";当前版本:" + currentVer)
if err != nil {
app.logger.Error("检查更新失败: %v", err)