import { NextResponse } from "next/server"; import * as XLSX from "xlsx"; import { auth } from "@/lib/auth"; /** 生成商户导入模板 Excel */ export async function GET() { if (!(await auth())) { return NextResponse.json({ error: "未登录" }, { status: 401 }); } const headers = [ "商户注名称", "商户经营名称", "商户编号", "联系人", "联系人电话", "结算人", "结算人电话", "产品明细", "押金", "买断", "生日", "业务员", "报单日期", "备注", ]; // 示例行(用户导入前需删除此行) const example = [ "模板注名称_此行删除", "模板经营名称", "Ab12Cd", "侯磊", "13813855555", "张三", "13913966666", "POS+CRM", "100", "100", "生日格式要求:2022-06-21", "A", "报单日期格式要求:2022-06-21", "导入备注", ]; const aoa: (string | number)[][] = [headers, example]; const ws = XLSX.utils.aoa_to_sheet(aoa); ws["!cols"] = headers.map(() => ({ wch: 18 })); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "商户导入模板"); const buf = XLSX.write(wb, { type: "buffer", bookType: "xlsx" }) as Buffer; const filename = "商户导入模板.xlsx"; return new NextResponse(new Uint8Array(buf), { status: 200, headers: { "Content-Type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Content-Disposition": `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`, }, }); }