From a62c5b3e62f4eaf6de003bd414e34fb262ad93c1 Mon Sep 17 00:00:00 2001 From: yanlongqi Date: Sat, 8 Nov 2025 21:20:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=80=83=E8=AF=95?= =?UTF-8?q?=E9=A2=98=E7=9B=AE=E9=80=89=E9=A1=B9=E6=95=B0=E6=8D=AE=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在GetExam API中使用convertToDTO函数转换题目数据 - 确保选项数据(options)正确解析并返回给前端 - 修复前端ExamOnline渲染选择题时的undefined错误 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- internal/handlers/exam_handler.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/internal/handlers/exam_handler.go b/internal/handlers/exam_handler.go index a9ed86c..11b6a18 100644 --- a/internal/handlers/exam_handler.go +++ b/internal/handlers/exam_handler.go @@ -140,6 +140,9 @@ func GetExam(c *gin.Context) { return } + // 是否显示答案 + showAnswer := c.Query("show_answer") == "true" + // 查询题目详情 var questions []models.PracticeQuestion if err := db.Where("id IN ?", questionIDs).Find(&questions).Error; err != nil { @@ -148,24 +151,20 @@ func GetExam(c *gin.Context) { return } - // 按原始顺序排序题目 + // 按原始顺序排序题目并转换为DTO questionMap := make(map[uint]models.PracticeQuestion) for _, q := range questions { questionMap[q.ID] = q } - orderedQuestions := make([]models.PracticeQuestion, 0, len(questionIDs)) + orderedDTOs := make([]models.PracticeQuestionDTO, 0, len(questionIDs)) for _, id := range questionIDs { if q, ok := questionMap[id]; ok { - orderedQuestions = append(orderedQuestions, q) - } - } - - // 是否显示答案 - showAnswer := c.Query("show_answer") == "true" - if !showAnswer { - // 不显示答案时,隐藏答案字段 - for i := range orderedQuestions { - orderedQuestions[i].AnswerData = "" + dto := convertToDTO(q) + // 是否显示答案 + if !showAnswer { + dto.Answer = nil + } + orderedDTOs = append(orderedDTOs, dto) } } @@ -173,7 +172,7 @@ func GetExam(c *gin.Context) { "success": true, "data": gin.H{ "exam": exam, - "questions": orderedQuestions, + "questions": orderedDTOs, }, }) }