// 灵感广场页面 Page({ data: { posts: [], leftPosts: [], rightPosts: [], page: 0, size: 20, loading: false, hasMore: true, currentTag: '', sortType: 'latest', showDetail: false, selectedPost: {} }, onLoad() { this.loadPosts(); }, onShow() { // 刷新列表 if (this.data.posts.length === 0) { this.loadPosts(); } }, // 加载帖子列表 loadPosts(reset = false) { if (this.data.loading) return; if (reset) { this.setData({ page: 0, posts: [], leftPosts: [], rightPosts: [], hasMore: true }); } this.setData({ loading: true }); const apiBaseUrl = getApp().globalData.apiBaseUrl; const { page, size, currentTag, sortType } = this.data; let url = `${apiBaseUrl}/api/square/posts?page=${page}&size=${size}&sort=${sortType}`; if (currentTag) { url += `&tag=${encodeURIComponent(currentTag)}`; } wx.request({ url, success: (res) => { if (res.data && res.data.success) { const newPosts = res.data.data || []; const allPosts = reset ? newPosts : [...this.data.posts, ...newPosts]; // 分配到左右两列 const leftPosts = []; const rightPosts = []; allPosts.forEach((post, index) => { if (index % 2 === 0) { leftPosts.push(post); } else { rightPosts.push(post); } }); this.setData({ posts: allPosts, leftPosts, rightPosts, hasMore: res.data.hasMore, page: page + 1, loading: false }); } else { this.setData({ loading: false }); } }, fail: () => { this.setData({ loading: false }); wx.showToast({ title: '加载失败', icon: 'none' }); } }); }, // 点击标签 onTagTap(e) { const tag = e.currentTarget.dataset.tag; this.setData({ currentTag: tag }); this.loadPosts(true); }, // 切换排序 onSortTap(e) { const type = e.currentTarget.dataset.type; this.setData({ sortType: type }); this.loadPosts(true); }, // 点击帖子 onPostTap(e) { const post = e.currentTarget.dataset.post; this.setData({ selectedPost: post, showDetail: true }); }, // 关闭详情 onCloseDetail() { this.setData({ showDetail: false }); }, // 阻止冒泡 onDetailTap() { // 什么都不做 }, // 点赞 onLikeTap() { const { selectedPost } = this.data; const apiBaseUrl = getApp().globalData.apiBaseUrl; wx.request({ url: `${apiBaseUrl}/api/square/posts/${selectedPost.id}/like`, method: 'POST', success: (res) => { if (res.data && res.data.success) { // 更新本地数据 const updatedPost = { ...selectedPost, likeCount: (selectedPost.likeCount || 0) + 1 }; this.setData({ selectedPost: updatedPost }); // 更新列表中的数据 const posts = this.data.posts.map(p => p.id === selectedPost.id ? updatedPost : p ); this.setData({ posts }); wx.showToast({ title: '已共鸣', icon: 'none' }); } } }); }, // 同款跳转(卡片上) onCopyTap(e) { const { keyword, mode } = e.currentTarget.dataset; this.navigateToHome(keyword, mode); }, // 同款跳转(详情中) onDetailCopyTap() { const { keyword, mode } = this.data.selectedPost; this.navigateToHome(keyword, mode); }, // 跳转到首页 navigateToHome(keyword, mode) { if (!keyword) { wx.showToast({ title: '无法获取关键词', icon: 'none' }); return; } wx.reLaunch({ url: `/pages/home/home?keyword=${encodeURIComponent(keyword)}&mode=${mode || 'poetic'}` }); }, // 下拉刷新 onPullDownRefresh() { this.loadPosts(true); wx.stopPullDownRefresh(); }, // 上拉加载更多 onReachBottom() { if (this.data.hasMore && !this.data.loading) { this.loadPosts(); } } });