feat(birthday): 提醒页改回列表并支持自定义字段

布局对齐商户列表,可勾选展示列;仅管理员看明文手机号,去掉操作列。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-12 09:52:23 +08:00
co-authored by Cursor
parent 6a63e404f1
commit 4b2334fb70
3 changed files with 396 additions and 130 deletions
@@ -0,0 +1,48 @@
export const BIRTHDAY_COLUMN_STORAGE_KEY = "crm-birthday-columns-v1";
export type BirthdayColumnKey =
| "customerName"
| "customerNo"
| "contacts"
| "balanceUser"
| "birthday"
| "salesman"
| "reportDate"
| "deposit"
| "buy"
| "detail"
| "remark";
export type BirthdayColumnDef = {
key: BirthdayColumnKey;
label: string;
/** 默认展示:打电话常用信息 */
defaultVisible: boolean;
/** 表头/单元格宽度,避免名称列被表格拉得很宽 */
className?: string;
};
export const BIRTHDAY_COLUMNS: BirthdayColumnDef[] = [
{ key: "customerName", label: "商户名称", defaultVisible: true, className: "w-[12%]" },
{ key: "customerNo", label: "商户编号", defaultVisible: true, className: "w-[10%]" },
{ key: "balanceUser", label: "结算人", defaultVisible: true, className: "w-[12%]" },
{ key: "contacts", label: "联系人", defaultVisible: true, className: "w-[12%]" },
{ key: "birthday", label: "生日", defaultVisible: true, className: "w-[14%]" },
{ key: "salesman", label: "业务员", defaultVisible: true, className: "w-[8%]" },
{ key: "remark", label: "备注", defaultVisible: true, className: "w-[20%]" },
{ key: "reportDate", label: "报单日期", defaultVisible: false, className: "w-[10%]" },
{ key: "deposit", label: "押金", defaultVisible: false, className: "w-[8%]" },
{ key: "buy", label: "买断", defaultVisible: false, className: "w-[8%]" },
{ key: "detail", label: "产品明细", defaultVisible: false, className: "w-[18%]" },
];
export function defaultBirthdayColumns(): BirthdayColumnKey[] {
return BIRTHDAY_COLUMNS.filter((c) => c.defaultVisible).map((c) => c.key);
}
export function normalizeBirthdayColumns(raw: unknown): BirthdayColumnKey[] {
const allowed = new Set(BIRTHDAY_COLUMNS.map((c) => c.key));
if (!Array.isArray(raw)) return defaultBirthdayColumns();
const next = raw.filter((k): k is BirthdayColumnKey => typeof k === "string" && allowed.has(k as BirthdayColumnKey));
return next.length > 0 ? next : defaultBirthdayColumns();
}
@@ -0,0 +1,341 @@
"use client";
import { useEffect, useRef, useState, type ReactNode } from "react";
import { Columns3, RotateCcw } from "lucide-react";
import { fmtDate, maskPhone } from "@/lib/utils";
import { lunarBirthdaySolarThisYear } from "@/lib/lunar";
import { Card } from "@/components/ui/card";
import { Table, THead, TR, TH, TD } from "@/components/ui/table";
import { Button } from "@/components/ui/button";
import {
BIRTHDAY_COLUMNS,
BIRTHDAY_COLUMN_STORAGE_KEY,
defaultBirthdayColumns,
normalizeBirthdayColumns,
type BirthdayColumnKey,
} from "./birthday-columns";
export type BirthdayRow = {
id: number;
customerName: string | null;
customerNo: string | null;
contacts: string | null;
contactsPhone: string | null;
balanceUser: string | null;
balanceUserPhone: string | null;
detail: string | null;
deposit: string | null;
buy: string | null;
salesman: string | null;
reportDate: Date | string | null;
birthday: Date | string | null;
birthdayType: number | null;
remark: string | null;
};
function asDate(v: Date | string | null | undefined): Date | null {
if (!v) return null;
if (v instanceof Date) return v;
const d = new Date(v);
return isNaN(d.getTime()) ? null : d;
}
function text(v?: string | null) {
return v?.trim() ? v : "—";
}
function PersonCell({
name,
phone,
callable,
}: {
name?: string | null;
phone?: string | null;
callable?: boolean;
}) {
const p = phone?.trim() || "";
return (
<div>
<div>{text(name)}</div>
{p ? (
callable ? (
<a
href={`tel:${p}`}
className="text-xs tabular-nums text-[var(--color-primary)] hover:underline"
>
{p}
</a>
) : (
<div className="text-xs tabular-nums text-[var(--color-muted-foreground)]">{p}</div>
)
) : (
<div className="text-xs text-[var(--color-muted-foreground)]">—</div>
)}
</div>
);
}
function BirthdayCell({ row }: { row: BirthdayRow }) {
const birthday = asDate(row.birthday);
const lunar = Number(row.birthdayType) === 2;
const iso = fmtDate(birthday);
const solarThisYear = lunar ? lunarBirthdaySolarThisYear(iso) : null;
if (!birthday) return <span>—</span>;
if (!lunar) return <span className="tabular-nums">{iso}</span>;
return (
<div>
<div className="tabular-nums">农历 {iso.slice(5) || "—"}</div>
<div className="text-xs text-[var(--color-warning)]">
按农历过生日{solarThisYear ? `,今年是 ${solarThisYear}` : ""}
</div>
</div>
);
}
function renderCell(key: BirthdayColumnKey, row: BirthdayRow): ReactNode {
switch (key) {
case "customerName":
return <span className="block break-words font-medium whitespace-normal">{text(row.customerName)}</span>;
case "customerNo":
return <span className="block break-all whitespace-normal">{text(row.customerNo)}</span>;
case "contacts":
return <PersonCell name={row.contacts} phone={row.contactsPhone} />;
case "balanceUser":
return <PersonCell name={row.balanceUser} phone={row.balanceUserPhone} />;
case "birthday":
return <BirthdayCell row={row} />;
case "salesman":
return text(row.salesman);
case "reportDate":
return <span className="tabular-nums">{fmtDate(asDate(row.reportDate)) || "—"}</span>;
case "deposit":
return text(row.deposit);
case "buy":
return text(row.buy);
case "detail":
return <span className="break-words whitespace-normal">{text(row.detail)}</span>;
case "remark":
return <span className="break-words whitespace-normal">{text(row.remark)}</span>;
default:
return "—";
}
}
function ColumnPicker({
visible,
onChange,
}: {
visible: BirthdayColumnKey[];
onChange: (next: BirthdayColumnKey[]) => void;
}) {
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
function onClick(e: Event) {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
}
document.addEventListener("pointerdown", onClick);
return () => document.removeEventListener("pointerdown", onClick);
}, []);
function toggle(key: BirthdayColumnKey) {
if (visible.includes(key)) {
if (visible.length <= 1) return;
onChange(visible.filter((k) => k !== key));
} else {
onChange([...visible, key]);
}
}
return (
<div className="relative" ref={ref}>
<Button type="button" variant="outline" size="sm" onClick={() => setOpen((v) => !v)}>
<Columns3 className="h-4 w-4" />
自定义字段
</Button>
{open && (
<div className="absolute right-0 z-30 mt-2 w-56 overflow-hidden rounded-[var(--radius)] border bg-[var(--color-surface)] py-2 shadow-lg">
<div className="px-3 pb-2 text-xs text-[var(--color-muted-foreground)]">勾选要显示的列</div>
<div className="max-h-72 space-y-0.5 overflow-y-auto px-1">
{BIRTHDAY_COLUMNS.map((col) => {
const checked = visible.includes(col.key);
return (
<label
key={col.key}
className="flex min-h-10 cursor-pointer items-center gap-2.5 rounded-md px-2 hover:bg-[var(--color-muted)]"
>
<input
type="checkbox"
className="h-4 w-4 accent-[var(--color-primary)]"
checked={checked}
onChange={() => toggle(col.key)}
/>
<span className="text-sm">{col.label}</span>
</label>
);
})}
</div>
<div className="mt-2 border-t px-2 pt-2">
<button
type="button"
className="flex min-h-10 w-full items-center gap-2 rounded-md px-2 text-sm text-[var(--color-muted-foreground)] hover:bg-[var(--color-muted)] hover:text-[var(--color-foreground)]"
onClick={() => onChange(defaultBirthdayColumns())}
>
<RotateCcw className="h-3.5 w-3.5" />
恢复默认
</button>
</div>
</div>
)}
</div>
);
}
export function BirthdayList({
rows,
isAdmin,
}: {
rows: BirthdayRow[];
/** 与商户列表一致:仅管理员看明文手机号 */
isAdmin: boolean;
}) {
const [visible, setVisible] = useState<BirthdayColumnKey[]>(defaultBirthdayColumns);
const [ready, setReady] = useState(false);
useEffect(() => {
try {
const raw = localStorage.getItem(BIRTHDAY_COLUMN_STORAGE_KEY);
if (raw) setVisible(normalizeBirthdayColumns(JSON.parse(raw)));
} catch {
// ignore bad local data
}
setReady(true);
}, []);
function save(next: BirthdayColumnKey[]) {
const normalized = normalizeBirthdayColumns(next);
setVisible(normalized);
localStorage.setItem(BIRTHDAY_COLUMN_STORAGE_KEY, JSON.stringify(normalized));
}
const ordered = BIRTHDAY_COLUMNS.filter((c) => visible.includes(c.key));
const colSpan = Math.max(1, ordered.length);
function phoneFor(v?: string | null) {
const value = v?.trim() || "";
if (!value) return "";
return isAdmin ? value : maskPhone(value);
}
return (
<div className="space-y-3">
<div className="flex justify-end">
<ColumnPicker visible={visible} onChange={save} />
</div>
{!ready ? (
<Card className="py-14 text-center text-sm text-[var(--color-muted-foreground)]">加载中…</Card>
) : (
<>
<Card className="hidden overflow-hidden md:block">
<Table striped bordered className="min-w-0 table-fixed">
<THead>
<tr>
{ordered.map((col) => (
<TH key={col.key} className={col.className}>
{col.label}
</TH>
))}
</tr>
</THead>
<tbody>
{rows.length === 0 && (
<tr>
<TD colSpan={colSpan} className="py-14 text-center text-[var(--color-muted-foreground)]">
没有过生日的商户
</TD>
</tr>
)}
{rows.map((row) => (
<TR key={row.id}>
{ordered.map((col) => (
<TD key={col.key} className={col.className}>
{col.key === "contacts" ? (
<PersonCell
name={row.contacts}
phone={phoneFor(row.contactsPhone)}
callable={isAdmin}
/>
) : col.key === "balanceUser" ? (
<PersonCell
name={row.balanceUser}
phone={phoneFor(row.balanceUserPhone)}
callable={isAdmin}
/>
) : (
renderCell(col.key, row)
)}
</TD>
))}
</TR>
))}
</tbody>
</Table>
</Card>
<div className="space-y-3 md:hidden">
{rows.length === 0 && (
<Card className="py-14 text-center text-sm text-[var(--color-muted-foreground)]">
没有过生日的商户
</Card>
)}
{rows.map((row) => (
<Card key={row.id} className="p-4">
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<div className="break-words font-medium whitespace-normal">{text(row.customerName)}</div>
{visible.includes("customerNo") && (
<div className="mt-0.5 break-all text-xs text-[var(--color-muted-foreground)]">
编号 {text(row.customerNo)}
</div>
)}
</div>
{visible.includes("birthday") && (
<div className="shrink-0 text-right text-xs">{renderCell("birthday", row)}</div>
)}
</div>
<dl className="mt-3 grid grid-cols-2 gap-x-3 gap-y-2 text-sm">
{ordered
.filter((c) => c.key !== "customerName" && c.key !== "customerNo" && c.key !== "birthday")
.map((col) => (
<div key={col.key} className={col.key === "detail" || col.key === "remark" ? "col-span-2" : undefined}>
<dt className="text-xs text-[var(--color-muted-foreground)]">{col.label}</dt>
<dd>
{col.key === "contacts" ? (
<PersonCell
name={row.contacts}
phone={phoneFor(row.contactsPhone)}
callable={isAdmin}
/>
) : col.key === "balanceUser" ? (
<PersonCell
name={row.balanceUser}
phone={phoneFor(row.balanceUserPhone)}
callable={isAdmin}
/>
) : (
renderCell(col.key, row)
)}
</dd>
</div>
))}
</dl>
</Card>
))}
</div>
</>
)}
</div>
);
}
+7 -130
View File
@@ -1,31 +1,11 @@
import type { ReactNode } from "react";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import { prisma } from "@/lib/prisma"; import { prisma } from "@/lib/prisma";
import { auth } from "@/lib/auth"; import { auth } from "@/lib/auth";
import { isStaff, isSuperAdmin } from "@/lib/acl"; import { isSuperAdmin } from "@/lib/acl";
import { birthdayMatchLists, lunarBirthdaySolarThisYear } from "@/lib/lunar"; import { birthdayMatchLists } from "@/lib/lunar";
import { fmtDate, maskPhone } from "@/lib/utils";
import { Card } from "@/components/ui/card"; import { Card } from "@/components/ui/card";
import { BirthdayControl } from "./birthday-control"; import { BirthdayControl } from "./birthday-control";
import { BirthdayList, type BirthdayRow } from "./birthday-list";
type Row = {
id: number;
customerName: string | null;
customerNo: string | null;
contacts: string | null;
contactsPhone: string | null;
balanceUser: string | null;
balanceUserPhone: string | null;
phone: string | null; // 旧公共电话,仅作结算人手机号为空时的回退
detail: string | null;
deposit: string | null;
buy: string | null;
salesman: string | null;
reportDate: Date | null;
birthday: Date | null;
birthdayType: number | null;
remark: string | null;
};
function baseDate(s?: string): Date { function baseDate(s?: string): Date {
if (s) { if (s) {
@@ -35,19 +15,13 @@ function baseDate(s?: string): Date {
return new Date(); return new Date();
} }
function showPhone(raw: string | null | undefined, full: boolean) {
const value = raw?.trim() || "";
if (!value) return "—";
return full ? value : maskPhone(value);
}
export default async function BirthdayPage({ export default async function BirthdayPage({
searchParams, searchParams,
}: { }: {
searchParams: Promise<{ mode?: string; date?: string }>; searchParams: Promise<{ mode?: string; date?: string }>;
}) { }) {
const session = await auth(); const session = await auth();
const fullPhone = isSuperAdmin(session?.user?.roles) || isStaff(session?.user?.roles); const isAdmin = isSuperAdmin(session?.user?.roles);
const sp = await searchParams; const sp = await searchParams;
const mode = sp.mode === "week" ? "week" : "today"; const mode = sp.mode === "week" ? "week" : "today";
const dateStr = sp.date ?? ""; const dateStr = sp.date ?? "";
@@ -58,7 +32,7 @@ export default async function BirthdayPage({
? birthdayMatchLists(base, 7, false) ? birthdayMatchLists(base, 7, false)
: birthdayMatchLists(base, 1, true); : birthdayMatchLists(base, 1, true);
const rows = await prisma.$queryRaw<Row[]>(Prisma.sql` const rows = await prisma.$queryRaw<BirthdayRow[]>(Prisma.sql`
SELECT SELECT
id, id,
customer_name AS customerName, customer_name AS customerName,
@@ -66,8 +40,7 @@ export default async function BirthdayPage({
contacts, contacts,
contacts_phone AS contactsPhone, contacts_phone AS contactsPhone,
balance_user AS balanceUser, balance_user AS balanceUser,
balance_user_phone AS balanceUserPhone, COALESCE(NULLIF(balance_user_phone, ''), phone) AS balanceUserPhone,
phone,
detail, detail,
deposit, deposit,
buy, buy,
@@ -94,103 +67,7 @@ export default async function BirthdayPage({
</div> </div>
</Card> </Card>
{rows.length === 0 && ( <BirthdayList rows={rows} isAdmin={isAdmin} />
<Card className="py-14 text-center text-sm text-[var(--color-muted-foreground)]">
没有过生日的商户
</Card>
)}
<div className="space-y-3">
{rows.map((c) => {
const balancePhone = c.balanceUserPhone || c.phone;
const lunar = Number(c.birthdayType) === 2;
const solarThisYear = lunar ? lunarBirthdaySolarThisYear(fmtDate(c.birthday)) : null;
return (
<Card key={c.id} className="p-4 md:p-5">
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<div className="truncate text-base font-semibold">{c.customerName || "—"}</div>
<div className="mt-0.5 break-all text-xs text-[var(--color-muted-foreground)]">
编号 {c.customerNo || "—"}
</div>
</div>
<div className="shrink-0 text-right">
<div className="text-xs text-[var(--color-muted-foreground)]">
{lunar ? "今年是" : "生日"}
</div>
<div className="mt-0.5 text-sm font-medium tabular-nums">
{lunar ? solarThisYear || fmtDate(c.birthday) : fmtDate(c.birthday)}
</div>
</div>
</div>
<div className="mt-4 grid grid-cols-2 gap-x-4 gap-y-3 sm:grid-cols-3">
<Field label="商户名称" value={c.customerName} />
<Field label="商户编号" value={c.customerNo} />
<Field label="业务员" value={c.salesman} />
<Field label="报单日期" value={fmtDate(c.reportDate)} />
<Field label="联系人" value={c.contacts} />
<Field label="联系人手机号">
<Phone value={showPhone(c.contactsPhone, fullPhone)} callable={fullPhone} />
</Field>
<Field label="结算人" value={c.balanceUser} />
<Field label="结算人手机号">
<Phone value={showPhone(balancePhone, fullPhone)} callable={fullPhone} />
</Field>
<Field label="生日">
{lunar ? (
<span>
农历 {fmtDate(c.birthday).slice(5) || "—"}
<span className="mt-0.5 block text-xs text-[var(--color-warning)]">
按农历过生日{solarThisYear ? `,今年是 ${solarThisYear}` : ""}
</span>
</span>
) : (
fmtDate(c.birthday) || "—"
)}
</Field>
<Field label="押金" value={c.deposit} />
<Field label="买断" value={c.buy} />
</div>
<div className="mt-3 space-y-3 border-t pt-3">
<Field label="产品明细" value={c.detail} pre />
<Field label="备注" value={c.remark} pre />
</div>
</Card>
);
})}
</div>
</div> </div>
); );
} }
function Field({
label,
value,
pre,
children,
}: {
label: string;
value?: string | null;
pre?: boolean;
children?: ReactNode;
}) {
return (
<div className="min-w-0">
<div className="text-xs text-[var(--color-muted-foreground)]">{label}</div>
<div className={pre ? "mt-0.5 whitespace-pre-wrap break-words text-sm" : "mt-0.5 break-words text-sm"}>
{children ?? (value?.trim() ? value : "—")}
</div>
</div>
);
}
function Phone({ value, callable }: { value: string; callable: boolean }) {
if (!callable || value === "—") return <span className="tabular-nums">{value}</span>;
return (
<a href={`tel:${value}`} className="tabular-nums text-[var(--color-primary)] hover:underline">
{value}
</a>
);
}