2026-05-28 15:11:36 +08:00

139 lines
4.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 { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
import { isCurrentSuperAdmin } from '@/lib/admin-permissions'
import { BUILTIN_TEMPLATE_KEYS } from '@/lib/system-integrity'
import { getSystemInitToken, initializeSystem } from '@/lib/system-init'
export async function POST(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
const existingSuperAdmin = await prisma.user.findFirst({
where: { isSuperAdmin: true },
select: { id: true },
})
let initToken = req.headers.get('x-system-init-token')?.trim() || ''
if (!initToken) {
const body = await req.json().catch(() => ({}))
if (typeof body?.initToken === 'string') {
initToken = body.initToken.trim()
}
}
if (existingSuperAdmin) {
if (!(await isCurrentSuperAdmin(session))) {
return NextResponse.json(
{
success: false,
error: '系统已初始化,后续初始化操作需要最高权限账号登录',
},
{ status: 403 }
)
}
} else {
const configuredInitToken = getSystemInitToken()
if (!configuredInitToken) {
return NextResponse.json(
{
success: false,
error: '未配置 SYSTEM_INIT_TOKEN,禁止公开初始化',
},
{ status: 503 }
)
}
if (!initToken || initToken !== configuredInitToken) {
return NextResponse.json(
{
success: false,
error: '初始化令牌无效',
},
{ status: 401 }
)
}
}
const result = await initializeSystem()
return NextResponse.json({
success: true,
message: result.alreadyInitialized ? '系统已初始化' : '系统初始化成功',
initialized: result.initialized,
data: {
admin: result.admin
? {
email: result.admin.email,
username: result.admin.username,
created: result.admin.created,
upgraded: result.admin.upgraded,
mustChangePassword: result.admin.mustChangePassword,
initialPassword: result.admin.passwordSource === 'generated' ? result.admin.initialPassword || null : null,
passwordSource: result.admin.passwordSource || null,
}
: null,
defaultDepartment: result.defaultDepartment,
defaultCategory: result.defaultCategory,
defaultScene: result.defaultScene,
templates: result.templates,
},
})
} catch (error: any) {
console.error('系统初始化失败:', error)
return NextResponse.json(
{
success: false,
error: '系统初始化失败',
details: error.message,
},
{ status: 500 }
)
}
}
export async function GET() {
try {
const admin = await prisma.user.findFirst({
where: {
OR: [{ isSuperAdmin: true }, { role: 'ADMIN' }],
},
})
const hasDepartment = await prisma.department.findFirst()
const hasCategory = await prisma.category.findFirst()
const hasScene = await prisma.scene.findFirst()
const builtinTemplateCount = await prisma.uploadTemplate.count({
where: {
id: {
in: BUILTIN_TEMPLATE_KEYS.map((key) => `builtin-${key}`),
},
},
})
const hasAllTemplates = builtinTemplateCount === BUILTIN_TEMPLATE_KEYS.length
return NextResponse.json({
initialized: !!admin && !!hasDepartment && !!hasCategory && !!hasScene && hasAllTemplates,
hasAdmin: !!admin,
hasDepartment: !!hasDepartment,
hasCategory: !!hasCategory,
hasScene: !!hasScene,
hasAllTemplates,
templateCount: builtinTemplateCount,
expectedTemplateCount: BUILTIN_TEMPLATE_KEYS.length,
publicInitAvailable: !admin && Boolean(getSystemInitToken()),
})
} catch (error: any) {
console.error('检查初始化状态失败:', error)
return NextResponse.json(
{
initialized: false,
error: error.message,
},
{ status: 500 }
)
}
}