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 = {}) {