From 228c37c8972fad01f44a12d0aa49e8a0c1a82449 Mon Sep 17 00:00:00 2001 From: luishou Date: Sat, 11 Jul 2026 21:17:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E8=A1=A8=E5=8D=95=E5=BC=BA=E5=88=B6=E5=BF=85=E5=A1=AB=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=EF=BC=88=E5=90=8D=E7=A7=B0=E6=97=A0=E7=A9=BA=E6=A0=BC?= =?UTF-8?q?/=E6=89=8B=E6=9C=BA=E5=8F=B711=E4=BD=8D/=E6=8A=BC=E9=87=91?= =?UTF-8?q?=E4=B9=B0=E6=96=AD=E7=94=9F=E6=97=A5=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 --- src/app/(dashboard)/customers/actions.ts | 29 +++++++++-- .../(dashboard)/customers/customer-form.tsx | 50 ++++++++++++++++--- src/app/(dashboard)/customers/new/page.tsx | 2 +- 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/app/(dashboard)/customers/actions.ts b/src/app/(dashboard)/customers/actions.ts index d1a29a2..04fbf05 100644 --- a/src/app/(dashboard)/customers/actions.ts +++ b/src/app/(dashboard)/customers/actions.ts @@ -29,10 +29,29 @@ const schema = z.object({ remark: z.string().trim().optional().default(""), }); +// 新增客户更严格:名称无空格、手机号必填 11 位、押金/买断/生日必填(仅用于创建,编辑仍用 schema) +const createSchema = schema.extend({ + customerName: z + .string() + .trim() + .min(1, "客户名称必填") + .max(500) + .refine((s) => !/\s/.test(s), "客户名称不能包含空格"), + phone: z.string().trim().regex(/^1\d{10}$/, "手机号必须为 11 位数字"), + deposit: z.string().trim().min(1, "押金必填"), + buy: z.string().trim().min(1, "买断必填"), + birthday: z + .string() + .trim() + .min(1, "生日必填") + .refine((s) => parseDate(s) !== null, "生日格式应为 YYYY-MM-DD"), +}); + export type ActionState = { error?: string }; -function parse(formData: FormData) { - return schema.safeParse({ +/** 从 FormData 取出待校验的原始字段值 */ +function pickForm(formData: FormData) { + return { customerName: formData.get("customerName"), contacts: formData.get("contacts"), balanceUser: formData.get("balanceUser"), @@ -45,7 +64,7 @@ function parse(formData: FormData) { birthdayType: formData.get("birthdayType") ?? 1, birthday: formData.get("birthday") ?? "", remark: formData.get("remark") ?? "", - }); + }; } /** 把校验后的表单值转成 Prisma 写入数据(日期字段转 Date) */ @@ -63,7 +82,7 @@ export async function createCustomer( formData: FormData ): Promise { if (!(await auth())) return { error: "未登录" }; - const r = parse(formData); + const r = createSchema.safeParse(pickForm(formData)); if (!r.success) return { error: r.error.issues[0].message }; await prisma.customer.create({ data: toData(r.data) }); @@ -77,7 +96,7 @@ export async function updateCustomer( formData: FormData ): Promise { if (!(await auth())) return { error: "未登录" }; - const r = parse(formData); + const r = schema.safeParse(pickForm(formData)); if (!r.success) return { error: r.error.issues[0].message }; await prisma.customer.update({ where: { id }, data: toData(r.data) }); diff --git a/src/app/(dashboard)/customers/customer-form.tsx b/src/app/(dashboard)/customers/customer-form.tsx index 8246f7c..373c7fd 100644 --- a/src/app/(dashboard)/customers/customer-form.tsx +++ b/src/app/(dashboard)/customers/customer-form.tsx @@ -29,12 +29,18 @@ function Field({ defaultValue, required, type = "text", + pattern, + inputMode, + title, }: { label: string; name: string; defaultValue?: string | null; required?: boolean; type?: string; + pattern?: string; + inputMode?: React.HTMLAttributes["inputMode"]; + title?: string; }) { return (
@@ -42,7 +48,16 @@ function Field({ {label} {required && *} - +
); } @@ -51,10 +66,12 @@ export function CustomerForm({ action, defaultValues, submitLabel = "保存", + requireAll = false, }: { action: (prev: ActionState, formData: FormData) => Promise; defaultValues?: CustomerValues; submitLabel?: string; + requireAll?: boolean; }) { const [state, formAction, pending] = useActionState(action, {}); const v = defaultValues ?? {}; @@ -62,11 +79,26 @@ export function CustomerForm({ return (
- + - + - - + +
- +
diff --git a/src/app/(dashboard)/customers/new/page.tsx b/src/app/(dashboard)/customers/new/page.tsx index be11cb7..93884ab 100644 --- a/src/app/(dashboard)/customers/new/page.tsx +++ b/src/app/(dashboard)/customers/new/page.tsx @@ -18,7 +18,7 @@ export default function NewCustomerPage() { 新增客户 - +