Files
inchstep/miniapp/pages/social/feed/index.js
T
2026-06-25 08:39:47 +08:00

29 lines
1.5 KiB
JavaScript

const api = require('../../../services/api')
Page({
data: { posts: [], page: 1, loading: true, showCreate: false, content: '' },
onShow() { this.loadFeed() },
onPullDownRefresh() { this.setData({ page: 1 }); this.loadFeed().then(() => wx.stopPullDownRefresh()) },
async loadFeed() {
try {
const data = await api.get('/api/social/feed?page=' + this.data.page + '&pageSize=20')
if (this.data.page === 1) this.setData({ posts: data.list || [] })
else this.setData({ posts: [...this.data.posts, ...(data.list || [])] })
} catch(e) {}
this.setData({ loading: false })
},
openCreate() { this.setData({ showCreate: true, content: '' }) },
closeCreate() { this.setData({ showCreate: false }) },
onContentInput(e) { this.setData({ content: e.detail.value }) },
async submitPost() {
if (!this.data.content.trim()) return
try { await api.post('/api/social/posts', { content: this.data.content.trim(), privacy: 1 }); this.closeCreate(); this.onPullDownRefresh() }
catch(e) { wx.showToast({ title: e.message || '发布失败', icon: 'none' }) }
},
async onLike(e) {
const id = e.currentTarget.dataset.id; const posts = this.data.posts
try { await api.post('/api/social/posts/' + id + '/like'); const idx = posts.findIndex(p => p.id === id); if (idx > -1) { posts[idx].likes++; this.setData({ posts }) } }
catch(e) { wx.showToast({ title: '操作失败', icon: 'none' }) }
},
loadMore() { this.setData({ page: this.data.page + 1 }); this.loadFeed() },
})