huaanglimeng/pages/home/home.js

214 lines
6.0 KiB
JavaScript

const { getPoints, checkDailyReward, canWatchAd, getTodayAdCount, rewardFromAd, AD_REWARD_CONFIG } = require('../../utils/pointsManager');
const { getDailyAdvice } = require('../../utils/dailyAdvice');
const { getDailyArticle, getCategories } = require('../../utils/knowledgeData');
Page({
data: {
currentPoints: 0,
dailyAdvice: '',
dailyArticle: {
id: '',
title: '',
summary: '',
category: '',
categoryName: '',
type: 'local'
},
energyVisible: false,
knowledgeVisible: false
},
onLoad: function () {
// 加载每日内容
this.loadDailyContent();
},
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);
},
// 显示积分说明
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 () {
const result = rewardFromAd();
if (result.success) {
// 刷新积分显示
this.setData({
currentPoints: getPoints()
});
// 显示奖励提示
wx.showToast({
title: result.message,
icon: 'success',
duration: 2000
});
} else {
// 显示失败提示
wx.showToast({
title: result.message,
icon: 'none',
duration: 2000
});
}
}
});