feat(version): 桌面端更新面板支持指定版本安装

- 新增公开接口 GET /api/version/list(最近 50 条,字段口径同管理端列表),
  桌面端下拉不再拼 OSS 地址(版本包被删后拼地址会 404)
- 更新面板(登录页 + 首页)加「指定版本更新」选择器:默认选中最新版,
  可选全部已发布版本(含回退),回退/同版给出明确文案与二次确认
- 客户端无需发版:do_update_app(file_url) 本就接受任意版本包直链
This commit is contained in:
2026-09-14 11:05:30 +08:00
parent 375b89154b
commit 8803e22f39
7 changed files with 515 additions and 20 deletions
@@ -10,16 +10,29 @@ import { computed, ref } from 'vue'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
import { selectChangelogEntries, type ClientChangelogEntry } from '@/shared/client-changelog'
import { compareVersions, normVersion } from '@/shared/utils/version-compare'
import {
classifyTargetVersion,
describeTargetVersion,
parseVersionList,
type ClientVersionItem,
} from '@/shared/utils/version-picker'
import {
normalizeUpdateProgress,
type UpdateProgress,
} from '@/shared/utils/update-progress'
export type { UpdateProgress, UpdateProgressStatus } from '@/shared/utils/update-progress'
export type { ClientVersionItem } from '@/shared/utils/version-picker'
const updateProgress = ref<UpdateProgress | null>(null)
let progressListenerBound = false
// 公开版本列表:登录页与首页共用同一份缓存(SPA 内路由切换不重复拉取)
const versions = ref<ClientVersionItem[]>([])
const versionListLoading = ref(false)
const versionListError = ref('')
let versionListLoaded = false
/**
* 绑定一次全局监听:下载进度由 Python 侧主动推事件(同 pywebview-download-progress 约定),
* JS 侧不轮询——桥调用虽然并发安全,但轮询会给主进程平白加请求。
@@ -70,6 +83,30 @@ export async function fetchLatestVersion(): Promise<{ version: string; fileUrl:
return { version: normVersion(data.version), fileUrl: String(data.file_url ?? '') }
}
/**
* 拉取公开版本列表("指定版本更新"可选项,匿名可达,最近若干条)。
* 只在首次展开面板/点刷新时请求;失败保留上一次成功的列表,错误交给 UI 提示重试。
*/
export async function loadVersions(force = false): Promise<void> {
if (versionListLoading.value) return
if (versionListLoaded && !force) return
versionListLoading.value = true
versionListError.value = ''
try {
const data = await fetchJson('/api/version/list')
const items = parseVersionList(data)
versions.value = items
versionListLoaded = true
if (!items.length) {
versionListError.value = '暂无可选版本'
}
} catch {
versionListError.value = '版本列表加载失败,点击"刷新列表"重试'
} finally {
versionListLoading.value = false
}
}
export function useVersionUpdate() {
const checking = ref(false)
const updating = ref(false)
@@ -133,20 +170,15 @@ export function useVersionUpdate() {
}
}
async function doUpdate() {
if (updating.value || !fileUrl.value) return
/** 下载并安装指定更新包("立即更新"与"指定版本更新"共用;确认框由各自入口负责)。 */
async function startUpdate(url: string) {
updating.value = true
const bridge = getPywebviewApi()
const doUpdateApp = bridge?.do_update_app
const desktopUpdate = Boolean(doUpdateApp)
const tip = desktopUpdate
? `有更新,是否现在更新?${changelogConfirmSuffix()}\n更新将下载安装包并重启程序。`
: `发现新版本,是否下载最新安装包?${changelogConfirmSuffix()}`
if (!window.confirm(tip)) return
updating.value = true
if (!desktopUpdate) {
if (!doUpdateApp) {
// 网页/无桌面桥形态:OSS 公开直链直接下载安装包(区别于桌面端自动下载安装)
hint.value = '开始下载安装包,若浏览器未响应请再次点击...'
window.location.href = fileUrl.value
window.location.href = url
updating.value = false
return
}
@@ -161,7 +193,7 @@ export function useVersionUpdate() {
}
hint.value = '正在下载并准备更新,程序将自动退出...'
try {
const result = await doUpdateApp!(fileUrl.value)
const result = await doUpdateApp(url)
if (result?.success) {
const last = updateProgress.value
updateProgress.value = {
@@ -198,6 +230,34 @@ export function useVersionUpdate() {
}
}
async function doUpdate() {
if (updating.value || !fileUrl.value) return
const desktopUpdate = Boolean(getPywebviewApi()?.do_update_app)
const tip = desktopUpdate
? `有更新,是否现在更新?${changelogConfirmSuffix(latestVersion.value)}\n更新将下载安装包并重启程序。`
: `发现新版本,是否下载最新安装包?${changelogConfirmSuffix(latestVersion.value)}`
if (!window.confirm(tip)) return
await startUpdate(fileUrl.value)
}
/**
* 指定版本更新:安装列表里挑中的那个版本(可高于、等于或低于本机)。
* 确认框必须写明目标版本与升级/回退性质——回退是用户显式选择,不能静默降级。
*/
async function doUpdateVersion(item: ClientVersionItem) {
if (updating.value || !item?.fileUrl) return
await loadCurrentVersion()
const kind = classifyTargetVersion(currentVersion.value, item.version)
const desktopUpdate = Boolean(getPywebviewApi()?.do_update_app)
const head = desktopUpdate ? `确认更新到 v${item.version}` : `确认下载 v${item.version} 安装包?`
const tip =
`${head}\n${describeTargetVersion(kind, currentVersion.value, item.version)}` +
`${changelogConfirmSuffix(item.version)}` +
(desktopUpdate ? '\n更新将下载安装包并重启程序。' : '')
if (!window.confirm(tip)) return
await startUpdate(item.fileUrl)
}
// 进度条的百分比/文案由 UpdateProgressBar 组件从 progress 自行派生(见 shared/utils/update-progress
const progress = computed(() => updateProgress.value)
@@ -206,9 +266,16 @@ export function useVersionUpdate() {
selectChangelogEntries({ currentVersion: currentVersion.value, latestVersion: latestVersion.value }),
)
/** 更新确认框里的"本次更新内容":只取最新一条、最多 5 行,避免弹窗过长 */
function changelogConfirmSuffix(): string {
const items = (changelog.value[0]?.items ?? []).slice(0, 5)
/**
* 更新确认框里的"本次更新内容":按"本机版本 → 目标版本"区间取最新一条、最多 5 行,
* 避免弹窗过长;目标版本不高于本机(同版/回退)时区间为空,不带这段文案。
*/
function changelogConfirmSuffix(targetVersion: string): string {
const entries = selectChangelogEntries({
currentVersion: currentVersion.value,
latestVersion: targetVersion,
})
const items = (entries[0]?.items ?? []).slice(0, 5)
return items.length ? `\n本次更新内容:\n${items.map((item) => `· ${item}`).join('\n')}` : ''
}
@@ -224,8 +291,13 @@ export function useVersionUpdate() {
checked,
progress,
changelog,
versions,
versionListLoading,
versionListError,
loadCurrentVersion,
loadVersions,
runCheck,
doUpdate,
doUpdateVersion,
}
}