Skip to content

Commit

Permalink
refactor(user-manage): 基于 next-auth 重构《用户管理》模块
Browse files Browse the repository at this point in the history
  • Loading branch information
baiwumm committed Jan 7, 2025
1 parent 1118503 commit 9abfe2c
Show file tree
Hide file tree
Showing 28 changed files with 543 additions and 801 deletions.
16 changes: 15 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DATABASE_URL="postgresql://postgres:123456@localhost:5432/next-admin?schema=publ
NEXT_PUBLIC_PROJECT_NAME = Next Admin

# 项目描述
NEXT_PUBLIC_PROJECT_DESC = 基于 Ant Design Next.js 的后台管理系统
NEXT_PUBLIC_PROJECT_DESC = 挖掘发现 Next.js 的乐趣所在

# 主题模式
NEXT_PUBLIC_THEME_MODE = light
Expand Down Expand Up @@ -33,3 +33,17 @@ NEXT_PUBLIC_AUTHOR_WECHAT = https://wechat.baiwumm.com/

# 邮箱
NEXT_PUBLIC_AUTHOR_EMAIL = [email protected]

AUTH_SECRET="qeY6JYj8JUEkkqsp+RHtusNfChlUKl1RhtZT5C2JkEE=" # Added by `npx auth`. Read more: https://cli.authjs.dev

# Github 授权信息
GITHUB_ID= 'Ov23liiX13UGfOAGJjnb'
GITHUB_SECRET= '084703c6c57e4b3c4fa9fadfb1aaf3be354f5b95'

# Google 授权信息
GOOGLE_CLIENT_ID= '512149633218-bne02tg2f496qtq3moak048na8c1tq3o.apps.googleusercontent.com'
GOOGLE_CLIENT_SECRET= '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'

# Gitee 授权信息
GITEE_CLIENT_ID= 'e1e6a35da0eef58d8a428d6b02fe3fcc93b5d4e198e21384b36257e241a564bd'
GITEE_CLIENT_SECRET= '464416f35faae65d9f908cffc7bb35c6acc11800172671f3b72a15c7b42fe040'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
!.yarn/plugins
!.yarn/releases
!.yarn/versions
.env

