fix(request): clazz API 使用 KC_SERVER/UC_SERVER 绝对 URL,避免相对路径落到错误 vhost 返回 HTML

Made-with: Cursor
This commit is contained in:
2026-04-28 00:38:09 +08:00
parent fb7d15e6a1
commit 27ce76bdfd
+15 -2
View File
@@ -69,8 +69,12 @@ axiosInstance.interceptors.request.use((options) => {
// @ts-ignore
url = import.meta.env.DEV ? cozePath : (AI_SERVER + cozePath);
} else if (url?.startsWith('/api/v1/clazz')) {
// @ts-ignore
url = KC_SERVER + url;
// 必须用绝对 URL:否则在 wx/static 等域打开 H5 时 axios 会把 /api/v1/clazz 发到当前域名,落到 SPA index.html(字符串),触发排课 normalize 报错
// @ts-ignore vite define
const kcBase = KC_SERVER || UC_SERVER;
if (kcBase) {
url = new URL(url, kcBase.endsWith('/') ? kcBase : `${kcBase}/`).href;
}
}
return {...rest, url, headers: headersObj};
@@ -148,6 +152,15 @@ export default function request({url = '', method = 'get', data, params, ...rest
resolved.e = getHtyErrUserMessage(res.hty_err, resolved.e ?? baseErrText);
}
// 网关未配置 /api/v1/clazz/ 时 SPA 返回 HTMLaxios 会得到 string,原先会当作 d 传入业务层触发「排课数据格式异常」
const rawBody = resolved.d;
if (typeof rawBody === 'string' && /^\s*</.test(rawBody)) {
resolved.r = false;
resolved.d = undefined;
resolved.e =
'接口返回了页面而非 JSON(请确认 Nginx 已将 /api/v1/clazz/ 反代到 htykc';
}
return Promise.resolve(resolved);
})
};