- 修复局域网访问

This commit is contained in:
Pine
2026-05-13 02:01:52 +08:00
parent ba737e18bd
commit 9590d5916f
+12 -6
View File
@@ -1,9 +1,15 @@
// API 基地址:Tauri 生产环境使用绝对路径,浏览器/开发环境使用相对路径
// 判断依据:Tauri v2 在 Windows 上使用 https://tauri.localhostprotocol=https:),
// 在 macOS 上使用 tauri://localhostprotocol=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 Taurihostname 含 "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 = {}) {