const { getPoints, checkDailyReward, canWatchAd, getTodayAdCount, rewardFromAd, AD_REWARD_CONFIG } = require('../../utils/pointsManager'); const { getDailyAdvice } = require('../../utils/dailyAdvice'); const { getDailyArticle, getCategories } = require('../../utils/knowledgeData'); /** * 🔧 开发模式配置 * * DEV_MODE = true: 使用模拟广告(开发测试) * DEV_MODE = false: 使用真实广告(正式上线) * * ⚠️ 上线前务必改为 false! */ const DEV_MODE = true; // 👈 开发时设为 true,上线前改为 false Page({ data: { currentPoints: 0, dailyAdvice: '', dailyArticle: { id: '', title: '', summary: '', category: '', categoryName: '', type: 'local' }, energyVisible: false, knowledgeVisible: false }, onLoad: function () { // 加载每日内容 this.loadDailyContent(); // 初始化激励视频广告 this.initRewardedAd(); }, /** * 初始化激励视频广告 * ⚠️ 需要在微信公众平台开通流量主并获取广告位ID */ initRewardedAd: function () { // 🔧 开发模式:跳过真实广告初始化 if (DEV_MODE) { console.log('[广告系统] 🔧 开发模式:使用模拟广告'); return; } // 检查是否支持激励视频广告 if (!wx.createRewardedVideoAd) { console.warn('[广告系统] 当前微信版本不支持激励视频广告'); return; } try { // 创建激励视频广告实例 // ⚠️ TODO: 替换为你的真实广告位ID this.rewardedAd = wx.createRewardedVideoAd({ adUnitId: 'adunit-xxxxxxxxxxxxxxxx' // 请替换为你的广告位ID }); // 监听广告关闭事件 this.rewardedAd.onClose((res) => { console.log('[广告系统] 广告关闭回调:', res); // 用户看完广告(isEnded = true 表示正常播放结束) if (res && res.isEnded) { console.log('[广告系统] 用户完整观看广告'); this.handleAdReward(); } else { // 用户中途关闭广告 console.log('[广告系统] 用户未看完广告'); wx.showToast({ title: '未看完广告,无法获得积分', icon: 'none', duration: 2000 }); } }); // 监听广告加载错误 this.rewardedAd.onError((err) => { console.error('[广告系统] 广告加载失败:', err); }); console.log('[广告系统] 激励视频广告初始化成功'); } catch (error) { console.error('[广告系统] 初始化失败:', error); } }, /** * 处理广告奖励 */ handleAdReward: function () { const result = rewardFromAd(); if (result.success) { // 刷新积分显示 this.setData({ currentPoints: getPoints() }); // 显示奖励提示 const message = `${result.message}!今日还可观看 ${result.remainingCount} 次`; wx.showToast({ title: message, icon: 'success', duration: 2500 }); console.log('[广告系统] 奖励发放成功:', result); } else { // 显示失败提示(通常是次数用完) wx.showToast({ title: result.message, icon: 'none', duration: 2000 }); } }, onShow: function () { // 检查每日登录奖励 const rewardResult = checkDailyReward(); // 如果获得了奖励,显示提示 if (rewardResult.rewarded) { wx.showToast({ title: rewardResult.message, icon: 'success', duration: 2000 }); } // 刷新积分显示 this.setData({ currentPoints: getPoints() }); }, // 加载每日内容 loadDailyContent: function () { try { // 获取今日建议 const advice = getDailyAdvice(); // 获取今日文章 const article = getDailyArticle(); // 获取分类名称 const categories = getCategories(); const category = categories.find(c => c.id === article.category); this.setData({ dailyAdvice: advice, dailyArticle: Object.assign({}, article, { categoryName: category ? category.name : '塔罗知识' }) }); console.log('[首页] 每日内容加载成功'); } catch (error) { console.error('[首页] 每日内容加载失败:', error); } }, // 显示能量卡浮层 showEnergyOverlay: function () { this.setData({ energyVisible: true }); }, // 隐藏能量卡浮层 hideEnergyOverlay: function () { this.setData({ energyVisible: false }); }, // 显示知识卡半屏 showKnowledgePanel: function () { this.setData({ knowledgeVisible: true }); }, // 隐藏知识卡半屏 hideKnowledgePanel: function () { this.setData({ knowledgeVisible: false }); }, // 阻止事件冒泡 stopPropagation: function () { // 空函数,用于阻止点击事件冒泡 }, // 跳转到塔罗占卜 goToTarot: function () { wx.navigateTo({ url: '/pages/index/index' }); }, // 跳转到知识模块 goToKnowledge: function () { wx.navigateTo({ url: '/pages/knowledge/index' }); }, // 跳转到文章详情 goToArticle: function () { const article = this.data.dailyArticle; // 先关闭半屏 this.setData({ knowledgeVisible: false }); // 延迟跳转,等待动画完成 setTimeout(() => { if (article.type === 'web') { // 外链文章,跳转到 webview wx.navigateTo({ url: `/pages/webview/index?url=${encodeURIComponent(article.url)}&title=${encodeURIComponent(article.title)}` }); } else { // 本地文章,跳转到文章详情页 wx.navigateTo({ url: `/pages/knowledge/article?id=${article.id}` }); } }, 300); }, // 跳转到隐私政策 goToPrivacy: function () { wx.navigateTo({ url: '/pages/privacy/privacy' }); }, // 显示积分说明 showPointsInfo: function () { try { console.log('[首页] 点击积分徽章'); const remaining = AD_REWARD_CONFIG.DAILY_LIMIT - getTodayAdCount(); const canWatch = canWatchAd(); console.log('[首页] 剩余广告次数:', remaining); console.log('[首页] 是否可以观看:', canWatch); console.log('[首页] 当前积分:', this.data.currentPoints); const lines = [ '当前积分:' + this.data.currentPoints, '', '每次占卜会消耗积分,不同牌阵消耗不同。', '每日首次登录可获得 +3 积分奖励。', '', '看广告可获得积分(今日剩余 ' + remaining + ' 次)' ]; const content = lines.join('\n'); console.log('[首页] 弹窗内容:', content); console.log('[首页] 准备调用 wx.showModal'); wx.showModal({ title: '积分说明', content: content, confirmText: canWatch ? '看广告' : '知道了', cancelText: '关闭', success: (res) => { console.log('[首页] 弹窗回调:', res); if (res.confirm && canWatch) { this.watchAdForPoints(); } }, fail: (err) => { console.error('[首页] 弹窗失败:', err); } }); console.log('[首页] wx.showModal 已调用'); } catch (error) { console.error('[首页] showPointsInfo 错误:', error); } }, // 观看广告获取积分(支持开发模式) watchAdForPoints: function () { // 检查是否还有观看次数 if (!canWatchAd()) { wx.showToast({ title: '今日广告次数已用完', icon: 'none', duration: 2000 }); return; } // 🔧 开发模式:模拟广告播放 if (DEV_MODE) { console.log('[广告系统] 🔧 开发模式:模拟广告播放中...'); wx.showLoading({ title: '广告播放中...', mask: true }); // 模拟广告播放时长(1.5秒) setTimeout(() => { wx.hideLoading(); console.log('[广告系统] 🔧 开发模式:模拟广告播放完成'); this.handleAdReward(); }, 1500); return; } // 生产模式:播放真实广告 // 检查广告实例是否存在 if (!this.rewardedAd) { wx.showToast({ title: '广告功能暂不可用', icon: 'none', duration: 2000 }); console.warn('[广告系统] 广告实例不存在'); return; } // 显示广告 this.rewardedAd.show() .catch((err) => { console.log('[广告系统] 广告未加载,尝试重新加载:', err); // 广告可能未加载,先加载再显示 this.rewardedAd.load() .then(() => { console.log('[广告系统] 广告加载成功,开始播放'); return this.rewardedAd.show(); }) .catch((loadErr) => { console.error('[广告系统] 广告加载失败:', loadErr); wx.showToast({ title: '广告加载失败,请稍后再试', icon: 'none', duration: 2000 }); }); }); } });