|
|
@@ -124,7 +124,7 @@ service.interceptors.response.use(
|
|
|
const msg = errorCode[code] || res.data.msg || errorCode['default'];
|
|
|
// 二进制数据则直接返回
|
|
|
if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
|
|
|
- return res.data;
|
|
|
+ return res;
|
|
|
}
|
|
|
if (code === 401) {
|
|
|
// prettier-ignore
|
|
|
@@ -187,7 +187,9 @@ export function download(url: string, params: any, fileName: string) {
|
|
|
],
|
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
responseType: 'blob'
|
|
|
- }).then(async (resp: any) => {
|
|
|
+ }).then(async (res: any) => {
|
|
|
+ console.log();
|
|
|
+ const resp = res.data;
|
|
|
const isLogin = blobValidate(resp);
|
|
|
if (isLogin) {
|
|
|
const blob = new Blob([resp]);
|
|
|
@@ -205,5 +207,91 @@ export function download(url: string, params: any, fileName: string) {
|
|
|
downloadLoadingInstance.close();
|
|
|
});
|
|
|
}
|
|
|
+// 通用下载方法
|
|
|
+export function downloadFile({ url, fileName = '', method = 'get', params = {}, data = {}, preview = false, headers = { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }, text = '正在下载数据,请稍候' }: any) {
|
|
|
+ if (!preview) {
|
|
|
+ downloadLoadingInstance = ElLoading.service({ text, background: 'rgba(0, 0, 0, 0.7)' });
|
|
|
+ }
|
|
|
+ // prettier-ignore
|
|
|
+ const config: any = {}
|
|
|
+ config.headers = headers;
|
|
|
+ config.responseType = 'blob';
|
|
|
+ let curl: string = url;
|
|
|
+ if (method === 'get' && params) {
|
|
|
+ curl = url + '?' + tansParams(params);
|
|
|
+ curl = curl.slice(0, -1);
|
|
|
+ }
|
|
|
+ if (method === 'get') {
|
|
|
+ return service
|
|
|
+ .get(curl, config)
|
|
|
+ .then(async (resp: any) => {
|
|
|
+ const isLogin = blobValidate(resp.data);
|
|
|
+ if (isLogin) {
|
|
|
+ if (preview) {
|
|
|
+ return resp.data;
|
|
|
+ }
|
|
|
+ const blob = new Blob([resp.data]);
|
|
|
+ if (fileName) {
|
|
|
+ FileSaver.saveAs(blob, fileName);
|
|
|
+ Promise.resolve(true);
|
|
|
+ } else {
|
|
|
+ const fileNameEncode = resp.headers['content-disposition'] && resp.headers['content-disposition'].split('filename=')[1].split(';')[0];
|
|
|
+ const fileNameCur = fileNameEncode ? decodeURI(fileNameEncode).replaceAll('"', '') : url;
|
|
|
+
|
|
|
+ FileSaver.saveAs(blob, fileNameCur);
|
|
|
+ Promise.resolve(true);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const resText = await resp.data.text();
|
|
|
+ const rspObj = JSON.parse(resText);
|
|
|
+ const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
|
|
|
+ ElMessage.error(errMsg);
|
|
|
+ Promise.reject('导出失败');
|
|
|
+ }
|
|
|
+ downloadLoadingInstance.close();
|
|
|
+ })
|
|
|
+ .catch((r: any) => {
|
|
|
+ console.error(r);
|
|
|
+ ElMessage.error('下载文件出现错误,请联系管理员!');
|
|
|
+ downloadLoadingInstance.close();
|
|
|
+ Promise.reject('导出失败');
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (method === 'post') {
|
|
|
+ return service
|
|
|
+ .post(curl, data, config)
|
|
|
+ .then(async (resp: any) => {
|
|
|
+ const isLogin = blobValidate(resp.data);
|
|
|
+ if (isLogin) {
|
|
|
+ if (preview) {
|
|
|
+ return resp.data;
|
|
|
+ }
|
|
|
+ const blob = new Blob([resp.data]);
|
|
|
+ if (fileName) {
|
|
|
+ FileSaver.saveAs(blob, fileName);
|
|
|
+ Promise.resolve(true);
|
|
|
+ } else {
|
|
|
+ const fileNameEncode = resp.headers['content-disposition'] && resp.headers['content-disposition'].split('filename=')[1].split(';')[0];
|
|
|
+ const fileNameCur = fileNameEncode ? decodeURI(fileNameEncode).replaceAll('"', '') : url;
|
|
|
+ FileSaver.saveAs(blob, fileNameCur);
|
|
|
+ Promise.resolve(true);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const resText = await resp.data.text();
|
|
|
+ const rspObj = JSON.parse(resText);
|
|
|
+ const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
|
|
|
+ ElMessage.error(errMsg);
|
|
|
+ Promise.reject('导出失败');
|
|
|
+ }
|
|
|
+ downloadLoadingInstance.close();
|
|
|
+ })
|
|
|
+ .catch((r: any) => {
|
|
|
+ console.error(r);
|
|
|
+ ElMessage.error('下载文件出现错误,请联系管理员!');
|
|
|
+ downloadLoadingInstance.close();
|
|
|
+ Promise.reject('导出失败');
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|
|
|
// 导出 axios 实例
|
|
|
export default service;
|