From c2a1f16915f55ddc6e8abd0e40ebe5da51b6f0b0 Mon Sep 17 00:00:00 2001 From: lijing Date: Tue, 20 Oct 2026 14:59:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(theme):=20appearance=20=E8=B5=B0=E5=90=8C?= =?UTF-8?q?=E6=BA=90=20ctx=20=E5=89=8D=E7=BC=80=EF=BC=8C=E4=B8=8D=E8=A2=AB?= =?UTF-8?q?=E8=B7=A8=E6=BA=90=E8=B7=B3=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主题通道 yh-theme-init.js 的 appearance 有硬性同源限制 (appearanceSameOrigin 不通过直接 return,连请求都不发)。 - host.config.ts:仅当最终 serviceUrl 与当前页面同源时,才回写 __APP_CONFIG__.serviceHost;dev 的绝对地址(跨源)不回写,保持 '/api/appearance' 同源相对,交由 vite 代理转发,避免被整段跳过。 - prod:serviceUrl = 同域 ctx(https://base.../userclass),回写后 appearance 命中 https://base.../userclass/api/appearance。 - vite.config.ts:/api 代理支持 YH_API_PROXY_TARGET 指向真实后端 (target 带 /userclass 前缀即可,无需 rewrite)。 --- src/assets/config/host.config.ts | 26 ++++++++++++++++++++++++++ vite.config.ts | 3 +++ 2 files changed, 29 insertions(+) diff --git a/src/assets/config/host.config.ts b/src/assets/config/host.config.ts index 7d0afd3..df07b39 100644 --- a/src/assets/config/host.config.ts +++ b/src/assets/config/host.config.ts @@ -69,4 +69,30 @@ try { /* 无运行时配置时沿用兜底 */ } +/** + * 回写 __APP_CONFIG__.serviceHost(仅同源场景)。 + * 主题通道 yh-theme-init.js 的 appearance 请求有硬性同源限制: + * if (!url || !appearanceSameOrigin(url)) return; → 跨源会被直接跳过、连请求都不发。 + * 本地 dev:serviceUrl 是 VITE_SERVICE_HOST 的绝对地址(跨源),此时不能回写, + * 保持空串让 init 走同源 "/api/appearance",由 vite 代理转发到后端。 + * 生产:serviceUrl = 同域 ctx(https://base.../userclass),同源,回写后 init 请求正确前缀。 + * 已有 env-config 下发值时不动(以平台配置为准)。 + */ +try { + const w = window as any; + w.__APP_CONFIG__ = w.__APP_CONFIG__ || {}; + if (!w.__APP_CONFIG__.serviceHost) { + const candidate = conf.serviceUrl || conf.serviceHost || ""; + let sameOrigin = !candidate; + try { + sameOrigin = new URL(candidate, window.location.href).origin === window.location.origin; + } catch { + sameOrigin = false; + } + if (sameOrigin) w.__APP_CONFIG__.serviceHost = candidate; + } +} catch { + /* 极端环境无 window:忽略,保持同源兜底 */ +} + export default conf; diff --git a/vite.config.ts b/vite.config.ts index 9047815..761e89f 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -43,6 +43,9 @@ export default defineConfig(({ mode }) => { open: false, proxy: { // 本地后端:serviceUrl 在 dev 下为空 → /api/* 同源相对,由本代理转发 + // 想直接连真实后端(含主题通道「同源」的 appearance): + // YH_API_PROXY_TARGET=http://base.yunhaixingtong.com/userclass npm run dev + // 注意 target 要带上下文前缀 /userclass,http-proxy 会把它拼在 /api 前 "/api": { target: process.env.YH_API_PROXY_TARGET || "http://localhost:8101", changeOrigin: true,