Files
liandong-crm/src/app/(dashboard)/customers/template/route.ts
T

62 lines
1.5 KiB
TypeScript
Raw Normal View History

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 = [
"模板名称_此行删除",
"侯磊",
"张三",
"13813855555",
"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)}`,
},
});
}