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,42 @@
|
||||
import type { NextAuthConfig, DefaultSession } from "next-auth";
|
||||
|
||||
// Edge 安全的基础配置:不引入 Prisma / bcrypt,供 middleware 使用。
|
||||
// 真正带数据库校验的 Credentials provider 在 auth.ts 里注入。
|
||||
|
||||
declare module "next-auth" {
|
||||
interface Session {
|
||||
user: {
|
||||
id: string;
|
||||
username: string;
|
||||
roles: string[];
|
||||
permissions: string[];
|
||||
} & DefaultSession["user"];
|
||||
}
|
||||
}
|
||||
|
||||
export const authConfig = {
|
||||
session: { strategy: "jwt" },
|
||||
pages: { signIn: "/login" },
|
||||
trustHost: true,
|
||||
providers: [], // 在 auth.ts 中补充
|
||||
callbacks: {
|
||||
jwt({ token, user }) {
|
||||
if (user) {
|
||||
token.uid = (user as { id: string }).id;
|
||||
token.username = (user as { username: string }).username;
|
||||
token.roles = (user as { roles?: string[] }).roles ?? [];
|
||||
token.permissions = (user as { permissions?: string[] }).permissions ?? [];
|
||||
}
|
||||
return token;
|
||||
},
|
||||
session({ session, token }) {
|
||||
if (session.user) {
|
||||
session.user.id = token.uid as string;
|
||||
session.user.username = token.username as string;
|
||||
session.user.roles = (token.roles as string[]) ?? [];
|
||||
session.user.permissions = (token.permissions as string[]) ?? [];
|
||||
}
|
||||
return session;
|
||||
},
|
||||
},
|
||||
} satisfies NextAuthConfig;
|
||||
@@ -0,0 +1,73 @@
|
||||
import NextAuth from "next-auth";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { authConfig } from "@/lib/auth.config";
|
||||
|
||||
/**
|
||||
* 读取某个后台账号的角色名与权限码(走现有 spatie 关联表)。
|
||||
*/
|
||||
async function loadAcl(adminId: number) {
|
||||
const modelRoles = await prisma.modelHasRole.findMany({
|
||||
where: { modelId: adminId, modelType: "App\\Model\\Admin" },
|
||||
select: { roleId: true },
|
||||
});
|
||||
const roleIds = modelRoles.map((r) => r.roleId);
|
||||
if (roleIds.length === 0) return { roles: [], permissions: [] };
|
||||
|
||||
const [roles, rolePerms] = await Promise.all([
|
||||
prisma.role.findMany({ where: { id: { in: roleIds } }, select: { name: true } }),
|
||||
prisma.roleHasPermission.findMany({
|
||||
where: { roleId: { in: roleIds } },
|
||||
select: { permissionId: true },
|
||||
}),
|
||||
]);
|
||||
|
||||
const permIds = [...new Set(rolePerms.map((p) => p.permissionId))];
|
||||
const perms = permIds.length
|
||||
? await prisma.permission.findMany({
|
||||
where: { id: { in: permIds } },
|
||||
select: { name: true },
|
||||
})
|
||||
: [];
|
||||
|
||||
return {
|
||||
roles: roles.map((r) => r.name),
|
||||
permissions: perms.map((p) => p.name),
|
||||
};
|
||||
}
|
||||
|
||||
export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
...authConfig,
|
||||
providers: [
|
||||
Credentials({
|
||||
credentials: {
|
||||
username: { label: "用户名" },
|
||||
password: { label: "密码", type: "password" },
|
||||
},
|
||||
authorize: async (credentials) => {
|
||||
const username = String(credentials?.username ?? "").trim();
|
||||
const password = String(credentials?.password ?? "");
|
||||
if (!username || !password) return null;
|
||||
|
||||
const admin = await prisma.admin.findUnique({ where: { username } });
|
||||
// 软删除账号不允许登录
|
||||
if (!admin || admin.deletedAt) return null;
|
||||
|
||||
// 复用 Laravel bcrypt($2y$)哈希,老账号密码原样可登录
|
||||
const ok = await bcrypt.compare(password, admin.password);
|
||||
if (!ok) return null;
|
||||
|
||||
const acl = await loadAcl(admin.id);
|
||||
return {
|
||||
id: String(admin.id),
|
||||
name: admin.nickname || admin.username,
|
||||
username: admin.username,
|
||||
image: admin.face ?? null,
|
||||
roles: acl.roles,
|
||||
permissions: acl.permissions,
|
||||
};
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
/** 品牌信息(全站统一引用,改这里即可) */
|
||||
export const brand = {
|
||||
name: "联动支付",
|
||||
nameEn: "Linkage Pay",
|
||||
product: "CRM 客户管理系统",
|
||||
tagline: "客户关系管理平台",
|
||||
company: "联动支付",
|
||||
};
|
||||
|
||||
/** 版权文案(当前年份) */
|
||||
export function copyright(): string {
|
||||
return `© ${new Date().getFullYear()} ${brand.company} · 保留所有权利`;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Solar } from "lunar-typescript";
|
||||
|
||||
function pad2(n: number): string {
|
||||
return String(Math.abs(n)).padStart(2, "0");
|
||||
}
|
||||
|
||||
function md(month: number, day: number): string {
|
||||
return `${pad2(month)}-${pad2(day)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算窗口内每一天的「公历月日」与「农历月日」集合(均为零补齐 MM-DD,
|
||||
* 与老系统 overtrue/chinese-calendar 口径一致,可直接与 DATE_FORMAT(birthday,'%m-%d') 比较)。
|
||||
*
|
||||
* @param base 基准日期(本地时区)
|
||||
* @param days 天数
|
||||
* @param includeBase true=从 base 当天算起;false=从 base 次日算起(老系统"未来7天"用后者)
|
||||
*/
|
||||
export function birthdayMatchLists(
|
||||
base: Date,
|
||||
days: number,
|
||||
includeBase: boolean
|
||||
): { solar: string[]; lunar: string[] } {
|
||||
const solar = new Set<string>();
|
||||
const lunar = new Set<string>();
|
||||
const start = includeBase ? 0 : 1;
|
||||
|
||||
for (let i = 0; i < days; i++) {
|
||||
const d = new Date(
|
||||
base.getFullYear(),
|
||||
base.getMonth(),
|
||||
base.getDate() + start + i
|
||||
);
|
||||
solar.add(md(d.getMonth() + 1, d.getDate()));
|
||||
|
||||
const l = Solar.fromYmd(d.getFullYear(), d.getMonth() + 1, d.getDate()).getLunar();
|
||||
lunar.add(md(l.getMonth(), l.getDay()));
|
||||
}
|
||||
|
||||
return { solar: [...solar], lunar: [...lunar] };
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
// 避免开发热重载下重复实例化
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined;
|
||||
};
|
||||
|
||||
export const prisma =
|
||||
globalForPrisma.prisma ??
|
||||
new PrismaClient({
|
||||
log: process.env.NODE_ENV === "development" ? ["error", "warn"] : ["error"],
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
||||
@@ -0,0 +1,21 @@
|
||||
import { clsx, type ClassValue } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
/** Date(Prisma @db.Date,UTC 午夜)→ "YYYY-MM-DD";空值返回 "" */
|
||||
export function fmtDate(d: Date | null | undefined): string {
|
||||
if (!d) return "";
|
||||
return d.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
/** "YYYY-MM-DD" 字符串 → Date(按 UTC 解析,避免时区偏移);空/非法返回 null */
|
||||
export function parseDate(s: string | null | undefined): Date | null {
|
||||
if (!s) return null;
|
||||
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s.trim());
|
||||
if (!m) return null;
|
||||
const d = new Date(`${s.trim()}T00:00:00.000Z`);
|
||||
return isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
Reference in New Issue
Block a user