更新开发环境和生产环境的API服务地址,统一后端服务配置,移除不再使用的服务实例,优化请求模块以使用统一的请求方法,调整相关接口以提高代码一致性和可读性。

This commit is contained in:
2026-04-01 15:45:39 +08:00
parent d9b4a012ee
commit e6d75c6660
21 changed files with 200 additions and 206 deletions

View File

@@ -14,12 +14,12 @@ const ERROR_MESSAGE_INTERVAL = 2000; // 2秒内只显示一个错误
const showErrorMessage = (message: string) => {
const now = Date.now();
// 2秒内只显示一个错误消息不管内容是否相同
if (now - lastErrorTime < ERROR_MESSAGE_INTERVAL) {
return; // 跳过
}
lastErrorTime = now;
ElMessage.error(message);
};
@@ -29,7 +29,7 @@ const showErrorMessage = (message: string) => {
// 地址配置见 .env.development 文件
// ============================================================
// 服务实例端口8808- 系统管理、用户认证、权限、模块开通等
// 统一服务实例端口8000- 全部模块共用
const service: AxiosInstance = axios.create({
baseURL: import.meta.env.VITE_API_URL,
timeout: 50000,
@@ -41,18 +41,6 @@ const service: AxiosInstance = axios.create({
},
});
// 新功能服务实例端口8000- 资产管理、分类、SKU、订单等新模块
const newService: AxiosInstance = axios.create({
baseURL: import.meta.env.VITE_NEW_API_URL,
timeout: 50000,
headers: { 'Content-Type': 'application/json' },
paramsSerializer: {
serialize(params) {
return qs.stringify(params, { allowDots: true, arrayFormat: 'brackets' });
},
},
});
// token 过期处理函数
const handleTokenExpired = () => {
if (isHandlingTokenExpired) return;
@@ -64,7 +52,7 @@ const handleTokenExpired = () => {
showClose: false,
closeOnClickModal: false,
closeOnPressEscape: false,
beforeClose: (action, instance, done) => {
beforeClose: (action, _instance, done) => {
if (action === 'confirm') {
done();
performLogout();
@@ -98,21 +86,21 @@ const requestInterceptor = (config: InternalAxiosRequestConfig) => {
// 可以在这里添加 token 有效性检查(如果需要)
config.headers!['Authorization'] = `Bearer ${token}`;
}
// PUT 请求最小化传参处理
// 如果请求数据中包含 _originalData则自动计算差异只传递修改过的字段
if (config.method?.toLowerCase() === 'put' && config.data && typeof config.data === 'object') {
const { _originalData, ...currentData } = config.data;
if (_originalData && typeof _originalData === 'object') {
// 获取 id 字段(必须保留)
const idField = currentData.id || currentData.Id || currentData.ID;
// 计算差异
const changedFields = getChangedFields(_originalData, currentData, {
exclude: ['_originalData', 'id', 'Id', 'ID'],
});
// 如果有变化,只传递 id + 变化的字段
if (Object.keys(changedFields).length > 0) {
config.data = { id: idField, ...changedFields };
@@ -120,11 +108,11 @@ const requestInterceptor = (config: InternalAxiosRequestConfig) => {
// 没有变化,只传递 id
config.data = { id: idField };
}
console.log('[最小化传参] 原始字段数:', Object.keys(currentData).length, '-> 传递字段数:', Object.keys(config.data).length);
}
}
return config;
};
@@ -222,7 +210,7 @@ const responseErrorHandler = (error: any) => {
showErrorMessage(responseMessage || '服务开通中,请稍后刷新页面');
return Promise.reject(new Error('模块开通中'));
}
const currentPath = window.location.hash.replace('#', '') || window.location.pathname;
console.log('[responseErrorHandler] 检测到HTTP 402错误当前路径:', currentPath);
handleModuleNotEnabled(currentPath);
@@ -262,9 +250,6 @@ const responseErrorHandler = (error: any) => {
service.interceptors.request.use(requestInterceptor, requestErrorHandler);
service.interceptors.response.use(responseInterceptor, responseErrorHandler);
newService.interceptors.request.use(requestInterceptor, requestErrorHandler);
newService.interceptors.response.use(responseInterceptor, responseErrorHandler);
// 导出
export default service;
export { newService, showErrorMessage };
export { showErrorMessage };