# testing
/coverage
Expand Down
17 changes: 6 additions & 11 deletions messages/en.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"Route":{
"login":"Login",
"logout":"Logout",
"about":"About",
"dashboard":"Dashboard",
"system-manage":"System Manage",
"internationalization":"Internationalization",
"features":"Features",
"captcha":"Captcha",
"viewer":"Viewer",
"user-manage":"User Manage"
"user-manage":"User Manage",
"personal-center":"Personal Center",
"/_not-found":"404"
},
"Pages":{
"internationalization":{
Expand Down Expand Up @@ -38,16 +42,7 @@
},
"user-manage":{
"title":"User",
"userName":"User Name",
"cnName":"Chinese Name",
"email":"Email",
"emailError":"The Email format is incorrect",
"phone":"Phone",
"phoneError":"The mobile phone number format is incorrect",
"sex":"Sex",
"password":"Password",
"confirmPassword":"Confirm Password",
"validateConfirmPassword":"The two password entries are inconsistent"
"email":"Email"
},
"captcha":{
"rotateCode": "Rotate Code",
Expand Down
17 changes: 6 additions & 11 deletions messages/zh.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"Route":{
"login":"登录",
"logout":"退出登录",
"about":"关于",
"dashboard":"仪表盘",
"system-manage":"系统管理",
"internationalization":"国际化",
"features":"功能页",
"captcha":"验证码",
"viewer":"图片预览",
"user-manage":"用户管理"
"user-manage":"用户管理",
"personal-center":"个人中心",
"/_not-found":"404"
},
"Pages":{
"internationalization":{
Expand Down Expand Up @@ -38,16 +42,7 @@
},
"user-manage":{
"title":"用户",
"userName":"用户名",
"cnName":"中文名",
"email":"邮箱",
"emailError":"邮箱格式不正确",
"phone":"手机号码",
"phoneError":"手机号码格式不正确",
"sex":"性别",
"password":"密码",
"confirmPassword":"确认密码",
"validateConfirmPassword":"两次密码输入不一致"
"email":"邮箱"
},
"captcha":{
"rotateCode": "图片旋转验证码",
Expand Down
2 changes: 2 additions & 0 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { handlers } from '@/auth';
export const { GET, POST } = handlers;
54 changes: 0 additions & 54 deletions src/app/api/system-manage/user-manage/[id]/route.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 白雾茫茫丶<baiwumm.com>
* @Date: 2024-12-23 17:34:18
* @LastEditors: 白雾茫茫丶<baiwumm.com>
* @LastEditTime: 2024-12-23 18:13:45
* @LastEditTime: 2025-01-07 15:14:54
* @Description: 用户管理模块
*/
import { Prisma } from '@prisma/client';
Expand All @@ -24,26 +24,28 @@ export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const size = searchParams.get('size') || '10';
const current = searchParams.get('current') || '1';
const userName = searchParams.get('userName');
const phone = searchParams.get('phone');
const name = searchParams.get('name');
const email = searchParams.get('email');
// 分页处理,这里获取到的分页是字符串,需要转换成整数
const take = toNumber(size);
const skip = (toNumber(current) - 1) * take;
// 条件判断
const where: Prisma.UserWhereInput = {}; // 查询参数
// 模糊查询
if (userName) {
where['userName'] = { contains: userName, mode: 'insensitive' };
if (name) {
where['name'] = { contains: name, mode: 'insensitive' };
}
if (phone) {
where['phone'] = { contains: phone, mode: 'insensitive' };
if (email) {
where['email'] = { contains: email, mode: 'insensitive' };
}
const records = await prisma.user.findMany({
skip,
take,
where,
include: {
accounts: true,
},
orderBy: [
{ sort: 'desc' }, // 按照sort字段升序
{ createdAt: 'desc' }, // 如果sort相同,再按照createdAt字段降序
],
});
Expand All @@ -61,30 +63,3 @@ export async function GET(request: NextRequest) {
return NextResponse.json(responseMessage(error, RESPONSE_MSG.ERROR, -1));
}
}

/**
* @description: 新增用户
* @param {Request} request
*/
export async function POST(request: Request) {
try {
// 解析请求体
const { password, ...body } = await request.json(); // 如果是 JSON 数据
// 密码加密
const salt = await bcryptjs.genSalt(10);
const hashPwd = await bcryptjs.hash(password, salt);
const result = await prisma.user.create({
data: {
...body,
password: hashPwd,
},
});
return NextResponse.json(responseMessage(result));
} catch (error) {
// 判断是否违反 postgresql 唯一性约束
if (error.code === 'P2002') {
return NextResponse.json(responseMessage(null, '用户名、电子邮箱、手机号已存在!', -1));
}
return NextResponse.json(responseMessage(error, RESPONSE_MSG.ERROR, -1));
}
}
2 changes: 1 addition & 1 deletion src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 白雾茫茫丶<baiwumm.com>
* @Date: 2024-12-04 09:59:04
* @LastEditors: 白雾茫茫丶<baiwumm.com>
* @LastEditTime: 2024-12-20 09:23:29
* @LastEditTime: 2025-01-06 12:02:28
* @Description: 工作台
*/
'use client';
Expand Down
19 changes: 3 additions & 16 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: 白雾茫茫丶<baiwumm.com>
* @Date: 2024-12-06 10:05:33
* @LastEditors: 白雾茫茫丶<baiwumm.com>
* @LastEditTime: 2024-12-30 16:46:08
* @LastEditTime: 2025-01-03 17:09:47
* @Description: 布局文件
*/
import './globals.scss';
Expand All @@ -12,13 +12,9 @@ import type { Metadata } from 'next';
import { NextIntlClientProvider } from 'next-intl';
import { getLocale, getMessages } from 'next-intl/server';

import AppSideBar from '@/components/AppSideBar';
import FullLoading from '@/components/FullLoading'; // 全局 Loading
import GlobalFooter from '@/components/GlobalFooter'; // 底部版权
import GlobalHeader from '@/components/GlobalHeader'; // 头部布局
import PageAnimatePresence from '@/components/PageAnimatePresence';
import GlobalLayout from '@/components/GlobalLayout'; // 全局布局
import ThemeProvider from '@/components/ThemeProvider';
import { SidebarInset, SidebarProvider } from '@/components/ui/sidebar';
import { Toaster } from '@/components/ui/sonner';

export const metadata: Metadata = {
Expand All @@ -41,16 +37,7 @@ export default async function RootLayout({
<ThemeProvider attribute="class" defaultTheme="light">
{/* 全局 Loading */}
<FullLoading />
<SidebarProvider>
<AppSideBar />
<SidebarInset>
{/* 头部布局 */}
<GlobalHeader />
<PageAnimatePresence>{children}</PageAnimatePresence>
{/* 底部版权 */}
<GlobalFooter />
</SidebarInset>
</SidebarProvider>
<GlobalLayout>{children}</GlobalLayout>
</ThemeProvider>
</NextIntlClientProvider>
<Toaster position="top-center" />
Expand Down
27 changes: 27 additions & 0 deletions src/app/login/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client';

import { useMount, useUnmount } from 'ahooks';

import LangSwitch from '@/components/LangSwitch';
import ThemeModeButton from '@/components/ThemeModeButton';
import { useLayoutStore } from '@/store/layoutStore';

export default function LoginLayout({ children }: { children: React.ReactNode }) {
useMount(() => {
useLayoutStore.setState({ skipGlobalLayout: true });
});

useUnmount(() => {
// 如果需要在离开页面时重置状态
useLayoutStore.setState({ skipGlobalLayout: false });
});
return (
<div className="relative flex h-[calc(100vh_-_2rem)] w-[calc(100vw_-_2rem)] overflow-hidden justify-center items-center">
{children}
<div className="flex absolute top-0 right-0">
<ThemeModeButton />
<LangSwitch />
</div>
</div>
);
}
Loading

0 comments on commit 9abfe2c

Please sign in to comment.