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}`); + } }