WuYongJie d1c6efc79d
Some checks failed
CI Check / check (push) Has been cancelled
feat: NAS 部署与 Harbor 推送流程
2025-12-03 14:39:06 +08:00

135 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.

/**
* 公共上传模板 API
*
* 获取启用的上传模板列表(不需要管理员权限)
* 包括预制模板和自定义模板,并做区分
* 用于上传页面选择模板
*/
import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import {
BUILTIN_TEMPLATE_KEYS,
BUILTIN_TEMPLATE_TYPES,
BUILTIN_TEMPLATE_NAMES,
BUILTIN_TEMPLATE_DESCRIPTIONS,
STANDARD_FIELDS,
BUILTIN_TEMPLATE_FIELDS,
createBuiltinTemplate,
} from '@/lib/upload-template-presets'
const publicSelect = {
id: true,
name: true,
description: true,
status: true,
materialType: true,
materialKind: true,
fields: {
where: {
// 只返回非只读字段,只读字段不需要用户填写
// 自定义模板中过滤掉 materialType 字段
isReadOnly: false,
key: { not: 'materialType' },
},
orderBy: { orderIndex: 'asc' },
select: {
id: true,
key: true,
label: true,
type: true,
required: true,
orderIndex: true,
layoutWidth: true,
validations: true,
optionsSource: true,
optionsStatic: true,
allowedExtensions: true,
visibleOnHomeFilter: true,
isSearchable: true,
addToCategory: true,
conditions: true,
},
},
} as const
export async function GET() {
try {
// 获取启用的预制模板列表
let enabledBuiltinTemplates: string[] = []
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',
},
orderBy: { updatedAt: 'desc' },
select: publicSelect,
})
// 生成所有预制模板,只包含启用的
// 如果 enabledBuiltinTemplates 为空数组,表示全部启用
const builtinTemplates = BUILTIN_TEMPLATE_KEYS
.filter(key => enabledBuiltinTemplates.length === 0 || enabledBuiltinTemplates.includes(key))
.map(key => createBuiltinTemplate(key))
// 合并预制模板和自定义模板,预制模板在前
const allTemplates = [
...builtinTemplates.map(t => ({ ...t, isBuiltin: true })),
...customTemplates.map(t => ({ ...t, isBuiltin: false })),
]
// 统计每个模板的素材数量,用于前端动态排序
const templateCounts = await Promise.all(
allTemplates.map((template) => {
if (template.isBuiltin) {
const key = template.id.replace('builtin-', '')
const materialType = BUILTIN_TEMPLATE_TYPES[key] || template.materialType
return prisma.material.count({
where: {
materialType,
OR: [
{ templateId: null },
{ templateId: template.id },
],
},
})
}
return prisma.material.count({
where: {
templateId: template.id,
},
})
})
)
const templatesWithCounts = allTemplates.map((template, index) => ({
...template,
materialCount: templateCounts[index],
}))
return NextResponse.json(templatesWithCounts)
} catch (error) {
console.error('Get active upload templates error:', error)
return NextResponse.json({ error: '获取上传模板失败' }, { status: 500 })
}
}