Router.register('/shipments/summary', async () => {
setTimeout(async () => {
const queryBtn = document.getElementById('summary-query-btn');
const exportBtn = document.getElementById('summary-export-btn');
const startDateInput = document.getElementById('start-date');
const endDateInput = document.getElementById('end-date');
const resultDiv = document.getElementById('summary-result');
// 设置默认日期为当前月份
const now = new Date();
const firstDay = new Date(now.getFullYear(), now.getMonth(), 1);
const lastDay = new Date(now.getFullYear(), now.getMonth() + 1, 0);
if (startDateInput) {
startDateInput.value = firstDay.toISOString().split('T')[0];
}
if (endDateInput) {
endDateInput.value = lastDay.toISOString().split('T')[0];
}
const performQuery = async () => {
const startDate = startDateInput?.value;
const endDate = endDateInput?.value;
if (!startDate || !endDate) {
resultDiv.innerHTML = '
请选择开始和结束日期
';
return;
}
try {
resultDiv.innerHTML = '查询中...
';
const res = await fetch(`/api/shipments/summary?start=${encodeURIComponent(startDate)}&end=${encodeURIComponent(endDate)}`, {
credentials: 'include'
});
const data = await res.json();
if (data.records && data.records.length > 0) {
const totalQty = data.records.reduce((sum, r) => sum + (r.qty || 0), 0);
const tableRows = data.records.map(record => `
| ${record.date} |
${record.qty} |
${record.receiver || '-'} |
${record.ts || '-'} |
`).join('');
resultDiv.innerHTML = `
查询时间段
${startDate} 至 ${endDate}
| 发货日期 |
数量 |
收货方 |
录入时间 |
${tableRows}
`;
} else {
resultDiv.innerHTML = `
`;
}
} catch (e) {
resultDiv.innerHTML = `查询失败:${e.message}
`;
}
};
const performExport = async () => {
const startDate = startDateInput?.value;
const endDate = endDateInput?.value;
if (!startDate || !endDate) {
alert('请选择开始和结束日期');
return;
}
try {
exportBtn.disabled = true;
exportBtn.textContent = '导出中...';
const url = `/api/shipments/export?start=${encodeURIComponent(startDate)}&end=${encodeURIComponent(endDate)}`;
// 使用 fetch 下载文件
const res = await fetch(url, {
credentials: 'include'
});
if (!res.ok) {
const error = await res.json();
throw new Error(error.error || '导出失败');
}
// 获取文件名
const contentDisposition = res.headers.get('Content-Disposition');
let filename = `发货记录_${startDate}_至_${endDate}.xlsx`;
if (contentDisposition) {
const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition);
if (matches && matches[1]) {
filename = matches[1].replace(/['"]/g, '');
}
}
// 下载文件
const blob = await res.blob();
const downloadUrl = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(downloadUrl);
document.body.removeChild(a);
alert('导出成功!');
} catch (e) {
alert('导出失败:' + e.message);
} finally {
exportBtn.disabled = false;
exportBtn.textContent = '导出Excel';
}
};
queryBtn?.addEventListener('click', performQuery);
exportBtn?.addEventListener('click', performExport);
// 自动执行一次查询
performQuery();
}, 0);
return `
`;
});