diff --git a/app/api/admin/users/[[...path]]/route.ts b/app/api/admin/users/[[...path]]/route.ts index 00276586a..9f8e8268a 100644 --- a/app/api/admin/users/[[...path]]/route.ts +++ b/app/api/admin/users/[[...path]]/route.ts @@ -1,5 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; import prisma from "@/lib/prisma"; +import { User } from "@prisma/client"; +import { hashPassword } from "@/lib/utils"; async function handle( req: NextRequest, @@ -14,41 +16,44 @@ async function handle( if (method === "GET") { // 是否有查询 - const result = searchText - ? await prisma.user.findMany({ - orderBy: { - createdAt: "desc", - }, - where: { - OR: [ - { - name: { - contains: searchText, - mode: "insensitive", // 不区分大小写 + try { + const result = searchText + ? await prisma.user.findMany({ + orderBy: { + createdAt: "desc", + }, + where: { + OR: [ + { + name: { + contains: searchText, + mode: "insensitive", // 不区分大小写 + }, }, - }, - { - username: { - contains: searchText, - mode: "insensitive", // 不区分大小写 + { + username: { + contains: searchText, + mode: "insensitive", // 不区分大小写 + }, }, - }, - { - email: { - contains: searchText, - mode: "insensitive", // 不区分大小写 + { + email: { + contains: searchText, + mode: "insensitive", // 不区分大小写 + }, }, - }, - ], - }, - }) - : await prisma.user.findMany({ - orderBy: { - createdAt: "desc", - }, - }); - const count = result.length; - return NextResponse.json({ count: count, results: result }); + ], + }, + }) + : await prisma.user.findMany({ + orderBy: { + createdAt: "desc", + }, + }); + const count = result.length; + return NextResponse.json({ count: count, results: result }); + } catch {} + return NextResponse.json({ error: "未知错误" }, { status: 500 }); } if (method === "DELETE") { @@ -69,9 +74,36 @@ async function handle( } return NextResponse.json({ result: "删除用户成功" }); } + + if (method === "PUT") { + try { + const userId = params.path[0]; + return await changeUserInfo(userId, await req.json()); + } catch { + return NextResponse.json({ error: "未知错误" }, { status: 500 }); + } + } 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) { + await prisma.user.update({ + where: { + id: id, + }, + data: { + password: hashDPassword, + }, + }); + return NextResponse.json({ result: "ok" }); + } + return NextResponse.json({ error: "未知错误" }, { status: 500 }); +} + export const GET = handle; export const POST = handle; +export const PUT = handle; export const DELETE = handle; diff --git a/app/app/(admin)/components/users-table.tsx b/app/app/(admin)/components/users-table.tsx index d8fd66d2e..3302c29d9 100644 --- a/app/app/(admin)/components/users-table.tsx +++ b/app/app/(admin)/components/users-table.tsx @@ -2,9 +2,17 @@ import React, { Dispatch, SetStateAction, useEffect, useState } from "react"; import { User } from "@prisma/client"; -import { Space, Table, Tag, Input, Button, notification } from "antd"; +import { + Space, + Table, + Tag, + Input, + Button, + notification, + Popconfirm, +} from "antd"; import type { GetRef, TableColumnsType } from "antd"; - +// import { headers } from 'next/headers' import type { NotificationArgsProps } from "antd"; import Highlighter from "react-highlight-words"; @@ -75,6 +83,53 @@ function UserTableSearchInput({ users, setUsers, setLoading }: UserInterface) { function UsersTable({ users, setUsers, loading }: UserInterface) { const [api, contextHolder] = notification.useNotification(); + const [newPassword, setNewPassword] = useState(""); + + const newPasswordChange = (e: React.ChangeEvent) => { + // if ((e.nativeEvent as InputEvent).isComposing) { + // return; + // } + setNewPassword(e.target.value.trim()); + }; + + 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") { api.error({ @@ -139,7 +194,25 @@ function UsersTable({ users, setUsers, loading }: UserInterface) { render: (_, record) => ( {contextHolder} - 编辑 + + e.preventDefault()} + onChange={newPasswordChange} + /> + + } + onConfirm={() => confirmPassword(record.id)} + onOpenChange={() => console.log("open change")} + > + + handleDeleteUser(record)}>删除 ), diff --git a/app/app/login.scss b/app/app/login.scss index a91bef414..3acaa4440 100644 --- a/app/app/login.scss +++ b/app/app/login.scss @@ -9,6 +9,10 @@ margin-right: auto } +input { + text-align: left !important; +} + .signin hr { text-align: center; border: 0; diff --git a/lib/utils.ts b/lib/utils.ts index 4336e02d2..1833f58d6 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -62,7 +62,9 @@ export const random = (min: number, max: number) => { // 将明文处理为 hash export function hashPassword(password: string) { - return bcrypt.hashSync(password, 10); + const p = password.trim() + if (!p) return ""; + return bcrypt.hashSync(password.trim(), 10); } // 对比明文和 hash 是否一致