mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-11-16 22:13:47 +08:00
更新token的计算方式
This commit is contained in:
@@ -1,11 +1,49 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { getTokenLength } from "@/lib/utils";
|
||||
import { getSession } from "@/lib/auth";
|
||||
import { getCurStartEnd } from "@/app/utils/custom";
|
||||
|
||||
async function handle(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { path: string[] } },
|
||||
) {
|
||||
// 判断网址和请求方法
|
||||
const method = req.method;
|
||||
// const url = req.url;
|
||||
const { pathname, searchParams } = new URL(req.url);
|
||||
const searchText = searchParams.get("search");
|
||||
if (method === "GET") {
|
||||
try {
|
||||
const session = await getSession();
|
||||
const user_id = session?.user.id;
|
||||
const { startOfTheDayInTimeZone, endOfTheDayInTimeZone } =
|
||||
getCurStartEnd();
|
||||
const current_token = await prisma.logEntry
|
||||
.findMany({
|
||||
where: {
|
||||
userID: user_id,
|
||||
createdAt: {
|
||||
gte: startOfTheDayInTimeZone.toISOString(), // gte 表示 '大于等于'
|
||||
lte: endOfTheDayInTimeZone.toISOString(), // lte 表示 '小于等于'
|
||||
},
|
||||
},
|
||||
select: {
|
||||
logToken: true,
|
||||
},
|
||||
})
|
||||
.then((result) => {
|
||||
return result.reduce(
|
||||
(acc, cur) => (cur.logToken ? acc + cur.logToken : acc),
|
||||
0,
|
||||
);
|
||||
});
|
||||
// console.log('-----------', user_id, current_token)
|
||||
return NextResponse.json({ result: { current_token: current_token } });
|
||||
} catch {}
|
||||
return NextResponse.json({ error: "未知错误" }, { status: 500 });
|
||||
}
|
||||
|
||||
try {
|
||||
const request_data = await req.json();
|
||||
try {
|
||||
@@ -43,7 +81,7 @@ async function handle(
|
||||
|
||||
return NextResponse.json({ status: 1 });
|
||||
}
|
||||
// export const GET = handle;
|
||||
export const GET = handle;
|
||||
export const POST = handle;
|
||||
|
||||
// export const runtime = "edge";
|
||||
|
||||
Reference in New Issue
Block a user