const app = getApp() const getReviewSettings = () => { const settings = app.globalData.userSettings || {} return { autoNextQuestion: Number(settings.auto_next_question || 0) === 1, showAnswerAfterSubmit: Number(settings.show_answer_after_submit || 0) === 1, blankCount: Number(settings.blank_count || 2), blankDifficulty: settings.blank_difficulty || 'normal', } } Page({ data: { progress: 0, current: 1, total: 0, word: '', meaning: '', answer: '', answers: [], clozeAnswers: [], feedback: '', revealAnswer: '', feedbackType: '', showFeedback: false, completed: false, masterHint: false, questions: [], loading: false, error: '', settings: getReviewSettings(), questionBadge: '', entryTypeHint: '', isCloze: false, currentClozeQuestion: null, currentClozeResults: [], showClozeFeedback: false, clozeSegments: [], }, submitting: false, judged: false, onLoad() { this.loadSettingsAndQuestions() }, loadSettingsAndQuestions() { if (!app.globalData.openid) { this.setData({ error: '请先登录' }) return } wx.request({ url: `${app.globalData.baseUrl}/api/v1/settings`, header: { 'X-OpenID': app.globalData.openid }, success: (res) => { app.globalData.userSettings = res.data?.data || {} this.setData({ settings: getReviewSettings() }) this.loadQuestions() }, fail: () => { this.setData({ error: '设置加载失败' }) this.loadQuestions() }, }) }, buildClozeSegments(clozeQuestion) { if (!clozeQuestion || !Array.isArray(clozeQuestion.blanks)) return [] const prompt = clozeQuestion.prompt_text || '' const segments = [] let blankIndex = 0 if (prompt.includes('____')) { prompt.split('____').forEach((text, index, parts) => { if (text) segments.push({ type: 'text', text, index: `text-${index}` }) if (index < parts.length - 1) { segments.push({ type: 'blank', blankIndex, index: `blank-${blankIndex}` }) blankIndex += 1 } }) return segments } const blanks = clozeQuestion.blanks || [] let cursor = 0 blanks.forEach((blank, idx) => { const start = Math.max(0, Number(blank.start || 0)) const end = Math.max(start, Number(blank.end || start + 1)) if (start > cursor) { segments.push({ type: 'text', text: prompt.slice(cursor, start), index: `text-${idx}` }) } segments.push({ type: 'blank', blankIndex: idx, index: `blank-${idx}` }) cursor = end }) if (cursor < prompt.length) { segments.push({ type: 'text', text: prompt.slice(cursor), index: `text-tail` }) } return segments }, loadQuestions() { const session = app.globalData.reviewSessionMeta || wx.getStorageSync('reviewSessionMeta') if (!session?.id) { this.setData({ error: '暂无复习会话' }) return } app.globalData.reviewSessionMeta = session this.setData({ loading: true, error: '' }) wx.request({ url: `${app.globalData.baseUrl}/api/v1/review/questions?session_id=${session.id}`, header: { 'X-OpenID': app.globalData.openid || '' }, success: (res) => { if (res.statusCode < 200 || res.statusCode >= 300) { const message = res.data?.detail || res.data?.message || '题目加载失败' this.setData({ error: message, questions: [] }) return } const questions = (res.data?.data || []).map((item) => { if (item.question_mode === 'C') { return { ...item, blank_prompt: item.blank_prompt || item.cloze_question?.prompt_text || item.prompt, cloze_question: item.cloze_question || null, } } return { ...item, entry_type: item.entry_type || 'word', } }) if (!questions.length) { this.setData({ error: '当前复习会话没有题目,请返回重新选择范围', questions: [] }) return } app.globalData.reviewSession = { deck: questions, currentIndex: 0, correctCount: 0, wrongCount: 0, completed: false, currentAnswer: '', currentAnswers: [], startedAt: Date.now(), } this.setData({ questions }) this.syncSession() }, fail: () => this.setData({ error: '题目加载失败' }), complete: () => this.setData({ loading: false }), }) }, syncSession() { const session = app.globalData.reviewSession const current = session.deck[session.currentIndex] const total = session.deck.length || 1 const isCloze = current?.question_mode === 'C' const clozeQuestion = isCloze ? (current?.cloze_question || null) : null this.setData({ progress: Math.round((session.currentIndex / total) * 100), current: session.currentIndex + 1, total, word: isCloze ? (current?.blank_prompt || clozeQuestion?.prompt_text || current?.prompt || '') : (current?.prompt || ''), meaning: isCloze ? (current?.answer_hint || '') : '', answer: session.currentAnswer || '', clozeAnswers: isCloze ? (Array.isArray(session.currentAnswers) ? session.currentAnswers : Array.from({ length: currentClozeQuestion?.blanks?.length || 0 }, () => '')) : [], feedback: '', revealAnswer: '', feedbackType: '', showFeedback: false, completed: session.completed, masterHint: false, questionBadge: isCloze ? `填空 · ${current?.entry_type === 'sentence' ? '句子' : '单词'}` : `${current?.entry_type === 'sentence' ? '句子' : '单词'} · ${current?.question_mode || 'A'}`, entryTypeHint: isCloze ? '请逐空作答,提交后会显示每个空的结果。' : (current?.entry_type === 'sentence' ? '当前题目来自句子库,注意上下文表达。' : ''), isCloze, currentClozeQuestion: clozeQuestion, currentClozeResults: session.lastClozeResults || [], showClozeFeedback: false, clozeSegments: isCloze ? this.buildClozeSegments(clozeQuestion) : [], }) }, onAnswerInput(e) { const answer = e.detail.value app.globalData.reviewSession.currentAnswer = answer this.setData({ answer, showFeedback: false, revealAnswer: '', feedback: '', feedbackType: '', judged: false, showClozeFeedback: false }) }, onClozeAnswerInput(e) { const { index } = e.currentTarget.dataset const value = e.detail.value const answers = [...(app.globalData.reviewSession.currentAnswers || [])] answers[index] = value app.globalData.reviewSession.currentAnswers = answers this.setData({ [`clozeAnswers[${index}]`]: value, showFeedback: false, revealAnswer: '', feedback: '', feedbackType: '', judged: false, showClozeFeedback: false, }) }, submitAnswer(showExplanation = false, advanceAfterSubmit = false) { const session = app.globalData.reviewSession const current = session.deck[session.currentIndex] if (!current || this.submitting || this.judged) return const answer = (this.data.answer || '').trim() const clozeQuestion = current.cloze_question const payload = { session_id: app.globalData.reviewSessionMeta.id, record_id: current.record_id, user_answer: answer, } if (current.question_mode === 'C' && clozeQuestion?.blanks?.length) { const answers = (this.data.clozeAnswers || []).map((item) => (item || '').trim()) while (answers.length < clozeQuestion.blanks.length) answers.push('') if (!answers.some((item) => item)) { wx.showToast({ title: '请先输入答案', icon: 'none' }) return } payload.user_answers = answers } else if (!answer) { wx.showToast({ title: '请先输入答案', icon: 'none' }) return } this.submitting = true wx.request({ url: `${app.globalData.baseUrl}/api/v1/review/submit`, method: 'POST', header: { 'Content-Type': 'application/json', 'X-OpenID': app.globalData.openid || '' }, data: payload, success: (res) => { const result = res.data?.data || {} session.correctCount += result.is_correct ? 1 : 0 session.wrongCount += result.is_correct ? 0 : 1 this.judged = true const shouldShowAnswer = showExplanation || this.data.settings.showAnswerAfterSubmit const correctAnswerText = result.correct_answer_text || '' const clozeResults = Array.isArray(result.cloze_results) ? result.cloze_results : [] if (clozeResults.length) { session.lastClozeResults = clozeResults app.globalData.reviewSession.lastClozeResults = clozeResults } this.setData({ feedback: result.is_correct ? '回答正确' : '回答错误', feedbackType: result.is_correct ? 'correct' : 'wrong', revealAnswer: shouldShowAnswer && correctAnswerText ? `正确答案:${correctAnswerText}` : '', showFeedback: true, showClozeFeedback: clozeResults.length > 0, currentClozeResults: clozeResults, }) if (this.data.isCloze) { app.globalData.reviewSession.currentAnswers = [] this.setData({ clozeAnswers: [] }) } if (this.data.settings.autoNextQuestion || advanceAfterSubmit) { setTimeout(() => this.nextQuestion(), shouldShowAnswer ? 1200 : 0) } }, complete: () => { this.submitting = false }, }) }, checkAnswer() { this.submitAnswer(true) }, nextQuestion() { const session = app.globalData.reviewSession if (!session.deck.length) return if (!this.judged) { this.submitAnswer(true, true) return } this.judged = false if (session.currentIndex >= session.deck.length - 1) { session.completed = true session.endedAt = Date.now() wx.request({ url: `${app.globalData.baseUrl}/api/v1/review/finish?session_id=${app.globalData.reviewSessionMeta.id}`, method: 'POST', header: { 'X-OpenID': app.globalData.openid || '' }, success: () => wx.navigateTo({ url: '/pages/review-result/review-result' }), fail: () => { wx.showToast({ title: '提交结果失败,已本地完成', icon: 'none' }) wx.navigateTo({ url: '/pages/review-result/review-result' }) }, }) return } session.currentIndex += 1 session.currentAnswer = '' session.currentAnswers = [] this.syncSession() }, markMastered() { this.setData({ masterHint: true }) wx.showToast({ title: '已标记', icon: 'success' }) }, })