diff --git a/src/composables/useAppUpdate.ts b/src/composables/useAppUpdate.ts index cc8106f..8af04fc 100644 --- a/src/composables/useAppUpdate.ts +++ b/src/composables/useAppUpdate.ts @@ -1,124 +1,128 @@ import { Capacitor } from "@capacitor/core"; import { CapacitorUpdater, type BundleInfo, type LatestVersion } from "@capgo/capacitor-updater"; +import { DialogPlugin } from "tdesign-mobile-vue"; import { ref } from "vue"; -/** 自动检查更新的存储键 */ +/** 自动更新的存储键 */ export const AUTO_UPDATE_KEY = "app.autoUpdate"; +// 模块级状态,页面与启动逻辑共享同一份 +const supported = Capacitor.isNativePlatform(); +const currentVersion = ref(""); +const nativeVersion = ref(""); +const latest = ref(null); +const checking = ref(false); +const downloading = ref(false); +const percent = ref(0); +const ready = ref(null); +const error = ref(""); +const autoUpdate = ref(localStorage.getItem(AUTO_UPDATE_KEY) === "1"); + +/** 读取当前运行的 Web 包版本与原生壳版本 */ +async function loadCurrent(): Promise { + if (!supported) { + return; + } + try { + const result = await CapacitorUpdater.current(); + currentVersion.value = result.bundle?.version ?? "builtin"; + nativeVersion.value = result.native ?? ""; + } catch { + currentVersion.value = ""; + nativeVersion.value = ""; + } +} + +/** 查询服务端最新版本 */ +async function check(): Promise { + if (!supported) { + error.value = "仅在 App 内可用"; + return null; + } + checking.value = true; + error.value = ""; + try { + const result = await CapacitorUpdater.getLatest(); + latest.value = result; + return result; + } catch (e) { + error.value = e instanceof Error ? e.message : "检查更新失败"; + return null; + } finally { + checking.value = false; + } +} + +/** 是否发现新版本 */ +function hasUpdate(): boolean { + const target = latest.value; + return Boolean(target?.url) && target?.version !== currentVersion.value; +} + +/** 下载最新版本,只落盘不激活 */ +async function download(): Promise { + const target = latest.value; + if (!target?.url) { + error.value = "没有可下载的版本"; + return null; + } + downloading.value = true; + percent.value = 0; + error.value = ""; + const listener = await CapacitorUpdater.addListener("download", info => { + percent.value = info.percent; + }); + try { + const bundle = await CapacitorUpdater.download({ + url: target.url, + version: target.version, + checksum: target.checksum, + sessionKey: target.sessionKey + }); + ready.value = bundle; + return bundle; + } catch (e) { + error.value = e instanceof Error ? e.message : "下载失败"; + return null; + } finally { + downloading.value = false; + await listener.remove(); + } +} + +/** 立即应用并重启 */ +async function apply(bundle: BundleInfo): Promise { + await CapacitorUpdater.set({ id: bundle.id }); +} + +/** 排队到下次启动应用 */ +async function defer(bundle: BundleInfo): Promise { + await CapacitorUpdater.next({ id: bundle.id }); +} + +/** 查找已下载但尚未应用的版本 */ +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]; + ready.value = pending ?? null; + } catch { + ready.value = null; + } +} + /** * 应用热更新 * - * 只在原生环境可用,Web 端调用会得到 supported = false - * - * 下载完成后调用 next 排队,下次切后台或重启自动生效,不打断当前操作 + * 只在原生环境可用,Web 端 supported 恒为 false */ export function useAppUpdate() { - const supported = Capacitor.isNativePlatform(); - const currentVersion = ref(""); - const nativeVersion = ref(""); - const latest = ref(null); - const checking = ref(false); - const downloading = ref(false); - const percent = ref(0); - const ready = ref(null); - const error = ref(""); - - /** 读取当前运行的 Web 包版本与原生壳版本 */ - async function loadCurrent(): Promise { - if (!supported) { - return; - } - try { - const result = await CapacitorUpdater.current(); - currentVersion.value = result.bundle?.version ?? "builtin"; - nativeVersion.value = result.native ?? ""; - } catch { - currentVersion.value = ""; - nativeVersion.value = ""; - } - } - - /** 查询服务端最新版本 */ - async function check(): Promise { - if (!supported) { - error.value = "仅在 App 内可用"; - return null; - } - checking.value = true; - error.value = ""; - try { - const result = await CapacitorUpdater.getLatest(); - latest.value = result; - return result; - } catch (e) { - error.value = e instanceof Error ? e.message : "检查更新失败"; - return null; - } finally { - checking.value = false; - } - } - - /** 是否发现新版本 */ - function hasUpdate(): boolean { - const target = latest.value; - return Boolean(target?.url) && target?.version !== currentVersion.value; - } - - /** 下载最新版本,只落盘不激活,等用户确认后再应用 */ - async function download(): Promise { - const target = latest.value; - if (!target?.url) { - error.value = "没有可下载的版本"; - return null; - } - downloading.value = true; - percent.value = 0; - error.value = ""; - const listener = await CapacitorUpdater.addListener("download", info => { - percent.value = info.percent; - }); - try { - const bundle = await CapacitorUpdater.download({ - url: target.url, - version: target.version, - checksum: target.checksum, - sessionKey: target.sessionKey - }); - ready.value = bundle; - return bundle; - } catch (e) { - error.value = e instanceof Error ? e.message : "下载失败"; - return null; - } finally { - downloading.value = false; - await listener.remove(); - } - } - - /** 应用已下载的版本并重启,只有用户明确点了才执行 */ - async function apply(bundle: BundleInfo): Promise { - await CapacitorUpdater.set({ id: bundle.id }); - } - - /** 查找已下载但尚未应用的版本,用户不点就一直停在这里 */ - 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 { supported, currentVersion, @@ -129,38 +133,74 @@ export function useAppUpdate() { percent, ready, error, + autoUpdate, loadCurrent, check, hasUpdate, download, apply, + defer, restorePending }; } -/** 是否开启自动检查更新 */ +/** 是否开启自动更新 */ export function isAutoUpdateEnabled(): boolean { - return localStorage.getItem(AUTO_UPDATE_KEY) === "1"; + return autoUpdate.value; +} + +/** 切换自动更新,同时写入本地存储 */ +export function setAutoUpdate(enabled: boolean): void { + autoUpdate.value = enabled; + localStorage.setItem(AUTO_UPDATE_KEY, enabled ? "1" : "0"); +} + +/** 弹窗让用户决定立即更新还是下次启动 */ +function promptUpdate(bundle: BundleInfo): void { + const dialog = DialogPlugin.confirm({ + title: "发现新版本", + content: `v${bundle.version} 已下载完成,重启后生效`, + cancelBtn: "稍后更新", + confirmBtn: "现在更新", + onCancel() { + dialog.destroy(); + // 稍后更新即排队,下次启动自动应用 + void defer(bundle); + }, + async onConfirm() { + dialog.destroy(); + await apply(bundle); + } + }); } /** - * 启动时静默检查更新 + * 启动时检查更新 * - * 只下载不应用,是否切换版本始终由用户决定 + * 检查每次启动都做;是否静默下载取决于自动更新开关 + * 下载完成后弹窗让用户选择立即重启还是稍后 */ export async function runAutoUpdate(): Promise { - if (!Capacitor.isNativePlatform() || !isAutoUpdateEnabled()) { + if (!supported) { return; } - const update = useAppUpdate(); - await update.loadCurrent(); - await update.restorePending(); - if (update.ready.value) { + await loadCurrent(); + await restorePending(); + if (ready.value) { + if (isAutoUpdateEnabled()) { + promptUpdate(ready.value); + } return; } - const result = await update.check(); - if (!result?.url || result.version === update.currentVersion.value) { + const result = await check(); + if (!result?.url || result.version === currentVersion.value) { return; } - await update.download(); + if (!isAutoUpdateEnabled()) { + return; + } + await download(); + if (ready.value) { + promptUpdate(ready.value); + } } diff --git a/src/pages/setting/AppSettings.vue b/src/pages/setting/AppSettings.vue index fcc41e1..400cd87 100644 --- a/src/pages/setting/AppSettings.vue +++ b/src/pages/setting/AppSettings.vue @@ -1,5 +1,5 @@