// pages/profile/index.js import { MOCK_FAVORITES, MOCK_BADGES, MOCK_POSTS } from '../../utils/mockData'; Page({ data: { view: 'profile', // profile, favorites, posts, badges favTab: 'all', // all, plant, article postsTab: 'published', // published, drafts favorites: [], filteredFavorites: [], myPublishedPosts: [], myDrafts: [], badges: [] }, onLoad(options) { this.setData({ favorites: MOCK_FAVORITES, badges: MOCK_BADGES }); this.filterFavorites(); }, onShow() { if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 4 // Index 4 is Profile }) } // Refresh posts data this.loadMyPosts(); this.loadDrafts(); }, loadMyPosts() { // Get published posts by current user const myPosts = MOCK_POSTS.filter(p => p.user === '我的花园'); this.setData({ myPublishedPosts: myPosts }); }, loadDrafts() { // Load drafts from storage try { const draft = wx.getStorageSync('post_draft'); if (draft && (draft.content || (draft.images && draft.images.length > 0))) { // Convert single draft to array for consistency this.setData({ myDrafts: [{ id: 'draft_1', content: draft.content || '', images: draft.images || [], selectedTopics: draft.selectedTopics || [] }] }); } else { this.setData({ myDrafts: [] }); } } catch (e) { this.setData({ myDrafts: [] }); } }, setView(e) { const view = e.currentTarget.dataset.view; this.setData({ view }); // Refresh data when entering posts view if (view === 'posts') { this.loadMyPosts(); this.loadDrafts(); } }, onFavTabChange(e) { const tab = e.detail.value; this.setData({ favTab: tab }, () => { this.filterFavorites(); }); }, onPostsTabChange(e) { const tab = e.detail.value; this.setData({ postsTab: tab }); }, filterFavorites() { const { favorites, favTab } = this.data; const filtered = favorites.filter(item => { if (favTab === 'all') return true; return item.type === favTab; }); this.setData({ filteredFavorites: filtered }); }, // Delete a published post deletePost(e) { const postId = e.currentTarget.dataset.id; wx.showModal({ title: '删除动态', content: '确定要删除这条动态吗?', confirmColor: '#EF5350', success: (res) => { if (res.confirm) { // Remove from MOCK_POSTS const idx = MOCK_POSTS.findIndex(p => p.id === postId); if (idx > -1) { MOCK_POSTS.splice(idx, 1); } this.loadMyPosts(); wx.showToast({ title: '已删除', icon: 'success' }); } } }); }, // Edit a draft editDraft(e) { // Navigate to create page, which will load the draft wx.navigateTo({ url: '/pages/community/create/index' }); }, // Delete a draft deleteDraft(e) { wx.showModal({ title: '删除草稿', content: '确定要删除这份草稿吗?', confirmColor: '#EF5350', success: (res) => { if (res.confirm) { try { wx.removeStorageSync('post_draft'); } catch (e) { } this.setData({ myDrafts: [] }); wx.showToast({ title: '已删除', icon: 'success' }); } } }); } })