/** * Purpose: commentApi supports index.js by holding or executing the comment api page or API role. * Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow. */ const commentApi = require('../../api/comment') Page({ /** * Purpose: data supports index.js by holding or executing the data page or API role. * Usage: import, call, read, or update this page-data from the owning page/module while preserving the surrounding loading and permission flow. */ data: { /** * Purpose: loading supports index.js by holding or executing the loading page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ loading: false, /** * Purpose: keyword supports index.js by holding or executing the keyword page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ keyword: '', /** * Purpose: comments supports index.js by holding or executing the comments page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ comments: [] }, /** * Purpose: onShow supports index.js by holding or executing the on show page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ onShow() { this.loadComments() }, async onPullDownRefresh() { await this.loadComments() wx.stopPullDownRefresh() }, /** * Purpose: handleKeywordInput supports index.js by holding or executing the handle keyword input page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ handleKeywordInput(event) { this.setData({ /** * Purpose: keyword supports index.js by holding or executing the keyword page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ keyword: event.detail.value }) }, async loadComments() { this.setData({ loading: true }) try { const comments = await commentApi.listComments({ /** * Purpose: keyword supports index.js by holding or executing the keyword page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ keyword: this.data.keyword || undefined }) this.setData({ /** * Purpose: comments supports index.js by holding or executing the comments page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ comments: Array.isArray(comments) ? comments : [] }) } catch (error) { wx.showToast({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: error.message || '评论加载失败', /** * Purpose: icon supports index.js by holding or executing the icon page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ icon: 'none' }) } finally { this.setData({ loading: false }) } }, /** * Purpose: handleSearch supports index.js by holding or executing the handle search page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ handleSearch() { this.loadComments() }, /** * Purpose: handleDelete supports index.js by holding or executing the handle delete page or API role. * Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow. */ handleDelete(event) { const id = event.currentTarget.dataset.id if (!id) { return } wx.showModal({ /** * Purpose: title supports index.js by holding or executing the title page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ title: '删除评论', /** * Purpose: content supports index.js by holding or executing the content page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ content: '确认删除这条评论?', /** * Purpose: confirmText supports index.js by holding or executing the confirm text page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ confirmText: '删除', /** * Purpose: success supports index.js by holding or executing the success page or API role. * Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow. */ 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' }) } } }) } })