feat: 支持评分字段的导入

This commit is contained in:
richarjiang
2026-06-07 09:50:03 +08:00
parent e0b88e68e9
commit 588b6fbc77
8 changed files with 193 additions and 24 deletions

View File

@@ -44,6 +44,9 @@ interface ParsedItem {
hint1: string
hint2: string
hint3: string
// 评分1-5可空
difficultyScore: number | null
funScore: number | null
// 解析警告/错误信息
parseError?: string
// 上传/创建过程状态
@@ -76,6 +79,18 @@ interface RiddleConfigItem {
anchor_text?: string
reference_image?: string
riddle_image?: string
// JSON 里通常是字符串,容忍数字
difficulty_score?: string | number
fun_score?: string | number
}
/**
* 解析评分字段,失败/缺失静默置 null,导入流程不因此阻塞。
*/
function parseScoreField(raw: string | number | undefined): number | null {
if (raw === undefined || raw === null || raw === '') return null
const n = typeof raw === 'number' ? raw : Number(raw)
return Number.isInteger(n) && n >= 1 && n <= 5 ? n : null
}
function truncate(s: string | undefined, max: number): string {
@@ -181,6 +196,8 @@ async function parseConfigJson(
hint1: (hints[0] || '').trim(),
hint2: (hints[1] || '').trim(),
hint3: (hints[2] || '').trim(),
difficultyScore: parseScoreField(config.difficulty_score),
funScore: parseScoreField(config.fun_score),
status: 'pending',
}
@@ -250,6 +267,8 @@ async function parseFolders(files: FileList): Promise<ParsedItem[]> {
hint1: '',
hint2: '',
hint3: '',
difficultyScore: null,
funScore: null,
status: 'pending',
}
@@ -412,6 +431,11 @@ export function BatchImportDialog({
hint1: it.hint1 || '',
hint2: it.hint2 || '',
hint3: it.hint3 || '',
difficultyScore: it.difficultyScore,
funScore: it.funScore,
// 故意不传 position / sortKey。
// 命中存量关卡时POST /api/levels 的 riddleId-update 分支会忽略
// 这两个字段以保护用户手动维护的关卡顺序。
}
const res = await apiFetch('/api/levels', {
@@ -695,6 +719,45 @@ function ItemCard({ index, item, disabled, onChange, onRemove }: ItemCardProps)
/>
</div>
</div>
<div className="col-span-2 grid grid-cols-2 gap-2">
<div className="space-y-1">
<Label className="text-xs"> (1-5)</Label>
<Input
type="number"
min={1}
max={5}
step={1}
value={item.difficultyScore ?? ''}
onChange={(e) =>
onChange({
difficultyScore:
e.target.value === '' ? null : Number(e.target.value),
})
}
disabled={disabled}
placeholder="可选"
/>
</div>
<div className="space-y-1">
<Label className="text-xs"> (1-5)</Label>
<Input
type="number"
min={1}
max={5}
step={1}
value={item.funScore ?? ''}
onChange={(e) =>
onChange({
funScore:
e.target.value === '' ? null : Number(e.target.value),
})
}
disabled={disabled}
placeholder="可选"
/>
</div>
</div>
</div>
</div>
</div>

View File

@@ -27,9 +27,12 @@ interface LevelDialogProps {
onSubmit: (data: LevelFormData) => Promise<void>
}
type FormState = Omit<LevelFormData, 'position'> & {
type FormState = Omit<LevelFormData, 'position' | 'difficultyScore' | 'funScore'> & {
/** 位置字段以字符串保存,便于处理"空输入"状态;提交时解析为数字 */
position: string
/** 评分字段以字符串保存;空串 = 未评分 */
difficultyScore: string
funScore: string
}
const defaultFormState: FormState = {
@@ -42,6 +45,8 @@ const defaultFormState: FormState = {
hint1: '',
hint2: '',
hint3: '',
difficultyScore: '',
funScore: '',
position: '',
}
@@ -85,6 +90,9 @@ export function LevelDialog({
hint1: level.hint1 || '',
hint2: level.hint2 || '',
hint3: level.hint3 || '',
difficultyScore:
level.difficultyScore != null ? String(level.difficultyScore) : '',
funScore: level.funScore != null ? String(level.funScore) : '',
position: String(defaultPos),
})
} else {
@@ -126,6 +134,21 @@ export function LevelDialog({
return
}
// 评分:空串 → null非空必须是 1-5 整数
const parseScoreField = (raw: string): number | null | undefined => {
const trimmed = raw.trim()
if (trimmed === '') return null
const n = Number(trimmed)
if (!Number.isInteger(n) || n < 1 || n > 5) return undefined
return n
}
const difficultyScore = parseScoreField(formData.difficultyScore)
const funScore = parseScoreField(formData.funScore)
if (difficultyScore === undefined || funScore === undefined) {
setError('难度/有趣度评分必须是 1-5 之间的整数')
return
}
// 位置:留空视为"不改位置"(编辑)/ "追加末尾"(创建)
let position: number | undefined
const raw = formData.position.trim()
@@ -153,6 +176,8 @@ export function LevelDialog({
hint1: formData.hint1,
hint2: formData.hint2,
hint3: formData.hint3,
difficultyScore,
funScore,
...(position !== undefined ? { position } : {}),
}
@@ -316,6 +341,45 @@ export function LevelDialog({
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="difficultyScore"> (1-5,)</Label>
<Input
id="difficultyScore"
type="number"
min={1}
max={5}
step={1}
value={formData.difficultyScore}
onChange={(e) =>
setFormData((prev) => ({
...prev,
difficultyScore: e.target.value,
}))
}
placeholder="1-5"
/>
</div>
<div className="space-y-2">
<Label htmlFor="funScore"> (1-5,)</Label>
<Input
id="funScore"
type="number"
min={1}
max={5}
step={1}
value={formData.funScore}
onChange={(e) =>
setFormData((prev) => ({
...prev,
funScore: e.target.value,
}))
}
placeholder="1-5"
/>
</div>
</div>
<DialogFooter>
<Button
type="button"

View File

@@ -6,9 +6,9 @@ import { Pencil, Trash2 } from 'lucide-react'
import Image from 'next/image'
// 列宽定义header 和 row 必须保持一致。用 grid-template-columns 统一控制。
// 序号 | 图片 | 答案 | 谐音梗 | 提示 | 创建时间 | 操作
// 序号 | 图片 | 答案 | 谐音梗 | 提示 | 创建时间 | 评分 | 操作
export const GRID_TEMPLATE =
'minmax(60px,60px) minmax(120px,120px) minmax(80px,1fr) minmax(100px,1fr) minmax(160px,2fr) minmax(100px,100px) minmax(100px,100px)'
'minmax(60px,60px) minmax(120px,120px) minmax(80px,1fr) minmax(100px,1fr) minmax(160px,2fr) minmax(100px,100px) minmax(110px,110px) minmax(100px,100px)'
interface LevelRowProps {
level: Level
@@ -91,6 +91,13 @@ export function LevelRow({ level, index, onEdit, onDelete }: LevelRowProps) {
})}
</span>
{/* 评分 */}
<span className="text-xs text-muted-foreground whitespace-nowrap">
{level.difficultyScore != null || level.funScore != null
? `难度 ${level.difficultyScore ?? '-'} · 有趣 ${level.funScore ?? '-'}`
: '—'}
</span>
{/* 操作 */}
<div className="flex items-center gap-2">
<Button

View File

@@ -19,6 +19,7 @@ const HEADER_COLUMNS = [
'谐音梗',
'提示',
'创建时间',
'评分',
'操作',
]