2025-12-17 20:15:18 +08:00

41 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextResponse } from "next/server";
import { db } from "@/lib/db";
import { generatedImage } from "@/lib/db/schema";
import { eq, sql } from "drizzle-orm";
import { auth } from "@/app/(auth)/auth";
// 缓存配置:每天中午12点更新(约12小时缓存)
export const revalidate = 43200; // 12小时,单位秒
export async function GET() {
try {
const session = await auth();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const userId = session.user.id;
// 查询该用户的生图总数
const result = await db
.select({ count: sql<number>`count(*)::int` })
.from(generatedImage)
.where(eq(generatedImage.userId, userId));
const totalImages = result[0]?.count ?? 0;
return NextResponse.json({
totalImages,
updatedAt: new Date().toISOString(),
});
} catch (error) {
console.error("[User Image Stats API] Error:", error);
return NextResponse.json(
{ error: "Failed to fetch image stats" },
{ status: 500 }
);
}
}