106 lines
3.4 KiB
JavaScript
106 lines
3.4 KiB
JavaScript
const appContext = require('../../utils/app-context.js')
|
|
|
|
Page({
|
|
data: {
|
|
accuracy: 0,
|
|
timeUsed: '--:--',
|
|
total: 0,
|
|
mastered: 0,
|
|
reviewNeeded: 0,
|
|
improved: 0,
|
|
regressed: 0,
|
|
loading: false,
|
|
error: '',
|
|
clozeResults: [],
|
|
},
|
|
onLoad() {
|
|
this.loadResult()
|
|
},
|
|
loadResult() {
|
|
const app = appContext.withApp()
|
|
const sessionId = app.globalData.reviewSessionMeta?.id || wx.getStorageSync('reviewSessionMeta')?.id
|
|
const fallbackSession = app.globalData.reviewSession
|
|
|
|
if (!sessionId) {
|
|
if (fallbackSession?.completed) {
|
|
this.setData({ error: '本地复习已完成,但结果会话信息已丢失' })
|
|
} else {
|
|
this.setData({ error: '暂无复习结果' })
|
|
}
|
|
return
|
|
}
|
|
|
|
this.setData({ loading: true, error: '' })
|
|
wx.request({
|
|
url: `${app.globalData.baseUrl}/api/v1/review-result?session_id=${sessionId}`,
|
|
header: { 'X-Auth-Token': app.globalData.authToken || '' },
|
|
success: (res) => {
|
|
if (appContext.isUnauthorizedResponse(res)) {
|
|
appContext.handleUnauthorized()
|
|
return
|
|
}
|
|
|
|
const ok = res.statusCode >= 200 && res.statusCode < 300 && res.data?.code === 0
|
|
if (!ok) {
|
|
this.setData({ error: res.data?.detail || res.data?.message || '结果加载失败' })
|
|
return
|
|
}
|
|
|
|
const data = res.data?.data || {}
|
|
const total = Number(data.total || 0)
|
|
const correct = Number(data.correct || 0)
|
|
const wrong = Number(data.wrong || Math.max(0, total - correct))
|
|
const accuracy = total ? Math.round((correct / total) * 100) : 0
|
|
const clozeResults = Array.isArray(data.cloze_results)
|
|
? data.cloze_results.map((item) => ({
|
|
record_id: item.record_id,
|
|
entry_id: item.entry_id,
|
|
question_mode: item.question_mode,
|
|
user_answer: item.user_answer || '',
|
|
prompt_text: item.cloze_question?.prompt_text || '',
|
|
blanks: Array.isArray(item.blanks)
|
|
? item.blanks.map((blank) => ({
|
|
index: blank.index,
|
|
user_answer: blank.user_answer || '',
|
|
correct_answers: Array.isArray(blank.correct_answers) ? blank.correct_answers : [],
|
|
is_correct: !!blank.is_correct,
|
|
}))
|
|
: [],
|
|
is_correct: Array.isArray(item.blanks) ? item.blanks.every((blank) => blank.is_correct) : false,
|
|
}))
|
|
: []
|
|
|
|
this.setData({
|
|
total,
|
|
accuracy,
|
|
mastered: Number(data.mastered ?? correct),
|
|
reviewNeeded: Number(data.review_needed ?? wrong),
|
|
improved: Number(data.improved ?? 0),
|
|
regressed: Number(data.regressed ?? 0),
|
|
timeUsed: data.time_used || '--:--',
|
|
clozeResults,
|
|
})
|
|
},
|
|
fail: () => this.setData({ error: '结果加载失败' }),
|
|
complete: () => this.setData({ loading: false }),
|
|
})
|
|
},
|
|
restart() {
|
|
const app = appContext.withApp()
|
|
app.globalData.reviewSession = {
|
|
deck: [],
|
|
currentIndex: 0,
|
|
correctCount: 0,
|
|
wrongCount: 0,
|
|
completed: false,
|
|
currentAnswer: '',
|
|
currentAnswers: [],
|
|
lastClozeResults: [],
|
|
}
|
|
wx.reLaunch({ url: '/pages/review-config/review-config' })
|
|
},
|
|
goHome() {
|
|
wx.reLaunch({ url: '/pages/index/index' })
|
|
},
|
|
})
|