- 新增 buildFileProxyUrl 函数构建永久代理路径,避免富文本图片链接过期 - 重构 uploadFile 函数,统一将后端返回的数值型 ID 转换为字符串 - 在业务富文本编辑器中使用永久代理路径替换临时签名 URL - 完善 API 适配层 ID 规范,确保所有 ID 字段统一转换为字符串类型 - 移除废弃的编辑器相关路由和组件 - 更新构建代理配置以支持富文本图片直连访问 - 删除冗余的类型定义和依赖包
67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import type { ProxyOptions } from 'vite';
|
||
import { bgRed, bgYellow, green, lightBlue } from 'kolorist';
|
||
import { consola } from 'consola';
|
||
import { WEB_SERVICE_PREFIX } from '../../src/constants/service';
|
||
import { createServiceConfig } from '../../src/utils/service';
|
||
|
||
/**
|
||
* Set http proxy
|
||
*
|
||
* @param env - The current env
|
||
* @param enable - If enable http proxy
|
||
*/
|
||
export function createViteProxy(env: Env.ImportMeta, enable: boolean) {
|
||
const isEnableHttpProxy = enable && env.VITE_HTTP_PROXY === 'Y';
|
||
|
||
if (!isEnableHttpProxy) return undefined;
|
||
|
||
const isEnableProxyLog = env.VITE_PROXY_LOG === 'Y';
|
||
|
||
const { baseURL, proxyPattern, other } = createServiceConfig(env);
|
||
|
||
const proxy: Record<string, ProxyOptions> = createProxyItem({ baseURL, proxyPattern }, isEnableProxyLog);
|
||
|
||
other.forEach(item => {
|
||
Object.assign(proxy, createProxyItem(item, isEnableProxyLog));
|
||
});
|
||
|
||
// 富文本图片 <img src="/admin-api/system/file/{configId}/get/{path}"> 由浏览器直接发起,
|
||
// 不经过 axios,没有 baseURL 前缀。这里加一条原样透传,避免被 Vite SPA fallback 兜底成 index.html。
|
||
// 不带 rewrite —— 原样把 /admin-api/* 转发到后端;不影响现有 /proxy-default 链路。
|
||
proxy[WEB_SERVICE_PREFIX] = {
|
||
target: baseURL,
|
||
changeOrigin: true
|
||
};
|
||
|
||
return proxy;
|
||
}
|
||
|
||
function createProxyItem(item: App.Service.ServiceConfigItem, enableLog: boolean) {
|
||
const proxy: Record<string, ProxyOptions> = {};
|
||
|
||
proxy[item.proxyPattern] = {
|
||
target: item.baseURL,
|
||
changeOrigin: true,
|
||
configure: (_proxy, options) => {
|
||
_proxy.on('proxyReq', (_proxyReq, req, _res) => {
|
||
if (!enableLog) return;
|
||
|
||
const requestUrl = `${lightBlue('[proxy url]')}: ${bgYellow(` ${req.method} `)} ${green(
|
||
`${item.proxyPattern}${req.url}`
|
||
)}`;
|
||
|
||
const proxyUrl = `${lightBlue('[real request url]')}: ${green(`${options.target}${req.url}`)}`;
|
||
|
||
consola.log(`${requestUrl}\n${proxyUrl}`);
|
||
});
|
||
_proxy.on('error', (_err, req, _res) => {
|
||
if (!enableLog) return;
|
||
consola.log(bgRed(`Error: ${req.method} `), green(`${options.target}${req.url}`));
|
||
});
|
||
},
|
||
rewrite: path => path.replace(new RegExp(`^${item.proxyPattern}`), '')
|
||
};
|
||
|
||
return proxy;
|
||
}
|