diff --git a/app/api/admin/charts/route.ts b/app/api/admin/charts/route.ts index 3289d679f..cadcd7522 100644 --- a/app/api/admin/charts/route.ts +++ b/app/api/admin/charts/route.ts @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from "next/server"; import prisma from "@/lib/prisma"; import { addHours, subMinutes } from "date-fns"; import { EChartsOption } from "echarts"; +import { getCurStartEnd } from "@/app/utils/custom"; interface StringKeyedObject { [key: string]: { [key: string]: number }; @@ -50,19 +51,7 @@ function HandleLogData( async function handle(req: NextRequest) { const { searchParams } = new URL(req.url); - const date = searchParams.get("date") ?? ""; - // 当天日期的开始和结束 - let currentTime = new Date(date); - // today = subMinutes(today, today.getTimezoneOffset()) - const startOfTheDayInTimeZone = new Date( - currentTime.getFullYear(), - currentTime.getMonth(), - currentTime.getDate(), - 0, - 0, - 0, - ); - const endOfTheDayInTimeZone = addHours(startOfTheDayInTimeZone, +24); // 当天的结束时间 + const { startOfTheDayInTimeZone, endOfTheDayInTimeZone } = getCurStartEnd(); const todayLog = await prisma.logEntry.findMany({ where: { createdAt: { diff --git a/app/utils/custom.ts b/app/utils/custom.ts index 43aba914c..d42f90a79 100644 --- a/app/utils/custom.ts +++ b/app/utils/custom.ts @@ -1,3 +1,5 @@ +import { addHours } from "date-fns"; + export function getCurrentTime(now?: Date): string { if (!now) { const now = new Date(); @@ -16,3 +18,19 @@ export function getCurrentTime(now?: Date): string { return formatter.format(now); } + +export function getCurStartEnd(now = new Date()) { + const startOfTheDayInTimeZone = new Date( + now.getFullYear(), + now.getMonth(), + now.getDate(), + 0, + 0, + 0, + ); + const endOfTheDayInTimeZone = addHours(startOfTheDayInTimeZone, +24); // 当天的结束时间 + return { + startOfTheDayInTimeZone, + endOfTheDayInTimeZone, + }; +} diff --git a/lib/auth.ts b/lib/auth.ts index 2060a20b6..343ea9f69 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -8,6 +8,7 @@ import { User } from "@prisma/client"; import {ADMIN_LIST, isEmail, isName} from "@/lib/auth_list"; import {createTransport} from "nodemailer"; import { comparePassword, hashPassword } from "@/lib/utils"; +import {getCurStartEnd} from "@/app/utils/custom"; const SECURE_COOKIES:boolean = !!process.env.SECURE_COOKIES;