WuYongJie 8705b9846a
Some checks failed
CI Check / check (push) Has been cancelled
feat: 统一弹窗样式系统与分类管理优化
- 创建统一的Modal组件,采用Apple风格设计
- 实现毛玻璃效果、圆角、动画和细边框描边
- 修复弹窗打开时背景滚动问题
- 修复分类管理中重复卡片和数组字符串显示问题
- 优化选项解析逻辑,添加去重和过滤机制
2025-11-25 09:37:00 +08:00

92 lines
2.8 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.

/**
* 模板列表 API
*
* 获取所有启用的模板列表(包括预制模板和自定义模板)
* 用于首页TAG筛选
*/
import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
// 预制模板配置
const BUILTIN_TEMPLATE_KEYS = [
'3dmax-material',
'psd-material',
'ai-material',
'video-material',
'model-material',
'other-material',
]
const BUILTIN_TEMPLATE_NAMES: Record<string, string> = {
'3dmax-material': '3DMAX 素材模板',
'psd-material': 'PSD 素材模板',
'ai-material': 'AI 工作流',
'video-material': '视频素材模板',
'model-material': '模特素材模板',
'other-material': '其他素材模板',
}
function createBuiltinTemplate(key: string) {
return {
id: `builtin-${key}`,
name: BUILTIN_TEMPLATE_NAMES[key] || key,
isBuiltin: true,
}
}
export async function GET() {
try {
// 获取启用的预制模板列表
let enabledBuiltinTemplates: string[] = []
const ALL_BUILTIN_KEYS = BUILTIN_TEMPLATE_KEYS // 所有预制模板key
try {
// 使用原始 SQL 查询,避免字段不存在时的错误
const settings = await prisma.$queryRaw<Array<{ enabledBuiltinTemplates?: string | null }>>`
SELECT "enabledBuiltinTemplates" FROM "system_settings" LIMIT 1
`
if (settings[0]?.enabledBuiltinTemplates) {
const parsed = JSON.parse(settings[0].enabledBuiltinTemplates)
if (Array.isArray(parsed) && parsed.length > 0) {
enabledBuiltinTemplates = parsed
}
// 如果为空数组或null,表示全部启用,enabledBuiltinTemplates保持为空数组
}
// 如果字段不存在或为null,表示全部启用,enabledBuiltinTemplates保持为空数组
} catch (error) {
// 字段不存在或解析失败,表示全部启用
console.error('Failed to parse enabledBuiltinTemplates:', error)
}
// 获取所有启用的自定义模板
const customTemplates = await prisma.uploadTemplate.findMany({
where: {
status: 'active',
},
select: {
id: true,
name: true,
},
orderBy: { updatedAt: 'desc' },
})
// 生成所有预制模板,只包含启用的
// 如果 enabledBuiltinTemplates 为空数组,表示全部启用
const builtinTemplates = BUILTIN_TEMPLATE_KEYS
.filter(key => enabledBuiltinTemplates.length === 0 || enabledBuiltinTemplates.includes(key))
.map(key => createBuiltinTemplate(key))
// 合并预制模板和自定义模板,预制模板在前
const allTemplates = [
...builtinTemplates,
...customTemplates.map(t => ({ ...t, isBuiltin: false })),
]
return NextResponse.json(allTemplates)
} catch (error) {
console.error('Error fetching templates:', error)
return NextResponse.json({ error: '获取模板列表失败' }, { status: 500 })
}
}