WuYongJie 63ec6b7dfb fix: 修复排行榜按钮消失、tag排序和输入框浅色模式问题
1. 排行榜悬浮窗关闭后小按钮消失问题
   - 修复面板居中模式关闭时按钮位置错误
   - 保存并恢复按钮的居中状态

2. 首页tag按钮按素材数量排序
   - 添加按素材数量降序排序逻辑

3. 上传弹窗输入框浅色模式文字不可见
   - 添加 text-slate-900 确保浅色模式下文字可见
   - 添加 placeholder 样式确保占位符正确显示
2025-12-14 21:41:29 +08:00

201 lines
7.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import bcrypt from 'bcryptjs'
import {
BUILTIN_TEMPLATE_KEYS,
ensureBuiltinTemplates,
} from '@/lib/system-integrity'
// 系统初始化:创建默认部门、店铺、管理员账户、内置模板
export async function POST(_req: NextRequest) {
try {
// 从环境变量读取初始管理员配置,如果没有则使用默认值(向后兼容)
const initAdminEmail = process.env.INIT_ADMIN_EMAIL || 'admin@flnxi.com'
const initAdminPassword = process.env.INIT_ADMIN_PASSWORD || '88888888'
const initAdminUsername = initAdminEmail.split('@')[0] // 从邮箱提取用户名
// 使用事务确保所有数据都创建成功或已存在
const result = await prisma.$transaction(async (tx) => {
// 1. 确保默认管理员账户存在
// 优先查找环境变量配置的邮箱,如果没有则查找旧的 'admin'
let admin = await tx.user.findFirst({
where: {
OR: [
{ email: initAdminEmail },
{ email: 'admin' } // 向后兼容旧配置
]
}
})
let adminCreated = false
if (!admin) {
// 检查是否已有任何用户(避免在已有用户的情况下创建默认管理员)
const userCount = await tx.user.count()
if (userCount === 0) {
// 只有在没有任何用户时才创建初始管理员
const hashedPassword = await bcrypt.hash(initAdminPassword, 10)
admin = await tx.user.create({
data: {
email: initAdminEmail,
username: initAdminUsername,
password: hashedPassword,
role: 'ADMIN',
isSuperAdmin: true, // 初始管理员设为超级管理员
isActive: true,
canDownload: true,
department: '系统管理'
}
})
adminCreated = true
} else {
// 如果已有用户,返回第一个管理员用户(如果有)
admin = await tx.user.findFirst({
where: { role: 'ADMIN' }
}) || await tx.user.findFirst()
}
}
const ensureDepartment = async () => {
const existing = await tx.department.findFirst({ where: { name: '默认部门' } })
if (existing) return { entity: existing, created: false }
const created = await tx.department.create({ data: { name: '默认部门' } })
return { entity: created, created: true }
}
const ensureCategory = async () => {
const existing = await tx.category.findFirst({ where: { name: '默认店铺' } })
if (existing) return { entity: existing, created: false }
const created = await tx.category.create({ data: { name: '默认店铺' } })
return { entity: created, created: true }
}
const ensureScene = async () => {
const existing = await tx.scene.findFirst({ where: { name: '默认场景' } })
if (existing) return { entity: existing, created: false }
const created = await tx.scene.create({ data: { name: '默认场景' } })
return { entity: created, created: true }
}
const departmentResult = await ensureDepartment()
const categoryResult = await ensureCategory()
const sceneResult = await ensureScene()
// 注意:内置模板在事务外创建,因为 ensureBuiltinTemplates 有自己的事务处理
// 这里只返回占位结果,实际创建在事务后执行
const templatesPlaceholder = {
total: BUILTIN_TEMPLATE_KEYS.length,
created: 0,
createdTemplates: [] as string[],
}
return {
admin,
defaultDepartment: departmentResult.entity,
defaultCategory: categoryResult.entity,
defaultScene: sceneResult.entity,
templates: templatesPlaceholder,
createdFlags: {
admin: adminCreated,
department: departmentResult.created,
category: categoryResult.created,
scene: sceneResult.created,
}
}
})
// 在事务外创建内置模板(使用统一的模块)
const templatesResult = await ensureBuiltinTemplates()
const alreadyInitialized =
!result.createdFlags.admin &&
!result.createdFlags.department &&
!result.createdFlags.category &&
!result.createdFlags.scene &&
templatesResult.created.length === 0
return NextResponse.json({
success: true,
message: alreadyInitialized ? '系统已初始化' : '系统初始化成功',
initialized: true,
data: {
admin: result.admin ? {
email: result.admin.email,
username: result.admin.username,
password: result.createdFlags.admin ? '(已使用配置的密码)' : '(未创建新账号)'
} : null,
defaultDepartment: result.defaultDepartment.name,
defaultCategory: result.defaultCategory.name,
defaultScene: result.defaultScene.name,
templates: {
total: BUILTIN_TEMPLATE_KEYS.length,
created: templatesResult.created.length,
createdTemplates: templatesResult.created,
errors: templatesResult.errors,
}
}
})
} catch (error: any) {
console.error('系统初始化失败:', error)
return NextResponse.json(
{
success: false,
error: '系统初始化失败',
details: error.message
},
{ status: 500 }
)
}
}
// 检查系统是否已初始化
export async function GET() {
try {
// 优先查找环境变量配置的管理员,如果没有则查找旧的 'admin'
const initAdminEmail = process.env.INIT_ADMIN_EMAIL || 'admin@flnxi.com'
const admin = await prisma.user.findFirst({
where: {
OR: [
{ email: initAdminEmail },
{ email: 'admin' }, // 向后兼容
{ 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
})
} catch (error: any) {
console.error('检查初始化状态失败:', error)
return NextResponse.json(
{
initialized: false,
error: error.message
},
{ status: 500 }
)
}
}