Files
SPDM/src/api/request.ts

175 lines
5.0 KiB
TypeScript
Raw Normal View History

2025-10-30 19:30:06 +08:00
import axios from 'axios';
import { ElMessage } from 'element-plus';
2026-01-12 18:45:32 +08:00
import { getUserData } from '@/utils/user';
2025-10-30 19:30:06 +08:00
const env = import.meta.env;
2025-11-04 12:03:33 +08:00
const w: any = window;
const $wujie: any = w.$wujie;
2025-10-30 19:30:06 +08:00
const service = axios.create({
baseURL: env.VITE_API_BASE_URL,
timeout: 60000,
headers: {
'Content-Type': 'application/json;application/xml;charset=utf-8;',
},
});
2025-12-29 19:18:02 +08:00
const company = 'carsafe';
2026-01-05 18:33:55 +08:00
let userId = $wujie?.props?.USER_ID || localStorage.getItem('USER_ID') || '';
let token = $wujie?.props?.TOKEN || localStorage.getItem('TOKEN') || '';
let tenantId = $wujie?.props?.TENANT_ID || localStorage.getItem('TENANT_ID') || '';
2025-12-29 19:18:02 +08:00
2025-10-30 19:30:06 +08:00
service.interceptors.request.use(
(config) => {
2026-01-14 17:24:21 +08:00
const configData = config.data || config.params || {};
if (configData.token) {
userId = configData.userId;
token = configData.token;
tenantId = configData.tenantId;
2026-01-05 18:33:55 +08:00
}
2025-12-29 19:18:02 +08:00
config.headers['company'] = company;
2026-01-12 18:45:32 +08:00
config.headers['jobNumber'] = getUserData().username || '';
2025-12-29 19:18:02 +08:00
config.headers['token'] = token;
2026-01-14 17:24:21 +08:00
config.headers['userId'] = userId;
config.headers['tenantId'] = tenantId;
2025-10-30 19:30:06 +08:00
return config;
},
(error) => {
return Promise.reject(error);
}
);
service.interceptors.response.use(
(res) => {
2026-02-25 11:31:47 +08:00
// 判断响应类型,如果是 文件流,返回完整响应对象
if (res.headers['content-type'].includes('octet-stream')) {
return res;
}
2025-10-30 19:30:06 +08:00
if (res.config.responseType === 'blob') {
2026-02-25 11:31:47 +08:00
// 判断响应类型,如果是 blob返回完整响应对象
2025-10-30 19:30:06 +08:00
return res;
}
// 普通接口返回原来的 res.data
2026-02-25 11:31:47 +08:00
if (res.data?.code && res.data?.code !== 200) {
2025-12-09 15:42:21 +08:00
ElMessage.warning(res.data.message);
2025-10-30 19:30:06 +08:00
}
return res.data;
},
(error) => {
2025-12-09 15:42:21 +08:00
ElMessage.warning('系统繁忙,请稍后再试');
2025-10-30 19:30:06 +08:00
return Promise.reject(error);
}
);
const get = (url: string, params = {}) => {
return service.get(url, { params });
};
const post = (url: string, data = {}) => {
return service.post(url, data);
};
const upload = (url: string, formData = {}, onProgress?: any) => {
return service.post(url, formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
onUploadProgress: (event: any) => {
if (onProgress) {
onProgress({
loaded: event.loaded,
total: event.total,
});
}
},
});
};
const download = (url: string, params = {}, filename = 'download') => {
return service
.post(url, params, {
responseType: 'blob',
})
.then(async (res: any) => {
if (res.data.type === 'application/json') {
const text = await res.data.text();
const result = JSON.parse(text);
if (result.code !== 200) {
2025-12-09 15:42:21 +08:00
ElMessage.warning(result.message);
return;
}
}
2025-11-13 16:03:19 +08:00
const contentDisposition =
res.headers?.['content-disposition'] || res.headers?.['Content-Disposition'] || '';
2025-10-30 19:30:06 +08:00
let fileName = filename;
2025-11-13 16:03:19 +08:00
if (contentDisposition) {
const parts = contentDisposition
.split(';')
.map((p: string) => p.trim())
.filter(Boolean);
2025-11-27 10:16:31 +08:00
const paramsMap: Record<string, string> = {};
parts.forEach((part: string) => {
const idx = part.indexOf('=');
if (idx === -1) return;
const key = part.slice(0, idx).trim().toLowerCase();
let val = part.slice(idx + 1).trim();
if (val.startsWith('"') && val.endsWith('"')) {
val = val.slice(1, -1);
}
paramsMap[key] = val;
});
if (paramsMap['filename*']) {
const val = paramsMap['filename*'];
const rfcMatch = val.match(/^([^']*)''(.*)$/);
if (rfcMatch) {
const encoded = rfcMatch[2];
2025-11-13 16:03:19 +08:00
try {
2025-11-27 10:16:31 +08:00
fileName = decodeURIComponent(encoded);
2025-11-13 16:03:19 +08:00
} catch {
2025-11-27 10:16:31 +08:00
fileName = encoded;
2025-11-13 16:03:19 +08:00
}
} else {
2025-11-27 10:16:31 +08:00
if (/^utf-?8$/i.test(val) && paramsMap['filename']) {
const candidate = paramsMap['filename'];
try {
fileName = candidate.includes('%') ? decodeURIComponent(candidate) : candidate;
} catch {
fileName = candidate;
}
} else {
try {
fileName = val.includes('%') ? decodeURIComponent(val) : val;
} catch {
fileName = val;
}
}
}
} else if (paramsMap['filename']) {
const candidate = paramsMap['filename'];
try {
fileName = candidate.includes('%') ? decodeURIComponent(candidate) : candidate;
} catch {
fileName = candidate;
2025-11-13 16:03:19 +08:00
}
}
2025-10-30 19:30:06 +08:00
}
2025-11-13 16:03:19 +08:00
2025-10-30 19:30:06 +08:00
const blob = new Blob([res.data]);
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(link.href);
})
.catch(() => {
2025-12-09 15:42:21 +08:00
ElMessage.warning('下载失败');
2025-10-30 19:30:06 +08:00
});
};
2025-12-23 09:54:06 +08:00
export { get, post, upload, download };