71 lines
2.0 KiB
JavaScript
71 lines
2.0 KiB
JavaScript
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
entry: null,
|
|
loading: false,
|
|
error: '',
|
|
},
|
|
onLoad(options) {
|
|
this.entryId = options.id
|
|
this.loadEntry()
|
|
},
|
|
loadEntry() {
|
|
if (!app.globalData.openid) {
|
|
this.setData({ error: '请先登录' })
|
|
return
|
|
}
|
|
if (!this.entryId) {
|
|
this.setData({ error: '缺少词条ID' })
|
|
return
|
|
}
|
|
this.setData({ loading: true, error: '' })
|
|
wx.request({
|
|
url: `${app.globalData.baseUrl}/api/v1/entries/${this.entryId}`,
|
|
header: { 'X-OpenID': app.globalData.openid },
|
|
success: (res) => {
|
|
const entry = res.data?.data || null
|
|
this.setData({ entry, error: entry ? '' : '未找到词条' })
|
|
},
|
|
fail: () => this.setData({ error: '词条加载失败' }),
|
|
complete: () => this.setData({ loading: false }),
|
|
})
|
|
},
|
|
editEntry() {
|
|
if (!this.data.entry?.id) return
|
|
wx.navigateTo({ url: `/pages/entry-edit/entry-edit?id=${this.data.entry.id}` })
|
|
},
|
|
deleteEntry() {
|
|
if (!this.data.entry?.id) return
|
|
wx.showModal({
|
|
title: '确认删除',
|
|
content: '删除后无法恢复,是否继续?',
|
|
success: (res) => {
|
|
if (!res.confirm) return
|
|
wx.request({
|
|
url: `${app.globalData.baseUrl}/api/v1/entries/${this.data.entry.id}`,
|
|
method: 'DELETE',
|
|
header: { 'X-OpenID': app.globalData.openid },
|
|
success: () => {
|
|
wx.showToast({ title: '已删除', icon: 'success' })
|
|
const pages = getCurrentPages()
|
|
const prevPage = pages[pages.length - 2]
|
|
if (prevPage && typeof prevPage.loadList === 'function') {
|
|
prevPage.loadList()
|
|
}
|
|
wx.navigateBack({ delta: 1 })
|
|
},
|
|
})
|
|
},
|
|
})
|
|
},
|
|
goBack() {
|
|
const pages = getCurrentPages()
|
|
const prevPage = pages[pages.length - 2]
|
|
if (prevPage && typeof prevPage.loadList === 'function') {
|
|
prevPage.loadList()
|
|
}
|
|
wx.navigateBack()
|
|
},
|
|
})
|