diff --git a/src/composables/useAppUpdate.ts b/src/composables/useAppUpdate.ts index 7564f95..cc8106f 100644 --- a/src/composables/useAppUpdate.ts +++ b/src/composables/useAppUpdate.ts @@ -64,7 +64,7 @@ export function useAppUpdate() { return Boolean(target?.url) && target?.version !== currentVersion.value; } - /** 下载最新版本并排队等待生效 */ + /** 下载最新版本,只落盘不激活,等用户确认后再应用 */ async function download(): Promise { const target = latest.value; if (!target?.url) { @@ -84,7 +84,6 @@ export function useAppUpdate() { checksum: target.checksum, sessionKey: target.sessionKey }); - await CapacitorUpdater.next({ id: bundle.id }); ready.value = bundle; return bundle; } catch (e) { @@ -96,19 +95,28 @@ export function useAppUpdate() { } } - /** 立即重启应用使更新生效 */ - async function reload(): Promise { - await CapacitorUpdater.reload(); + /** 应用已下载的版本并重启,只有用户明确点了才执行 */ + async function apply(bundle: BundleInfo): Promise { + await CapacitorUpdater.set({ id: bundle.id }); } - /** 检查并下载,供启动时自动更新调用 */ - async function checkAndDownload(): Promise { - await loadCurrent(); - const result = await check(); - if (!result?.url || result.version === currentVersion.value) { - return false; + /** 查找已下载但尚未应用的版本,用户不点就一直停在这里 */ + async function restorePending(): Promise { + if (!supported) { + return; + } + try { + const [current, list] = await Promise.all([CapacitorUpdater.current(), CapacitorUpdater.list()]); + const currentId = current.bundle?.id; + const pending = list.bundles + .filter(bundle => bundle.id !== currentId) + .sort((a, b) => Date.parse(b.downloaded) - Date.parse(a.downloaded))[0]; + if (pending) { + ready.value = pending; + } + } catch { + ready.value = null; } - return (await download()) !== null; } return { @@ -125,8 +133,8 @@ export function useAppUpdate() { check, hasUpdate, download, - reload, - checkAndDownload + apply, + restorePending }; } @@ -138,11 +146,21 @@ export function isAutoUpdateEnabled(): boolean { /** * 启动时静默检查更新 * - * 只下载排队,不重启,下次打开 App 自动生效 + * 只下载不应用,是否切换版本始终由用户决定 */ export async function runAutoUpdate(): Promise { if (!Capacitor.isNativePlatform() || !isAutoUpdateEnabled()) { return; } - await useAppUpdate().checkAndDownload(); + const update = useAppUpdate(); + await update.loadCurrent(); + await update.restorePending(); + if (update.ready.value) { + return; + } + const result = await update.check(); + if (!result?.url || result.version === update.currentVersion.value) { + return; + } + await update.download(); } diff --git a/src/pages/setting/AppSettings.vue b/src/pages/setting/AppSettings.vue index 5fc722c..5219dda 100644 --- a/src/pages/setting/AppSettings.vue +++ b/src/pages/setting/AppSettings.vue @@ -56,20 +56,14 @@
- @@ -155,25 +149,51 @@ const { check, hasUpdate, download, - reload + apply, + restorePending } = useAppUpdate(); const autoUpdate = ref(isAutoUpdateEnabled()); -const canCheckUpdate = computed(() => updateSupported && !checking.value && !downloading.value); +/** 当前可执行的动作,按检查、下载、应用三阶段推进 */ +const actionTitle = computed(() => { + if (!updateSupported) { + return "检查更新"; + } + if (checking.value) { + return "正在检查"; + } + if (downloading.value) { + return "正在下载"; + } + if (ready.value) { + return "现在更新"; + } + if (latest.value && hasUpdate()) { + return "开始下载"; + } + return "检查更新"; +}); + +const actionEnabled = computed(() => { + if (!updateSupported || checking.value || downloading.value) { + return false; + } + return true; +}); const updateNote = computed(() => { if (!updateSupported) { return "仅在 App 内可用"; } if (downloading.value) { - return `下载中 ${percent.value}%`; + return `${percent.value}%`; } if (ready.value) { - return "已下载,下次启动生效"; + return `v${ready.value.version} 已下载`; } if (checking.value) { - return "检查中"; + return "请稍候"; } if (latest.value) { return hasUpdate() ? `发现 v${latest.value.version}` : "已是最新"; @@ -183,6 +203,7 @@ const updateNote = computed(() => { onMounted(async () => { await loadCurrent(); + await restorePending(); }); function handleAutoUpdateChange(value: boolean | string | number): void { @@ -191,10 +212,26 @@ function handleAutoUpdateChange(value: boolean | string | number): void { localStorage.setItem(AUTO_UPDATE_KEY, enabled ? "1" : "0"); } -async function handleCheckUpdate(): Promise { - if (!canCheckUpdate.value) { +async function handleAction(): Promise { + if (!actionEnabled.value) { return; } + // 已下载,应用并重启 + if (ready.value) { + await apply(ready.value); + return; + } + // 已发现新版本,开始下载 + if (latest.value && hasUpdate()) { + const bundle = await download(); + if (bundle) { + Message.success(`v${bundle.version} 已下载,点「现在更新」生效`); + return; + } + Message.error(updateError.value || "下载失败"); + return; + } + // 检查更新 const result = await check(); if (!result) { Message.error(updateError.value || "检查更新失败"); @@ -202,18 +239,7 @@ async function handleCheckUpdate(): Promise { } if (!hasUpdate()) { Message.info("已是最新版本"); - return; } - const bundle = await download(); - if (bundle) { - Message.success(`v${bundle.version} 已下载,下次启动生效`); - return; - } - Message.error(updateError.value || "下载失败"); -} - -async function handleReload(): Promise { - await reload(); }