feat(training): 培训配置改造

This commit is contained in:
runny committed 2026-09-03 08:45:05 +08:00
1 parent d21c83433e
commit 15225ef1ce
15 files changed
+602 -56

No files matched your search

@@ -35,7 +35,15 @@ import {
parseAdminTrainingQuestionBank,
updateAdminTrainingConfig,
} from '../../api/admin.api';
import type { TrainingConfig, TrainingConfigPayload, TrainingLevel } from '../../types/training-config.types';
import type {
GenericTrainingStage,
SafetyTrainingStage,
TrainingConfig,
TrainingConfigPayload,
TrainingConfigSettings,
TrainingLevel,
TrainingProgressItem,
} from '../../types/training-config.types';
import TrainingTypeFormModal from './components/TrainingTypeFormModal.vue';
const { t } = useI18n();
@@ -93,6 +101,25 @@ const sectionItems = computed(() => [
const publishedCount = computed(() => configs.value.filter((config) => config.status === 'published').length);
const resourceCount = computed(() => contentResources.value.length);
const configuredLevelCount = computed(() => trainingLevels.filter((level) => resourcesByLevel(level).length > 0).length);
const genericProgressStageOptions: Array<{ value: GenericTrainingStage; label: string }> = [
{ value: 'video', label: t('admin.trainingConfig.progressStages.video') },
{ value: 'summary', label: t('admin.trainingConfig.progressStages.summary') },
{ value: 'exam', label: t('admin.trainingConfig.progressStages.exam') },
{ value: 'result', label: t('admin.trainingConfig.progressStages.result') },
];
const safetyProgressStageOptions: Array<{ value: SafetyTrainingStage; label: string }> = [
{ value: 'company', label: t('admin.trainingConfig.progressStages.company') },
{ value: 'department', label: t('admin.trainingConfig.progressStages.department') },
{ value: 'team', label: t('admin.trainingConfig.progressStages.team') },
{ value: 'exam', label: t('admin.trainingConfig.progressStages.exam') },
];
const currentProgressStageOptions = computed(() => (
form.value.training_type === 'safety' ? safetyProgressStageOptions : genericProgressStageOptions
));
const progressItems = computed(() => {
const settings = form.value.settings as TrainingConfigSettings;
return Array.isArray(settings.progress_items) ? settings.progress_items : [];
});
/** 创建新增培训类型所需的完整默认配置,确保沿用后端既有字段契约。 */
function emptyForm(): TrainingConfigPayload {
@@ -102,11 +129,34 @@ function emptyForm(): TrainingConfigPayload {
version: 'v1.0',
status: 'draft',
pass_score: 90,
settings: { exam_mode: 'unified', questions: [] },
settings: {
exam_mode: 'unified',
questions: [],
progress_items: [],
progress_hint_text: '',
step_intro_text: '',
},
resources: [],
};
}
/** 确保 settings 为可写对象,便于同一页面持续编辑进度项和文案配置。 */
const ensureSettings = (): TrainingConfigSettings => {
if (!form.value.settings || typeof form.value.settings !== 'object' || Array.isArray(form.value.settings)) {
form.value.settings = {};
}
return form.value.settings as TrainingConfigSettings;
};
/** 确保 progress_items 为数组,避免模板编辑时出现空引用。 */
const ensureProgressItems = (): TrainingProgressItem[] => {
const settings = ensureSettings();
if (!Array.isArray(settings.progress_items)) {
settings.progress_items = [];
}
return settings.progress_items;
};
const showDeleteConfirm = computed({
get: () => deleteTarget.value !== null,
set: (visible: boolean) => {
@@ -185,6 +235,63 @@ const updateSettingText = (key: string, value: string) => {
form.value.settings[key] = value.split(/\r?\n/).map((item) => item.trim()).filter(Boolean);
};
/** 新增一条培训进度项,按当前培训类型自动选择首个未使用的 stage。 */
const addProgressItem = () => {
const nextStage = currentProgressStageOptions.value.find((option) => !progressItems.value.some((item) => item.stage === option.value))?.value
|| currentProgressStageOptions.value[0]?.value;
if (!nextStage) return;
ensureProgressItems().push({ stage: nextStage, label: '' });
};
/** 删除一条培训进度项。 */
const removeProgressItem = (index: number) => {
ensureProgressItems().splice(index, 1);
};
/** 校验学员侧文案与进度项,避免空值或超长文本写入发布配置。 */
const validateLearnerCopySettings = () => {
const settings = ensureSettings();
const progressHintText = String(settings.progress_hint_text || '').trim();
const stepIntroText = String(settings.step_intro_text || '').trim();
if (!progressHintText) {
msg.warning(t('admin.trainingConfig.messages.progressHintRequired'));
return false;
}
if (!stepIntroText) {
msg.warning(t('admin.trainingConfig.messages.stepIntroRequired'));
return false;
}
if (progressHintText.length > 500 || stepIntroText.length > 500) {
msg.warning(t('admin.trainingConfig.messages.copyTooLong'));
return false;
}
const allowedStages = new Set<TrainingProgressItem['stage']>(
currentProgressStageOptions.value.map((option) => option.value as TrainingProgressItem['stage']),
);
const seenStages = new Set<string>();
const normalizedItems: TrainingProgressItem[] = [];
for (const item of progressItems.value) {
const stage = String(item.stage || '').trim() as TrainingProgressItem['stage'];
const label = String(item.label || '').trim();
if (!stage || !label || !allowedStages.has(stage)) {
msg.warning(t('admin.trainingConfig.messages.progressItemRequired'));
return false;
}
if (seenStages.has(stage)) {
msg.warning(t('admin.trainingConfig.messages.progressStageDuplicate'));
return false;
}
seenStages.add(stage);
normalizedItems.push({ stage, label });
}
settings.progress_hint_text = progressHintText;
settings.step_intro_text = stepIntroText;
settings.progress_items = normalizedItems;
return true;
};
const settingSummaryByStageText = (level: string) => {
const summaries = (form.value.settings.summaryByStage as Record<string, string[]>) || {};
const value = summaries[level];
@@ -267,6 +374,7 @@ const saveType = async () => {
/** 校验题目 JSON,将题库双写至兼容字段后通过既有更新接口保存完整配置。 */
const saveConfig = async () => {
if (!selected.value) return;
if (!validateLearnerCopySettings()) return;
try {
const questions = JSON.parse(questionJsonText.value);
if (!Array.isArray(questions)) throw new Error();
@@ -445,6 +553,52 @@ onMounted(() => void load());
<label class="label-field">{{ t('admin.trainingConfig.fields.passScore') }}<div class="relative mt-2"><input v-model.number="form.pass_score" type="number" min="0" max="100" class="input-field pr-10" /><span class="pointer-events-none absolute right-3 top-1/2 -translate-y-1/2 text-sm text-muted">%</span></div></label>
<label class="label-field md:col-span-2 xl:col-span-2">{{ t('admin.trainingConfig.fields.examMode') }}<select v-model="form.settings.exam_mode" class="select-field mt-2"><option value="unified">{{ t('admin.trainingConfig.examMode.unified') }}</option><option value="per_level">{{ t('admin.trainingConfig.examMode.perLevel') }}</option></select></label>
</div>
<div class="mt-8 space-y-5 rounded-2xl border border-dashed border-default bg-surface-soft/40 p-5">
<div>
<h4 class="text-sm font-semibold text-primary">{{ t('admin.trainingConfig.copySectionTitle') }}</h4>
<p class="mt-1 text-xs leading-5 text-muted">{{ t('admin.trainingConfig.copySectionDesc') }}</p>
</div>
<div class="space-y-3">
<div class="flex items-center justify-between gap-3">
<label class="label-field">{{ t('admin.trainingConfig.fields.progressItems') }}</label>
<button class="btn-secondary btn-sm" type="button" @click="addProgressItem"><Plus class="h-4 w-4" />{{ t('admin.trainingConfig.addProgressItem') }}</button>
</div>
<div v-if="!progressItems.length" class="rounded-xl border border-dashed border-default bg-white px-4 py-6 text-center text-sm text-muted">
{{ t('admin.trainingConfig.progressItemsEmpty') }}
</div>
<div v-else class="space-y-3">
<div v-for="(item, index) in progressItems" :key="`${item.stage}-${index}`" class="grid gap-3 rounded-xl border border-default bg-white p-4 md:grid-cols-[180px_minmax(0,1fr)_auto] md:items-end">
<label class="label-field">
{{ t('admin.trainingConfig.fields.progressStage') }}
<select v-model="item.stage" class="select-field mt-2">
<option v-for="option in currentProgressStageOptions" :key="option.value" :value="option.value">{{ option.label }}</option>
</select>
</label>
<label class="label-field">
{{ t('admin.trainingConfig.fields.progressItemLabel') }}
<input v-model="item.label" maxlength="100" class="input-field mt-2" :placeholder="t('admin.trainingConfig.placeholders.progressItemLabel')" />
</label>
<button class="btn-danger btn-sm md:mb-0.5" type="button" @click="removeProgressItem(index)"><Trash2 class="h-4 w-4" />{{ t('admin.delete') }}</button>
</div>
</div>
</div>
<div class="grid gap-5 xl:grid-cols-2">
<label class="label-field">
<span class="flex items-center justify-between gap-3">
<span>{{ t('admin.trainingConfig.fields.progressHintText') }}</span>
<span class="text-xs text-muted">{{ `${String((form.settings.progress_hint_text || '')).length}/500` }}</span>
</span>
<textarea v-model="form.settings.progress_hint_text" maxlength="500" class="input-field mt-2 min-h-32" :placeholder="t('admin.trainingConfig.placeholders.progressHintText')" />
</label>
<label class="label-field">
<span class="flex items-center justify-between gap-3">
<span>{{ t('admin.trainingConfig.fields.stepIntroText') }}</span>
<span class="text-xs text-muted">{{ `${String((form.settings.step_intro_text || '')).length}/500` }}</span>
</span>
<textarea v-model="form.settings.step_intro_text" maxlength="500" class="input-field mt-2 min-h-32" :placeholder="t('admin.trainingConfig.placeholders.stepIntroText')" />
</label>
</div>
</div>
</section>
<section id="training-section-resources" class="surface-panel scroll-mt-20 p-5 md:p-7">