2026-07-08 15:02:03 +08:00
|
|
|
import Link from "next/link";
|
|
|
|
|
import { prisma } from "@/lib/prisma";
|
2026-07-12 00:06:00 +08:00
|
|
|
import { auth } from "@/lib/auth";
|
2026-07-31 17:10:30 +08:00
|
|
|
import { isStaff, isSuperAdmin } from "@/lib/acl";
|
2026-07-12 00:06:00 +08:00
|
|
|
import { fmtDate, maskPhone } from "@/lib/utils";
|
2026-07-08 15:02:03 +08:00
|
|
|
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";
|
2026-07-12 00:06:00 +08:00
|
|
|
import { CustomerView } from "./customer-view";
|
2026-07-08 15:02:03 +08:00
|
|
|
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>;
|
|
|
|
|
}) {
|
2026-07-12 00:06:00 +08:00
|
|
|
const session = await auth();
|
|
|
|
|
const superAdmin = isSuperAdmin(session?.user?.roles);
|
2026-07-31 17:10:30 +08:00
|
|
|
// 查看弹窗:业务员角色也展示明文手机号(列表仍脱敏)
|
|
|
|
|
const viewFullPhone = superAdmin || isStaff(session?.user?.roles);
|
2026-07-08 15:02:03 +08:00
|
|
|
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()}`}
|
2026-07-13 17:48:22 +08:00
|
|
|
canExport={superAdmin}
|
2026-07-08 15:02:03 +08:00
|
|
|
/>
|
|
|
|
|
|
2026-08-29 21:55:10 +08:00
|
|
|
<Card className="hidden overflow-hidden md:block">
|
2026-07-11 23:19:59 +08:00
|
|
|
<Table striped bordered>
|
2026-07-08 15:02:03 +08:00
|
|
|
<THead>
|
|
|
|
|
<tr>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TH>商户名称</TH>
|
2026-09-11 14:55:24 +08:00
|
|
|
<TH>商户编号</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TH>联系人</TH>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TH>结算人</TH>
|
|
|
|
|
<TH>产品明细</TH>
|
|
|
|
|
<TH>押金</TH>
|
|
|
|
|
<TH>买断</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TH>生日</TH>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TH>业务员</TH>
|
|
|
|
|
<TH>报单日期</TH>
|
|
|
|
|
<TH>备注</TH>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TH className="text-right">操作</TH>
|
|
|
|
|
</tr>
|
|
|
|
|
</THead>
|
|
|
|
|
<tbody>
|
|
|
|
|
{rows.length === 0 && (
|
|
|
|
|
<tr>
|
2026-09-11 14:55:24 +08:00
|
|
|
<TD colSpan={12} className="py-14 text-center text-[var(--color-muted-foreground)]">
|
2026-07-08 15:02:03 +08:00
|
|
|
暂无数据
|
|
|
|
|
</TD>
|
|
|
|
|
</tr>
|
|
|
|
|
)}
|
2026-07-19 23:45:20 +08:00
|
|
|
{rows.map((c) => {
|
|
|
|
|
// 旧公共 phone 已停用,不随序列化下发到客户端
|
|
|
|
|
const { phone: _legacyPhone, ...rest } = c;
|
|
|
|
|
return (
|
2026-07-08 15:02:03 +08:00
|
|
|
<TR key={c.id}>
|
|
|
|
|
<TD className="font-medium">{c.customerName}</TD>
|
2026-09-11 14:55:24 +08:00
|
|
|
<TD className="font-medium break-all">{c.customerNo || "—"}</TD>
|
2026-07-19 23:45:20 +08:00
|
|
|
<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>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TD className="max-w-[280px] whitespace-normal break-words">{c.detail}</TD>
|
|
|
|
|
<TD>{c.deposit}</TD>
|
|
|
|
|
<TD>{c.buy}</TD>
|
2026-09-11 14:55:24 +08:00
|
|
|
<TD className="tabular-nums">{fmtDate(c.birthday)}</TD>
|
2026-07-11 23:19:59 +08:00
|
|
|
<TD>{c.salesman}</TD>
|
|
|
|
|
<TD className="tabular-nums">{fmtDate(c.reportDate)}</TD>
|
|
|
|
|
<TD className="max-w-[280px] whitespace-normal break-words">{c.remark}</TD>
|
2026-07-08 15:02:03 +08:00
|
|
|
<TD>
|
2026-08-29 21:55:10 +08:00
|
|
|
<div className="flex flex-wrap justify-end gap-1.5">
|
2026-07-31 17:10:30 +08:00
|
|
|
{/* 弹窗展示用的手机号在服务端预处理:超管和业务员看明文,其余角色脱敏 */}
|
2026-07-19 23:45:20 +08:00
|
|
|
<CustomerView
|
|
|
|
|
customer={{
|
|
|
|
|
...rest,
|
2026-07-31 17:10:30 +08:00
|
|
|
contactsPhone: viewFullPhone ? c.contactsPhone : maskPhone(c.contactsPhone),
|
|
|
|
|
balanceUserPhone: viewFullPhone ? c.balanceUserPhone : maskPhone(c.balanceUserPhone),
|
2026-07-19 23:45:20 +08:00
|
|
|
}}
|
|
|
|
|
/>
|
2026-07-08 15:02:03 +08:00
|
|
|
<Link href={`/customers/${c.id}/edit`}>
|
|
|
|
|
<Button variant="outline" size="sm">编辑</Button>
|
|
|
|
|
</Link>
|
2026-07-12 00:06:00 +08:00
|
|
|
{superAdmin && <DeleteButton id={c.id} name={c.customerName ?? ""} />}
|
2026-07-08 15:02:03 +08:00
|
|
|
</div>
|
|
|
|
|
</TD>
|
|
|
|
|
</TR>
|
2026-07-19 23:45:20 +08:00
|
|
|
);
|
|
|
|
|
})}
|
2026-07-08 15:02:03 +08:00
|
|
|
</tbody>
|
|
|
|
|
</Table>
|
|
|
|
|
</Card>
|
|
|
|
|
|
2026-08-29 21:55:10 +08:00
|
|
|
<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>
|
2026-09-11 14:55:24 +08:00
|
|
|
<div className="mt-0.5 break-all text-xs text-[var(--color-muted-foreground)]">
|
|
|
|
|
编号 {c.customerNo || "—"}
|
|
|
|
|
</div>
|
2026-08-29 21:55:10 +08:00
|
|
|
<div className="mt-1 text-xs text-[var(--color-muted-foreground)]">
|
|
|
|
|
{c.salesman}
|
|
|
|
|
{c.reportDate ? ` · ${fmtDate(c.reportDate)}` : ""}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-09-11 14:55:24 +08:00
|
|
|
{c.birthday ? (
|
|
|
|
|
<span className="shrink-0 text-xs tabular-nums text-[var(--color-muted-foreground)]">
|
|
|
|
|
{fmtDate(c.birthday)}
|
|
|
|
|
</span>
|
|
|
|
|
) : null}
|
2026-08-29 21:55:10 +08:00
|
|
|
</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>
|
|
|
|
|
|
2026-07-08 15:02:03 +08:00
|
|
|
{/* 分页 */}
|
2026-08-29 21:55:10 +08:00
|
|
|
<div className="flex flex-wrap items-center justify-between gap-2 text-sm text-[var(--color-muted-foreground)]">
|
2026-07-08 15:02:03 +08:00
|
|
|
<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 (
|
2026-08-29 21:55:10 +08:00
|
|
|
<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>
|
2026-07-08 15:02:03 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
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()}`}
|
2026-08-29 21:55:10 +08:00
|
|
|
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"
|
2026-07-08 15:02:03 +08:00
|
|
|
>
|
|
|
|
|
{label}
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
}
|