From 209f23f2f05d21df0a68017b76c41a90e4ee6b81 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 29 Apr 2024 18:32:05 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/admin-setting-table.tsx | 14 ++-- app/app/(admin)/components/users-table.tsx | 65 ++++++++++++------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/app/app/(admin)/components/admin-setting-table.tsx b/app/app/(admin)/components/admin-setting-table.tsx index a75691e8b..43cfb5327 100644 --- a/app/app/(admin)/components/admin-setting-table.tsx +++ b/app/app/(admin)/components/admin-setting-table.tsx @@ -20,13 +20,13 @@ function SettingForm() { const [form] = Form.useForm(); const [setting, setSetting] = useState([]); - const [isModalVisible, setIsModalVisible] = useState(false); - const openModal = () => setIsModalVisible(true); - const closeModal = () => setIsModalVisible(false); - - const handleFormSubmit = async (record: Setting) => { - console.log("-------", record); - }; + // const [isModalVisible, setIsModalVisible] = useState(false); + // const openModal = () => setIsModalVisible(true); + // const closeModal = () => setIsModalVisible(false); + // + // const handleFormSubmit = async (record: Setting) => { + // console.log("-------", record); + // }; const handelDel = (record: Setting) => { fetch(`/api/admin/setting/${record.key}`, { diff --git a/app/app/(admin)/components/users-table.tsx b/app/app/(admin)/components/users-table.tsx index 2756085a5..51a8bfc77 100644 --- a/app/app/(admin)/components/users-table.tsx +++ b/app/app/(admin)/components/users-table.tsx @@ -8,17 +8,19 @@ import { Tag, Input, Button, - notification, + notification as notificationModule, Popconfirm, Checkbox, + Modal, + Form, } from "antd"; import type { GetRef, TableColumnsType } from "antd"; // import { headers } from 'next/headers' import type { NotificationArgsProps } from "antd"; -import Highlighter from "react-highlight-words"; +// import Highlighter from "react-highlight-words"; // 后期考虑删除该依赖 -type NotificationPlacement = NotificationArgsProps["placement"]; +// type NotificationPlacement = NotificationArgsProps["placement"]; import type { SearchProps } from "antd/es/input/Search"; @@ -82,8 +84,10 @@ function UserTableSearchInput({ users, setUsers, setLoading }: UserInterface) { } function UsersTable({ users, setUsers, loading }: UserInterface) { - const [api, contextHolder] = notification.useNotification(); - + const [notification, notificationContextHolder] = + notificationModule.useNotification(); + const [editUserModal, editUserModalContextHolder] = Modal.useModal(); + const [editUserForm] = Form.useForm(); const [newPassword, setNewPassword] = useState(""); const newPasswordChange = (e: React.ChangeEvent) => { @@ -133,12 +137,12 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { const openNotification = (level: string, arms: NotificationArgsProps) => { if (level === "error") { - api.error({ + notification.error({ ...arms, placement: "topRight", }); } else { - api.info({ + notification.info({ ...arms, placement: "topRight", }); @@ -189,7 +193,7 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { render: (value) => getCurrentTime(new Date(value)), }, { - title: "isAdmin", + title: "管理员", dataIndex: "isAdmin", width: 80, render: (value) => { @@ -201,9 +205,9 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { }, }, { - title: "allowToLogin", + title: "允许登录", dataIndex: "allowToLogin", - width: 120, + width: 80, render: (value) => { return (
@@ -212,13 +216,26 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { ); }, }, + // { + // title: "编辑", + // dataIndex: "", + // key: "id", + // width: 80, + // render: (value) => { + // return ( + //
+ // 编辑 + //
+ // ); + // }, + // }, { title: "Action", dataIndex: "", key: "id", render: (_, record) => ( - - {contextHolder} + + 编辑 + <> + {notificationContextHolder} + {editUserModalContextHolder} + + ); } From fe48b5acfc5508006c0e21634833f7ea9dfd3dac Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 29 Apr 2024 18:35:51 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app/(admin)/components/users-table.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/app/(admin)/components/users-table.tsx b/app/app/(admin)/components/users-table.tsx index 51a8bfc77..adbed2185 100644 --- a/app/app/(admin)/components/users-table.tsx +++ b/app/app/(admin)/components/users-table.tsx @@ -97,6 +97,11 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { setNewPassword(e.target.value.trim()); }; + const handleUserEdit = ( + method: "POST" | "PUT", + record: User | undefined, + ) => {}; + const confirmPassword = async (id: string) => { console.log("-----", newPassword, id); try { From a64f52ecbbebb52aed9389f8cf2029a11742fbb6 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 29 Apr 2024 23:04:59 +0800 Subject: [PATCH 3/5] update user admin --- app/api/admin/users/[[...path]]/route.ts | 23 +++-- app/api/test/route.ts | 0 app/app/(admin)/components/users-table.tsx | 103 ++++++++++++++++++++- lib/auth.ts | 8 +- 4 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 app/api/test/route.ts diff --git a/app/api/admin/users/[[...path]]/route.ts b/app/api/admin/users/[[...path]]/route.ts index 95e989ac8..ebe9cc7e6 100644 --- a/app/api/admin/users/[[...path]]/route.ts +++ b/app/api/admin/users/[[...path]]/route.ts @@ -95,7 +95,16 @@ async function handle( if (method === "PUT") { try { const userId = params.path[0]; - return await changeUserInfo(userId, await req.json()); + let new_user_info: Partial = Object.entries( + await req.json(), + ).reduce((acc, [key, value]) => { + if (value !== null) { + // @ts-ignore + acc[key] = value; + } + return acc; + }, {}); + return await changeUserInfo(userId, new_user_info); } catch { return NextResponse.json({ error: "未知错误" }, { status: 500 }); } @@ -103,16 +112,18 @@ async function handle( return NextResponse.json({ error: "当前方法不支持" }, { status: 405 }); } -async function changeUserInfo(id: string, info: User) { - const hashDPassword = hashPassword(info?.password ?? ""); - console.log("-----------", id, info, hashDPassword); - if (hashDPassword) { +async function changeUserInfo(id: string, info: Partial) { + if (info.password) { + info["password"] = hashPassword(info.password); + } + // console.log("-----------", id, info, hashDPassword); + if (info) { await prisma.user.update({ where: { id: id, }, data: { - password: hashDPassword, + ...info, }, }); return NextResponse.json({ result: "ok" }); diff --git a/app/api/test/route.ts b/app/api/test/route.ts new file mode 100644 index 000000000..e69de29bb diff --git a/app/app/(admin)/components/users-table.tsx b/app/app/(admin)/components/users-table.tsx index adbed2185..6b13820fc 100644 --- a/app/app/(admin)/components/users-table.tsx +++ b/app/app/(admin)/components/users-table.tsx @@ -15,6 +15,7 @@ import { Form, } from "antd"; import type { GetRef, TableColumnsType } from "antd"; +import { LockOutlined } from "@ant-design/icons"; // import { headers } from 'next/headers' import type { NotificationArgsProps } from "antd"; @@ -97,10 +98,100 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { setNewPassword(e.target.value.trim()); }; - const handleUserEdit = ( - method: "POST" | "PUT", - record: User | undefined, - ) => {}; + const handleUserEdit = (method: "POST" | "PUT", record: User | undefined) => { + editUserModal.confirm({ + title: "编辑用户", + content: ( +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + } + type="password" + placeholder="Password" + autoComplete="new-password" + /> + + + ), + onOk: () => { + const setting_key = method === "PUT" ? record?.id : ""; + editUserForm.validateFields().then((values) => { + const dataToSubmit = { + username: values.username ?? null, + email: values.email ?? null, + allowToLogin: values.allowToLogin ?? true, + isAdmin: values.isAdmin ?? false, + password: values.password ?? null, + }; + fetch(`/api/admin/users/${values.id}`, { + method: method, + credentials: "include", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(dataToSubmit), + }) + .then((response) => response.json()) + .then((result) => { + if (result["result"] == "ok") { + openNotification("info", { + message: "修改信息", + description: `${values.id} 信息修改成功`, + }); + } + }) + .catch((error) => { + console.log("e", error); + openNotification("error", { + message: "修改信息", + description: `${values.id} 信息修改失败`, + }); + }); + }); + }, + }); + }; const confirmPassword = async (id: string) => { console.log("-----", newPassword, id); @@ -240,7 +331,9 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { key: "id", render: (_, record) => ( - 编辑 + handleUserEdit("PUT", record)}> + 编辑 + ; + export const authOptions: NextAuthOptions = { @@ -89,7 +89,7 @@ export const authOptions: NextAuthOptions = { // 判断姓名格式是否符合要求,不符合则拒绝 if (username && isName(username)) { // Any object returned will be saved in `user` property of the JWT - let user: PartialUser = {} + let user: Partial = {} if (isEmail(username)) { user['email'] = username; } else { @@ -228,7 +228,7 @@ async function getSetting(key: string) { } } -async function existUser(user: PartialUser ) { +async function existUser(user: Partial ) { const conditions = []; if (user?.name) { conditions.push({ name: user.name }); @@ -243,7 +243,7 @@ async function existUser(user: PartialUser ) { }) : null } -export async function insertUser(user: PartialUser ) { +export async function insertUser(user: Partial ) { console.log('------------', user) try { return await prisma.user.create({ From 48517034f23b6874e1c8f29cb46467e727a8f5ce Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 29 Apr 2024 23:09:57 +0800 Subject: [PATCH 4/5] fix little bug --- app/app/(admin)/components/users-table.tsx | 92 ++-------------------- 1 file changed, 5 insertions(+), 87 deletions(-) diff --git a/app/app/(admin)/components/users-table.tsx b/app/app/(admin)/components/users-table.tsx index 6b13820fc..84c3ac0a0 100644 --- a/app/app/(admin)/components/users-table.tsx +++ b/app/app/(admin)/components/users-table.tsx @@ -5,11 +5,9 @@ import { User } from "@prisma/client"; import { Space, Table, - Tag, Input, Button, notification as notificationModule, - Popconfirm, Checkbox, Modal, Form, @@ -40,9 +38,6 @@ interface SearchTextProps { setSearchText: Dispatch>; } -type DataIndex = keyof User; -type InputRef = GetRef; - function UserTableSearchInput({ users, setUsers, setLoading }: UserInterface) { const [searchText, setSearchText] = useState(""); // 这里直接搜索,并获取数据不传递搜索的值给表格了。 @@ -89,14 +84,6 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { notificationModule.useNotification(); const [editUserModal, editUserModalContextHolder] = Modal.useModal(); const [editUserForm] = Form.useForm(); - const [newPassword, setNewPassword] = useState(""); - - const newPasswordChange = (e: React.ChangeEvent) => { - // if ((e.nativeEvent as InputEvent).isComposing) { - // return; - // } - setNewPassword(e.target.value.trim()); - }; const handleUserEdit = (method: "POST" | "PUT", record: User | undefined) => { editUserModal.confirm({ @@ -193,44 +180,6 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { }); }; - const confirmPassword = async (id: string) => { - console.log("-----", newPassword, id); - try { - fetch(`/api/admin/users/${id}`, { - method: "put", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - password: newPassword, - }), - credentials: "include", - }) - .then((response) => response.json()) - .then((res) => { - if (res["result"] == "ok") { - openNotification("info", { - message: "修改密码", - description: `${id} 密码修改成功`, - }); - } - }) - .catch((error) => { - console.log("e", error); - openNotification("error", { - message: "修改密码", - description: `${id} 密码修改失败`, - }); - }); - } catch { - openNotification("error", { - message: "修改密码", - description: `${id} 密码修改失败`, - }); - } - setNewPassword(""); - }; - const openNotification = (level: string, arms: NotificationArgsProps) => { if (level === "error") { notification.error({ @@ -312,49 +261,18 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { ); }, }, - // { - // title: "编辑", - // dataIndex: "", - // key: "id", - // width: 80, - // render: (value) => { - // return ( - //
- // 编辑 - //
- // ); - // }, - // }, { title: "Action", dataIndex: "", key: "id", render: (_, record) => ( - handleUserEdit("PUT", record)}> + -
- handleDeleteUser(record)}>删除 + +
), }, From 852401d556cc923c85d57490539b1c5949b438c0 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 29 Apr 2024 23:11:16 +0800 Subject: [PATCH 5/5] fix little bug --- app/api/test/route.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/api/test/route.ts b/app/api/test/route.ts index e69de29bb..51174a6da 100644 --- a/app/api/test/route.ts +++ b/app/api/test/route.ts @@ -0,0 +1,10 @@ +import { NextRequest, NextResponse } from "next/server"; + +async function handle( + req: NextRequest, + { params }: { params: { path: string[] } }, +) { + return NextResponse.json({}); +} + +export const GET = handle;