Files
web-Iot/src/api/reportDownloader.ts
2026-04-21 10:13:31 +08:00

320 lines
8.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>${reportData.title}</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Microsoft YaHei', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: #050b18;
color: #e0e6ff;
padding: 30px;
line-height: 1.5;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: #0a1428;
border-radius: 20px;
padding: 30px;
box-shadow: 0 0 30px rgba(14, 82, 255, 0.2);
border: 1px solid #162955;
}
h2 {
font-size: 24px;
font-weight: 600;
margin-bottom: 30px;
color: #4d7cff;
text-shadow: 0 0 10px rgba(77, 124, 255, 0.5);
border-bottom: 1px solid #162955;
padding-bottom: 15px;
}
.metrics-row {
display: flex;
gap: 20px;
flex-wrap: wrap;
margin-bottom: 30px;
}
.metric-card {
flex: 1;
min-width: 200px;
background: linear-gradient(135deg, #0f1c3f 0%, #162955 100%);
border: 1px solid #244289;
border-radius: 16px;
padding: 20px;
position: relative;
overflow: hidden;
transition: all 0.3s ease;
}
.metric-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 3px;
background: linear-gradient(90deg, #4d7cff, #00c6ff);
}
.metric-label {
font-size: 13px;
color: #94a3b8;
margin-bottom: 8px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.metric-value {
font-size: 28px;
font-weight: 700;
color: #4d7cff;
text-shadow: 0 0 8px rgba(77, 124, 255, 0.4);
}
.best-time-card {
background: linear-gradient(135deg, #0f1c3f 0%, #162955 100%);
border: 1px solid #244289;
border-radius: 16px;
padding: 20px;
margin-bottom: 30px;
position: relative;
}
.best-time-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
font-weight: 600;
color: #4d7cff;
}
.best-time-value {
font-size: 16px;
margin-bottom: 8px;
color: #e0e6ff;
font-weight: 500;
}
.best-time-desc {
font-size: 13px;
color: #94a3b8;
line-height: 1.4;
}
.task-table {
width: 100%;
border-collapse: collapse;
margin-bottom: 30px;
border-radius: 16px;
overflow: hidden;
background: #0f1c3f;
border: 1px solid #244289;
}
.task-table th, .task-table td {
padding: 15px 12px;
text-align: left;
border-bottom: 1px solid #162955;
}
.task-table th {
background: #162955;
color: #4d7cff;
font-size: 13px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.task-table td {
font-size: 14px;
color: #e0e6ff;
}
.task-table tr:last-child td {
border-bottom: none;
}
.task-table tr:hover td {
background: rgba(77, 124, 255, 0.1);
}
.priority-high {
background: rgba(255, 77, 79, 0.2);
color: #ff4d4f;
padding: 4px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
border: 1px solid rgba(255, 77, 79, 0.3);
}
.priority-medium {
background: rgba(255, 171, 0, 0.2);
color: #ffab00;
padding: 4px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
border: 1px solid rgba(255, 171, 0, 0.3);
}
.priority-low {
background: rgba(0, 255, 127, 0.2);
color: #00ff7f;
padding: 4px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: 500;
border: 1px solid rgba(0, 255, 127, 0.3);
}
.timeline-section {
background: linear-gradient(135deg, #0f1c3f 0%, #162955 100%);
border: 1px solid #244289;
border-radius: 16px;
padding: 20px;
}
.timeline-title {
font-weight: 600;
margin-bottom: 20px;
color: #4d7cff;
font-size: 16px;
}
.timeline-item {
position: relative;
padding-left: 30px;
margin-bottom: 20px;
font-size: 14px;
color: #e0e6ff;
padding-bottom: 20px;
border-left: 2px solid #162955;
}
.timeline-item:last-child {
margin-bottom: 0;
padding-bottom: 0;
border-left: none;
}
.timeline-item::before {
content: '';
position: absolute;
left: -9px;
top: 0;
width: 16px;
height: 16px;
border-radius: 50%;
background: #00ff7f;
border: 2px solid #0a1428;
z-index: 1;
}
.timeline-item:nth-child(3)::before {
background: #3b82f6;
}
.timeline-item:last-child::before {
background: #94a3b8;
}
</style>
</head>
<body>
<div class="container">
<h2>${reportData.title}</h2>
<!-- 核心指标 -->
<div class="metrics-row">
${reportData.metrics
.map(
(metric) => `
<div class="metric-card">
<div class="metric-label">${metric.label}</div>
<div class="metric-value">${metric.value}</div>
</div>
`
)
.join('')}
</div>
<!-- 最佳作业窗口 -->
<div class="best-time-card">
<div class="best-time-header">
<span>${reportData.bestTime.label}</span>
<span>⏰</span>
</div>
<div class="best-time-value">${reportData.bestTime.value}</div>
<div class="best-time-desc">${reportData.bestTime.desc}</div>
</div>
<!-- 任务列表 -->
<table class="task-table">
<thead>
<tr>
${reportData.taskTableHeader
.map((header) => `<th>${header}</th>`)
.join('')}
</tr>
</thead>
<tbody>
${reportData.taskList
.map(
(task) => `
<tr>
${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 `<td><span class="${className}">${task[key]}</span></td>`;
}
return `<td>${task[key]}</td>`;
}).join('')}
</tr>
`
)
.join('')}
</tbody>
</table>
<!-- 算法执行流程 -->
<div class="timeline-section">
<div class="timeline-title">${reportData.processTitle}</div>
<div class="timeline">
${reportData.processSteps
.map((step) => `<div class="timeline-item">${step}</div>`)
.join('')}
</div>
</div>
</div>
</body>
</html>
`;
// 创建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);
};