huaanglimeng/pages/zodiac/index.js

135 lines
3.9 KiB
JavaScript

const { getZodiacList } = require('../../utils/zodiacData');
const { fetchZodiacFortune, fetchDailyOverview } = require('../../utils/zodiacAI');
const { checkShareReward } = require('../../utils/pointsManager');
Page({
data: {
selectedZodiac: null,
currentFortune: null,
isLoading: false,
isMatchExpanded: false,
zodiacList: [],
dailyOverview: null // 新增:星象概览
},
onLoad: async function () {
this.setData({
zodiacList: getZodiacList()
});
// 加载星象概览
const overview = await fetchDailyOverview();
this.setData({ dailyOverview: overview });
},
// 返回上一页或列表
goBack: function () {
console.log('[Zodiac] goBack triggered');
if (this.data.selectedZodiac) {
// 如果在详情页,返回列表
this.setData({
selectedZodiac: null
});
} else {
// 如果在列表页,尝试返回上一页
const pages = getCurrentPages();
if (pages.length > 1) {
wx.navigateBack({
delta: 1
});
} else {
// 如果是直接打开(如分享进入),则返回首页
wx.reLaunch({
url: '/pages/home/home'
});
}
}
},
// 选择星座
selectZodiac: async function (e) {
const index = e.currentTarget.dataset.index;
const zodiac = this.data.zodiacList[index];
// 1. 设置加载状态 & 选中星座
this.setData({
selectedZodiac: zodiac,
currentFortune: null, // 清空旧数据
isLoading: true, // 开启 Loading
isMatchExpanded: false
});
// 2. 获取运势 (AI 或 缓存)
try {
const fortuneData = await fetchZodiacFortune(zodiac.name);
// 3. 渲染数据
this.setData({
currentFortune: fortuneData,
isLoading: false
});
} catch (err) {
console.error('获取运势失败', err);
this.setData({ isLoading: false });
wx.showToast({ title: '网络小差,请重试', icon: 'none' });
}
},
// 切换关系提示展开状态
toggleMatch: function () {
this.setData({
isMatchExpanded: !this.data.isMatchExpanded
});
},
// 返回列表
backToList: function () {
this.setData({
selectedZodiac: null,
currentFortune: null,
isLoading: false,
isMatchExpanded: false // 重置展开状态
});
},
// 跳转回塔罗页面
goToTarot: function () {
wx.navigateTo({
url: '/pages/index/index'
});
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
const titles = [
"我的今日星象运势竟然是...",
"宇宙刚刚给我发了一条星语指引 ✨",
"准到离谱!快来看看你的今日星象",
"这是我此刻的星象能量状态 🌟",
"为你抽取了一份宇宙星象指引"
];
const randomTitle = titles[Math.floor(Math.random() * titles.length)];
// 尝试发放分享奖励
const rewardResult = checkShareReward();
if (rewardResult.success) {
wx.showToast({
title: rewardResult.message,
icon: 'none',
duration: 2000
});
}
// 模拟生成 fromUserId
const mockUserId = 'user_' + Math.random().toString(36).substr(2, 9);
return {
title: randomTitle,
path: `/pages/zodiac/index?fromUserId=${mockUserId}`,
imageUrl: '/images/share-cover.png' // 使用统一的分享图或后续专用图
};
}
})