Files
liandong-crm/src/app/(dashboard)/customers/page.tsx
T

248 lines
9.4 KiB
TypeScript
Raw Normal View History

import Link from "next/link";
import { prisma } from "@/lib/prisma";
import { auth } from "@/lib/auth";
import { isStaff, isSuperAdmin } from "@/lib/acl";
import { fmtDate, maskPhone } from "@/lib/utils";
import { Card } from "@/components/ui/card";
import { Table, THead, TR, TH, TD } from "@/components/ui/table";
import { Button } from "@/components/ui/button";
import { CustomerToolbar } from "./customer-toolbar";
import { DeleteButton } from "./delete-button";
import { CustomerView } from "./customer-view";
import { buildCustomerWhere } from "./query";
const PAGE_SIZE = 15;
type SearchParams = {
keyword?: string;
start?: string;
end?: string;
page?: string;
};
export default async function CustomersPage({
searchParams,
}: {
searchParams: Promise<SearchParams>;
}) {
const session = await auth();
const superAdmin = isSuperAdmin(session?.user?.roles);
// 查看弹窗:业务员角色也展示明文手机号(列表仍脱敏)
const viewFullPhone = superAdmin || isStaff(session?.user?.roles);
const sp = await searchParams;
const keyword = (sp.keyword ?? "").trim();
const page = Math.max(1, Number(sp.page) || 1);
const where = buildCustomerWhere(sp);
const [rows, total] = await Promise.all([
prisma.customer.findMany({
where,
orderBy: { reportDate: "desc" },
skip: (page - 1) * PAGE_SIZE,
take: PAGE_SIZE,
}),
prisma.customer.count({ where }),
]);
const totalPages = Math.max(1, Math.ceil(total / PAGE_SIZE));
const exportQs = new URLSearchParams();
if (sp.keyword) exportQs.set("keyword", sp.keyword);
if (sp.start) exportQs.set("start", sp.start);
if (sp.end) exportQs.set("end", sp.end);
return (
<div className="space-y-4">
<CustomerToolbar
defaultKeyword={keyword}
defaultStart={sp.start}
defaultEnd={sp.end}
exportHref={`/customers/export?${exportQs.toString()}`}
canExport={superAdmin}
/>
<Card className="hidden overflow-hidden md:block">
<Table striped bordered>
<THead>
<tr>
<TH>商户名称</TH>
<TH>商户编号</TH>
<TH>联系人</TH>
<TH>结算人</TH>
<TH>产品明细</TH>
<TH>押金</TH>
<TH>买断</TH>
<TH>生日</TH>
<TH>业务员</TH>
<TH>报单日期</TH>
<TH>备注</TH>
<TH className="text-right">操作</TH>
</tr>
</THead>
<tbody>
{rows.length === 0 && (
<tr>
<TD colSpan={12} className="py-14 text-center text-[var(--color-muted-foreground)]">
暂无数据
</TD>
</tr>
)}
{rows.map((c) => {
// 旧公共 phone 已停用,不随序列化下发到客户端
const { phone: _legacyPhone, ...rest } = c;
return (
<TR key={c.id}>
<TD className="font-medium">{c.customerName}</TD>
<TD className="font-medium break-all">{c.customerNo || "—"}</TD>
<TD>
<div>{c.contacts}</div>
<div className="text-xs tabular-nums text-[var(--color-muted-foreground)]">
{superAdmin ? c.contactsPhone : maskPhone(c.contactsPhone)}
</div>
</TD>
<TD>
<div>{c.balanceUser}</div>
<div className="text-xs tabular-nums text-[var(--color-muted-foreground)]">
{superAdmin ? c.balanceUserPhone : maskPhone(c.balanceUserPhone)}
</div>
</TD>
<TD className="max-w-[280px] whitespace-normal break-words">{c.detail}</TD>
<TD>{c.deposit}</TD>
<TD>{c.buy}</TD>
<TD className="tabular-nums">{fmtDate(c.birthday)}</TD>
<TD>{c.salesman}</TD>
<TD className="tabular-nums">{fmtDate(c.reportDate)}</TD>
<TD className="max-w-[280px] whitespace-normal break-words">{c.remark}</TD>
<TD>
<div className="flex flex-wrap justify-end gap-1.5">
{/* 弹窗展示用的手机号在服务端预处理:超管和业务员看明文,其余角色脱敏 */}
<CustomerView
customer={{
...rest,
contactsPhone: viewFullPhone ? c.contactsPhone : maskPhone(c.contactsPhone),
balanceUserPhone: viewFullPhone ? c.balanceUserPhone : maskPhone(c.balanceUserPhone),
}}
/>
<Link href={`/customers/${c.id}/edit`}>
<Button variant="outline" size="sm">编辑</Button>
</Link>
{superAdmin && <DeleteButton id={c.id} name={c.customerName ?? ""} />}
</div>
</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((c) => {
const { phone: _legacyPhone, ...rest } = c;
return (
<Card key={c.id} className="p-4">
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<div className="truncate font-medium">{c.customerName}</div>
<div className="mt-0.5 break-all text-xs text-[var(--color-muted-foreground)]">
编号 {c.customerNo || "—"}
</div>
<div className="mt-1 text-xs text-[var(--color-muted-foreground)]">
{c.salesman}
{c.reportDate ? ` · ${fmtDate(c.reportDate)}` : ""}
</div>
</div>
{c.birthday ? (
<span className="shrink-0 text-xs tabular-nums text-[var(--color-muted-foreground)]">
{fmtDate(c.birthday)}
</span>
) : null}
</div>
<dl className="mt-3 grid grid-cols-2 gap-x-3 gap-y-2 text-sm">
<div>
<dt className="text-xs text-[var(--color-muted-foreground)]">联系人</dt>
<dd>{c.contacts || "—"}</dd>
<dd className="text-xs tabular-nums text-[var(--color-muted-foreground)]">
{superAdmin ? c.contactsPhone : maskPhone(c.contactsPhone)}
</dd>
</div>
<div>
<dt className="text-xs text-[var(--color-muted-foreground)]">结算人</dt>
<dd>{c.balanceUser || "—"}</dd>
<dd className="text-xs tabular-nums text-[var(--color-muted-foreground)]">
{superAdmin ? c.balanceUserPhone : maskPhone(c.balanceUserPhone)}
</dd>
</div>
{c.detail && (
<div className="col-span-2">
<dt className="text-xs text-[var(--color-muted-foreground)]">产品明细</dt>
<dd className="break-words">{c.detail}</dd>
</div>
)}
</dl>
<div className="mt-3 flex flex-wrap gap-1.5 border-t pt-3">
<CustomerView
customer={{
...rest,
contactsPhone: viewFullPhone ? c.contactsPhone : maskPhone(c.contactsPhone),
balanceUserPhone: viewFullPhone ? c.balanceUserPhone : maskPhone(c.balanceUserPhone),
}}
/>
<Link href={`/customers/${c.id}/edit`}>
<Button variant="outline" size="sm">编辑</Button>
</Link>
{superAdmin && <DeleteButton id={c.id} name={c.customerName ?? ""} />}
</div>
</Card>
);
})}
</div>
{/* 分页 */}
<div className="flex flex-wrap items-center justify-between gap-2 text-sm text-[var(--color-muted-foreground)]">
<span>共 <span className="font-medium text-[var(--color-foreground)]">{total.toLocaleString()}</span> 条</span>
<div className="flex items-center gap-2">
<PageLink page={page - 1} disabled={page <= 1} sp={sp} label="上一页" />
<span className="px-1 tabular-nums">{page} / {totalPages}</span>
<PageLink page={page + 1} disabled={page >= totalPages} sp={sp} label="下一页" />
</div>
</div>
</div>
);
}
function PageLink({
page,
disabled,
sp,
label,
}: {
page: number;
disabled: boolean;
sp: SearchParams;
label: string;
}) {
if (disabled) {
return (
<span className="inline-flex min-h-11 items-center rounded-md border px-3 opacity-40 md:min-h-0 md:py-1.5">{label}</span>
);
}
const params = new URLSearchParams();
if (sp.keyword) params.set("keyword", sp.keyword);
if (sp.start) params.set("start", sp.start);
if (sp.end) params.set("end", sp.end);
params.set("page", String(page));
return (
<Link
href={`/customers?${params.toString()}`}
className="inline-flex min-h-11 items-center rounded-md border bg-[var(--color-surface)] px-3 hover:bg-[var(--color-muted)] md:min-h-0 md:py-1.5"
>
{label}
</Link>
);
}