feat: add manual and optional auto update in settings

This commit is contained in:
Timi
2026-11-07 01:08:59 +08:00
parent b00bba0377
commit 1a5855e1d0
4 changed files with 261 additions and 3 deletions
+105
View File
@@ -47,6 +47,29 @@
</template>
</t-cell>
</t-cell-group>
<t-cell-group title="版本更新" theme="card" v-entrance-transition>
<t-cell title="当前版本" :note="currentVersion || '—'" />
<t-cell title="自动检查更新">
<template #right-icon>
<t-switch :model-value="autoUpdate" @change="handleAutoUpdateChange" />
</template>
</t-cell>
<t-cell
title="检查更新"
:note="updateNote"
:arrow="canCheckUpdate"
@click="handleCheckUpdate"
/>
<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>
</div>
@@ -57,6 +80,8 @@ import type { ThemeMode } from "@/store/globalUI";
import { useGlobalUIStore } from "@/store/globalUI";
import SegmentedSlider from "@/components/SegmentedSlider.vue";
import { useSettingStore } from "@/store/settingStore";
import { AUTO_UPDATE_KEY, isAutoUpdateEnabled, useAppUpdate } from "@/composables/useAppUpdate";
import { Message } from "@/utils/message";
defineOptions({
name: "AppSettings"
@@ -112,6 +137,82 @@ async function playSettingSoundPreview(type: "scanSuccess" | "registerSuccess"):
await settingStore.playRegisterSuccessSound();
}
// ---------- 版本更新 ----------
const {
supported: updateSupported,
currentVersion,
latest,
checking,
downloading,
percent,
ready,
error: updateError,
loadCurrent,
check,
hasUpdate,
download,
reload
} = useAppUpdate();
const autoUpdate = ref(isAutoUpdateEnabled());
const canCheckUpdate = computed(() => updateSupported && !checking.value && !downloading.value);
const updateNote = computed<string>(() => {
if (!updateSupported) {
return "仅在 App 内可用";
}
if (downloading.value) {
return `下载中 ${percent.value}%`;
}
if (ready.value) {
return "已下载,下次启动生效";
}
if (checking.value) {
return "检查中";
}
if (latest.value) {
return hasUpdate() ? `发现 v${latest.value.version}` : "已是最新";
}
return "";
});
onMounted(async () => {
await loadCurrent();
});
function handleAutoUpdateChange(value: boolean | string | number): void {
const enabled = value === true;
autoUpdate.value = enabled;
localStorage.setItem(AUTO_UPDATE_KEY, enabled ? "1" : "0");
}
async function handleCheckUpdate(): Promise<void> {
if (!canCheckUpdate.value) {
return;
}
const result = await check();
if (!result) {
Message.error(updateError.value || "检查更新失败");
return;
}
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>
<style scoped lang="less">
@@ -150,6 +251,10 @@ async function playSettingSoundPreview(type: "scanSuccess" | "registerSuccess"):
text-align: right;
}
}
.update-progress {
padding: 0 1rem .75rem;
}
}
:global(html.theme-dark) {