fix(ai): AI 接口协议跟随当前页面,不再写死 https

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
fucheng
2026-09-20 19:29:53 +08:00
parent f4c9579e4f
commit 053c0e3cd8
+22 -4
View File
@@ -13,13 +13,31 @@ import {
} from "./luxsinApi"; } from "./luxsinApi";
import { AI_NETWORK_ERROR_CODE, isLikelyNetworkFetchError } from "./networkError"; import { AI_NETWORK_ERROR_CODE, isLikelyNetworkFetchError } from "./networkError";
// Allow override via Vite env / localStorage; default to same origin in prod. const AI_HOST = "v2ai.luxsin.com.cn";
/** Match the page protocol so http://device-ip does not call https://v2ai (and vice versa). */
function currentPageProtocol(): "http:" | "https:" {
if (typeof window !== "undefined" && window.location.protocol === "http:") return "http:";
return "https:";
}
function withPageProtocol(url: string): string {
const trimmed = url.replace(/\/$/, "");
const protocol = currentPageProtocol();
if (/^https?:\/\//i.test(trimmed)) {
return trimmed.replace(/^https?:\/\//i, `${protocol}//`);
}
if (trimmed.startsWith("//")) return `${protocol}${trimmed}`;
return trimmed;
}
// Allow override via Vite env / localStorage; protocol follows the current page.
export function getAIBaseUrl(): string { export function getAIBaseUrl(): string {
const fromLs = typeof window !== "undefined" ? window.localStorage.getItem("luxsin_ai_url") : null; const fromLs = typeof window !== "undefined" ? window.localStorage.getItem("luxsin_ai_url") : null;
if (fromLs) return fromLs.replace(/\/$/, ""); if (fromLs) return withPageProtocol(fromLs);
const fromEnv = (import.meta as any).env?.VITE_AI_API_URL as string | undefined; const fromEnv = (import.meta as any).env?.VITE_AI_API_URL as string | undefined;
if (fromEnv) return fromEnv.replace(/\/$/, ""); if (fromEnv) return withPageProtocol(fromEnv);
return "https://v2ai.luxsin.com.cn"; return `${currentPageProtocol()}//${AI_HOST}`;
} }
// ============================================================ // ============================================================