From 48cf006f83dd71f097eb8db4f91572438e989dc0 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Thu, 29 Aug 2024 19:05:59 +0800 Subject: [PATCH] temp user token --- app/api/admin/users/[[...path]]/route.ts | 1 + app/api/logs/[[...path]]/route.ts | 21 +++++++++++++++++++-- app/components/chat.tsx | 8 ++++++-- app/store/chat.ts | 5 ++++- prisma/schema.prisma | 2 ++ 5 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/api/admin/users/[[...path]]/route.ts b/app/api/admin/users/[[...path]]/route.ts index 90f3a0dba..1a01ae8bf 100644 --- a/app/api/admin/users/[[...path]]/route.ts +++ b/app/api/admin/users/[[...path]]/route.ts @@ -63,6 +63,7 @@ async function handle( image: item.image, email: item.email, emailVerified: item.emailVerified, + everyLimitToken: item.everyLimitToken, createdAt: item.createdAt, updatedAt: item.updatedAt, allowToLogin: item.allowToLogin, diff --git a/app/api/logs/[[...path]]/route.ts b/app/api/logs/[[...path]]/route.ts index 2ec175bb4..24466bc4b 100644 --- a/app/api/logs/[[...path]]/route.ts +++ b/app/api/logs/[[...path]]/route.ts @@ -19,7 +19,19 @@ async function handle( const user_id = session?.user.id; const { startOfTheDayInTimeZone, endOfTheDayInTimeZone } = getCurStartEnd(); - const current_token = await prisma.logEntry + let current_day_limit_token = 0; + const current_user = await prisma.user.findUnique({ + where: { + id: user_id, + }, + }); + if (current_user && current_user.everyLimitToken !== 0) { + current_day_limit_token = current_user.everyLimitToken * 1000; + } else { + current_day_limit_token = 0; + } + + const current_day_token = await prisma.logEntry .findMany({ where: { userID: user_id, @@ -39,7 +51,12 @@ async function handle( ); }); // console.log('-----------', user_id, current_token) - return NextResponse.json({ result: { current_token: current_token } }); + return NextResponse.json({ + result: { + current_day_token: current_day_token, + current_day_limit_token: current_day_limit_token, + }, + }); } catch {} return NextResponse.json({ error: "未知错误" }, { status: 500 }); } diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 5836a9278..7adb4d844 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1420,7 +1420,11 @@ function _Chat() { // console.log("请求成功,", result); localStorage.setItem( "current_day_token", - result["result"]["current_token"], + result["result"]["current_day_token"], + ); + localStorage.setItem( + "current_day_limit_token", + result["result"]["current_day_limit_token"], ); }); } catch {} @@ -1484,7 +1488,7 @@ function _Chat() { = 200000) { + if (current_day_token >= current_day_limit_token) { botMessage.content += "\n\n" + prettyObject({ diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4d2f32680..b63668732 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -27,6 +27,8 @@ model User { emailVerified DateTime? image String? password String? + // 默认每人每天限额20k,但数据库存储0 + everyLimitToken Int? @default(0) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt allowToLogin Boolean @default(true)