|
|
@@ -176,36 +176,43 @@ service.interceptors.response.use(
|
|
|
}
|
|
|
);
|
|
|
// 通用下载方法
|
|
|
-export function download(url: string, params: any, fileName: string) {
|
|
|
- downloadLoadingInstance = ElLoading.service({ text: '正在下载数据,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
|
|
|
+export function download(url: any, params = {}, fileName = '') {
|
|
|
+ downloadLoadingInstance = ElLoading.service({ text: '正在下载数据,请稍候', background: 'rgba(255, 255, 255, 0.8)' });
|
|
|
// prettier-ignore
|
|
|
- return service.post(url, params, {
|
|
|
- transformRequest: [
|
|
|
- (params: any) => {
|
|
|
- return tansParams(params);
|
|
|
- }
|
|
|
- ],
|
|
|
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
- responseType: 'blob'
|
|
|
- }).then(async (res: any) => {
|
|
|
- console.log();
|
|
|
- const resp = res.data;
|
|
|
- const isLogin = blobValidate(resp);
|
|
|
- if (isLogin) {
|
|
|
- const blob = new Blob([resp]);
|
|
|
- FileSaver.saveAs(blob, fileName);
|
|
|
- } else {
|
|
|
- const resText = await resp.data.text();
|
|
|
- const rspObj = JSON.parse(resText);
|
|
|
- const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
|
|
|
- ElMessage.error(errMsg);
|
|
|
- }
|
|
|
- downloadLoadingInstance.close();
|
|
|
- }).catch((r: any) => {
|
|
|
- console.error(r);
|
|
|
- ElMessage.error('下载文件出现错误,请联系管理员!');
|
|
|
- downloadLoadingInstance.close();
|
|
|
- });
|
|
|
+ const config: any = {}
|
|
|
+ config.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
|
|
|
+ config.responseType = 'blob';
|
|
|
+ return service
|
|
|
+ .get(url, config)
|
|
|
+ .then(async (resp: any) => {
|
|
|
+ const isLogin = blobValidate(resp.data);
|
|
|
+ if (isLogin) {
|
|
|
+ const blob = new Blob([resp.data]);
|
|
|
+ if (fileName) {
|
|
|
+ console.log();
|
|
|
+ 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.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('导出失败');
|
|
|
+ });
|
|
|
}
|
|
|
// 通用下载方法
|
|
|
export function downloadFile({ url, fileName = '', method = 'get', params = {}, data = {}, preview = false, headers = { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }, text = '正在下载数据,请稍候' }: any) {
|