feat(electron): adapt renderer API and WebSocket layer for dynamic backend URL

This commit is contained in:
DrSmoothl
2026-03-03 00:54:14 +08:00
parent b5cc361ce9
commit fc394f4412
6 changed files with 55 additions and 27 deletions

View File

@@ -1,5 +1,20 @@
import { getApiBaseUrl } from './api-base'
import { isElectron } from './runtime'
// 带自动认证处理的 fetch 封装
/**
* 将相对路径在 Electron 端转换为绝对路径
* 浏览器端直接返回原始 input行为不变
*/
async function resolveUrl(input: RequestInfo | URL): Promise<RequestInfo | URL> {
if (isElectron() && typeof input === 'string' && input.startsWith('/')) {
const base = await getApiBaseUrl()
return base ? `${base}${input}` : input
}
return input
}
/**
* 增强的 fetch 函数,自动处理 401 错误并跳转到登录页
* 使用 HttpOnly Cookie 进行认证,自动携带 credentials
@@ -25,7 +40,7 @@ export async function fetchWithAuth(
headers,
}
const response = await fetch(input, config)
const response = await fetch(await resolveUrl(input), config)
// 检测 401 未授权错误
if (response.status === 401) {
@@ -54,7 +69,7 @@ export function getAuthHeaders(): HeadersInit {
*/
export async function logout(): Promise<void> {
try {
await fetch('/api/webui/auth/logout', {
await fetch(await resolveUrl('/api/webui/auth/logout'), {
method: 'POST',
credentials: 'include',
})
@@ -70,7 +85,7 @@ export async function logout(): Promise<void> {
*/
export async function checkAuthStatus(): Promise<boolean> {
try {
const response = await fetch('/api/webui/auth/check', {
const response = await fetch(await resolveUrl('/api/webui/auth/check'), {
method: 'GET',
credentials: 'include',
})