From 8c5bbf1cf00251015ff37276e5a9a35cb961d3d2 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Wed, 24 Apr 2024 18:28:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E5=AD=98-=E8=AE=BE=E7=BD=AE=E9=A1=B9?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/admin/setting/[[...path]]/route.ts | 25 ++++++++++ app/app/(admin)/admin/setting/page.tsx | 9 ++++ .../components/admin-setting-table.tsx | 50 +++++++++++++++++++ app/app/(admin)/components/sidebar.tsx | 1 + lib/auth.ts | 35 +++++++++++-- prisma/schema.prisma | 10 ++++ 6 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 app/api/admin/setting/[[...path]]/route.ts create mode 100644 app/app/(admin)/admin/setting/page.tsx create mode 100644 app/app/(admin)/components/admin-setting-table.tsx diff --git a/app/api/admin/setting/[[...path]]/route.ts b/app/api/admin/setting/[[...path]]/route.ts new file mode 100644 index 000000000..5413ea07b --- /dev/null +++ b/app/api/admin/setting/[[...path]]/route.ts @@ -0,0 +1,25 @@ +import { NextRequest, NextResponse } from "next/server"; +import prisma from "@/lib/prisma"; + +async function handle( + req: NextRequest, + { params }: { params: { path: string[] } }, +) { + // 判断网址和请求方法 + const method = req.method; + // const url = req.url; + const { pathname, searchParams } = new URL(req.url); + // console.log('----', pathname, searchParams, params.path) + if (method === "GET" && !params.path) { + const all_setting = await prisma.setting.findMany(); + console.log("all_setting,", all_setting); + return NextResponse.json({ result: all_setting }); + } + + return NextResponse.json({ error: "当前方法不支持" }, { status: 405 }); +} + +export const GET = handle; +export const POST = handle; +export const PUT = handle; +export const DELETE = handle; diff --git a/app/app/(admin)/admin/setting/page.tsx b/app/app/(admin)/admin/setting/page.tsx new file mode 100644 index 000000000..06e5b2ea6 --- /dev/null +++ b/app/app/(admin)/admin/setting/page.tsx @@ -0,0 +1,9 @@ +import { Flex } from "antd"; +import SettingForm from "../../components/admin-setting-table"; +export default async function SettingPage() { + return ( + + + + ); +} diff --git a/app/app/(admin)/components/admin-setting-table.tsx b/app/app/(admin)/components/admin-setting-table.tsx new file mode 100644 index 000000000..0660e9e88 --- /dev/null +++ b/app/app/(admin)/components/admin-setting-table.tsx @@ -0,0 +1,50 @@ +"use client"; +import type { FormProps } from "antd"; +import { Button, Checkbox, Form, Input, Table } from "antd"; +import { useEffect, useState } from "react"; +import { Setting } from "@prisma/client"; + +function SettingForm() { + const [setting, setSetting] = useState([]); + + useEffect(() => { + fetch("/api/admin/setting", { + headers: { "Content-Type": "application/json" }, + credentials: "include", + }) + .then((response) => response.json()) + .then((result) => { + setSetting(result["result"]); + }); + }, []); + + const columns = [ + { + title: "key", + dataIndex: "key", + key: "key", + }, + { + title: "value", + dataIndex: "value", + }, + { + title: "type", + dataIndex: "type", + }, + { + title: "action", + dataIndex: "", + key: "key", + render: (_, record) => ( + <> + + + ), + }, + ]; + + return ; +} + +export default SettingForm; diff --git a/app/app/(admin)/components/sidebar.tsx b/app/app/(admin)/components/sidebar.tsx index c59ac5770..6e07b3408 100644 --- a/app/app/(admin)/components/sidebar.tsx +++ b/app/app/(admin)/components/sidebar.tsx @@ -38,6 +38,7 @@ const items: MenuItem[] = [ getItem("管理", "manage", , [ getItem("用户管理", "/admin/users"), + getItem("系统设置", "/admin/setting"), ]), // getItem("Navigation Three", "sub4", , [ diff --git a/lib/auth.ts b/lib/auth.ts index 503a3c032..88ae4396b 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -96,10 +96,13 @@ export const authOptions: NextAuthOptions = { // 目前用户不存在,则会创建新用户。 let existingUser = await existUser(user); // await insertUser(user) if (!existingUser) { - // 如果不存在,则报错 - // throw new Error("用户名或密码不正确") - // 如果不存在,则创建 - existingUser = await insertUser(user); + if (await getSetting("allowNewUser")) { + // 如果不存在,则创建 + existingUser = await insertUser(user); + } else { + // 如果不存在,则报错 + throw new Error("未知用户") + } } // 有密码就校验密码,没有就直接返回用户 (password || existingUser.password) && validatePassword(password, existingUser.password); @@ -158,7 +161,7 @@ export const authOptions: NextAuthOptions = { // 过滤不存在的用户 async signIn({ user, account, profile, email, credentials }) { const existingUser = await existUser(user as User); - console.log('---', user, 'account', account, 'email', email, 'exist', existingUser) + // console.log('---', user, 'account', account, 'email', email, 'exist', existingUser) // 顺便过滤掉不允许登录的用户 return !!existingUser && existingUser.allowToLogin; } @@ -210,6 +213,28 @@ export function validatePassword(password: string, hashPassword: string | null | } } +async function getSetting(key: string) { + const setting = await prisma.setting.findUnique({ + where: { + key: key + } + }) + console.log('setting,------', setting) + if (!setting) { + return null; + } + // 根据类型字段转换值 + switch (setting.type) { + case 'boolean': + return setting.value === 'true'; + case 'number': + return Number(setting.value); + case 'string': + default: + return setting.value; + } +} + async function existUser(user: {[key: string]: string} | User ) { const conditions = []; if (user?.name) { diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e5b40d003..a0480f73d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -136,3 +136,13 @@ model VerificationToken { // image String? @db.Text // imageBlurhash String? @db.Text // } + +model Setting { + key String @id + value String + type String + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @default(now()) @updatedAt @map("updated_at") + + @@map("settings") +}