feat: 打开时提示新版本并自动更新

This commit is contained in:
Nixevol
2026-10-29 07:33:38 +08:00
parent 54d0460cfb
commit 623c7c28e9
10 changed files with 414 additions and 3 deletions
+80
View File
@@ -1262,6 +1262,11 @@ async function logout() {
const closeAsk = ref(false);
const closeCount = ref(0);
let allowClose = false;
const updateOpen = ref(false);
const updateInfo = ref({ latest: "", current: "", notes: "", can_apply: false });
const updateBusy = ref(false);
const updateMessage = ref("");
let updateTimer = 0;
function inTauri() {
return "__TAURI_INTERNALS__" in window;
@@ -1304,9 +1309,68 @@ async function confirmQuit() {
await getCurrentWindow().destroy();
}
async function checkForUpdate() {
try {
const response = await fetch("/api/update");
if (!response.ok) return;
const payload = await response.json();
if (!payload.available) return;
updateInfo.value = {
latest: String(payload.latest || ""),
current: String(payload.current || ""),
notes: String(payload.notes || ""),
can_apply: Boolean(payload.can_apply),
};
updateMessage.value = "";
updateOpen.value = true;
} catch {
// 检查失败时不打断使用
}
}
async function confirmUpdate() {
updateBusy.value = true;
updateMessage.value = "正在下载更新";
try {
const response = await fetch("/api/update/apply", { method: "POST" });
const payload = await response.json().catch(() => ({}));
if (!response.ok) {
updateBusy.value = false;
updateMessage.value = typeof payload.detail === "string" ? payload.detail : "更新没有完成,请稍后再试";
return;
}
} catch {
updateBusy.value = false;
updateMessage.value = "更新没有完成,请稍后再试";
return;
}
updateTimer = window.setInterval(async () => {
try {
const response = await fetch("/api/update/status");
const payload = await response.json();
updateMessage.value = String(payload.message || "正在下载更新");
if (payload.state === "failed") {
window.clearInterval(updateTimer);
updateBusy.value = false;
return;
}
if (payload.state === "restarting") {
window.clearInterval(updateTimer);
allowClose = true;
if (inTauri()) await getCurrentWindow().destroy();
}
} catch {
window.clearInterval(updateTimer);
updateBusy.value = false;
updateMessage.value = "更新没有完成,请稍后再试";
}
}, 500);
}
onMounted(async () => {
document.title = "VideosDownTools";
void watchClose();
void checkForUpdate();
await loadDeviceDefaults();
loadSettings();
try {
@@ -1653,6 +1717,20 @@ onUnmounted(stopLive);
</div>
</template>
</n-modal>
<n-modal v-model:show="updateOpen" preset="card" title="发现新版本" style="width: 440px" :mask-closable="!updateBusy" :closable="!updateBusy">
<p class="update-copy">新版本 {{ updateInfo.latest }},当前是 {{ updateInfo.current }}。</p>
<p v-if="updateInfo.notes" class="update-notes">{{ updateInfo.notes }}</p>
<p v-if="updateInfo.can_apply" class="update-copy">选择更新后会自动下载并重新打开。已下载的视频和登录状态会保留,正在进行的下载会中断。</p>
<p v-else class="update-copy">当前这份程序不能自动更新。</p>
<p v-if="updateMessage" class="update-copy">{{ updateMessage }}</p>
<template #footer>
<div class="modal-actions">
<span class="modal-spacer"></span>
<n-button :disabled="updateBusy" @click="updateOpen = false">稍后</n-button>
<n-button v-if="updateInfo.can_apply" type="primary" :loading="updateBusy" @click="confirmUpdate">立即更新</n-button>
</div>
</template>
</n-modal>
<n-modal
v-model:show="closeAsk"
preset="dialog"
@@ -1918,6 +1996,8 @@ html { color-scheme: dark; }
.gear-icon { width: 18px; height: 18px; display: block; }
.settings-modal-body { max-height: min(58vh, 520px); overflow: auto; padding-right: 4px; }
.modal-actions { display: flex; align-items: center; gap: 8px; }
.update-copy { margin: 0 0 8px; line-height: 1.5; }
.update-notes { margin: 0 0 8px; white-space: pre-wrap; line-height: 1.5; color: rgba(255, 255, 255, 0.72); }
.modal-spacer { flex: 1; }
.card .n-tabs { margin-bottom: 16px; }
.card .n-form-item { margin-bottom: 16px; }