feat: split update into check, download and apply steps

This commit is contained in:
Timi
2026-11-07 16:24:29 +08:00
parent 235bb25b02
commit a3bae17581
2 changed files with 87 additions and 43 deletions
+34 -16
View File
@@ -64,7 +64,7 @@ export function useAppUpdate() {
return Boolean(target?.url) && target?.version !== currentVersion.value;
}
/** 下载最新版本并排队等待生效 */
/** 下载最新版本,只落盘不激活,等用户确认后再应用 */
async function download(): Promise<BundleInfo | null> {
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<void> {
await CapacitorUpdater.reload();
/** 应用已下载的版本并重启,只有用户明确点了才执行 */
async function apply(bundle: BundleInfo): Promise<void> {
await CapacitorUpdater.set({ id: bundle.id });
}
/** 检查并下载,供启动时自动更新调用 */
async function checkAndDownload(): Promise<boolean> {
await loadCurrent();
const result = await check();
if (!result?.url || result.version === currentVersion.value) {
return false;
/** 查找已下载但尚未应用的版本,用户不点就一直停在这里 */
async function restorePending(): Promise<void> {
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<void> {
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();
}
+53 -27
View File
@@ -56,20 +56,14 @@
</template>
</t-cell>
<t-cell
title="检查更新"
:title="actionTitle"
:note="updateNote"
:arrow="canCheckUpdate"
@click="handleCheckUpdate"
:arrow="actionEnabled"
@click="handleAction"
/>
<div v-if="downloading" class="update-progress">
<t-progress :percentage="percent" :label="false" />
</div>
<t-cell
v-if="ready"
title="立即重启生效"
arrow
@click="handleReload"
/>
</t-cell-group>
</div>
</main>
@@ -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<string>(() => {
if (!updateSupported) {
return "检查更新";
}
if (checking.value) {
return "正在检查";
}
if (downloading.value) {
return "正在下载";
}
if (ready.value) {
return "现在更新";
}
if (latest.value && hasUpdate()) {
return "开始下载";
}
return "检查更新";
});
const actionEnabled = computed<boolean>(() => {
if (!updateSupported || checking.value || downloading.value) {
return false;
}
return true;
});
const updateNote = computed<string>(() => {
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<string>(() => {
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<void> {
if (!canCheckUpdate.value) {
async function handleAction(): Promise<void> {
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<void> {
}
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<void> {
await reload();
}
</script>