来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
164 lines
5.1 KiB
TypeScript
164 lines
5.1 KiB
TypeScript
import { downloadCsv, downloadExcelData } from './excelImportExport';
|
|
|
|
export type ExportFormat = 'xlsx' | 'csv' | 'print';
|
|
|
|
export type ExportLoadOptions = {
|
|
expandItems: boolean;
|
|
customerCode?: string;
|
|
};
|
|
|
|
export type ExportColumnGroup = 'header' | 'item' | 'summary';
|
|
|
|
export interface ExportColumnDef<T = Record<string, unknown>> {
|
|
id: string;
|
|
label: string;
|
|
group?: ExportColumnGroup;
|
|
defaultSelected?: boolean;
|
|
getValue: (row: T) => string | number;
|
|
}
|
|
|
|
export interface DocumentExportOptions<T = Record<string, unknown>> {
|
|
format: ExportFormat;
|
|
filename: string;
|
|
sheetName: string;
|
|
title: string;
|
|
columns: ExportColumnDef<T>[];
|
|
selectedColumnIds: string[];
|
|
rows: T[];
|
|
meta?: Record<string, string>;
|
|
/** 打印时标题是否居中,默认 true */
|
|
titleCentered?: boolean;
|
|
/** 为 false 时不再在文件名后追加当天日期(调用方已带筛选时段) */
|
|
appendDateStamp?: boolean;
|
|
}
|
|
|
|
export function getDefaultSelectedColumnIds<T>(columns: ExportColumnDef<T>[]): string[] {
|
|
return columns.filter((col) => col.defaultSelected !== false).map((col) => col.id);
|
|
}
|
|
|
|
export function buildExportMatrix<T>(
|
|
columns: ExportColumnDef<T>[],
|
|
selectedColumnIds: string[],
|
|
rows: T[]
|
|
): { headers: string[]; matrix: (string | number)[][] } {
|
|
const active = columns.filter((col) => selectedColumnIds.includes(col.id));
|
|
const headers = active.map((col) => col.label);
|
|
const matrix = rows.map((row) =>
|
|
active.map((col) => {
|
|
const value = col.getValue(row);
|
|
return value === null || value === undefined ? '' : value;
|
|
})
|
|
);
|
|
return { headers, matrix };
|
|
}
|
|
|
|
function escapeHtml(value: unknown): string {
|
|
return String(value ?? '')
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"');
|
|
}
|
|
|
|
export function buildExportTableHtml(options: {
|
|
title: string;
|
|
headers: string[];
|
|
rows: (string | number)[][];
|
|
meta?: Record<string, string>;
|
|
autoPrint?: boolean;
|
|
/** 标题是否居中,默认 true */
|
|
titleCentered?: boolean;
|
|
}): string {
|
|
const metaHtml = options.meta
|
|
? Object.entries(options.meta)
|
|
.map(([key, value]) => `<div class="meta-row"><span class="meta-key">${escapeHtml(key)}</span><span>${escapeHtml(value)}</span></div>`)
|
|
.join('')
|
|
: '';
|
|
|
|
const tableHead = options.headers.map((h) => `<th>${escapeHtml(h)}</th>`).join('');
|
|
const tableBody = options.rows
|
|
.map(
|
|
(row) =>
|
|
`<tr>${row.map((cell) => `<td>${escapeHtml(cell)}</td>`).join('')}</tr>`
|
|
)
|
|
.join('');
|
|
|
|
const autoPrintScript =
|
|
options.autoPrint === false ? '' : '<script>window.onload=function(){window.print();}</script>';
|
|
const titleAlign = options.titleCentered === false ? 'left' : 'center';
|
|
|
|
return `<!DOCTYPE html><html><head><meta charset="utf-8"/><title>${escapeHtml(options.title)}</title>
|
|
<style>
|
|
body { font-family: "Microsoft YaHei", sans-serif; padding: 24px; color: #111; }
|
|
h1 { font-size: 20px; margin: 0 0 12px; text-align: ${titleAlign}; }
|
|
.meta { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 6px 16px; margin-bottom: 16px; font-size: 13px; }
|
|
.meta-key { color: #666; margin-right: 6px; }
|
|
table { width: 100%; border-collapse: collapse; font-size: 12px; }
|
|
th, td { border: 1px solid #ddd; padding: 6px 8px; text-align: left; }
|
|
th { background: #f5f5f5; }
|
|
@media print { body { padding: 0; } }
|
|
</style></head><body>
|
|
<h1>${escapeHtml(options.title)}</h1>
|
|
${metaHtml ? `<div class="meta">${metaHtml}</div>` : ''}
|
|
<table><thead><tr>${tableHead}</tr></thead><tbody>${tableBody}</tbody></table>
|
|
${autoPrintScript}
|
|
</body></html>`;
|
|
}
|
|
|
|
export function printExportTable(options: {
|
|
title: string;
|
|
headers: string[];
|
|
rows: (string | number)[][];
|
|
meta?: Record<string, string>;
|
|
titleCentered?: boolean;
|
|
}) {
|
|
const html = buildExportTableHtml({ ...options, autoPrint: true });
|
|
const win = window.open('', '_blank');
|
|
if (!win) {
|
|
throw new Error('无法打开打印窗口,请允许浏览器弹窗');
|
|
}
|
|
win.document.write(html);
|
|
win.document.close();
|
|
}
|
|
|
|
export async function executeDocumentExport<T>(options: DocumentExportOptions<T>) {
|
|
const { headers, matrix } = buildExportMatrix(
|
|
options.columns,
|
|
options.selectedColumnIds,
|
|
options.rows
|
|
);
|
|
|
|
if (headers.length === 0) {
|
|
throw new Error('请至少选择一列');
|
|
}
|
|
if (matrix.length === 0) {
|
|
throw new Error('没有可导出的数据');
|
|
}
|
|
|
|
const stamp = new Date().toISOString().slice(0, 10);
|
|
const baseName = options.filename.replace(/\.(xlsx|csv)$/i, '');
|
|
const datedName = options.appendDateStamp === false ? baseName : `${baseName}_${stamp}`;
|
|
|
|
if (options.format === 'print') {
|
|
printExportTable({
|
|
title: options.title,
|
|
headers,
|
|
rows: matrix,
|
|
meta: {
|
|
...options.meta,
|
|
导出时间: new Date().toLocaleString('zh-CN'),
|
|
记录数: String(matrix.length),
|
|
},
|
|
titleCentered: options.titleCentered,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (options.format === 'csv') {
|
|
downloadCsv(`${datedName}.csv`, headers, matrix);
|
|
return;
|
|
}
|
|
|
|
await downloadExcelData(`${datedName}.xlsx`, options.sheetName, headers, matrix);
|
|
}
|