kydc/review-session/review-session.js

190 lines
5.9 KiB
JavaScript

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: '',
feedback: '',
revealAnswer: '',
feedbackType: '',
showFeedback: false,
completed: false,
masterHint: false,
questions: [],
loading: false,
error: '',
settings: getReviewSettings(),
},
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()
},
})
},
loadQuestions() {
const session = app.globalData.reviewSessionMeta
if (!session) {
this.setData({ error: '暂无复习会话' })
return
}
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) => {
const questions = (res.data?.data || []).map((item) => {
if (item.question_mode === 'C') {
return {
...item,
blank_prompt: item.blank_prompt || item.prompt,
}
}
return item
})
app.globalData.reviewSession = {
deck: questions,
currentIndex: 0,
correctCount: 0,
wrongCount: 0,
completed: false,
currentAnswer: '',
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
this.setData({
progress: Math.round((session.currentIndex / total) * 100),
current: session.currentIndex + 1,
total,
word: current?.question_mode === 'C' ? (current?.blank_prompt || current?.prompt || '') : (current?.prompt || ''),
meaning: current?.question_mode === 'C' ? (current?.answer_hint || '') : '',
answer: session.currentAnswer || '',
feedback: '',
revealAnswer: '',
feedbackType: '',
showFeedback: false,
completed: session.completed,
masterHint: false,
})
},
onAnswerInput(e) {
const answer = e.detail.value
app.globalData.reviewSession.currentAnswer = answer
this.setData({ answer, showFeedback: false, revealAnswer: '', feedback: '', feedbackType: '', judged: false })
},
submitAnswer(showExplanation = 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()
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: {
session_id: app.globalData.reviewSessionMeta.id,
entry_id: current.entry_id,
question_mode: current.question_mode,
user_answer: answer,
},
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
this.setData({
feedback: result.is_correct ? '回答正确' : '回答错误',
feedbackType: result.is_correct ? 'correct' : 'wrong',
revealAnswer: shouldShowAnswer ? `正确答案:${result.correct_answer}` : '',
showFeedback: true,
})
if (this.data.settings.autoNextQuestion) {
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) {
wx.showToast({ title: '请先提交答案', icon: 'none' })
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' }),
})
return
}
session.currentIndex += 1
session.currentAnswer = ''
this.syncSession()
},
markMastered() {
this.setData({ masterHint: true })
wx.showToast({ title: '已标记', icon: 'success' })
},
})