feat: split update into check, download and apply steps
This commit is contained in:
@@ -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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user