Files
JayuCloud/YumiNail-Admin-Program/pages/comments/index.js
T

72 lines
1.4 KiB
JavaScript

const commentApi = require('../../api/comment')
Page({
data: {
loading: false,
keyword: '',
comments: []
},
onShow() {
this.loadComments()
},
async onPullDownRefresh() {
await this.loadComments()
wx.stopPullDownRefresh()
},
handleKeywordInput(event) {
this.setData({
keyword: event.detail.value
})
},
async loadComments() {
this.setData({ loading: true })
try {
const comments = await commentApi.listComments({
keyword: this.data.keyword || undefined
})
this.setData({
comments: Array.isArray(comments) ? comments : []
})
} catch (error) {
wx.showToast({
title: error.message || '评论加载失败',
icon: 'none'
})
} finally {
this.setData({ loading: false })
}
},
handleSearch() {
this.loadComments()
},
handleDelete(event) {
const id = event.currentTarget.dataset.id
if (!id) {
return
}
wx.showModal({
title: '删除评论',
content: '确认删除这条评论?',
confirmText: '删除',
success: async res => {
if (!res.confirm) {
return
}
try {
await commentApi.deleteComment(id)
wx.showToast({ title: '评论已删除', icon: 'none' })
this.loadComments()
} catch (error) {
wx.showToast({ title: error.message || '删除失败', icon: 'none' })
}
}
})
}
})