From 9590d5916fd8e8275f0f9c0c3a1b9663a42508c7 Mon Sep 17 00:00:00 2001 From: Pine Date: Wed, 13 May 2026 02:01:52 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=E5=B1=80=E5=9F=9F?= =?UTF-8?q?=E7=BD=91=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/api.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/utils/api.js b/src/utils/api.js index cb8368b..38f09b6 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -1,9 +1,15 @@ -// API 基地址:Tauri 生产环境使用绝对路径,浏览器/开发环境使用相对路径 -// 判断依据:Tauri v2 在 Windows 上使用 https://tauri.localhost(protocol=https:), -// 在 macOS 上使用 tauri://localhost(protocol=tauri:),因此用 hostname 而非 protocol 判断 -const BASE = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1' - ? '' // 同源请求(开发模式 localhost:1420 或浏览器) - : 'http://localhost:10801'; // Tauri 生产环境 (tauri://localhost / https://tauri.localhost) +// API 基地址 +// 同源请求(hostname=localhost/127.0.0.1):`''` 相对路径,开发模式或 macOS Tauri +// Windows Tauri(hostname 含 "tauri"):后端在本机,固定 http://localhost:10801 +// 局域网访问(hostname 为 LAN IP):后端在同一台机器的 10801 端口 +const hostname = window.location.hostname; +const isLocal = hostname === 'localhost' || hostname === '127.0.0.1'; +const isTauri = hostname.includes('tauri'); +const BASE = isLocal + ? '' + : isTauri + ? 'http://localhost:10801' + : `http://${hostname}:10801`; export const API_BASE = BASE; async function request(url, options = {}) {