当返回有错误信息需要处理时
const exportEvent = async val => {
const params = {
...formData
};
if (val == '2') {
params.currentPage = 1;
params.pageSize = 100000;
} else {
params.currentPage = exportPage.value.currentPage;
params.pageSize = exportPage.value.pageSize;
}
if (window.origin.includes('fsfund.com')) {
baseUrl.value = `${window.origin}/fund-grab/`;
} else {
baseUrl.value = 'http://localhost:7779/opinion/';
}
loadingFlag.value = true;
axios({
url: `${baseUrl.value}/roBotController/msgExport`,
method: 'post',
responseType: 'blob',
headers: {
token: knowledgeBaseToken.value.token
},
data: params
})
.then(res => {
if (res.headers['content-type'] === 'application/json') {
// 解析 JSON 错误信息
const reader = new FileReader();
// eslint-disable-next-line func-names
reader.onload = function (e) {
if (e.target && typeof e.target.result === 'string') {
const json = JSON.parse(e.target.result);
ElMessage.warning(json.message);
}
// 在这里处理错误,例如显示错误消息给用户
};
reader.readAsText(res.data);
} else {
loadingFlag.value = false;
const link = document.createElement('a');
const blob = new Blob([res.data]);
link.style.display = 'none';
const contentDisposition = res.headers['content-disposition'];
console.log(contentDisposition, '0000---------');
let fileName = '消息记录.xlsx'; // 默认文件名
if (contentDisposition) {
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition);
if (matches !== null && matches[1]) {
fileName = decodeURIComponent(matches[1].replace(/['"]/g, ''));
}
}
link.href = URL.createObjectURL(blob);
link.setAttribute('download', `${fileName}`);
// link.setAttribute('download', `${name}.xlsx`)
document.body.appendChild(link);
link.click();
ElMessage.success('导出成功');
setTimeout(() => {
document.body.removeChild(link);
}, 1000); // 可以根据需要调整延迟时间
}
})
.catch(error => {
console.error('Error:', error);
});
};
//通过链接下载
downTemp() {
fetch("https://data.fsfund.com/oss/file/v1/fileView/448a6f0a-24a1-4be0-a297-de8a691cccbb")
.then(response => response.blob())
.then(blob => {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "回帖模板"; // 设置下载文件的名称
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url); // 释放URL对象
})
.catch(error => console.error("下载失败:", error));
},
// downTemp() {
// const downloadUrl = "https://data.fsfund.com/oss/file/v1/fileView/448a6f0a-24a1-4be0-a297-de8a691cccbb";
// const a = document.createElement("a");
// a.href = downloadUrl;
// a.download = "回帖模板"; // 设置下载文件的名称
// document.body.appendChild(a);
// a.click();
// document.body.removeChild(a);
// },
//通过链接下载
//带文件名在响应头中,解码中文,blob
const exportData = async () => {
const params = {
startDate: startDate.value,
endDate: endDate.value,
operators: currOperarionIds.value
};
if (window.origin.includes('hj.fsfund.com')) {
baseUrl.value = `${window.origin}/fscc-kb-backend`;
} else {
baseUrl.value = 'http://localhost:7779/opinion/';
}
loadingFlag.value = true;
axios({
url: `${baseUrl.value}/AdsStatisticsController/accountFullReport`,
method: 'post',
responseType: 'blob',
headers: {
token: knowledgeBaseToken.value.token
},
data: params
})
.then(res => {
loadingFlag.value = false;
const link = document.createElement('a');
const blob = new Blob([res.data]);
link.style.display = 'none';
const contentDisposition = res.headers['content-disposition'];
let fileName = 'default.xlsx'; // 默认文件名
if (contentDisposition) {
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition);
if (matches !== null && matches[1]) {
fileName = decodeURIComponent(matches[1].replace(/['"]/g, ''));
}
}
link.href = URL.createObjectURL(blob);
link.setAttribute('download', `${fileName}`);
// link.setAttribute('download', `${name}.xlsx`)
document.body.appendChild(link);
link.click();
ElMessage.success('导出成功');
setTimeout(() => {
document.body.removeChild(link);
}, 1000); // 可以根据需要调整延迟时间
})
.catch(error => {
console.error('Error:', error);
});
};
// 下载
const baseUrl = ref();
const downloadRowEvent = async (row: RowVO) => {
if (window.origin.includes('hj.fsfund.com')) {
baseUrl.value = `${window.origin}/fscc-kb-backend`;
} else {
baseUrl.value = 'http://172.30.113.199:9011';
}
axios({
url: `${baseUrl.value}/kb/knowledge-file/download-file?id=${row.id}`,
method: 'get',
responseType: 'blob',
headers: {
token: knowledgeBaseToken.value.token
// 'Content-Type': 'application/json' // 确保在 GET 请求中使用 JSON 格式
// Authorization: 'Bearer fastgpt-fZcXWBhUifVno0EIkL4IQ6bedrEiDly8Qbox0ml6hYAVS5gVraP4kJiNfW' // 添加自定义头部信息
}
})
.then(response => {
const blob = response.data;
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = row.fileName || 'downloaded_file'; // 设置文件名,如果服务器未提供文件名
link.click(); // 触发点击事件,下载文件
// 下载完成后,解除绑定的 URL 和移除链接元素
setTimeout(() => {
URL.revokeObjectURL(url);
link.remove();
}, 1000); // 可以根据需要调整延迟时间
})
.catch(error => {
console.error('Error:', error);
});
// if (window.origin.includes('hj.fsfund.com')) {
// baseUrl.value = `${window.origin}/fscc-kb-backend`;
// } else {
// baseUrl.value = 'http://172.30.113.199:9011';
// }
// const downloadUrl = `${baseUrl.value}/kb/knowledge-file/download-file?id=${row.id}`;
// window.open(downloadUrl, '_blank');
// const link = document.createElement('a');
// link.href = downloadUrl;
// // link.download = name; // 可以指定下载后的文件名
// document.body.appendChild(link);
// link.click();
// document.body.removeChild(link);
};