kydc/review-config/review-config.js

103 lines
3.4 KiB
JavaScript

const app = getApp()
Page({
data: {
timeRanges: ['今日', '本周', '本月', '自定义区间'],
activeTimeRange: 0,
contentTypes: ['单词', '句子'],
activeContentType: 0,
modes: ['中文 -> 英文拼写', '英文 -> 中文释义', '英文挖空填空'],
activeMode: 0,
onlyReview: true,
shuffle: false,
questionCount: 30,
loading: false,
error: '',
},
onLoad() {
const plan = app.globalData.reviewPlan
this.setData({
activeTimeRange: plan.timeRangeIndex,
activeContentType: plan.contentTypeIndex,
activeMode: plan.modeIndex,
onlyReview: plan.onlyReview,
shuffle: plan.shuffle,
questionCount: plan.questionCount,
})
},
persistPatch(patch) {
app.globalData.reviewPlan = { ...app.globalData.reviewPlan, ...patch }
},
setTimeRange(e) {
const activeTimeRange = Number(e.currentTarget.dataset.index)
this.setData({ activeTimeRange })
this.persistPatch({ timeRangeIndex: activeTimeRange })
},
setContentType(e) {
const activeContentType = Number(e.currentTarget.dataset.index)
this.setData({ activeContentType })
this.persistPatch({ contentTypeIndex: activeContentType })
},
setMode(e) {
const activeMode = Number(e.currentTarget.dataset.index)
this.setData({ activeMode })
this.persistPatch({ modeIndex: activeMode })
},
toggleOnlyReview() {
const onlyReview = !this.data.onlyReview
this.setData({ onlyReview })
this.persistPatch({ onlyReview })
},
toggleShuffle() {
const shuffle = !this.data.shuffle
this.setData({ shuffle })
this.persistPatch({ shuffle })
},
onQuestionCountInput(e) {
const questionCount = Number(e.detail.value || 0)
this.setData({ questionCount })
this.persistPatch({ questionCount })
},
startReview() {
const scopeTypeMap = ['today', 'week', 'month', 'custom']
const contentTypeMap = ['word', 'sentence']
const questionModeMap = ['A', 'B', 'C']
const scope_type = scopeTypeMap[this.data.activeTimeRange]
const content_type = contentTypeMap[this.data.activeContentType]
const question_mode = questionModeMap[this.data.activeMode]
const today = new Date()
const yyyy = today.getFullYear()
const mm = String(today.getMonth() + 1).padStart(2, '0')
const dd = String(today.getDate()).padStart(2, '0')
const scope_config = scope_type === 'custom' ? { start_date: `${yyyy}-${mm}-${dd}`, end_date: `${yyyy}-${mm}-${dd}` } : {}
this.setData({ loading: true, error: '' })
wx.request({
url: `${app.globalData.baseUrl}/api/v1/review/generate`,
method: 'POST',
header: { 'Content-Type': 'application/json', 'X-OpenID': app.globalData.openid || '' },
data: {
scope_type,
scope_config,
content_type,
question_mode,
review_strategy: this.data.onlyReview ? 'review_only' : 'all',
sort_mode: this.data.shuffle ? 'shuffle' : 'sequential',
total_count: this.data.questionCount,
},
success: (res) => {
app.globalData.reviewSessionMeta = res.data?.data || null
wx.navigateTo({ url: '/pages/review-session/review-session' })
},
fail: () => this.setData({ error: '生成复习会话失败' }),
complete: () => this.setData({ loading: false }),
})
},
goHome() {
wx.reLaunch({ url: '/pages/index/index' })
},
goToReview() {},
goToMe() {
wx.navigateTo({ url: '/pages/me/me' })
},
})