From ec3c324ef2931f6aa2f694a92f95eab2bac07832 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Tue, 19 Dec 2023 23:44:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=98=E6=9C=89=E9=81=97=E7=95=99bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/logs/[...path]/route.ts | 12 +- app/app/(admin)/admin/page.tsx | 30 +---- app/app/(admin)/admin/usage-analysis.tsx | 133 +++++++++++++++++++++++ middleware.ts | 8 ++ 4 files changed, 157 insertions(+), 26 deletions(-) create mode 100644 app/app/(admin)/admin/usage-analysis.tsx diff --git a/app/api/logs/[...path]/route.ts b/app/api/logs/[...path]/route.ts index 47b6951da..2e1f1fc77 100644 --- a/app/api/logs/[...path]/route.ts +++ b/app/api/logs/[...path]/route.ts @@ -2,6 +2,16 @@ import { NextRequest, NextResponse } from "next/server"; import prisma from "@/lib/prisma"; import { insertUser } from "@/lib/auth"; +function cleanObjectKeys(input: { [key: string]: string }): { + [key: string]: string; +} { + const cleanedObj: { [key: string]: string } = {}; + Object.keys(input).forEach((key) => { + cleanedObj[key] = input[key].trim(); + }); + return cleanedObj; +} + async function handle( req: NextRequest, { params }: { params: { path: string[] } }, @@ -13,7 +23,7 @@ async function handle( } // console.log("===========4", request_data); await prisma.logEntry.create({ - data: request_data, + data: cleanObjectKeys(request_data), }); } catch (e) { return NextResponse.json({ status: 0 }); diff --git a/app/app/(admin)/admin/page.tsx b/app/app/(admin)/admin/page.tsx index 9e0f43e2c..c32d89fa9 100644 --- a/app/app/(admin)/admin/page.tsx +++ b/app/app/(admin)/admin/page.tsx @@ -1,37 +1,17 @@ -"use client"; +// "use client"; import { Grid, Col, Card, Text, AreaChart, Metric } from "@tremor/react"; +import UsageAnalysis from "./usage-analysis"; export default function AdminPage() { return ( - - - Title - {/**/} - {/* `${Intl.NumberFormat("us").format(number).toString()}`*/} - {/* }*/} - {/* categories={["Total Visitors"]}*/} - {/* colors={["blue"]}*/} - {/* showXAxis={true}*/} - {/* showGridLines={false}*/} - {/* startEndOnly={true}*/} - {/* showYAxis={false}*/} - {/* showLegend={false}*/} - {/*/>*/} - + + - - Title - KPI 2 - Title - KPI 3 + KPI 2 diff --git a/app/app/(admin)/admin/usage-analysis.tsx b/app/app/(admin)/admin/usage-analysis.tsx new file mode 100644 index 000000000..e43c3ccfd --- /dev/null +++ b/app/app/(admin)/admin/usage-analysis.tsx @@ -0,0 +1,133 @@ +// "use client"; +import { BarList, Bold, Card, Flex, Text, Title } from "@tremor/react"; +import prisma from "@/lib/prisma"; +export default async function UsageAnalysis() { + // 今天日期的开始和结束 + const startDate = new Date(); + startDate.setHours(0, 0, 0, 0); + + const endDate = new Date(); + endDate.setHours(23, 59, 59, 999); + + const todayLog = await prisma.logEntry.findMany({ + where: { + createdAt: { + gte: startDate, // gte 表示 '大于等于' + lte: endDate, // lte 表示 '小于等于' + }, + }, + }); + console.log("======", todayLog); + + const data = [ + { + name: "Twitter", + value: 456, + href: "https://twitter.com/tremorlabs", + icon: function TwitterIcon() { + return ( + + + + + ); + }, + }, + { + name: "Google", + value: 351, + href: "https://google.com", + icon: function GoogleIcon() { + return ( + + + + + ); + }, + }, + { + name: "GitHub", + value: 271, + href: "https://github.com/tremorlabs/tremor", + icon: function GitHubIcon() { + return ( + + + + + ); + }, + }, + { + name: "Reddit", + value: 191, + href: "https://reddit.com", + icon: function RedditIcon() { + return ( + + + + + ); + }, + }, + { + name: "Youtube", + value: 91, + href: "https://www.youtube.com/@tremorlabs3079", + icon: function YouTubeIcon() { + return ( + + + + + ); + }, + }, + ]; + return ( + + Website Analytics + + + Source + + + Visits + + + + + ); +} diff --git a/middleware.ts b/middleware.ts index cafb99b3b..4c7990da2 100644 --- a/middleware.ts +++ b/middleware.ts @@ -22,6 +22,14 @@ export default async function middleware(req: NextRequest) { return NextResponse.redirect(new URL("/login", req.url)); } else if (session) { if (path.startsWith("/login") || path.startsWith('/app/login')) return NextResponse.redirect(new URL("/", req.url)); + // admin 认证 + const admin_user = ["sijinhui", "司金辉"] + // @ts-ignore + if ((path.startsWith("/admin") || path.startsWith("/app/admin")) && !admin_user.includes(session?.user?.name)) { + return NextResponse.redirect(new URL("/", req.url)); + } else { + console.log('[admin]', session?.user) + } } if (path == '/login') {