Files
MindScope/lib/assessment-export.ts
T

54 lines
2.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.
import { AssessmentProfile, AssessmentRecord } from '@/lib/assessment-types';
function date(value: string) {
return new Date(value).toLocaleString('zh-CN', { hour12: false });
}
export function recordToMarkdown(record: AssessmentRecord, profileName: string) {
const lines = [
`# ${record.questionnaireTitle}测评记录`, '',
'## 基本信息',
`- 档案:${profileName}`,
`- 测评时间:${date(record.completedAt)}`,
`- 量表编号:${record.questionnaireId}`,
`- 题目数量:${record.answers.length}`, '',
];
if (record.scoreSummary.metrics.length) {
lines.push('## 分数摘要');
record.scoreSummary.metrics.forEach((item) => {
lines.push(`- ${item.label}${item.value}${item.max ? ` / ${item.max}` : ''}${item.level ? `${item.level}` : ''}`);
});
if (record.scoreSummary.note) lines.push('', record.scoreSummary.note);
lines.push('');
}
if (record.analysisText) lines.push('## 结果说明', record.analysisText, '');
lines.push('## 完整问答');
record.answers.forEach((item, index) => lines.push(`${index + 1}. ${item.question}`, ` 回答:${item.answer}`, ''));
lines.push('## 使用说明', '本记录仅供自我了解、教育和研究参考,不构成医学或心理诊断。');
return lines.join('\n');
}
export function profileToMarkdown(profile: AssessmentProfile, records: AssessmentRecord[]) {
const lines = [
`# ${profile.name}的完整测评档案`, '',
`- 导出时间:${date(new Date().toISOString())}`,
`- 测评次数:${records.length}`, '',
'---', '',
];
records
.slice()
.sort((a, b) => a.completedAt.localeCompare(b.completedAt))
.forEach((record) => lines.push(recordToMarkdown(record, profile.name), '', '---', ''));
return lines.join('\n');
}
export function downloadText(filename: string, content: string, type: string) {
const blob = new Blob([content], { type });
const url = URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = url;
anchor.download = filename;
anchor.click();
URL.revokeObjectURL(url);
}