fix(更新检测): 版本比较改真实比大小——线上版本不高于本机一律不提示更新;本机高于线上时明确提示无需更新(灰度/回滚/漏发版场景不再被引导降级)

This commit is contained in:
2026-09-11 16:09:53 +08:00
parent cd055f8ccd
commit bb52574767
3 changed files with 73 additions and 6 deletions
@@ -8,6 +8,7 @@
import { computed, ref } from 'vue'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { compareVersions, normVersion } from '@/shared/utils/version-compare'
import {
normalizeUpdateProgress,
type UpdateProgress,
@@ -32,10 +33,6 @@ function bindProgressListener() {
})
}
function normVersion(value: unknown): string {
return String(value ?? '').trim().replace(/^[vV]/, '')
}
async function fetchJson(url: string): Promise<Record<string, unknown>> {
const resp = await window.fetch(url, { credentials: 'same-origin' })
if (!resp.ok) throw new Error(`HTTP ${resp.status}`)
@@ -116,11 +113,16 @@ export function useVersionUpdate() {
: `线上最新版本 v${latest.version},暂无下载地址`
return
}
hasUpdate.value = local !== latest.version
// 只有线上版本严格高于本机版本才算"有更新":本机领先(灰度/回滚/漏发版)
// 时不能反过来提示更新,否则会把用户往低版本上引导。
const diff = compareVersions(latest.version, local)
hasUpdate.value = diff > 0
canDownload.value = hasUpdate.value && Boolean(latest.fileUrl)
hint.value = hasUpdate.value
? `发现新版本 v${latest.version}${latest.fileUrl ? '' : ',暂无下载地址'}`
: `已是最新版本 v${local}`
: diff < 0
? `当前版本 v${local} 高于线上发布版本 v${latest.version},无需更新`
: `已是最新版本 v${local}`
} catch {
hasUpdate.value = false
canDownload.value = false
@@ -0,0 +1,26 @@
/**
* 客户端版本号归一化与比较(桌面端更新检测用)。
*
* 只比数字段、缺失段按 0 补齐(3.0.7 == 3.0.7.0);非纯数字段(如
* 3.0.71-beta 的 beta)按 0 处理,避免 NaN 让比较结果乱序。
*/
/** 归一化版本号:去空白、去前缀 v/V,便于 'v3.0.71' 与 '3.0.71' 相等比较。 */
export function normVersion(value: unknown): string {
return String(value ?? '').trim().replace(/^[vV]/, '')
}
/** 返回 >0 表示 a 比 b 新,<0 表示 a 比 b 旧,0 表示相同。 */
export function compareVersions(a: string, b: string): number {
const pa = normVersion(a).split('.')
const pb = normVersion(b).split('.')
const len = Math.max(pa.length, pb.length)
for (let i = 0; i < len; i++) {
const na = parseInt(pa[i] ?? '', 10)
const nb = parseInt(pb[i] ?? '', 10)
const va = Number.isFinite(na) ? na : 0
const vb = Number.isFinite(nb) ? nb : 0
if (va !== vb) return va - vb
}
return 0
}