From 316d817e5cb077d0fc41dfb57bf68b9f52dac244 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Sun, 25 Oct 2026 07:03:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AF=B7=E6=B1=82=E6=90=BA=E5=B8=A6?= =?UTF-8?q?=E4=BB=A4=E7=89=8C=E5=B9=B6=E6=94=B9=E4=B8=BA=E5=8F=AA=E8=AF=BB?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E4=BF=9D=E5=AD=98=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../features/download/GridDownloadView.vue | 16 ++-- desktop/src/shared/http.ts | 73 +++++++++++++++---- desktop/src/styles.css | 18 +++++ 3 files changed, 87 insertions(+), 20 deletions(-) diff --git a/desktop/src/features/download/GridDownloadView.vue b/desktop/src/features/download/GridDownloadView.vue index 4b0bce4..a942200 100644 --- a/desktop/src/features/download/GridDownloadView.vue +++ b/desktop/src/features/download/GridDownloadView.vue @@ -6,13 +6,12 @@ import { NCheckboxGroup, NDatePicker, NIcon, - NInput, NInputNumber, NSelect, NTooltip, useMessage, } from "naive-ui"; -import { FolderOpenOutline, HelpCircleOutline } from "@vicons/ionicons5"; +import { HelpCircleOutline } from "@vicons/ionicons5"; import { ApiError, chooseDirectory } from "../../shared/http"; import { createJob, loadMeta, type Meta } from "./api"; @@ -105,6 +104,10 @@ async function pickDir() { if (selected) outputDir.value = selected; } +function clearOutput() { + outputDir.value = ""; +} + async function start() { if (file.value && !file.value.name.toLowerCase().endsWith(".csv")) { message.warning("区域表只支持 CSV"); @@ -234,10 +237,11 @@ async function start() {
- - - - +
+ {{ outputDir || "默认目录" }} +
+ 选择 + 清除
diff --git a/desktop/src/shared/http.ts b/desktop/src/shared/http.ts index f721bba..5c8d33c 100644 --- a/desktop/src/shared/http.ts +++ b/desktop/src/shared/http.ts @@ -1,7 +1,15 @@ let apiBase = ""; +let apiToken = ""; let preparing: Promise | null = null; let deepScanDone = false; +interface BackendInfo { + port?: number; + token?: string; + alive?: boolean; + error?: string; +} + type UnauthorizedHandler = () => void; let unauthorizedHandler: UnauthorizedHandler | null = null; let unauthorizedNotified = false; @@ -40,21 +48,57 @@ export function initApi(): Promise { return preparing; } -async function resolveApiBase(): Promise { - if (!("__TAURI_INTERNALS__" in window)) return; +function isTauri() { + return "__TAURI_INTERNALS__" in window; +} + +function errorText(error: unknown) { + if (typeof error === "string") return error; + if (error instanceof Error) return error.message; + if (error && typeof error === "object" && "message" in error) return String(error.message); + return String(error); +} + +function commandMissing(error: unknown, name: string) { + const text = errorText(error).toLowerCase(); + return text.includes("not found") && text.includes(name.toLowerCase()); +} + +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; + } catch (error) { + if (!commandMissing(error, "backend_info")) return false; + } + apiToken = ""; + try { + const port = await invoke("backend_port"); + if (port > 0) { + apiBase = `http://127.0.0.1:${port}`; + return true; + } + } catch { + // 窗口可能比端口命令更早就绪。 + } + apiBase = ""; + return false; +} + +async function resolveApiBase(): Promise { + if (!isTauri()) return; const attempts = deepScanDone ? 1 : 50; deepScanDone = true; for (let attempt = 0; attempt < attempts; attempt += 1) { - try { - const port = await invoke("backend_port"); - if (port > 0) { - apiBase = `http://127.0.0.1:${port}`; - return; - } - } catch { - // 窗口可能比端口命令更早就绪。 - } + if (await probeBackend()) return; if (attempt + 1 < attempts) { await new Promise((resolve) => window.setTimeout(resolve, 100)); } @@ -70,6 +114,7 @@ export async function api(path: string, init?: RequestInit): Promise { if (init?.body && !(init.body instanceof FormData) && !headers.has("Content-Type")) { headers.set("Content-Type", "application/json"); } + if (apiToken) headers.set("X-Nqi-Token", apiToken); response = await fetch(`${apiBase}${path}`, { ...init, headers }); } catch { throw new ApiError(0, "连接失败"); @@ -98,21 +143,21 @@ export function health() { } export async function openLocal(path: string) { - if (!("__TAURI_INTERNALS__" in window)) return false; + if (!isTauri()) return false; const { openPath } = await import("@tauri-apps/plugin-opener"); await openPath(path); return true; } export async function revealLocal(path: string) { - if (!("__TAURI_INTERNALS__" in window)) return false; + if (!isTauri()) return false; const { revealItemInDir } = await import("@tauri-apps/plugin-opener"); await revealItemInDir(path); return true; } export async function chooseDirectory() { - if (!("__TAURI_INTERNALS__" in window)) return null; + if (!isTauri()) return null; const { open } = await import("@tauri-apps/plugin-dialog"); const selected = await open({ directory: true, multiple: false }); return typeof selected === "string" ? selected : null; diff --git a/desktop/src/styles.css b/desktop/src/styles.css index b19a2ad..6a44ea3 100644 --- a/desktop/src/styles.css +++ b/desktop/src/styles.css @@ -399,6 +399,24 @@ select { word-break: break-all; } +.path-text { + flex: 1; + min-width: 0; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + border: 1px solid var(--line); + border-radius: 6px; + background: var(--bg); + padding: 6px 10px; + min-height: 34px; + line-height: 22px; +} + +.path-text.placeholder { + color: var(--muted); +} + .task.focus { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent);