feat: 联动支付 CRM 后台(Next.js + TS + Prisma 重构)

从旧 Laravel 5.7 重构为现代技术栈,对接现有 MySQL。

- 技术栈: Next.js 15 App Router + TypeScript + Prisma + Auth.js + Tailwind v4
- 认证: 复用旧库 admins 的 bcrypt 密码,Auth.js Credentials
- 客户管理: 列表(搜索/报单区间/分页)、增改删、Excel 导入导出
- 生日提醒: 阴阳历(lunar-typescript),口径对齐旧 overtrue 库
- RBAC: 管理员/角色/权限(菜单树) 完整 CRUD + 分配
- 控制台: 统计卡 + 月度新增报单柱状图
- 个人中心: 改昵称/改密码
- 现代 CMS 界面 + 联动支付品牌(logo/登录/底部/icon)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-08 15:02:03 +08:00
co-authored by Claude Opus 4.8
commit 5d46afe439
68 changed files with 6998 additions and 0 deletions
@@ -0,0 +1,29 @@
import { notFound } from "next/navigation";
import { prisma } from "@/lib/prisma";
import { saveRole } from "../../../actions";
import { RoleForm } from "../../role-form";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
export default async function EditRolePage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const role = await prisma.role.findUnique({ where: { id: Number(id) } });
if (!role) notFound();
const action = saveRole.bind(null, role.id);
return (
<div className="mx-auto max-w-2xl">
<Card>
<CardHeader><CardTitle>编辑角色 · {role.title || role.name}</CardTitle></CardHeader>
<CardContent>
<RoleForm
action={action}
defaultValues={{ name: role.name, title: role.title ?? "", remark: role.remark ?? "" }}
/>
</CardContent>
</Card>
</div>
);
}
@@ -0,0 +1,84 @@
"use client";
import { useState, useTransition } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { assignPermissions } from "../../../actions";
export type PermNode = {
id: number;
title: string;
name: string;
children: PermNode[];
};
export function AssignPermissions({
roleId,
tree,
checkedIds,
}: {
roleId: number;
tree: PermNode[];
checkedIds: number[];
}) {
const router = useRouter();
const [checked, setChecked] = useState<Set<number>>(new Set(checkedIds));
const [pending, start] = useTransition();
function toggle(node: PermNode, on: boolean) {
const next = new Set(checked);
const walk = (n: PermNode) => {
if (on) next.add(n.id);
else next.delete(n.id);
n.children.forEach(walk);
};
walk(node);
setChecked(next);
}
function save() {
start(async () => {
const res = await assignPermissions(roleId, [...checked]);
if (res.ok) {
toast.success("权限已保存");
router.push("/rbac/roles");
router.refresh();
} else toast.error(res.error ?? "保存失败");
});
}
const renderNode = (node: PermNode, depth: number) => (
<div key={node.id}>
<label
className="flex items-center gap-2.5 rounded-md px-2 py-1.5 hover:bg-[var(--color-muted)]"
style={{ paddingLeft: depth * 20 + 8 }}
>
<input
type="checkbox"
className="h-4 w-4 accent-[var(--color-primary)]"
checked={checked.has(node.id)}
onChange={(e) => toggle(node, e.target.checked)}
/>
<span className="text-sm">{node.title}</span>
<span className="text-xs text-[var(--color-muted-foreground)]">{node.name}</span>
</label>
{node.children.map((c) => renderNode(c, depth + 1))}
</div>
);
return (
<div className="space-y-4">
<div className="rounded-[var(--radius)] border p-2">
{tree.map((n) => renderNode(n, 0))}
</div>
<div className="flex gap-2">
<Button onClick={save} disabled={pending}>
{pending ? "保存中…" : `保存(已选 ${checked.size} 项)`}
</Button>
<Link href="/rbac/roles"><Button variant="outline">取消</Button></Link>
</div>
</div>
);
}
@@ -0,0 +1,50 @@
import { notFound } from "next/navigation";
import { prisma } from "@/lib/prisma";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { AssignPermissions, type PermNode } from "./assign-permissions";
function buildTree(
perms: { id: number; pid: number | null; title: string | null; name: string }[]
): PermNode[] {
const byId = new Map<number, PermNode>();
perms.forEach((p) =>
byId.set(p.id, { id: p.id, title: p.title || p.name, name: p.name, children: [] })
);
const roots: PermNode[] = [];
perms.forEach((p) => {
const node = byId.get(p.id)!;
const parent = p.pid ? byId.get(p.pid) : undefined;
if (parent) parent.children.push(node);
else roots.push(node);
});
return roots;
}
export default async function AssignPermissionsPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const roleId = Number(id);
const [role, perms, owned] = await Promise.all([
prisma.role.findUnique({ where: { id: roleId } }),
prisma.permission.findMany({ orderBy: [{ sort: "asc" }, { id: "asc" }] }),
prisma.roleHasPermission.findMany({ where: { roleId } }),
]);
if (!role) notFound();
const tree = buildTree(perms);
const checkedIds = owned.map((o) => o.permissionId);
return (
<div className="mx-auto max-w-2xl">
<Card>
<CardHeader><CardTitle>分配权限 · {role.title || role.name}</CardTitle></CardHeader>
<CardContent>
<AssignPermissions roleId={roleId} tree={tree} checkedIds={checkedIds} />
</CardContent>
</Card>
</div>
);
}
@@ -0,0 +1,15 @@
import { saveRole } from "../../actions";
import { RoleForm } from "../role-form";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
export default function NewRolePage() {
const action = saveRole.bind(null, null);
return (
<div className="mx-auto max-w-2xl">
<Card>
<CardHeader><CardTitle>新增角色</CardTitle></CardHeader>
<CardContent><RoleForm action={action} /></CardContent>
</Card>
</div>
);
}
+67
View File
@@ -0,0 +1,67 @@
import Link from "next/link";
import { Plus, ShieldCheck } from "lucide-react";
import { prisma } from "@/lib/prisma";
import { Card } from "@/components/ui/card";
import { Table, THead, TR, TH, TD } from "@/components/ui/table";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { deleteRole } from "../actions";
import { ConfirmDelete } from "../confirm-delete";
export default async function RolesPage() {
const roles = await prisma.role.findMany({ orderBy: { id: "asc" } });
const perms = await prisma.roleHasPermission.groupBy({
by: ["roleId"],
_count: { permissionId: true },
});
const countMap = new Map(perms.map((p) => [p.roleId, p._count.permissionId]));
return (
<div className="space-y-4">
<div className="flex justify-end">
<Link href="/rbac/roles/new">
<Button><Plus className="h-4 w-4" />新增角色</Button>
</Link>
</div>
<Card className="overflow-hidden">
<Table>
<THead>
<tr>
<TH>角色名称</TH>
<TH>标识</TH>
<TH>权限数</TH>
<TH>备注</TH>
<TH className="text-right">操作</TH>
</tr>
</THead>
<tbody>
{roles.map((r) => (
<TR key={r.id}>
<TD className="font-medium">
<span className="inline-flex items-center gap-2">
<ShieldCheck className="h-4 w-4 text-[var(--color-primary)]" />
{r.title || r.name}
</span>
</TD>
<TD className="text-[var(--color-muted-foreground)]">{r.name}</TD>
<TD><Badge tone="info">{countMap.get(r.id) ?? 0}</Badge></TD>
<TD className="text-[var(--color-muted-foreground)]">{r.remark}</TD>
<TD>
<div className="flex justify-end gap-1.5">
<Link href={`/rbac/roles/${r.id}/permissions`}>
<Button variant="soft" size="sm">分配权限</Button>
</Link>
<Link href={`/rbac/roles/${r.id}/edit`}>
<Button variant="outline" size="sm">编辑</Button>
</Link>
<ConfirmDelete action={deleteRole.bind(null, r.id)} name={r.title || r.name} disabled={r.id === 1} />
</div>
</TD>
</TR>
))}
</tbody>
</Table>
</Card>
</div>
);
}
@@ -0,0 +1,44 @@
"use client";
import { useActionState } from "react";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { Input, Textarea } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import type { FormState } from "../actions";
export function RoleForm({
action,
defaultValues,
}: {
action: (prev: FormState, formData: FormData) => Promise<FormState>;
defaultValues?: { name?: string; title?: string; remark?: string };
}) {
const [state, formAction, pending] = useActionState(action, {});
const v = defaultValues ?? {};
return (
<form action={formAction} className="max-w-lg space-y-4">
<div className="space-y-1.5">
<Label htmlFor="title">角色名称<span className="ml-0.5 text-[var(--color-danger)]">*</span></Label>
<Input id="title" name="title" defaultValue={v.title} placeholder="如:业务员" />
</div>
<div className="space-y-1.5">
<Label htmlFor="name">角色标识<span className="ml-0.5 text-[var(--color-danger)]">*</span></Label>
<Input id="name" name="name" defaultValue={v.name} placeholder="如:staff(英文唯一标识)" />
</div>
<div className="space-y-1.5">
<Label htmlFor="remark">备注</Label>
<Textarea id="remark" name="remark" defaultValue={v.remark} rows={2} />
</div>
{state.error && (
<p className="rounded-md bg-[var(--color-danger-soft)] px-3 py-2 text-sm text-[var(--color-danger)]">
{state.error}
</p>
)}
<div className="flex gap-2 border-t pt-4">
<Button type="submit" disabled={pending}>{pending ? "保存中…" : "保存"}</Button>
<Link href="/rbac/roles"><Button type="button" variant="outline">取消</Button></Link>
</div>
</form>
);
}