Page({ data: { favorites: [], showDetail: false, selectedItem: {}, selectedIndex: -1, openid: null, creditsInfo: { dailyCredits: 5, totalCredits: 0, watchedAdCount: 0 }, // 静心阅读 showMeditation: false, meditationPoem: null, meditationProgress: 0 }, onLoad() { this.setOpenid(); }, onShow() { // 每次显示页面时刷新列表 this.loadFavorites(); // 加载积分信息 this.loadCreditsInfo(); }, // 设置 openid setOpenid() { const openid = getApp().getOpenid(); if (openid) { this.setData({ openid }); this.loadFavorites(); } else { // openid 还未获取到,延迟重试 setTimeout(() => this.setOpenid(), 500); } }, // 加载收藏列表 loadFavorites() { if (!this.data.openid) { console.log('openid 未获取到,跳过加载收藏'); return; } const apiBaseUrl = getApp().globalData.apiBaseUrl; wx.request({ url: `${apiBaseUrl}/api/favorites/list`, data: { openid: this.data.openid }, success: (res) => { if (res.data && res.data.success) { this.setData({ favorites: res.data.data || [] }); } }, fail: () => { wx.showToast({ title: '加载失败', icon: 'none' }); } }); }, // 返回上一页 onBack() { wx.navigateBack(); }, // 加载积分信息 loadCreditsInfo() { const openid = getApp().getOpenid(); if (!openid) { console.log('openid 未获取到,跳过加载积分'); return; } const apiBaseUrl = getApp().globalData.apiBaseUrl; wx.request({ url: `${apiBaseUrl}/api/credits/info`, data: { openid }, success: (res) => { if (res.data && res.data.success) { this.setData({ creditsInfo: res.data.data }); } } }); }, // 点击积分区域 onCreditsTap() { const { creditsInfo } = this.data; // 如果还有剩余次数,显示提示 if (creditsInfo.dailyCredits > 0) { wx.showToast({ title: `今日还有 ${creditsInfo.dailyCredits} 次灵感`, icon: 'none' }); return; } // 如果次数用完且还可以看广告 if (creditsInfo.watchedAdCount < 5) { wx.showModal({ title: '静心阅读', content: '观看 15 秒静心画报,即可获得 3 次灵感', confirmText: '开始阅读', cancelText: '取消', success: (res) => { if (res.confirm) { this.watchAd(); } } }); } else { wx.showToast({ title: '今日次数已达上限,明日再来', icon: 'none' }); } }, // 静心阅读 - 模拟 15 秒阅读体验 watchAd() { const openid = getApp().getOpenid(); if (!openid) { wx.showToast({ title: '请先登录', icon: 'none' }); return; } // 随机选择一首诗词 const poems = [ { text: '「采菊东篱下,悠然见南山」', author: '陶渊明《饮酒》' }, { text: '「行到水穷处,坐看云起时」', author: '王维《终南别业》' }, { text: '「人闲桂花落,夜静春山空」', author: '王维《鸟鸣涧》' }, { text: '「松风吹解带,山月照弹琴」', author: '王维《酬张少府》' }, { text: '「曲径通幽处,禅房花木深」', author: '常建《题破山寺后禅院》' }, { text: '「明月松间照,清泉石上流」', author: '王维《山居秋暝》' } ]; const poem = poems[Math.floor(Math.random() * poems.length)]; // 显示静心阅读页面 this.setData({ showMeditation: true, meditationPoem: poem, meditationProgress: 0 }); // 15 秒倒计时 - 每 150ms 增加 1%,总共 150 * 100 = 15000ms = 15秒 let progress = 0; const timer = setInterval(() => { progress += 1; this.setData({ meditationProgress: progress }); if (progress >= 100) { clearInterval(timer); this.meditationTimer = null; // 延迟一下再关闭,让用户看到完成状态 setTimeout(() => { this.setData({ showMeditation: false }); // 领取奖励 this.rewardAd(openid); }, 500); } }, 150); // 保存 timer 以便可以提前关闭 this.meditationTimer = timer; }, // 提前关闭静心阅读 closeMeditation() { if (this.meditationTimer) { clearInterval(this.meditationTimer); this.meditationTimer = null; } this.setData({ showMeditation: false }); }, // 领取广告奖励 rewardAd(openid) { const apiBaseUrl = getApp().globalData.apiBaseUrl; wx.request({ url: `${apiBaseUrl}/api/credits/reward`, method: 'POST', header: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: `openid=${encodeURIComponent(openid)}`, success: (res) => { if (res.data && res.data.success) { wx.showToast({ title: '获得 3 次灵感', icon: 'success' }); this.loadCreditsInfo(); } else { wx.showToast({ title: res.data.message || '领取失败', icon: 'none' }); } }, fail: () => { wx.showToast({ title: '领取失败', icon: 'none' }); } }); }, // 点击收藏项 onItemTap(e) { const index = e.currentTarget.dataset.index; const item = this.data.favorites[index]; this.setData({ selectedItem: item, selectedIndex: index, showDetail: true }); wx.vibrateShort({ type: 'light' }); }, // 关闭详情弹窗 closeDetail() { this.setData({ showDetail: false }); }, // 阻止冒泡 preventBubble() { // 什么都不做,只是阻止事件冒泡 }, // 分享 onShare() { const item = this.data.selectedItem; wx.showShareImageMenu({ path: '/images/share.png' // 实际应生成海报图片 }); }, // 移除收藏 onRemove() { const item = this.data.selectedItem; wx.showModal({ title: '确认移除', content: `确定要从心动名单中移除「${item.name}」吗?`, confirmColor: '#B22222', success: (res) => { if (res.confirm) { const apiBaseUrl = getApp().globalData.apiBaseUrl; wx.request({ url: `${apiBaseUrl}/api/favorites/remove?openid=${this.data.openid}&name=${encodeURIComponent(item.name)}`, method: 'POST', success: (res) => { if (res.data && res.data.success) { wx.showToast({ title: '已移除', icon: 'success' }); this.closeDetail(); this.loadFavorites(); } } }); } } }); }, // 下拉刷新 onPullDownRefresh() { this.loadFavorites(); wx.stopPullDownRefresh(); } });