feat: 业务员角色权限限制 + 客户查看弹窗
- 新增 acl.ts 工具:isSuperAdmin/SUPER_ADMIN;utils.ts 加 maskPhone - 侧边栏按角色过滤:系统管理菜单仅超管可见 - /rbac 路由服务端拦截:非超管访问重定向首页 - 客户列表:业务员手机号脱敏(中间5位星号)、删除按钮隐藏、新增查看按钮 - 删除服务端校验:仅超管可执行 - 新增 customer-view.tsx 查看弹窗,展示完整商户信息(含完整手机号) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import * as XLSX from "xlsx";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { isSuperAdmin } from "@/lib/acl";
|
||||
import { parseDate } from "@/lib/utils";
|
||||
|
||||
// 校验规则对齐老系统必填项(customer_name/contacts/balance_user/detail/salesman/report_date)
|
||||
@@ -105,7 +106,8 @@ export async function updateCustomer(
|
||||
}
|
||||
|
||||
export async function deleteCustomer(id: number): Promise<void> {
|
||||
if (!(await auth())) return;
|
||||
const session = await auth();
|
||||
if (!isSuperAdmin(session?.user?.roles)) return;
|
||||
// 软删除,对齐老系统 deleted_at
|
||||
await prisma.customer.update({
|
||||
where: { id },
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { Eye, X } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { fmtDate } from "@/lib/utils";
|
||||
|
||||
type CustomerViewProps = {
|
||||
customer: {
|
||||
customerName?: string | null;
|
||||
contacts?: string | null;
|
||||
balanceUser?: string | null;
|
||||
phone?: string | null;
|
||||
detail?: string | null;
|
||||
deposit?: string | null;
|
||||
buy?: string | null;
|
||||
birthdayType?: number | null;
|
||||
birthday?: Date | null;
|
||||
salesman?: string | null;
|
||||
reportDate?: Date | null;
|
||||
remark?: string | null;
|
||||
};
|
||||
};
|
||||
|
||||
export function CustomerView({ customer }: CustomerViewProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
// Esc 键关闭弹窗
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
const handleEsc = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") setOpen(false);
|
||||
};
|
||||
window.addEventListener("keydown", handleEsc);
|
||||
return () => window.removeEventListener("keydown", handleEsc);
|
||||
}, [open]);
|
||||
|
||||
const birthdayLabel = customer.birthdayType === 2 ? "阴历" : "阳历";
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button variant="outline" size="sm" onClick={() => setOpen(true)}>
|
||||
<Eye className="h-3.5 w-3.5" />
|
||||
<span>查看</span>
|
||||
</Button>
|
||||
|
||||
{open && (
|
||||
<div
|
||||
className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
<Card
|
||||
className="w-full max-w-lg max-h-[85vh] overflow-y-auto"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<CardTitle>商户信息</CardTitle>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen(false)}
|
||||
className="text-[var(--color-muted-foreground)] hover:text-[var(--color-foreground)] transition-colors"
|
||||
aria-label="关闭"
|
||||
>
|
||||
<X className="h-5 w-5" />
|
||||
</button>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<InfoRow label="商户名称" value={customer.customerName} />
|
||||
<InfoRow label="联系人" value={customer.contacts} />
|
||||
<InfoRow label="结算人" value={customer.balanceUser} />
|
||||
<InfoRow label="联系电话" value={customer.phone} />
|
||||
<InfoRow label="产品明细" value={customer.detail} className="whitespace-pre-wrap" />
|
||||
<InfoRow label="押金" value={customer.deposit} />
|
||||
<InfoRow label="买断" value={customer.buy} />
|
||||
<InfoRow label="生日类型" value={customer.birthday ? birthdayLabel : undefined} />
|
||||
<InfoRow label="生日" value={fmtDate(customer.birthday)} />
|
||||
<InfoRow label="业务员" value={customer.salesman} />
|
||||
<InfoRow label="报单日期" value={fmtDate(customer.reportDate)} />
|
||||
<InfoRow label="备注" value={customer.remark} className="whitespace-pre-wrap" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function InfoRow({
|
||||
label,
|
||||
value,
|
||||
className,
|
||||
}: {
|
||||
label: string;
|
||||
value?: string | null;
|
||||
className?: string;
|
||||
}) {
|
||||
if (!value) return null;
|
||||
return (
|
||||
<div className="flex gap-3 text-sm">
|
||||
<span className="w-24 text-[var(--color-muted-foreground)] flex-shrink-0">{label}</span>
|
||||
<span className={className}>{value}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
import Link from "next/link";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { fmtDate } from "@/lib/utils";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { 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 { Badge } from "@/components/ui/badge";
|
||||
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;
|
||||
@@ -23,6 +26,8 @@ export default async function CustomersPage({
|
||||
}: {
|
||||
searchParams: Promise<SearchParams>;
|
||||
}) {
|
||||
const session = await auth();
|
||||
const superAdmin = isSuperAdmin(session?.user?.roles);
|
||||
const sp = await searchParams;
|
||||
const keyword = (sp.keyword ?? "").trim();
|
||||
const page = Math.max(1, Number(sp.page) || 1);
|
||||
@@ -84,7 +89,7 @@ export default async function CustomersPage({
|
||||
<TD className="font-medium">{c.customerName}</TD>
|
||||
<TD>{c.contacts}</TD>
|
||||
<TD>{c.balanceUser}</TD>
|
||||
<TD className="tabular-nums">{c.phone}</TD>
|
||||
<TD className="tabular-nums">{superAdmin ? c.phone : maskPhone(c.phone)}</TD>
|
||||
<TD className="max-w-[280px] whitespace-normal break-words">{c.detail}</TD>
|
||||
<TD>{c.deposit}</TD>
|
||||
<TD>{c.buy}</TD>
|
||||
@@ -101,10 +106,11 @@ export default async function CustomersPage({
|
||||
<TD className="max-w-[280px] whitespace-normal break-words">{c.remark}</TD>
|
||||
<TD>
|
||||
<div className="flex justify-end gap-1.5">
|
||||
<CustomerView customer={c} />
|
||||
<Link href={`/customers/${c.id}/edit`}>
|
||||
<Button variant="outline" size="sm">编辑</Button>
|
||||
</Link>
|
||||
<DeleteButton id={c.id} name={c.customerName ?? ""} />
|
||||
{superAdmin && <DeleteButton id={c.id} name={c.customerName ?? ""} />}
|
||||
</div>
|
||||
</TD>
|
||||
</TR>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { isSuperAdmin } from "@/lib/acl";
|
||||
import { Sidebar } from "@/components/app/sidebar";
|
||||
import { Topbar } from "@/components/app/topbar";
|
||||
import { SidebarProvider } from "@/components/app/sidebar-context";
|
||||
@@ -13,11 +14,12 @@ export default async function DashboardLayout({
|
||||
const session = await auth();
|
||||
if (!session?.user) redirect("/login");
|
||||
const u = session.user;
|
||||
const superAdmin = isSuperAdmin(u.roles);
|
||||
|
||||
return (
|
||||
<SidebarProvider>
|
||||
<div className="flex h-screen overflow-hidden">
|
||||
<Sidebar />
|
||||
<Sidebar isSuperAdmin={superAdmin} />
|
||||
<div className="flex min-w-0 flex-1 flex-col">
|
||||
<Topbar
|
||||
user={{
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { isSuperAdmin } from "@/lib/acl";
|
||||
|
||||
/** RBAC 路由守卫:仅超级管理员可访问 /rbac/* */
|
||||
export default async function RbacLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const session = await auth();
|
||||
if (!session?.user) redirect("/login");
|
||||
if (!isSuperAdmin(session.user.roles)) redirect("/");
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
@@ -17,6 +17,8 @@ export type NavItem = {
|
||||
export type NavSection = {
|
||||
title: string;
|
||||
items: NavItem[];
|
||||
/** 是否仅超级管理员可见 */
|
||||
superAdminOnly?: boolean;
|
||||
};
|
||||
|
||||
export const navSections: NavSection[] = [
|
||||
@@ -35,6 +37,7 @@ export const navSections: NavSection[] = [
|
||||
{ href: "/rbac/roles", label: "角色", icon: ShieldCheck },
|
||||
{ href: "/rbac/permissions", label: "权限菜单", icon: KeyRound },
|
||||
],
|
||||
superAdminOnly: true,
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
@@ -8,10 +8,11 @@ import { BrandLogo } from "./brand-logo";
|
||||
import { navSections, activeHref } from "./nav";
|
||||
import { useSidebar } from "./sidebar-context";
|
||||
|
||||
export function Sidebar() {
|
||||
export function Sidebar({ isSuperAdmin }: { isSuperAdmin: boolean }) {
|
||||
const pathname = usePathname();
|
||||
const active = activeHref(pathname);
|
||||
const { collapsed } = useSidebar();
|
||||
const sections = navSections.filter((s) => !s.superAdminOnly || isSuperAdmin);
|
||||
|
||||
return (
|
||||
<aside
|
||||
@@ -30,7 +31,7 @@ export function Sidebar() {
|
||||
</div>
|
||||
|
||||
<nav className="flex-1 space-y-6 overflow-y-auto px-3 py-4">
|
||||
{navSections.map((section) => (
|
||||
{sections.map((section) => (
|
||||
<div key={section.title}>
|
||||
<div className="px-3 pb-2 text-[11px] font-medium uppercase tracking-wider text-[var(--color-sidebar-muted)]">
|
||||
{section.title}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import type { Session } from "next-auth";
|
||||
|
||||
/** 超级管理员角色名称(与数据库 Role.name 对应) */
|
||||
export const SUPER_ADMIN = "super admin";
|
||||
|
||||
/** 判断是否为超级管理员(基于 session.user.roles) */
|
||||
export function isSuperAdmin(roles?: string[] | null): boolean {
|
||||
return !!roles?.includes(SUPER_ADMIN);
|
||||
}
|
||||
|
||||
/** 从 Session 提取 roles 并判断(便捷版) */
|
||||
export function sessionIsSuperAdmin(session: Session | null): boolean {
|
||||
return isSuperAdmin(session?.user?.roles);
|
||||
}
|
||||
@@ -19,3 +19,11 @@ export function parseDate(s: string | null | undefined): Date | null {
|
||||
const d = new Date(`${s.trim()}T00:00:00.000Z`);
|
||||
return isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
/** 手机号脱敏:11 位 → 前3+5星+后3(隐藏中间五位);其它长度保留前后各 3 位 */
|
||||
export function maskPhone(phone: string | null | undefined): string {
|
||||
const s = (phone ?? "").trim();
|
||||
if (/^\d{11}$/.test(s)) return s.slice(0, 3) + "*****" + s.slice(8);
|
||||
if (s.length > 6) return s.slice(0, 3) + "*".repeat(s.length - 6) + s.slice(-3);
|
||||
return s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user