// reportDownloader.ts
export interface ReportData {
title: string;
metrics: {
label: string;
value: string;
}[];
bestTime: {
label: string;
value: string;
desc: string;
};
taskList: {
area: string;
[key: string]: string; // 适配割草(植被高度)、清洗(灰损率)的不同字段
lossPower: string;
priority: string;
}[];
taskTableHeader: string[];
taskTableKeys: string[];
processSteps: string[];
processTitle: string;
}
/**
* 生成HTML报告并下载
* @param reportData 报告数据
* @param fileName 下载文件名
*/
export const downloadHTMLReport = (reportData: ReportData, fileName: string) => {
// HTML报告模板(深蓝色科技主题,专业可视化)
const htmlContent = `
${reportData.title}
${reportData.title}
${reportData.metrics
.map(
(metric) => `
${metric.label}
${metric.value}
`
)
.join('')}
${reportData.bestTime.value}
${reportData.bestTime.desc}
${reportData.taskTableHeader
.map((header) => `| ${header} | `)
.join('')}
${reportData.taskList
.map(
(task) => `
${reportData.taskTableKeys.map((key) => {
if (key === 'priority') {
let className = '';
if (task[key] === '高') className = 'priority-high';
else if (task[key] === '中') className = 'priority-medium';
else className = 'priority-low';
return `| ${task[key]} | `;
}
return `${task[key]} | `;
}).join('')}
`
)
.join('')}
${reportData.processTitle}
${reportData.processSteps
.map((step) => `
${step}
`)
.join('')}
`;
// 创建Blob并下载
const blob = new Blob([htmlContent], { type: 'text/html; charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${fileName}.html`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};