fix: 请求携带令牌并改为只读选择保存位置
This commit is contained in:
@@ -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() {
|
||||
<div class="form-row">
|
||||
<label>保存到</label>
|
||||
<div class="captcha-line">
|
||||
<n-input v-model:value="outputDir" placeholder="默认目录" />
|
||||
<n-button @click="pickDir">
|
||||
<template #icon><n-icon><FolderOpenOutline /></n-icon></template>
|
||||
</n-button>
|
||||
<div class="path-text" :class="{ placeholder: !outputDir }" :title="outputDir || '默认目录'">
|
||||
{{ outputDir || "默认目录" }}
|
||||
</div>
|
||||
<n-button @click="pickDir">选择</n-button>
|
||||
<n-button :disabled="!outputDir" @click="clearOutput">清除</n-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+59
-14
@@ -1,7 +1,15 @@
|
||||
let apiBase = "";
|
||||
let apiToken = "";
|
||||
let preparing: Promise<void> | 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<void> {
|
||||
return preparing;
|
||||
}
|
||||
|
||||
async function resolveApiBase(): Promise<void> {
|
||||
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<boolean> {
|
||||
const { invoke } = await import("@tauri-apps/api/core");
|
||||
try {
|
||||
const info = await invoke<BackendInfo>("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<number>("backend_port");
|
||||
if (port > 0) {
|
||||
apiBase = `http://127.0.0.1:${port}`;
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
// 窗口可能比端口命令更早就绪。
|
||||
}
|
||||
apiBase = "";
|
||||
return false;
|
||||
}
|
||||
|
||||
async function resolveApiBase(): Promise<void> {
|
||||
if (!isTauri()) return;
|
||||
const attempts = deepScanDone ? 1 : 50;
|
||||
deepScanDone = true;
|
||||
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
||||
try {
|
||||
const port = await invoke<number>("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<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user