From 02a41663a1e0f7d4ec2c49a2576c669887f380a5 Mon Sep 17 00:00:00 2001 From: Timi Date: Tue, 17 Nov 2026 16:07:43 +0800 Subject: [PATCH] =?UTF-8?q?fix(update):=20=E5=88=AB=E8=AE=A9=E5=9D=8F?= =?UTF-8?q?=E5=8C=85=E5=8D=A1=E4=BD=8F=E6=9B=B4=E6=96=B0=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - restorePending 只认 success / pending 的包:插件把失败包也列在 list() 里, 之前会被当成「已下载待应用」,每次启动就弹窗 - apply 失败时删除该包并清空待应用状态,不再卡在「已下载,重启生效」 - 版本更新页在应用失败后提示原因,用户可以直接重新检查更新 --- src/composables/useAppUpdate.ts | 16 ++++++++++++++-- src/pages/setting/AppUpdate.vue | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/composables/useAppUpdate.ts b/src/composables/useAppUpdate.ts index d619d09..2aeec2e 100644 --- a/src/composables/useAppUpdate.ts +++ b/src/composables/useAppUpdate.ts @@ -135,7 +135,16 @@ async function download(): Promise { /** 立即应用并重启 */ async function apply(bundle: BundleInfo): Promise { - await CapacitorUpdater.set({ id: bundle.id }); + error.value = ""; + try { + await CapacitorUpdater.set({ id: bundle.id }); + } catch (e) { + // 包目录已被清理或本身就是坏的(例如把安装包当热更包下下来), + // set 会报 id 不存在。不清理的话每次启动都卡在「已下载,重启生效」 + await CapacitorUpdater.delete({ id: bundle.id }).catch(() => undefined); + ready.value = null; + error.value = e instanceof Error ? e.message : "更新失败"; + } } /** 排队到下次启动应用 */ @@ -143,6 +152,9 @@ async function defer(bundle: BundleInfo): Promise { await CapacitorUpdater.next({ id: bundle.id }); } +/** 可以应用的包状态:剩下的都是删除中或下载失败的残骸,set 会报 id 不存在 */ +const USABLE_STATUS: Array = ["success", "pending"]; + /** 查找已下载但尚未应用的版本 */ async function restorePending(): Promise { if (!supported) { @@ -152,7 +164,7 @@ async function restorePending(): Promise { const [current, list] = await Promise.all([CapacitorUpdater.current(), CapacitorUpdater.list()]); const currentId = current.bundle?.id; const pending = list.bundles - .filter(bundle => bundle.id !== currentId) + .filter(bundle => bundle.id !== currentId && USABLE_STATUS.includes(bundle.status)) .sort((a, b) => Date.parse(b.downloaded) - Date.parse(a.downloaded))[0]; ready.value = pending ?? null; } catch { diff --git a/src/pages/setting/AppUpdate.vue b/src/pages/setting/AppUpdate.vue index a1d62b3..4daa4e0 100644 --- a/src/pages/setting/AppUpdate.vue +++ b/src/pages/setting/AppUpdate.vue @@ -279,6 +279,10 @@ async function handleApply(): Promise { return; } await apply(bundle); + // 应用成功会直接重启,走到这里说明包是坏的,已经清理掉,提示后可以重新检查 + if (updateError.value) { + Message.error(`更新失败:${updateError.value}`); + } }