fix: 修复考试题目选项数据缺失问题

- 在GetExam API中使用convertToDTO函数转换题目数据
- 确保选项数据(options)正确解析并返回给前端
- 修复前端ExamOnline渲染选择题时的undefined错误

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
燕陇琪 2025-11-08 21:20:34 +08:00
parent 52fff11f07
commit a62c5b3e62

View File

@ -140,6 +140,9 @@ func GetExam(c *gin.Context) {
return return
} }
// 是否显示答案
showAnswer := c.Query("show_answer") == "true"
// 查询题目详情 // 查询题目详情
var questions []models.PracticeQuestion var questions []models.PracticeQuestion
if err := db.Where("id IN ?", questionIDs).Find(&questions).Error; err != nil { if err := db.Where("id IN ?", questionIDs).Find(&questions).Error; err != nil {
@ -148,24 +151,20 @@ func GetExam(c *gin.Context) {
return return
} }
// 按原始顺序排序题目 // 按原始顺序排序题目并转换为DTO
questionMap := make(map[uint]models.PracticeQuestion) questionMap := make(map[uint]models.PracticeQuestion)
for _, q := range questions { for _, q := range questions {
questionMap[q.ID] = q questionMap[q.ID] = q
} }
orderedQuestions := make([]models.PracticeQuestion, 0, len(questionIDs)) orderedDTOs := make([]models.PracticeQuestionDTO, 0, len(questionIDs))
for _, id := range questionIDs { for _, id := range questionIDs {
if q, ok := questionMap[id]; ok { if q, ok := questionMap[id]; ok {
orderedQuestions = append(orderedQuestions, q) dto := convertToDTO(q)
} // 是否显示答案
} if !showAnswer {
dto.Answer = nil
// 是否显示答案 }
showAnswer := c.Query("show_answer") == "true" orderedDTOs = append(orderedDTOs, dto)
if !showAnswer {
// 不显示答案时,隐藏答案字段
for i := range orderedQuestions {
orderedQuestions[i].AnswerData = ""
} }
} }
@ -173,7 +172,7 @@ func GetExam(c *gin.Context) {
"success": true, "success": true,
"data": gin.H{ "data": gin.H{
"exam": exam, "exam": exam,
"questions": orderedQuestions, "questions": orderedDTOs,
}, },
}) })
} }