const api = require('../../../services/api') const app = getApp() Page({ data: { posts: [], page: 1, loading: true, showCreate: false, content: '', themeMode: 'light' }, onShow() { this.setData({ themeMode: app.globalData.themeMode }) 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() }, })