130 lines
4.3 KiB
TypeScript
130 lines
4.3 KiB
TypeScript
import axios from 'axios';
|
|
import FileSaver from 'file-saver';
|
|
import JSZip from 'jszip';
|
|
import errorCode from '@/utils/errorCode';
|
|
import { blobValidate } from '@/utils/ruoyi';
|
|
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading';
|
|
import { globalHeaders } from '@/utils/request';
|
|
|
|
const baseURL = import.meta.env.VITE_APP_BASE_API;
|
|
let downloadLoadingInstance: LoadingInstance;
|
|
export default {
|
|
async oss(ossId: string | number) {
|
|
const url = baseURL + '/resource/oss/download/' + ossId;
|
|
downloadLoadingInstance = ElLoading.service({ text: '正在下载数据,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
|
|
try {
|
|
const res = await axios({
|
|
method: 'get',
|
|
url: url,
|
|
responseType: 'blob',
|
|
headers: globalHeaders()
|
|
});
|
|
const isBlob = blobValidate(res.data);
|
|
if (isBlob) {
|
|
const blob = new Blob([res.data], { type: 'application/octet-stream' });
|
|
FileSaver.saveAs(blob, decodeURIComponent(res.headers['download-filename'] as string));
|
|
} else {
|
|
this.printErrMsg(res.data);
|
|
}
|
|
downloadLoadingInstance.close();
|
|
} catch (r) {
|
|
console.error(r);
|
|
ElMessage.error('下载文件出现错误,请联系管理员!');
|
|
downloadLoadingInstance.close();
|
|
}
|
|
},
|
|
async zip(url: string, name: string) {
|
|
url = baseURL + url;
|
|
downloadLoadingInstance = ElLoading.service({ text: '正在下载数据,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
|
|
try {
|
|
const res = await axios({
|
|
method: 'get',
|
|
url: url,
|
|
responseType: 'blob',
|
|
headers: globalHeaders()
|
|
});
|
|
const isBlob = blobValidate(res.data);
|
|
if (isBlob) {
|
|
const blob = new Blob([res.data], { type: 'application/zip' });
|
|
FileSaver.saveAs(blob, name);
|
|
} else {
|
|
this.printErrMsg(res.data);
|
|
}
|
|
downloadLoadingInstance.close();
|
|
} catch (r) {
|
|
console.error(r);
|
|
ElMessage.error('下载文件出现错误,请联系管理员!');
|
|
downloadLoadingInstance.close();
|
|
}
|
|
},
|
|
async printErrMsg(data: any) {
|
|
const resText = await data.text();
|
|
const rspObj = JSON.parse(resText);
|
|
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
|
|
ElMessage.error(errMsg);
|
|
},
|
|
async direct(fileUrl: string, filename?: string) {
|
|
downloadLoadingInstance = ElLoading.service({ text: '正在下载文件,请稍候...', background: 'rgba(0, 0, 0, 0.7)' });
|
|
try {
|
|
const res = await axios({
|
|
method: 'get',
|
|
url: fileUrl,
|
|
responseType: 'blob',
|
|
headers: globalHeaders() // 可根据是否跨域决定是否保留
|
|
});
|
|
const blob = new Blob([res.data], { type: 'application/octet-stream' });
|
|
const name = filename || decodeURIComponent(fileUrl.split('/').pop()!);
|
|
FileSaver.saveAs(blob, name);
|
|
} catch (error) {
|
|
console.error(error);
|
|
ElMessage.error('下载文件失败,请检查链接或联系管理员');
|
|
} finally {
|
|
downloadLoadingInstance.close();
|
|
}
|
|
},
|
|
/**
|
|
* 下载多个文件并打包成 zip
|
|
* @param files 文件信息列表,包含 { url, name }
|
|
* @param zipName 压缩包名称
|
|
*/
|
|
async downloadFilesAsZip(
|
|
files: any[],
|
|
options: {
|
|
urlKey?: string; // 默认为 'url'
|
|
nameKey?: string; // 默认为 'name'
|
|
zipName?: string; // 默认为 '打包下载.zip'
|
|
} = {}
|
|
) {
|
|
const { urlKey = 'url', nameKey = 'name', zipName = '打包下载.zip' } = options;
|
|
|
|
if (!files.length) {
|
|
ElMessage.warning('没有可下载的文件');
|
|
return;
|
|
}
|
|
|
|
const zip = new JSZip();
|
|
const folder = zip.folder('files');
|
|
const loading = ElLoading.service({ text: '正在打包文件,请稍候...', background: 'rgba(0,0,0,0.7)' });
|
|
|
|
try {
|
|
for (const file of files) {
|
|
const fileUrl = file[urlKey];
|
|
const fileName = file[nameKey] || '未命名文件';
|
|
|
|
if (!fileUrl) continue;
|
|
|
|
const res = await axios.get(fileUrl, { responseType: 'blob' });
|
|
folder?.file(fileName, res.data);
|
|
}
|
|
|
|
const content = await zip.generateAsync({ type: 'blob' });
|
|
FileSaver.saveAs(content, zipName);
|
|
} catch (error) {
|
|
console.error(error);
|
|
ElMessage.error('打包下载失败,请联系管理员');
|
|
} finally {
|
|
loading.close();
|
|
}
|
|
}
|
|
};
|