update:接口返回中间件
This commit is contained in:
@@ -41,19 +41,15 @@ service.interceptors.request.use(
|
||||
|
||||
service.interceptors.response.use(
|
||||
(res) => {
|
||||
// 判断响应类型,如果是 文件流,返回完整响应对象
|
||||
if (res.headers['content-type'].includes('octet-stream')) {
|
||||
return res;
|
||||
}
|
||||
// // 判断响应类型,如果是 文件流,返回完整响应对象
|
||||
// if (res.headers['content-type'].includes('octet-stream')) {
|
||||
// return res;
|
||||
// }
|
||||
if (res.config.responseType === 'blob') {
|
||||
// 判断响应类型,如果是 blob,返回完整响应对象
|
||||
return res;
|
||||
}
|
||||
// 普通接口返回原来的 res.data
|
||||
if (res.data?.code && res.data?.code !== 200) {
|
||||
ElMessage.warning(res.data.message);
|
||||
}
|
||||
return res.data;
|
||||
return res;
|
||||
},
|
||||
(error) => {
|
||||
ElMessage.warning('系统繁忙,请稍后再试');
|
||||
@@ -61,16 +57,24 @@ service.interceptors.response.use(
|
||||
}
|
||||
);
|
||||
|
||||
const get = (url: string, params = {}) => {
|
||||
return service.get(url, { params });
|
||||
const get = async (url: string, params = {}) => {
|
||||
const res: any = await service.get(url, { params });
|
||||
if (res.data.code !== 200) {
|
||||
ElMessage.warning(res.data.message);
|
||||
}
|
||||
return res.data;
|
||||
};
|
||||
|
||||
const post = (url: string, data = {}) => {
|
||||
return service.post(url, data);
|
||||
const post = async (url: string, data = {}) => {
|
||||
const res: any = await service.post(url, data);
|
||||
if (res.data.code !== 200) {
|
||||
ElMessage.warning(res.data.message);
|
||||
}
|
||||
return res.data;
|
||||
};
|
||||
|
||||
const upload = (url: string, formData = {}, onProgress?: any) => {
|
||||
return service.post(url, formData, {
|
||||
const upload = async (url: string, formData = {}, onProgress?: any) => {
|
||||
const res: any = await service.post(url, formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
@@ -83,10 +87,11 @@ const upload = (url: string, formData = {}, onProgress?: any) => {
|
||||
}
|
||||
},
|
||||
});
|
||||
return res.data;
|
||||
};
|
||||
|
||||
const download = (url: string, params = {}, filename = 'download') => {
|
||||
return service
|
||||
const download = async (url: string, params = {}, filename = 'download') => {
|
||||
const res: any = await service
|
||||
.post(url, params, {
|
||||
responseType: 'blob',
|
||||
})
|
||||
@@ -169,6 +174,7 @@ const download = (url: string, params = {}, filename = 'download') => {
|
||||
.catch(() => {
|
||||
ElMessage.warning('下载失败');
|
||||
});
|
||||
return res;
|
||||
};
|
||||
|
||||
export { get, post, upload, download };
|
||||
|
||||
Reference in New Issue
Block a user