mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-10-01 23:56:39 +08:00
完成管理员面用户密码修改
This commit is contained in:
parent
f9ff55ccdc
commit
869d3ab27b
@ -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;
|
||||
|
@ -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<HTMLInputElement>) => {
|
||||
// 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) => (
|
||||
<Space size="middle">
|
||||
{contextHolder}
|
||||
<a>编辑</a>
|
||||
<Popconfirm
|
||||
title="输入新密码"
|
||||
description={
|
||||
<>
|
||||
<Input.Password
|
||||
autoComplete="new-password"
|
||||
value={newPassword}
|
||||
onCompositionStart={(e) => e.preventDefault()}
|
||||
onChange={newPasswordChange}
|
||||
/>
|
||||
</>
|
||||
}
|
||||
onConfirm={() => confirmPassword(record.id)}
|
||||
onOpenChange={() => console.log("open change")}
|
||||
>
|
||||
<Button type="primary" size="small">
|
||||
设置密码
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
<a onClick={() => handleDeleteUser(record)}>删除</a>
|
||||
</Space>
|
||||
),
|
||||
|
@ -9,6 +9,10 @@
|
||||
margin-right: auto
|
||||
}
|
||||
|
||||
input {
|
||||
text-align: left !important;
|
||||
}
|
||||
|
||||
.signin hr {
|
||||
text-align: center;
|
||||
border: 0;
|
||||
|
@ -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 是否一致
|
||||
|
Loading…
Reference in New Issue
Block a user