kydc/settings/settings.js

111 lines
3.3 KiB
JavaScript
Raw Normal View History

const app = getApp()
2026-04-19 16:39:02 +08:00
const difficultyOptions = [
{ label: '简单', value: 'easy' },
{ label: '普通', value: 'normal' },
{ label: '困难', value: 'hard' },
]
2026-04-27 12:37:15 +08:00
const reviewOrderOptions = [
{ label: '顺序复习', value: 'sequential' },
{ label: '随机复习', value: 'shuffle' },
]
Page({
data: {
settings: null,
2026-04-19 11:56:49 +08:00
loading: false,
error: '',
2026-04-19 16:39:02 +08:00
difficultyOptions,
2026-04-27 12:37:15 +08:00
reviewOrderOptions,
2026-04-19 16:39:02 +08:00
difficultyIndex: 1,
2026-04-27 12:37:15 +08:00
reviewOrderIndex: 0,
2026-04-19 16:39:02 +08:00
difficultyLabel: '普通',
2026-04-27 12:37:15 +08:00
reviewOrderLabel: '顺序复习',
},
onShow() {
2026-04-19 11:56:49 +08:00
this.loadSettings()
},
2026-04-19 16:39:02 +08:00
syncDerivedState(settings) {
const difficultyIndex = Math.max(0, difficultyOptions.findIndex((item) => item.value === settings.blank_difficulty))
2026-04-27 12:37:15 +08:00
const reviewOrderIndex = Math.max(0, reviewOrderOptions.findIndex((item) => item.value === settings.review_order))
2026-04-19 16:39:02 +08:00
this.setData({
settings,
difficultyIndex: difficultyIndex >= 0 ? difficultyIndex : 1,
difficultyLabel: difficultyOptions[difficultyIndex >= 0 ? difficultyIndex : 1].label,
2026-04-27 12:37:15 +08:00
reviewOrderIndex: reviewOrderIndex >= 0 ? reviewOrderIndex : 0,
reviewOrderLabel: reviewOrderOptions[reviewOrderIndex >= 0 ? reviewOrderIndex : 0].label,
2026-04-19 16:39:02 +08:00
})
},
2026-04-19 11:56:49 +08:00
loadSettings() {
if (!app.globalData.openid) {
this.setData({ error: '请先登录' })
return
}
this.setData({ loading: true, error: '' })
wx.request({
url: `${app.globalData.baseUrl}/api/v1/settings`,
2026-04-19 11:56:49 +08:00
header: { 'X-OpenID': app.globalData.openid },
success: (res) => {
2026-04-19 16:39:02 +08:00
this.syncDerivedState(res.data?.data || null)
},
2026-04-19 11:56:49 +08:00
fail: () => this.setData({ error: '设置加载失败' }),
complete: () => this.setData({ loading: false }),
})
},
2026-04-19 16:39:02 +08:00
onBlankCountInput(e) {
const value = Number(e.detail.value || 0)
this.setData({
settings: {
...this.data.settings,
blank_count: Number.isFinite(value) ? value : 0,
},
})
},
onDifficultyChange(e) {
const index = Number(e.detail.value)
const option = difficultyOptions[index] || difficultyOptions[1]
this.setData({
difficultyIndex: index,
difficultyLabel: option.label,
settings: {
...this.data.settings,
blank_difficulty: option.value,
},
})
},
2026-04-27 12:37:15 +08:00
onReviewOrderChange(e) {
const index = Number(e.detail.value)
const option = reviewOrderOptions[index] || reviewOrderOptions[0]
this.setData({
reviewOrderIndex: index,
reviewOrderLabel: option.label,
settings: {
...this.data.settings,
review_order: option.value,
},
})
},
2026-04-19 16:39:02 +08:00
toggleSetting(e) {
const key = e.currentTarget.dataset.key
const settings = { ...this.data.settings }
settings[key] = settings[key] ? 0 : 1
this.setData({ settings })
},
save() {
2026-04-19 11:56:49 +08:00
if (!app.globalData.openid) {
wx.showToast({ title: '请先登录', icon: 'none' })
return
}
const settings = this.data.settings || {}
wx.request({
url: `${app.globalData.baseUrl}/api/v1/settings`,
method: 'PUT',
2026-04-19 11:56:49 +08:00
header: { 'Content-Type': 'application/json', 'X-OpenID': app.globalData.openid },
data: settings,
success: () => wx.showToast({ title: '已保存', icon: 'success' }),
2026-04-19 11:56:49 +08:00
fail: () => wx.showToast({ title: '保存失败', icon: 'none' }),
})
},
})