开源软件列表导出接口对接

This commit is contained in:
付民康
2025-03-26 10:14:40 +08:00
parent d0e6d56624
commit 8e99519a0d
2 changed files with 106 additions and 43 deletions

View File

@@ -241,3 +241,15 @@ export const getGroupSubscribeList = (data): Promise<any> => {
})
);
}
// 搜索结果导出接口
export const getReleaseExport = (data): Promise<any> => {
return reqCatchV2(() =>
request({
url: `/api/v1/repo/release/export`,
method: 'POST',
data,
responseType: 'blob' // 关键告诉axios这是二进制数据
})
);
}

View File

@@ -108,7 +108,7 @@
<a @click="gotoDetail(scope.row)">{{ scope.row.version }}</a>
</template>
<template v-else-if="column.key === 'releaseDate'">
<span>{{ formatTime(scope.row.releaseDate, 'YYYY-MM-DD') }}</span>
<span>{{ formatTime(scope.row.releaseDate, 'YYYY-MM-DD HH:mm:ss') }}</span>
</template>
<template v-else-if="column.key === 'softwareVersion'">
<a @click="gotoDetail(scope.row)">{{ scope.row.softwareVersion }}</a>
@@ -207,7 +207,7 @@ import NoData from '@/components/NoData/NoData.vue';
import { useAccountStore } from '@/stores/user';
import * as XLSX from 'xlsx';
const { formatTime } = useTimeFormat();
import { releasePage, searchLanguageList } from '@/api/jyh';
import { getReleaseExport, releasePage, searchLanguageList } from '@/api/jyh';
import { useTimeFormat } from '@/utils/hooks/useTimeFormat';
import { useGlobalInfoStore } from '@/stores/Global';
@@ -503,6 +503,57 @@ onMounted(async () => {
// getReleaseList();
});
const exportResult = async () => {
const { data, error } = await getReleaseExport({
softwareName: newFormData.value.softwareName || '',
searchType: newFormData.value.searchType || '',
softwareVersion: newFormData.value.softwareVersion || '',
programmingLanguage: newFormData.value.programmingLanguage || '',
interestTag: newFormData.value.interestTag || '',
industry: newFormData.value.industry || '',
licenseName: newFormData.value.licenseName || '',
dependentSoftware: newFormData.value.dependentSoftware || '',
releaseDateStart:
newFormData.value.dateRange && newFormData.value.dateRange.length > 0 ? newFormData.value.dateRange[0] : '',
releaseDateEnd:
newFormData.value.dateRange && newFormData.value.dateRange.length > 0 ? newFormData.value.dateRange[1] : '',
current: pager.value.pageIndex,
size: pager.value.pageSize
});
if (data && !error) {
debugger
// 方法1如果后端返回的是文件流Blob
const blob = new Blob([data.data]);
const downloadUrl = window.URL.createObjectURL(blob);
// 创建一个临时的a标签触发下载
const link = document.createElement('a');
link.href = downloadUrl;
// 从Content-Disposition头中获取文件名或者使用默认文件名
const contentDisposition = data.headers['content-disposition'];
let fileName = 'export.xlsx'; // 默认文件名
if (contentDisposition) {
const fileNameMatch = contentDisposition.match(/filename="?(.+)"?/);
if (fileNameMatch && fileNameMatch[1]) {
fileName = fileNameMatch[1].replace(/^[^0-9]*|[^0-9.xlsx]*$/g, "");
}
}
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
// 清理
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl);
} else {
console.error('导出失败:', error);
// 这里可以添加错误提示比如使用Element Plus的ElMessage
ElMessage.error('文件导出失败');
}
};
// onMounted(() => {
// const query: any = route.query;
// newFormData.value = {
@@ -510,47 +561,47 @@ onMounted(async () => {
// };
// });
const exportResult = () => {
const selectDatas = filterTableRef.value.store.getCheckedRows().map((d) => d.id);
if (!allTableData.value.length || selectDatas.length <= 0) {
Message.info('未选择任何数据');
return;
}
const exportData = allTableData.value
.filter((d) => {
return selectDatas.includes(d.id);
})
.map((item) => {
let res = {};
columns.value.forEach((col) => {
if (col.key === 'validStatus') {
res[col.label] = validStatusMap[item[col.key]] ? validStatusMap[item[col.key]] : '';
} else {
res[col.label] = item[col.key] ? item[col.key] : '';
}
});
return res;
});
try {
const workSheet = XLSX.utils.json_to_sheet(exportData);
const workBook: any = { Sheets: { 校验结果: workSheet }, SheetNames: ['校验结果'] };
const excelBuffer = XLSX.write(workBook, { bookType: 'xlsx', type: 'array' });
const link = document.createElement('a');
const blob = new Blob([excelBuffer]);
link.setAttribute('href', window.URL.createObjectURL(blob));
link.setAttribute('download', '软件存在及可信校验结果.xlsx');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
Message.success('导出成功');
} catch {
Message.error('导出失败');
}
};
// const exportResult = () => {
// const selectDatas = filterTableRef.value.store.getCheckedRows().map((d) => d.id);
// if (!allTableData.value.length || selectDatas.length <= 0) {
// Message.info('未选择任何数据');
// return;
// }
//
// const exportData = allTableData.value
// .filter((d) => {
// return selectDatas.includes(d.id);
// })
// .map((item) => {
// let res = {};
// columns.value.forEach((col) => {
// if (col.key === 'validStatus') {
// res[col.label] = validStatusMap[item[col.key]] ? validStatusMap[item[col.key]] : '';
// } else {
// res[col.label] = item[col.key] ? item[col.key] : '';
// }
// });
// return res;
// });
//
// try {
// const workSheet = XLSX.utils.json_to_sheet(exportData);
// const workBook: any = { Sheets: { 校验结果: workSheet }, SheetNames: ['校验结果'] };
// const excelBuffer = XLSX.write(workBook, { bookType: 'xlsx', type: 'array' });
//
// const link = document.createElement('a');
// const blob = new Blob([excelBuffer]);
// link.setAttribute('href', window.URL.createObjectURL(blob));
// link.setAttribute('download', '软件存在及可信校验结果.xlsx');
// link.style.visibility = 'hidden';
// document.body.appendChild(link);
// link.click();
// document.body.removeChild(link);
// Message.success('导出成功');
// } catch {
// Message.error('导出失败');
// }
// };
</script>
<style scoped lang="scss">