kydc/entry-edit/entry-edit.js

58 lines
1.4 KiB
JavaScript

const app = getApp()
Page({
data: {
entryType: 'word',
enText: '',
zhText: '',
exampleText: '',
loading: false,
error: '',
},
setType(e) {
this.setData({ entryType: e.currentTarget.dataset.type })
},
onEnInput(e) {
this.setData({ enText: e.detail.value })
},
onZhInput(e) {
this.setData({ zhText: e.detail.value })
},
onExampleInput(e) {
this.setData({ exampleText: e.detail.value })
},
goBack() {
wx.navigateBack()
},
save() {
if (!app.globalData.openid) {
wx.showToast({ title: '请先登录', icon: 'none' })
return
}
this.setData({ loading: true, error: '' })
wx.request({
url: `${app.globalData.baseUrl}/api/v1/entries`,
method: 'POST',
header: { 'Content-Type': 'application/json', 'X-OpenID': app.globalData.openid },
data: {
entry_type: this.data.entryType,
en_text: this.data.enText,
zh_text: this.data.zhText,
example_text: this.data.exampleText,
source_type: 'manual',
upload_date: new Date().toISOString().slice(0, 10),
},
success: () => {
wx.showToast({ title: '保存成功', icon: 'success' })
this.setData({
enText: '',
zhText: '',
exampleText: '',
})
},
fail: () => this.setData({ error: '保存失败' }),
complete: () => this.setData({ loading: false }),
})
},
})