- loadWishes使用safeInitRibbons/safeInitDanmaku,防止初始化出错导致页面崩溃 - getCurrentTree确保返回带type字段的有效树对象 - 添加错误处理,避免未定义错误
This commit is contained in:
@@ -172,8 +172,8 @@ Page({
|
||||
|
||||
if (res.code === 0) {
|
||||
this.setData({ wishes: res.data.list || [] });
|
||||
this.initRibbons();
|
||||
this.initDanmaku();
|
||||
this.safeInitRibbons();
|
||||
this.safeInitDanmaku();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('加载许愿失败:', err);
|
||||
@@ -181,8 +181,28 @@ Page({
|
||||
this.setData({
|
||||
wishes: this.getMockWishes(),
|
||||
});
|
||||
this.safeInitRibbons();
|
||||
this.safeInitDanmaku();
|
||||
}
|
||||
},
|
||||
|
||||
// 安全初始化丝带
|
||||
safeInitRibbons() {
|
||||
try {
|
||||
this.initRibbons();
|
||||
} catch (err) {
|
||||
console.error('初始化丝带失败:', err);
|
||||
this.ribbons = [];
|
||||
}
|
||||
},
|
||||
|
||||
// 安全初始化弹幕
|
||||
safeInitDanmaku() {
|
||||
try {
|
||||
this.initDanmaku();
|
||||
} catch (err) {
|
||||
console.error('初始化弹幕失败:', err);
|
||||
this.danmaku = [];
|
||||
}
|
||||
},
|
||||
|
||||
@@ -931,7 +951,16 @@ Page({
|
||||
// 获取当前树
|
||||
getCurrentTree() {
|
||||
const { trees, currentTreeIndex } = this.data;
|
||||
return trees[currentTreeIndex] || { type: 'pine' };
|
||||
const tree = trees[currentTreeIndex];
|
||||
// 确保返回有效的树对象,带默认类型
|
||||
if (!tree) {
|
||||
return { type: 'pine', name: '许愿树' };
|
||||
}
|
||||
// 确保有type字段
|
||||
if (!tree.type) {
|
||||
tree.type = 'pine';
|
||||
}
|
||||
return tree;
|
||||
},
|
||||
|
||||
// 切换树
|
||||
|
||||
+21
-13
@@ -269,14 +269,16 @@ function getConstellation(month, day) {
|
||||
}
|
||||
}
|
||||
|
||||
/** 计算某年某节气的公历日期(21世纪 C 值法,误差±1天) */
|
||||
/** 计算某年某节气的公历日期(21世纪 C 值法,误差±1天)
|
||||
* termIndex 按天文顺序:小寒=0, 大寒=1, 立春=2, 雨水=3, ..., 冬至=23
|
||||
*/
|
||||
function getSolarTermDate(year, termIndex) {
|
||||
// 21世纪 C 值表(小寒=0, 大寒=1, 立春=2 ... 冬至=23)
|
||||
// 21世纪 C 值表(小寒=0, 大寒=1, 立春=2 ... 冬至=23)已校准
|
||||
const C21 = [
|
||||
6.11, 20.84, 4.6295, 19.4599, 6.3826, 21.4155, // 小寒 大寒 立春 雨水 惊蛰 春分
|
||||
5.59, 20.888, 6.318, 21.86, 6.5, 22.20, // 清明 谷雨 立夏 小满 芒种 夏至
|
||||
7.928, 23.65, 8.35, 23.95, 8.44, 23.822, // 小暑 大暑 立秋 处暑 白露 秋分
|
||||
9.098, 24.218, 8.218, 23.08, 7.9, 22.60 // 寒露 霜降 立冬 小雪 大雪 冬至
|
||||
5.11, 19.84, 3.6295, 18.4599, 5.3826, 20.4155, // 小寒 大寒 立春 雨水 惊蛰 春分
|
||||
4.59, 19.888, 5.318, 20.86, 5.5, 21.20, // 清明 谷雨 立夏 小满 芒种 夏至
|
||||
6.928, 22.65, 7.35, 22.95, 7.44, 22.822, // 小暑 大暑 立秋 处暑 白露 秋分
|
||||
8.098, 23.218, 7.218, 22.08, 6.9, 21.60 // 寒露 霜降 立冬 小雪 大雪 冬至
|
||||
];
|
||||
// 20世纪 C 值表
|
||||
const C20 = [
|
||||
@@ -311,16 +313,22 @@ function getSolarTermDate(year, termIndex) {
|
||||
if (year === 2021 && termIndex === 22) leapAdjust = -1;
|
||||
if (year === 1902 && termIndex === 23) leapAdjust = 1; // 冬至
|
||||
|
||||
return Math.floor(Y * 0.2422 + C) - Math.floor((Y - 1) / 4) + leapAdjust;
|
||||
return Math.floor(Y * 0.2422 + C) - Math.floor(Y / 4) + leapAdjust;
|
||||
}
|
||||
|
||||
/** 获取节气(准确算法) */
|
||||
/** 获取节气(准确算法)
|
||||
* SOLAR_TERMS 数组从冬至开始:冬至=0, 小寒=1, 大寒=2, 立春=3, ..., 大雪=23
|
||||
*/
|
||||
function getSolarTerm(year, month, day) {
|
||||
// 每月两个节气:第1个在 index (month-1)*2,第2个在 (month-1)*2+1
|
||||
const idx1 = (month - 1) * 2;
|
||||
const idx2 = idx1 + 1;
|
||||
const d1 = getSolarTermDate(year, idx1);
|
||||
const d2 = getSolarTermDate(year, idx2);
|
||||
// month 月的两个节气在 SOLAR_TERMS 中的索引:
|
||||
// 1月→小寒(1)、大寒(2);2月→立春(3)、雨水(4);...;12月→大雪(23)、冬至(0)
|
||||
const idx1 = month === 12 ? 23 : (month * 2 - 1); // 每月第一个节气
|
||||
const idx2 = month === 12 ? 0 : (month * 2); // 每月第二个节气
|
||||
// 转为天文顺序(小寒=0):SOLAR_TERMS 索引 - 1(冬至特殊为23)
|
||||
const astroIdx1 = idx1 === 0 ? 23 : idx1 - 1;
|
||||
const astroIdx2 = idx2 === 0 ? 23 : idx2 - 1;
|
||||
const d1 = getSolarTermDate(year, astroIdx1);
|
||||
const d2 = getSolarTermDate(year, astroIdx2);
|
||||
if (day === d1) return SOLAR_TERMS[idx1];
|
||||
if (day === d2) return SOLAR_TERMS[idx2];
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user