fix: 关闭窗口前确认并允许重新启动后台
This commit is contained in:
@@ -3,6 +3,7 @@ import { ref } from "vue";
|
|||||||
import { darkTheme, NConfigProvider, NDialogProvider, NMessageProvider, zhCN, dateZhCN } from "naive-ui";
|
import { darkTheme, NConfigProvider, NDialogProvider, NMessageProvider, zhCN, dateZhCN } from "naive-ui";
|
||||||
import LoginView from "./features/auth/LoginView.vue";
|
import LoginView from "./features/auth/LoginView.vue";
|
||||||
import Shell from "./shell/Shell.vue";
|
import Shell from "./shell/Shell.vue";
|
||||||
|
import CloseGuard from "./shell/CloseGuard.vue";
|
||||||
import type { UserInfo } from "./features/auth/api";
|
import type { UserInfo } from "./features/auth/api";
|
||||||
import { isDark, themeOverrides } from "./shared/theme";
|
import { isDark, themeOverrides } from "./shared/theme";
|
||||||
import { clearUnauthorized, setUnauthorizedHandler } from "./shared/http";
|
import { clearUnauthorized, setUnauthorizedHandler } from "./shared/http";
|
||||||
@@ -28,6 +29,7 @@ function onSuccess(next: UserInfo) {
|
|||||||
<n-message-provider class="app-root">
|
<n-message-provider class="app-root">
|
||||||
<n-dialog-provider>
|
<n-dialog-provider>
|
||||||
<div class="app-root">
|
<div class="app-root">
|
||||||
|
<CloseGuard :enabled="!!user" />
|
||||||
<LoginView v-if="!user" :notice="authNotice" @success="onSuccess" />
|
<LoginView v-if="!user" :notice="authNotice" @success="onSuccess" />
|
||||||
<Shell v-else :user="user" @logout="user = null" />
|
<Shell v-else :user="user" @logout="user = null" />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { onMounted, onUnmounted, ref } from "vue";
|
import { onMounted, onUnmounted, ref } from "vue";
|
||||||
import { NAlert, NButton, NIcon, NInput, NModal, NSelect, NSpin, NTooltip } from "naive-ui";
|
import { NAlert, NButton, NIcon, NInput, NModal, NSelect, NSpin, NTooltip } from "naive-ui";
|
||||||
import { HelpCircleOutline, MoonOutline, SunnyOutline } from "@vicons/ionicons5";
|
import { HelpCircleOutline, MoonOutline, SunnyOutline } from "@vicons/ionicons5";
|
||||||
import { ApiError } from "../../shared/http";
|
import { ApiError, canRestartBackend, resetApi, restartBackend } from "../../shared/http";
|
||||||
import { loadCaptcha, loadSaved, login, refreshCaptcha, resumeSaved, sendSms, type UserInfo } from "./api";
|
import { loadCaptcha, loadSaved, login, refreshCaptcha, resumeSaved, sendSms, type UserInfo } from "./api";
|
||||||
import { isDark, toggleTheme } from "../../shared/theme";
|
import { isDark, toggleTheme } from "../../shared/theme";
|
||||||
|
|
||||||
@@ -12,6 +12,9 @@ const emit = defineEmits<{ success: [user: UserInfo] }>();
|
|||||||
const ready = ref(false);
|
const ready = ref(false);
|
||||||
const connecting = ref(false);
|
const connecting = ref(false);
|
||||||
const connectFailed = ref(false);
|
const connectFailed = ref(false);
|
||||||
|
const backendUnavailable = ref(false);
|
||||||
|
const allowRestart = ref(false);
|
||||||
|
const restarting = ref(false);
|
||||||
const offline = ref("");
|
const offline = ref("");
|
||||||
const sessionId = ref("");
|
const sessionId = ref("");
|
||||||
const captchaUrl = ref("");
|
const captchaUrl = ref("");
|
||||||
@@ -49,8 +52,11 @@ async function refreshSaved() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function connect() {
|
async function connect() {
|
||||||
|
resetApi();
|
||||||
connecting.value = true;
|
connecting.value = true;
|
||||||
connectFailed.value = false;
|
connectFailed.value = false;
|
||||||
|
backendUnavailable.value = false;
|
||||||
|
allowRestart.value = false;
|
||||||
offline.value = "";
|
offline.value = "";
|
||||||
ready.value = false;
|
ready.value = false;
|
||||||
for (let attempt = 0; attempt < 40; attempt += 1) {
|
for (let attempt = 0; attempt < 40; attempt += 1) {
|
||||||
@@ -65,6 +71,7 @@ async function connect() {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
const failedLocally = !(error instanceof ApiError) || error.status === 0;
|
const failedLocally = !(error instanceof ApiError) || error.status === 0;
|
||||||
offline.value = error instanceof ApiError ? error.message : "连接失败";
|
offline.value = error instanceof ApiError ? error.message : "连接失败";
|
||||||
|
backendUnavailable.value = error instanceof ApiError && error.message === "后台服务没有启动";
|
||||||
if (!failedLocally) {
|
if (!failedLocally) {
|
||||||
ready.value = true;
|
ready.value = true;
|
||||||
connecting.value = false;
|
connecting.value = false;
|
||||||
@@ -76,7 +83,23 @@ async function connect() {
|
|||||||
connecting.value = false;
|
connecting.value = false;
|
||||||
connectFailed.value = true;
|
connectFailed.value = true;
|
||||||
ready.value = false;
|
ready.value = false;
|
||||||
offline.value = "连接失败";
|
offline.value = backendUnavailable.value ? "后台服务没有启动" : "连接失败";
|
||||||
|
allowRestart.value = backendUnavailable.value && canRestartBackend();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function onRestart() {
|
||||||
|
if (restarting.value) return;
|
||||||
|
restarting.value = true;
|
||||||
|
try {
|
||||||
|
const result = await restartBackend();
|
||||||
|
if (result === "missing") {
|
||||||
|
allowRestart.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await connect();
|
||||||
|
} finally {
|
||||||
|
restarting.value = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function reloadCaptcha(keepError = false) {
|
async function reloadCaptcha(keepError = false) {
|
||||||
@@ -219,9 +242,10 @@ onUnmounted(stopCountdown);
|
|||||||
<n-spin :show="connecting && !offline">
|
<n-spin :show="connecting && !offline">
|
||||||
<n-alert v-if="connecting && offline" type="warning" style="margin-bottom: 12px">连接失败,正在重试。</n-alert>
|
<n-alert v-if="connecting && offline" type="warning" style="margin-bottom: 12px">连接失败,正在重试。</n-alert>
|
||||||
<template v-else-if="connectFailed">
|
<template v-else-if="connectFailed">
|
||||||
<n-alert type="warning" style="margin-bottom: 12px">连接失败</n-alert>
|
<n-alert type="warning" style="margin-bottom: 12px">{{ backendUnavailable ? "后台服务没有启动" : "连接失败" }}</n-alert>
|
||||||
<div class="retry-row">
|
<div class="retry-row">
|
||||||
<n-button type="primary" secondary @click="connect">重试</n-button>
|
<n-button v-if="allowRestart" :loading="restarting" @click="onRestart">重新启动</n-button>
|
||||||
|
<n-button type="primary" secondary :disabled="restarting" @click="connect">重试</n-button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<n-alert v-else-if="offline" type="warning" style="margin-bottom: 12px">{{ offline }}</n-alert>
|
<n-alert v-else-if="offline" type="warning" style="margin-bottom: 12px">{{ offline }}</n-alert>
|
||||||
|
|||||||
+94
-10
@@ -2,6 +2,7 @@ let apiBase = "";
|
|||||||
let apiToken = "";
|
let apiToken = "";
|
||||||
let preparing: Promise<void> | null = null;
|
let preparing: Promise<void> | null = null;
|
||||||
let deepScanDone = false;
|
let deepScanDone = false;
|
||||||
|
let restartAvailable = false;
|
||||||
|
|
||||||
interface BackendInfo {
|
interface BackendInfo {
|
||||||
port?: number;
|
port?: number;
|
||||||
@@ -38,6 +39,27 @@ export class ApiError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resetApi() {
|
||||||
|
preparing = null;
|
||||||
|
deepScanDone = false;
|
||||||
|
apiBase = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function canRestartBackend() {
|
||||||
|
return restartAvailable;
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyBackend(info: BackendInfo | null | undefined) {
|
||||||
|
const port = Number(info?.port || 0);
|
||||||
|
apiToken = typeof info?.token === "string" ? info.token : "";
|
||||||
|
if (port > 0 && info?.alive !== false) {
|
||||||
|
apiBase = `http://127.0.0.1:${port}`;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
apiBase = "";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
export function initApi(): Promise<void> {
|
export function initApi(): Promise<void> {
|
||||||
if (!preparing) {
|
if (!preparing) {
|
||||||
preparing = resolveApiBase().catch((error: unknown) => {
|
preparing = resolveApiBase().catch((error: unknown) => {
|
||||||
@@ -68,16 +90,14 @@ async function probeBackend(): Promise<boolean> {
|
|||||||
const { invoke } = await import("@tauri-apps/api/core");
|
const { invoke } = await import("@tauri-apps/api/core");
|
||||||
try {
|
try {
|
||||||
const info = await invoke<BackendInfo>("backend_info");
|
const info = await invoke<BackendInfo>("backend_info");
|
||||||
const port = Number(info?.port || 0);
|
restartAvailable = true;
|
||||||
apiToken = typeof info?.token === "string" ? info.token : "";
|
return applyBackend(info);
|
||||||
if (port > 0 && info?.alive !== false) {
|
|
||||||
apiBase = `http://127.0.0.1:${port}`;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
apiBase = "";
|
|
||||||
return false;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!commandMissing(error, "backend_info")) return false;
|
if (!commandMissing(error, "backend_info")) {
|
||||||
|
restartAvailable = true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
restartAvailable = false;
|
||||||
}
|
}
|
||||||
apiToken = "";
|
apiToken = "";
|
||||||
try {
|
try {
|
||||||
@@ -103,7 +123,7 @@ async function resolveApiBase(): Promise<void> {
|
|||||||
await new Promise((resolve) => window.setTimeout(resolve, 100));
|
await new Promise((resolve) => window.setTimeout(resolve, 100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new ApiError(0, "连接失败");
|
throw new ApiError(0, "后台服务没有启动");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
||||||
@@ -162,3 +182,67 @@ export async function chooseDirectory() {
|
|||||||
const selected = await open({ directory: true, multiple: false });
|
const selected = await open({ directory: true, multiple: false });
|
||||||
return typeof selected === "string" ? selected : null;
|
return typeof selected === "string" ? selected : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function restartBackend() {
|
||||||
|
if (!isTauri()) return "missing" as const;
|
||||||
|
try {
|
||||||
|
const { invoke } = await import("@tauri-apps/api/core");
|
||||||
|
const info = await invoke<BackendInfo>("restart_backend");
|
||||||
|
restartAvailable = true;
|
||||||
|
if (applyBackend(info)) {
|
||||||
|
preparing = Promise.resolve();
|
||||||
|
deepScanDone = true;
|
||||||
|
return "ok" as const;
|
||||||
|
}
|
||||||
|
preparing = null;
|
||||||
|
return "failed" as const;
|
||||||
|
} catch (error) {
|
||||||
|
if (commandMissing(error, "restart_backend")) {
|
||||||
|
restartAvailable = false;
|
||||||
|
return "missing" as const;
|
||||||
|
}
|
||||||
|
preparing = null;
|
||||||
|
return "failed" as const;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setCloseGuard(active: number) {
|
||||||
|
if (!isTauri()) return;
|
||||||
|
try {
|
||||||
|
const { invoke } = await import("@tauri-apps/api/core");
|
||||||
|
await invoke("set_close_guard", { active });
|
||||||
|
} catch {
|
||||||
|
// 桌面壳还没有关窗保护时不提示。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function closeApp() {
|
||||||
|
if (!isTauri()) return;
|
||||||
|
try {
|
||||||
|
const { invoke } = await import("@tauri-apps/api/core");
|
||||||
|
await invoke("close_app");
|
||||||
|
} catch {
|
||||||
|
// 命令不存在时不提示。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function readActive(payload: unknown) {
|
||||||
|
if (typeof payload === "number" && Number.isFinite(payload)) return payload;
|
||||||
|
if (payload && typeof payload === "object" && "active" in payload) {
|
||||||
|
const value = Number((payload as { active?: unknown }).active ?? 0);
|
||||||
|
return Number.isFinite(value) ? value : 0;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listenCloseRequested(handler: (active: number) => void) {
|
||||||
|
if (!isTauri()) return () => undefined;
|
||||||
|
try {
|
||||||
|
const { listen } = await import("@tauri-apps/api/event");
|
||||||
|
return await listen<unknown>("nqi://close-requested", (event) => {
|
||||||
|
handler(readActive(event.payload));
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
return () => undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { onMounted, onUnmounted, watch } from "vue";
|
||||||
|
import { useDialog } from "naive-ui";
|
||||||
|
import { loadActiveDownloadCount } from "../features/download/api";
|
||||||
|
import { closeApp, listenCloseRequested, setCloseGuard } from "../shared/http";
|
||||||
|
|
||||||
|
const props = defineProps<{ enabled: boolean }>();
|
||||||
|
const dialog = useDialog();
|
||||||
|
let timer = 0;
|
||||||
|
let stopped = false;
|
||||||
|
let asking = false;
|
||||||
|
let unlisten = () => {};
|
||||||
|
|
||||||
|
async function syncGuard() {
|
||||||
|
if (!props.enabled) {
|
||||||
|
await setCloseGuard(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const count = await loadActiveDownloadCount();
|
||||||
|
await setCloseGuard(count);
|
||||||
|
} catch {
|
||||||
|
// 登录失效或后台未就绪时不提示。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function schedule() {
|
||||||
|
if (stopped) return;
|
||||||
|
timer = window.setTimeout(() => {
|
||||||
|
void syncGuard().finally(schedule);
|
||||||
|
}, 2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
function askClose(active: number) {
|
||||||
|
if (asking) return;
|
||||||
|
if (active <= 0) {
|
||||||
|
void closeApp();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
asking = true;
|
||||||
|
dialog.warning({
|
||||||
|
title: "关闭程序",
|
||||||
|
content: `还有 ${active} 个任务在下载,关闭后会暂停,下次打开点继续`,
|
||||||
|
positiveText: "关闭",
|
||||||
|
negativeText: "取消",
|
||||||
|
maskClosable: false,
|
||||||
|
onPositiveClick: () => {
|
||||||
|
asking = false;
|
||||||
|
void closeApp();
|
||||||
|
},
|
||||||
|
onNegativeClick: () => {
|
||||||
|
asking = false;
|
||||||
|
},
|
||||||
|
onClose: () => {
|
||||||
|
asking = false;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
stopped = false;
|
||||||
|
void listenCloseRequested((active) => {
|
||||||
|
if (stopped) return;
|
||||||
|
askClose(active);
|
||||||
|
}).then((stop) => {
|
||||||
|
if (stopped) {
|
||||||
|
stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unlisten = stop;
|
||||||
|
});
|
||||||
|
void syncGuard();
|
||||||
|
schedule();
|
||||||
|
});
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
stopped = true;
|
||||||
|
window.clearTimeout(timer);
|
||||||
|
unlisten();
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => props.enabled,
|
||||||
|
() => {
|
||||||
|
void syncGuard();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<span hidden></span>
|
||||||
|
</template>
|
||||||
Reference in New Issue
Block a user