fix: 请求携带令牌并改为只读选择保存位置
This commit is contained in:
@@ -6,13 +6,12 @@ import {
|
|||||||
NCheckboxGroup,
|
NCheckboxGroup,
|
||||||
NDatePicker,
|
NDatePicker,
|
||||||
NIcon,
|
NIcon,
|
||||||
NInput,
|
|
||||||
NInputNumber,
|
NInputNumber,
|
||||||
NSelect,
|
NSelect,
|
||||||
NTooltip,
|
NTooltip,
|
||||||
useMessage,
|
useMessage,
|
||||||
} from "naive-ui";
|
} from "naive-ui";
|
||||||
import { FolderOpenOutline, HelpCircleOutline } from "@vicons/ionicons5";
|
import { HelpCircleOutline } from "@vicons/ionicons5";
|
||||||
import { ApiError, chooseDirectory } from "../../shared/http";
|
import { ApiError, chooseDirectory } from "../../shared/http";
|
||||||
import { createJob, loadMeta, type Meta } from "./api";
|
import { createJob, loadMeta, type Meta } from "./api";
|
||||||
|
|
||||||
@@ -105,6 +104,10 @@ async function pickDir() {
|
|||||||
if (selected) outputDir.value = selected;
|
if (selected) outputDir.value = selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clearOutput() {
|
||||||
|
outputDir.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
async function start() {
|
async function start() {
|
||||||
if (file.value && !file.value.name.toLowerCase().endsWith(".csv")) {
|
if (file.value && !file.value.name.toLowerCase().endsWith(".csv")) {
|
||||||
message.warning("区域表只支持 CSV");
|
message.warning("区域表只支持 CSV");
|
||||||
@@ -234,10 +237,11 @@ async function start() {
|
|||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label>保存到</label>
|
<label>保存到</label>
|
||||||
<div class="captcha-line">
|
<div class="captcha-line">
|
||||||
<n-input v-model:value="outputDir" placeholder="默认目录" />
|
<div class="path-text" :class="{ placeholder: !outputDir }" :title="outputDir || '默认目录'">
|
||||||
<n-button @click="pickDir">
|
{{ outputDir || "默认目录" }}
|
||||||
<template #icon><n-icon><FolderOpenOutline /></n-icon></template>
|
</div>
|
||||||
</n-button>
|
<n-button @click="pickDir">选择</n-button>
|
||||||
|
<n-button :disabled="!outputDir" @click="clearOutput">清除</n-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+59
-14
@@ -1,7 +1,15 @@
|
|||||||
let apiBase = "";
|
let apiBase = "";
|
||||||
|
let apiToken = "";
|
||||||
let preparing: Promise<void> | null = null;
|
let preparing: Promise<void> | null = null;
|
||||||
let deepScanDone = false;
|
let deepScanDone = false;
|
||||||
|
|
||||||
|
interface BackendInfo {
|
||||||
|
port?: number;
|
||||||
|
token?: string;
|
||||||
|
alive?: boolean;
|
||||||
|
error?: string;
|
||||||
|
}
|
||||||
|
|
||||||
type UnauthorizedHandler = () => void;
|
type UnauthorizedHandler = () => void;
|
||||||
let unauthorizedHandler: UnauthorizedHandler | null = null;
|
let unauthorizedHandler: UnauthorizedHandler | null = null;
|
||||||
let unauthorizedNotified = false;
|
let unauthorizedNotified = false;
|
||||||
@@ -40,21 +48,57 @@ export function initApi(): Promise<void> {
|
|||||||
return preparing;
|
return preparing;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolveApiBase(): Promise<void> {
|
function isTauri() {
|
||||||
if (!("__TAURI_INTERNALS__" in window)) return;
|
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");
|
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;
|
const attempts = deepScanDone ? 1 : 50;
|
||||||
deepScanDone = true;
|
deepScanDone = true;
|
||||||
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
||||||
try {
|
if (await probeBackend()) return;
|
||||||
const port = await invoke<number>("backend_port");
|
|
||||||
if (port > 0) {
|
|
||||||
apiBase = `http://127.0.0.1:${port}`;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// 窗口可能比端口命令更早就绪。
|
|
||||||
}
|
|
||||||
if (attempt + 1 < attempts) {
|
if (attempt + 1 < attempts) {
|
||||||
await new Promise((resolve) => window.setTimeout(resolve, 100));
|
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")) {
|
if (init?.body && !(init.body instanceof FormData) && !headers.has("Content-Type")) {
|
||||||
headers.set("Content-Type", "application/json");
|
headers.set("Content-Type", "application/json");
|
||||||
}
|
}
|
||||||
|
if (apiToken) headers.set("X-Nqi-Token", apiToken);
|
||||||
response = await fetch(`${apiBase}${path}`, { ...init, headers });
|
response = await fetch(`${apiBase}${path}`, { ...init, headers });
|
||||||
} catch {
|
} catch {
|
||||||
throw new ApiError(0, "连接失败");
|
throw new ApiError(0, "连接失败");
|
||||||
@@ -98,21 +143,21 @@ export function health() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function openLocal(path: string) {
|
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");
|
const { openPath } = await import("@tauri-apps/plugin-opener");
|
||||||
await openPath(path);
|
await openPath(path);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function revealLocal(path: string) {
|
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");
|
const { revealItemInDir } = await import("@tauri-apps/plugin-opener");
|
||||||
await revealItemInDir(path);
|
await revealItemInDir(path);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function chooseDirectory() {
|
export async function chooseDirectory() {
|
||||||
if (!("__TAURI_INTERNALS__" in window)) return null;
|
if (!isTauri()) return null;
|
||||||
const { open } = await import("@tauri-apps/plugin-dialog");
|
const { open } = await import("@tauri-apps/plugin-dialog");
|
||||||
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;
|
||||||
|
|||||||
@@ -399,6 +399,24 @@ select {
|
|||||||
word-break: break-all;
|
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 {
|
.task.focus {
|
||||||
border-color: var(--accent);
|
border-color: var(--accent);
|
||||||
box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent);
|
box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent);
|
||||||
|
|||||||
Reference in New Issue
Block a user