127 lines
4.7 KiB
TypeScript
127 lines
4.7 KiB
TypeScript
import axios from 'axios';
|
|
import FileSaver from 'file-saver';
|
|
import errorCode from '@/utils/errorCode';
|
|
import { blobValidate } from '@/utils/ruoyi';
|
|
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading.mjs';
|
|
import { globalHeaders } from '@/utils/request';
|
|
import { getFileDownloadUrl } from '@/api/system/file';
|
|
import request from '@/utils/request';
|
|
|
|
const baseURL = import.meta.env.VITE_APP_BASE_API;
|
|
let downloadLoadingInstance: LoadingInstance;
|
|
export default {
|
|
async oss(ossId: string | number) {
|
|
downloadLoadingInstance = ElLoading.service({ text: '正在获取文件访问链接,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
|
|
try {
|
|
// 使用新的安全下载方法,避免Number()转换导致的大整数精度丢失
|
|
// const { data: downloadUrl } = await getFileDownloadUrl(String(ossId));
|
|
|
|
const response = await request({
|
|
url: '/resource/oss/download/' + ossId,
|
|
method: 'get',
|
|
responseType: 'blob',
|
|
timeout: 300000 // 300秒超时
|
|
});
|
|
// 由于axios响应拦截器对blob类型响应直接返回blob数据
|
|
// 这里response直接就是blob对象,而不是包含data属性的响应对象
|
|
const blob = response;
|
|
|
|
// 验证响应数据
|
|
if (!blob) {
|
|
throw new Error('响应数据为空');
|
|
}
|
|
const filename = response.headers.get('content-disposition')?.split('filename=')[1]?.replace(/"/g, '') || `file_${ossId}`;
|
|
|
|
FileSaver.saveAs(blob, decodeURIComponent(filename));
|
|
downloadLoadingInstance.close();
|
|
} catch (r) {
|
|
console.error(r);
|
|
ElMessage.error('下载文件出现错误,请联系管理员!');
|
|
downloadLoadingInstance.close();
|
|
}
|
|
},
|
|
|
|
// 新增:安全下载文件方法
|
|
async secureOss(ossId: string | number, fileName?: string) {
|
|
console.log(ossId, fileName);
|
|
downloadLoadingInstance = ElLoading.service({ text: '正在获取文件访问链接,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
|
|
try {
|
|
// 获取临时下载URL,避免Number()转换导致的大整数精度丢失
|
|
// const { data: downloadUrl } = await getFileDownloadUrl(String(ossId));
|
|
|
|
const response = await request({
|
|
url: '/resource/oss/download/' + ossId,
|
|
method: 'get',
|
|
responseType: 'blob',
|
|
timeout: 300000 // 300秒超时
|
|
});
|
|
// 由于axios响应拦截器对blob类型响应直接返回blob数据
|
|
// 这里response直接就是blob对象,而不是包含data属性的响应对象
|
|
const blob = response;
|
|
|
|
// 验证响应数据
|
|
if (!blob) {
|
|
throw new Error('响应数据为空');
|
|
}
|
|
|
|
const finalFileName = fileName || response.headers.get('content-disposition')?.split('filename=')[1]?.replace(/"/g, '') || `file_${ossId}`;
|
|
|
|
FileSaver.saveAs(blob, decodeURIComponent(finalFileName));
|
|
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',
|
|
timeout: 300000, // 300秒超时
|
|
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 excel(data: any, filename: string) {
|
|
downloadLoadingInstance = ElLoading.service({ text: '正在下载数据,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
|
|
try {
|
|
const isBlob = blobValidate(data);
|
|
if (isBlob) {
|
|
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
|
|
FileSaver.saveAs(blob, filename);
|
|
} else {
|
|
this.printErrMsg(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);
|
|
}
|
|
};
|