huaanglimeng/pages/home/home.js

375 lines
11 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { getPoints, checkDailyReward, canWatchAd, getTodayAdCount, rewardFromAd, AD_REWARD_CONFIG } = require('../../utils/pointsManager');
const { getDailyAdvice } = require('../../utils/dailyAdvice');
const { getDailyArticle, getCategories } = require('../../utils/knowledgeData');
/**
* 🔧 广告功能开关 (上线前配置)
*
* 1. ENABLE_AD (总开关):
* - true: 开启广告功能
* - false: 关闭广告功能 (隐藏按钮, 点击提示"即将上线")
*
* 2. DEV_MODE (开发模式):
* - true: 使用模拟广告 (不拉起微信广告, 直接发奖励)
* - false: 使用真实广告 (拉起微信广告组件)
*
* ⚠️ 上线前:
* - 确保已开通流量主并填入真实 ID
* - 将 DEV_MODE 改为 false
* - 将 ENABLE_AD 改为 true
*/
const ENABLE_AD = false; // 👈 暂时关闭广告功能
const DEV_MODE = true; // 👈 开发测试模式
// 积分系统开关 (即便 ENABLE_AD 为 true, 如果此开关为 false, UI 也不显示积分)
const ENABLE_POINTS_UI = true;
Page({
data: {
currentPoints: 0,
dailyAdvice: '',
dailyArticle: {
id: '',
title: '',
summary: '',
category: '',
categoryName: '',
type: 'local'
},
showAdButton: ENABLE_AD, // 控制广告按钮显示
knowledgeVisible: false
},
onLoad: function () {
// 加载每日内容
this.loadDailyContent();
// 初始化激励视频广告
this.initRewardedAd();
},
// ... (keeping other methods)
// 跳转到星座模块
goToZodiac: function () {
wx.navigateTo({
url: '/pages/zodiac/index'
});
},
/**
* 初始化激励视频广告
* ⚠️ 需要在微信公众平台开通流量主并获取广告位ID
*/
initRewardedAd: function () {
// 1. 如果广告功能关闭,直接返回
if (!ENABLE_AD) {
console.log('[广告系统] 🔧 广告功能已关闭 (ENABLE_AD = false)');
return;
}
// 2. 如果是开发模式,使用模拟广告,不需要初始化真实组件
if (DEV_MODE) {
console.log('[广告系统] 🔧 开发模式:使用模拟广告 (DEV_MODE = true)');
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 () {
if (ENABLE_POINTS_UI) {
// 检查每日登录奖励
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'
});
},
// 跳转到星座模块
goToZodiac: function () {
wx.navigateTo({
url: '/pages/zodiac/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);
},
// 跳转到设置页
goToSettings: function () {
wx.navigateTo({
url: '/pages/settings/settings'
});
},
// 显示积分说明 (切换为自定义浮层)
showPointsInfo: function () {
this.setData({
pointsVisible: true
});
},
// 关闭积分说明
hidePointsInfo: function () {
this.setData({
pointsVisible: false
});
},
// 观看广告获取积分(支持开发模式)
watchAdForPoints: function () {
// 1. 全局开关检查
if (!ENABLE_AD) {
wx.showToast({
title: '功能即将上线',
icon: 'none',
duration: 2000
});
return;
}
// 2. 检查是否还有观看次数
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
});
});
});
}
});