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:
@@ -0,0 +1,44 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { auth } from "@/lib/auth";
|
||||
|
||||
export type MeState = { ok?: boolean; error?: string };
|
||||
|
||||
/** 修改昵称 */
|
||||
export async function updateProfile(_prev: MeState, formData: FormData): Promise<MeState> {
|
||||
const session = await auth();
|
||||
if (!session?.user) return { error: "未登录" };
|
||||
const nickname = String(formData.get("nickname") ?? "").trim();
|
||||
if (!nickname) return { error: "昵称不能为空" };
|
||||
await prisma.admin.update({
|
||||
where: { id: Number(session.user.id) },
|
||||
data: { nickname },
|
||||
});
|
||||
revalidatePath("/me");
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
/** 修改密码:校验原密码 */
|
||||
export async function changePassword(_prev: MeState, formData: FormData): Promise<MeState> {
|
||||
const session = await auth();
|
||||
if (!session?.user) return { error: "未登录" };
|
||||
const oldPw = String(formData.get("oldPassword") ?? "");
|
||||
const newPw = String(formData.get("newPassword") ?? "");
|
||||
const confirm = String(formData.get("confirmPassword") ?? "");
|
||||
|
||||
if (newPw.length < 6) return { error: "新密码至少 6 位" };
|
||||
if (newPw !== confirm) return { error: "两次输入的新密码不一致" };
|
||||
|
||||
const admin = await prisma.admin.findUnique({ where: { id: Number(session.user.id) } });
|
||||
if (!admin) return { error: "账号不存在" };
|
||||
if (!bcrypt.compareSync(oldPw, admin.password)) return { error: "原密码错误" };
|
||||
|
||||
await prisma.admin.update({
|
||||
where: { id: admin.id },
|
||||
data: { password: bcrypt.hashSync(newPw, 10) },
|
||||
});
|
||||
return { ok: true };
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
"use client";
|
||||
|
||||
import { useActionState, useEffect } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { updateProfile, changePassword, type MeState } from "./actions";
|
||||
|
||||
function useToastOnDone(state: MeState, okMsg: string) {
|
||||
useEffect(() => {
|
||||
if (state.ok) toast.success(okMsg);
|
||||
else if (state.error) toast.error(state.error);
|
||||
}, [state, okMsg]);
|
||||
}
|
||||
|
||||
export function ProfileForm({ nickname, username }: { nickname: string; username: string }) {
|
||||
const [state, action, pending] = useActionState(updateProfile, {});
|
||||
useToastOnDone(state, "资料已更新");
|
||||
return (
|
||||
<form action={action} className="max-w-md space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label>用户名</Label>
|
||||
<Input value={username} disabled />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="nickname">昵称</Label>
|
||||
<Input id="nickname" name="nickname" defaultValue={nickname} />
|
||||
</div>
|
||||
<Button type="submit" disabled={pending}>{pending ? "保存中…" : "保存资料"}</Button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
export function PasswordForm() {
|
||||
const [state, action, pending] = useActionState(changePassword, {});
|
||||
useToastOnDone(state, "密码已修改");
|
||||
return (
|
||||
<form action={action} className="max-w-md space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="oldPassword">原密码</Label>
|
||||
<Input id="oldPassword" name="oldPassword" type="password" autoComplete="current-password" />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="newPassword">新密码</Label>
|
||||
<Input id="newPassword" name="newPassword" type="password" autoComplete="new-password" placeholder="至少 6 位" />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="confirmPassword">确认新密码</Label>
|
||||
<Input id="confirmPassword" name="confirmPassword" type="password" autoComplete="new-password" />
|
||||
</div>
|
||||
<Button type="submit" disabled={pending}>{pending ? "提交中…" : "修改密码"}</Button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { Avatar } from "@/components/ui/avatar";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { ProfileForm, PasswordForm } from "./me-forms";
|
||||
|
||||
export default async function MePage() {
|
||||
const session = await auth();
|
||||
if (!session?.user) redirect("/login");
|
||||
const admin = await prisma.admin.findUnique({ where: { id: Number(session.user.id) } });
|
||||
if (!admin) redirect("/login");
|
||||
|
||||
const roleLabel = session.user.roles.includes("super admin") ? "超级管理员" : "业务员";
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-3xl space-y-4">
|
||||
<Card>
|
||||
<CardContent className="flex items-center gap-4">
|
||||
<Avatar name={admin.nickname} size={56} />
|
||||
<div>
|
||||
<div className="text-lg font-semibold">{admin.nickname}</div>
|
||||
<div className="mt-1 flex items-center gap-2 text-sm text-[var(--color-muted-foreground)]">
|
||||
<span className="tabular-nums">{admin.username}</span>
|
||||
<Badge tone="primary">{roleLabel}</Badge>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<Card>
|
||||
<CardHeader><CardTitle>基本资料</CardTitle></CardHeader>
|
||||
<CardContent>
|
||||
<ProfileForm nickname={admin.nickname} username={admin.username} />
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardHeader><CardTitle>修改密码</CardTitle></CardHeader>
|
||||
<CardContent>
|
||||
<PasswordForm />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user