fix(安全培训): 解决下载培训报告内容显示不全问题

This commit is contained in:
runny committed 2026-08-18 16:14:51 +08:00
1 parent 31182a83d7
commit 8cadc6cc17
1 file changed
+124 -19
@@ -31,7 +31,7 @@ import FilePreviewModal from '@/shared/components/FilePreviewModal.vue';
import { useMessage } from '@/shared/composables/useMessage';
import { resolveTrainingExamPdfGuide, type TrainingGuideLike } from '@/features/chat/training/trainingWorkspace.utils';
import { getMyTrainingConfig } from '@/features/chat/api/training.api';
import type { TrainingRecord } from '@/features/chat/training/training.types';
import type { TrainingAnswerRecord, TrainingRecord, TrainingQuestion } from '@/features/chat/training/training.types';
import html2pdf from 'html2pdf.js';
import { getAuthHeaders } from '@/shared/utils/auth';
@@ -103,21 +103,89 @@ const handleVideoEnded = () => {
const reportModalVisible = ref(false);
const currentReportRecord = ref<TrainingRecord | null>(null);
const reportQuestions = ref<Record<string, any>>({});
const reportQuestions = ref<Record<string, TrainingQuestion>>({});
const reportLoading = ref(false);
/**
* 将学员文本答案回填到题干中的横线位置。
* 核心逻辑:仅替换填空题题干的连续下划线;若题干没有横线或答案为空,则保留原题干,避免改变非填空题与历史题目展示。
* @param question - 当前题目的题库定义。
* @param answer - 该题已保存的员工答题记录。
* @returns 含回填答案的题干片段列表,模板按 `isAnswer` 区分答案样式。
*/
const buildReportPromptParts = (question: TrainingQuestion | undefined, answer?: TrainingAnswerRecord) => {
const prompt = question?.prompt || '';
const textAnswer = String(answer?.answerText || '').trim();
if (question?.type !== 'fill' || !textAnswer || !/_{2,}/.test(prompt)) {
return [{ text: prompt, isAnswer: false }];
}
const parts = prompt.split(/(_{2,})/g);
let answerFilled = false;
return parts.map((part) => {
if (!answerFilled && /^_{2,}$/.test(part)) {
answerFilled = true;
return { text: textAnswer, isAnswer: true };
}
return { text: part, isAnswer: false };
});
};
/**
* 将判断题的员工答案转换为报告中的可读文本。
* 核心逻辑:优先按 selectedOptionId 查找题库选项;历史题库没有选项时兼容 true/false、是/否等值。
* @param question - 当前题目的题库定义。
* @param answer - 当前题目的员工答题记录。
* @returns 员工实际作答的答案文本;无法解析时返回原始选项 ID。
*/
const resolveReportAnswerLabel = (question: TrainingQuestion | undefined, answer: TrainingAnswerRecord) => {
if (!question) return answer.selectedOptionId || answer.answerText || '--';
if (question.type === 'fill' || question.type === 'essay') return answer.answerText || '--';
return question.options?.find((option) => option.id === answer.selectedOptionId)?.label
|| answer.selectedOptionId
|| '--';
};
/**
* 组合报告题库与实际答题记录,确保未作答题目仍出现在考试报告。
* 核心逻辑:以当前发布题库为完整题目顺序,按题目 ID 合并答题记录;再保留历史记录中的已下线题目,避免旧考试报告丢失内容。
* @returns 用于在线预览与 PDF 下载的完整题目及答案行。
*/
const reportQuestionRows = computed(() => {
const answers = currentReportRecord.value?.answerDetails || [];
const answersByQuestionId = new Map(answers.map((answer) => [answer.questionId, answer]));
const configuredQuestions = Object.values(reportQuestions.value);
const configuredQuestionIds = new Set(configuredQuestions.map((question) => question.id));
const defaultAnswer = (questionId: string): TrainingAnswerRecord => ({
questionId,
selectedOptionId: '',
answerText: '',
correct: false,
});
return [
...configuredQuestions.map((question) => ({
question,
answer: answersByQuestionId.get(question.id) || defaultAnswer(question.id),
})),
...answers
.filter((answer) => !configuredQuestionIds.has(answer.questionId))
.map((answer) => ({ question: undefined, answer })),
];
});
const openReport = async (record: TrainingRecord) => {
currentReportRecord.value = record;
reportModalVisible.value = true;
reportLoading.value = true;
try {
const config = await getMyTrainingConfig(record.trainingType || 'onboarding');
const qMap: Record<string, any> = {};
const qMap: Record<string, TrainingQuestion> = {};
if (config?.resources) {
config.resources.forEach(r => {
config.resources.forEach((r) => {
if (Array.isArray(r.inlineQuestions)) {
r.inlineQuestions.forEach((q: any) => {
qMap[q.id] = q;
r.inlineQuestions.forEach((q: unknown) => {
const question = q as TrainingQuestion;
qMap[question.id] = question;
});
}
});
@@ -1563,43 +1631,55 @@ const currentSafetyStageText = computed(() => {
<!-- Exam Details -->
<div class="space-y-6">
<div v-for="(answer, idx) in currentReportRecord.answerDetails || []" :key="answer.questionId" class="report-question rounded-xl border border-default p-4">
<div v-for="(row, idx) in reportQuestionRows" :key="row.answer.questionId" class="report-question rounded-xl border border-default p-4">
<div class="report-question-header mb-3">
<div class="report-question-title font-medium text-primary">
<span class="mr-2 text-secondary">{{ idx + 1 }}.</span>
{{ reportQuestions[answer.questionId]?.prompt || `[ID: ${answer.questionId}]` }}
<template v-if="row.question">
<template v-for="(part, partIndex) in buildReportPromptParts(row.question, row.answer)" :key="partIndex">
<span v-if="part.isAnswer" class="report-filled-answer">{{ part.text }}</span>
<span v-else>{{ part.text }}</span>
</template>
</template>
<template v-else>[ID: {{ row.answer.questionId }}]</template>
</div>
<div class="report-question-result">
<span v-if="answer.correct" class="inline-flex rounded-full bg-emerald-50 px-2 py-1 text-xs font-semibold text-emerald-700">正确</span>
<span v-if="row.answer.correct" class="inline-flex rounded-full bg-emerald-50 px-2 py-1 text-xs font-semibold text-emerald-700">正确</span>
<span v-else class="inline-flex rounded-full bg-rose-50 px-2 py-1 text-xs font-semibold text-rose-700">错误</span>
</div>
</div>
<div class="report-options pl-6">
<div v-for="(opt, optIdx) in reportQuestions[answer.questionId]?.options || []" :key="opt.id"
<div v-if="row.question?.type === 'boolean'" class="report-answer-row text-sm">
员工作答:<span class="report-employee-answer">{{ resolveReportAnswerLabel(row.question, row.answer) }}</span>
</div>
<div v-else-if="row.question?.type === 'essay'" class="report-answer-row text-sm">
员工作答:<span class="report-employee-answer report-essay-answer">{{ resolveReportAnswerLabel(row.question, row.answer) }}</span>
</div>
<div v-for="(opt, optIdx) in row.question?.options || []" :key="opt.id"
class="report-option text-sm"
:class="{
'font-semibold text-emerald-600': opt.id === reportQuestions[answer.questionId]?.answer,
'text-rose-600': !answer.correct && opt.id === answer.selectedOptionId,
'text-secondary': opt.id !== reportQuestions[answer.questionId]?.answer && opt.id !== answer.selectedOptionId
'font-semibold text-emerald-600': opt.id === row.question?.answer,
'text-rose-600': !row.answer.correct && opt.id === row.answer.selectedOptionId,
'text-secondary': opt.id !== row.question?.answer && opt.id !== row.answer.selectedOptionId
}"
>
<span class="report-option-marker"
:class="{
'border-emerald-200 bg-emerald-50 text-emerald-600': opt.id === reportQuestions[answer.questionId]?.answer,
'border-rose-200 bg-rose-50 text-rose-600': !answer.correct && opt.id === answer.selectedOptionId,
'border-default': opt.id !== reportQuestions[answer.questionId]?.answer && opt.id !== answer.selectedOptionId
'border-emerald-200 bg-emerald-50 text-emerald-600': opt.id === row.question?.answer,
'border-rose-200 bg-rose-50 text-rose-600': !row.answer.correct && opt.id === row.answer.selectedOptionId,
'border-default': opt.id !== row.question?.answer && opt.id !== row.answer.selectedOptionId
}"
> {{ String.fromCharCode(65 + Number(optIdx)) }}</span>
<span class="report-option-text">{{ opt.label }}</span>
</div>
<div v-if="!reportQuestions[answer.questionId]" class="text-sm text-secondary">
<div class="mb-1">Selected Option: {{ answer.selectedOptionId }}</div>
<div v-if="!row.question" class="text-sm text-secondary">
<div class="mb-1">员工作答:{{ resolveReportAnswerLabel(undefined, row.answer) }}</div>
</div>
</div>
</div>
<div v-if="!currentReportRecord.answerDetails?.length" class="text-center text-sm text-secondary py-8">
<div v-if="!reportQuestionRows.length" class="text-center text-sm text-secondary py-8">
{{ $t('chat.training.hr.report.noReport') }}
</div>
</div>
@@ -1634,6 +1714,31 @@ const currentSafetyStageText = computed(() => {
line-height: 1.5;
}
.report-filled-answer {
display: inline-block;
min-width: 5rem;
margin: 0 0.15rem;
padding: 0 0.25rem;
border-bottom: 1px solid currentColor;
color: #2563eb;
font-weight: 700;
text-align: center;
}
.report-answer-row {
color: #475569;
line-height: 1.6;
}
.report-employee-answer {
color: #2563eb;
font-weight: 700;
}
.report-essay-answer {
white-space: pre-wrap;
}
.report-question-result {
flex-shrink: 0;
white-space: nowrap;