diff --git a/desktop/src/App.vue b/desktop/src/App.vue index de936ca..a144a54 100644 --- a/desktop/src/App.vue +++ b/desktop/src/App.vue @@ -3,6 +3,7 @@ import { ref } from "vue"; import { darkTheme, NConfigProvider, NDialogProvider, NMessageProvider, zhCN, dateZhCN } from "naive-ui"; import LoginView from "./features/auth/LoginView.vue"; import Shell from "./shell/Shell.vue"; +import CloseGuard from "./shell/CloseGuard.vue"; import type { UserInfo } from "./features/auth/api"; import { isDark, themeOverrides } from "./shared/theme"; import { clearUnauthorized, setUnauthorizedHandler } from "./shared/http"; @@ -28,6 +29,7 @@ function onSuccess(next: UserInfo) {
+
diff --git a/desktop/src/features/auth/LoginView.vue b/desktop/src/features/auth/LoginView.vue index 6460994..f01a721 100644 --- a/desktop/src/features/auth/LoginView.vue +++ b/desktop/src/features/auth/LoginView.vue @@ -2,7 +2,7 @@ import { onMounted, onUnmounted, ref } from "vue"; import { NAlert, NButton, NIcon, NInput, NModal, NSelect, NSpin, NTooltip } from "naive-ui"; 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 { isDark, toggleTheme } from "../../shared/theme"; @@ -12,6 +12,9 @@ const emit = defineEmits<{ success: [user: UserInfo] }>(); const ready = ref(false); const connecting = ref(false); const connectFailed = ref(false); +const backendUnavailable = ref(false); +const allowRestart = ref(false); +const restarting = ref(false); const offline = ref(""); const sessionId = ref(""); const captchaUrl = ref(""); @@ -49,8 +52,11 @@ async function refreshSaved() { } async function connect() { + resetApi(); connecting.value = true; connectFailed.value = false; + backendUnavailable.value = false; + allowRestart.value = false; offline.value = ""; ready.value = false; for (let attempt = 0; attempt < 40; attempt += 1) { @@ -65,6 +71,7 @@ async function connect() { } catch (error) { const failedLocally = !(error instanceof ApiError) || error.status === 0; offline.value = error instanceof ApiError ? error.message : "连接失败"; + backendUnavailable.value = error instanceof ApiError && error.message === "后台服务没有启动"; if (!failedLocally) { ready.value = true; connecting.value = false; @@ -76,7 +83,23 @@ async function connect() { connecting.value = false; connectFailed.value = true; 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) { @@ -219,9 +242,10 @@ onUnmounted(stopCountdown); 连接失败,正在重试。 {{ offline }} diff --git a/desktop/src/shared/http.ts b/desktop/src/shared/http.ts index 5c8d33c..3758159 100644 --- a/desktop/src/shared/http.ts +++ b/desktop/src/shared/http.ts @@ -2,6 +2,7 @@ let apiBase = ""; let apiToken = ""; let preparing: Promise | null = null; let deepScanDone = false; +let restartAvailable = false; interface BackendInfo { 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 { if (!preparing) { preparing = resolveApiBase().catch((error: unknown) => { @@ -68,16 +90,14 @@ async function probeBackend(): Promise { const { invoke } = await import("@tauri-apps/api/core"); try { const info = await invoke("backend_info"); - 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; + restartAvailable = true; + return applyBackend(info); } catch (error) { - if (!commandMissing(error, "backend_info")) return false; + if (!commandMissing(error, "backend_info")) { + restartAvailable = true; + return false; + } + restartAvailable = false; } apiToken = ""; try { @@ -103,7 +123,7 @@ async function resolveApiBase(): Promise { await new Promise((resolve) => window.setTimeout(resolve, 100)); } } - throw new ApiError(0, "连接失败"); + throw new ApiError(0, "后台服务没有启动"); } export async function api(path: string, init?: RequestInit): Promise { @@ -162,3 +182,67 @@ export async function chooseDirectory() { const selected = await open({ directory: true, multiple: false }); 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("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("nqi://close-requested", (event) => { + handler(readActive(event.payload)); + }); + } catch { + return () => undefined; + } +} diff --git a/desktop/src/shell/CloseGuard.vue b/desktop/src/shell/CloseGuard.vue new file mode 100644 index 0000000..0249322 --- /dev/null +++ b/desktop/src/shell/CloseGuard.vue @@ -0,0 +1,92 @@ + + +