feat: 联动支付 CRM 后台(Next.js + TS + Prisma 重构)
从旧 Laravel 5.7 重构为现代技术栈,对接现有 MySQL。 - 技术栈: Next.js 15 App Router + TypeScript + Prisma + Auth.js + Tailwind v4 - 认证: 复用旧库 admins 的 bcrypt 密码,Auth.js Credentials - 客户管理: 列表(搜索/报单区间/分页)、增改删、Excel 导入导出 - 生日提醒: 阴阳历(lunar-typescript),口径对齐旧 overtrue 库 - RBAC: 管理员/角色/权限(菜单树) 完整 CRUD + 分配 - 控制台: 统计卡 + 月度新增报单柱状图 - 个人中心: 改昵称/改密码 - 现代 CMS 界面 + 联动支付品牌(logo/登录/底部/icon) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
import { z } from "zod";
|
||||
import * as XLSX from "xlsx";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { parseDate } from "@/lib/utils";
|
||||
|
||||
// 校验规则对齐老系统必填项(customer_name/contacts/balance_user/detail/salesman/report_date)
|
||||
const schema = z.object({
|
||||
customerName: z.string().trim().min(1, "客户名称必填").max(500),
|
||||
contacts: z.string().trim().min(1, "联系人必填").max(500),
|
||||
balanceUser: z.string().trim().min(1, "结算人必填").max(500),
|
||||
detail: z.string().trim().min(1, "详情必填"),
|
||||
salesman: z.string().trim().min(1, "业务员必填"),
|
||||
reportDate: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1, "报单时间必填")
|
||||
.refine((s) => parseDate(s) !== null, "报单时间格式应为 YYYY-MM-DD"),
|
||||
phone: z.string().trim().optional().default(""),
|
||||
deposit: z.string().trim().optional().default(""),
|
||||
buy: z.string().trim().optional().default(""),
|
||||
birthdayType: z.coerce.number().int().min(1).max(2).default(1),
|
||||
birthday: z.string().trim().optional().default(""),
|
||||
remark: z.string().trim().optional().default(""),
|
||||
});
|
||||
|
||||
export type ActionState = { error?: string };
|
||||
|
||||
function parse(formData: FormData) {
|
||||
return schema.safeParse({
|
||||
customerName: formData.get("customerName"),
|
||||
contacts: formData.get("contacts"),
|
||||
balanceUser: formData.get("balanceUser"),
|
||||
detail: formData.get("detail"),
|
||||
salesman: formData.get("salesman"),
|
||||
reportDate: formData.get("reportDate"),
|
||||
phone: formData.get("phone") ?? "",
|
||||
deposit: formData.get("deposit") ?? "",
|
||||
buy: formData.get("buy") ?? "",
|
||||
birthdayType: formData.get("birthdayType") ?? 1,
|
||||
birthday: formData.get("birthday") ?? "",
|
||||
remark: formData.get("remark") ?? "",
|
||||
});
|
||||
}
|
||||
|
||||
/** 把校验后的表单值转成 Prisma 写入数据(日期字段转 Date) */
|
||||
function toData(v: z.infer<typeof schema>) {
|
||||
const { reportDate, birthday, ...rest } = v;
|
||||
return {
|
||||
...rest,
|
||||
reportDate: parseDate(reportDate),
|
||||
birthday: parseDate(birthday),
|
||||
};
|
||||
}
|
||||
|
||||
export async function createCustomer(
|
||||
_prev: ActionState,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
if (!(await auth())) return { error: "未登录" };
|
||||
const r = parse(formData);
|
||||
if (!r.success) return { error: r.error.issues[0].message };
|
||||
|
||||
await prisma.customer.create({ data: toData(r.data) });
|
||||
revalidatePath("/customers");
|
||||
redirect("/customers");
|
||||
}
|
||||
|
||||
export async function updateCustomer(
|
||||
id: number,
|
||||
_prev: ActionState,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
if (!(await auth())) return { error: "未登录" };
|
||||
const r = parse(formData);
|
||||
if (!r.success) return { error: r.error.issues[0].message };
|
||||
|
||||
await prisma.customer.update({ where: { id }, data: toData(r.data) });
|
||||
revalidatePath("/customers");
|
||||
redirect("/customers");
|
||||
}
|
||||
|
||||
export async function deleteCustomer(id: number): Promise<void> {
|
||||
if (!(await auth())) return;
|
||||
// 软删除,对齐老系统 deleted_at
|
||||
await prisma.customer.update({
|
||||
where: { id },
|
||||
data: { deletedAt: new Date() },
|
||||
});
|
||||
revalidatePath("/customers");
|
||||
}
|
||||
|
||||
// ============ Excel 导入 ============
|
||||
|
||||
const HEADER_MAP: Record<string, string> = {
|
||||
客户名称: "customerName",
|
||||
联系人: "contacts",
|
||||
结算人: "balanceUser",
|
||||
电话: "phone",
|
||||
详情: "detail",
|
||||
押金: "deposit",
|
||||
买断: "buy",
|
||||
生日类型: "birthdayType",
|
||||
生日: "birthday",
|
||||
业务员: "salesman",
|
||||
报单时间: "reportDate",
|
||||
备注: "remark",
|
||||
};
|
||||
|
||||
export type ImportResult = { ok: boolean; inserted?: number; skipped?: number; error?: string };
|
||||
|
||||
export async function importCustomers(formData: FormData): Promise<ImportResult> {
|
||||
if (!(await auth())) return { ok: false, error: "未登录" };
|
||||
const file = formData.get("file");
|
||||
if (!(file instanceof File) || file.size === 0) return { ok: false, error: "请选择文件" };
|
||||
|
||||
try {
|
||||
const buf = Buffer.from(await file.arrayBuffer());
|
||||
const wb = XLSX.read(buf, { type: "buffer", cellDates: true });
|
||||
const sheet = wb.Sheets[wb.SheetNames[0]];
|
||||
const raw = XLSX.utils.sheet_to_json<Record<string, unknown>>(sheet, { raw: false, defval: "" });
|
||||
|
||||
const data: Prisma.CustomerCreateManyInput[] = [];
|
||||
let skipped = 0;
|
||||
for (const row of raw) {
|
||||
const rec: Record<string, string> = {};
|
||||
for (const [header, val] of Object.entries(row)) {
|
||||
const key = HEADER_MAP[String(header).trim()];
|
||||
if (key) rec[key] = String(val ?? "").trim();
|
||||
}
|
||||
// 报单时间为必填且需可解析,否则跳过
|
||||
const reportDate = parseDate(rec.reportDate);
|
||||
if (!rec.customerName || !reportDate) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
data.push({
|
||||
customerName: rec.customerName,
|
||||
contacts: rec.contacts || null,
|
||||
balanceUser: rec.balanceUser || null,
|
||||
phone: rec.phone || null,
|
||||
detail: rec.detail || null,
|
||||
deposit: rec.deposit || null,
|
||||
buy: rec.buy || null,
|
||||
salesman: rec.salesman || null,
|
||||
remark: rec.remark || null,
|
||||
birthdayType: rec.birthdayType === "阴历" || rec.birthdayType === "2" ? 2 : 1,
|
||||
birthday: parseDate(rec.birthday),
|
||||
reportDate,
|
||||
});
|
||||
}
|
||||
|
||||
if (data.length === 0) return { ok: false, error: `无有效数据(跳过 ${skipped} 行)` };
|
||||
|
||||
await prisma.customer.createMany({ data });
|
||||
revalidatePath("/customers");
|
||||
return { ok: true, inserted: data.length, skipped };
|
||||
} catch (e) {
|
||||
return { ok: false, error: (e as Error).message };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user