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

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
fucheng
2026-09-20 19:29:50 +08:00
parent 4a86114da6
commit 6b3dea2f05
+22 -4
View File
@@ -13,13 +13,31 @@ import {
} from "./luxsinApi";
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 {
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;
if (fromEnv) return fromEnv.replace(/\/$/, "");
return "https://v2ai.luxsin.com.cn";
if (fromEnv) return withPageProtocol(fromEnv);
return `${currentPageProtocol()}//${AI_HOST}`;
}
// ============================================================