- 新增 server/:Node + Express + SQLite + node-cron 实现登录、纪念日 CRUD 和定时订阅消息推送 - 新增 .gitea/workflows/deploy.yml:推送即触发群晖 Docker 部署,监听 15002 - utils/api.js:自动按 envVersion 切换本地/线上 BASE_URL - app.js 与 add-anniversary.js 移除 wx.cloud 调用,改走自建后端 - cloudfunctions/ 暂保留以便回滚 - 一并提交此前未入库的首页 / 设置页 / 日历 / 万年历等改造 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+161
-81
@@ -1,122 +1,203 @@
|
||||
/**
|
||||
* 农历日期转换工具
|
||||
* 使用简化版的农历计算方法
|
||||
* 基于寿星万年历算法(1900-2100年)
|
||||
*/
|
||||
|
||||
// 1900-2100年的农历数据
|
||||
// 农历数据:每条数据记录该年的月份大小和闰月信息
|
||||
// 格式:高4位=闰月大小(0无闰/1闰大月), 中4位=闰月位置, 低12位=12个月大小(1大/0小)
|
||||
const lunarInfo = [
|
||||
0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
|
||||
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
|
||||
0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,
|
||||
0x06566, 0x0d4a0, 0x0ea50, 0x16a95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950
|
||||
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2, // 1900-1909
|
||||
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977, // 1910-1919
|
||||
0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970, // 1920-1929
|
||||
0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950, // 1930-1939
|
||||
0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557, // 1940-1949
|
||||
0x06ca0,0x0b550,0x15355,0x04da0,0x0a5b0,0x14573,0x052b0,0x0a9a8,0x0e950,0x06aa0, // 1950-1959
|
||||
0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0, // 1960-1969
|
||||
0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b6a0,0x195a6, // 1970-1979
|
||||
0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570, // 1980-1989
|
||||
0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06aa0,0x0a6b6,0x056a0,0x02b60,0x09570, // 1990-1999
|
||||
0x049b0,0x0a4b0,0x0aa50,0x1b255,0x06d40,0x0ad50,0x14b55,0x056a0,0x0a6d0,0x055d4, // 2000-2009
|
||||
0x052d0,0x0a9b8,0x0a950,0x0b4a0,0x0b6a6,0x0ad50,0x055a0,0x0aba4,0x0a5b0,0x052b0, // 2010-2019
|
||||
0x0b273,0x06930,0x07337,0x06aa0,0x0ad50,0x14b55,0x04b60,0x0a570,0x054e4,0x0d160, // 2020-2029
|
||||
0x0e968,0x0d520,0x0daa0,0x16aa6,0x056d0,0x04ae0,0x0a9d4,0x0a2d0,0x0d150,0x0f252, // 2030-2039
|
||||
0x0d520,0x0daa0,0x16aa6,0x056d0,0x04ae0,0x0a9d4,0x0a2d0,0x0d150,0x0f252,0x0d520, // 2040-2049
|
||||
0x0b54f,0x0d6a0,0x0ada0,0x14955,0x056a0,0x0a6d0,0x0155b,0x025d0,0x092d0,0x0d954, // 2050-2059
|
||||
0x0d4a0,0x0b550,0x0b4a9,0x04da0,0x0a5b0,0x15176,0x052b0,0x0a930,0x07954,0x06aa0, // 2060-2069
|
||||
0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,0x05aa0,0x076a3, // 2070-2079
|
||||
0x096d0,0x04afb,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,0x0b5a0,0x056d0, // 2080-2089
|
||||
0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0,0x14b63,0x09370 // 2090-2099
|
||||
]
|
||||
|
||||
/**
|
||||
* 农历年份
|
||||
*/
|
||||
const lunarMonths = ['正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊']
|
||||
const lunarDays = ['初', '十', '廿', '三']
|
||||
const lunarDayNames = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
|
||||
// 1900年1月31日是农历正月初一(庚子年)
|
||||
const BASE_DATE = new Date(1900, 0, 31)
|
||||
|
||||
const LUNAR_MONTHS = ['正','二','三','四','五','六','七','八','九','十','冬','腊']
|
||||
const LUNAR_DAYS_TENS = ['初','十','廿','三']
|
||||
const LUNAR_DAYS_UNITS = ['一','二','三','四','五','六','七','八','九','十']
|
||||
|
||||
/**
|
||||
* 获取农历月份名称
|
||||
* 获取农历某年的总天数
|
||||
*/
|
||||
function getLunarMonthName(month) {
|
||||
return lunarMonths[month - 1]
|
||||
function _lunarYearDays(year) {
|
||||
let total = 348
|
||||
for (let i = 0x8000; i > 0x8; i >>= 1) {
|
||||
total += (lunarInfo[year - 1900] & i) ? 1 : 0
|
||||
}
|
||||
return total + _leapDays(year)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取农历日期名称
|
||||
* 获取农历某年闰月的天数
|
||||
*/
|
||||
function getLunarDayName(day) {
|
||||
if (day === 10) return '初十'
|
||||
if (day === 20) return '二十'
|
||||
if (day === 30) return '三十'
|
||||
|
||||
const tens = Math.floor(day / 10)
|
||||
const units = day % 10
|
||||
|
||||
let name = lunarDays[tens]
|
||||
if (units > 0) {
|
||||
name += lunarDayNames[units - 1]
|
||||
function _leapDays(year) {
|
||||
if (_leapMonth(year)) {
|
||||
return (lunarInfo[year - 1900] & 0x10000) ? 30 : 29
|
||||
}
|
||||
|
||||
return name
|
||||
return 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算农历指定年的总天数
|
||||
* 获取农历某年的闰月月份,0表示无闰月
|
||||
*/
|
||||
function getLunarYearDays(year) {
|
||||
let total = 0
|
||||
for (let i = 0; i < 13; i++) {
|
||||
const days = lunarInfo[year] & (0x8000 >> i) ? 30 : 29
|
||||
total += days
|
||||
}
|
||||
return total
|
||||
function _leapMonth(year) {
|
||||
return lunarInfo[year - 1900] & 0xf
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取农历某年某月的天数
|
||||
*/
|
||||
function _monthDays(year, month) {
|
||||
return (lunarInfo[year - 1900] & (0x10000 >> month)) ? 30 : 29
|
||||
}
|
||||
|
||||
/**
|
||||
* 公历转农历
|
||||
* @param {Date} solarDate - 公历日期对象
|
||||
* @returns {Object} 农历日期对象 {year, month, day, lunarText}
|
||||
* @param {Date} solarDate
|
||||
* @returns {{ year, month, day, isLeap, lunarText }}
|
||||
*/
|
||||
function solarToLunar(solarDate) {
|
||||
const year = solarDate.getFullYear()
|
||||
const month = solarDate.getMonth() + 1
|
||||
const day = solarDate.getDate()
|
||||
|
||||
// 这是一个简化版本,实际计算需要完整的农历转换算法
|
||||
// 这里返回一个示例结果
|
||||
// 实际项目中建议使用成熟的农历库如 lunar-javascript
|
||||
|
||||
const offset = Math.floor((year - 1900) / 4) + (year - 1900) % 4
|
||||
|
||||
// 返回格式化的农历
|
||||
const date = new Date(solarDate.getFullYear(), solarDate.getMonth(), solarDate.getDate())
|
||||
let offset = Math.round((date - BASE_DATE) / 86400000)
|
||||
|
||||
let lunarYear, lunarMonth, lunarDay
|
||||
let isLeap = false
|
||||
|
||||
for (lunarYear = 1900; lunarYear < 2100 && offset > 0; lunarYear++) {
|
||||
const days = _lunarYearDays(lunarYear)
|
||||
offset -= days
|
||||
}
|
||||
if (offset < 0) {
|
||||
offset += _lunarYearDays(--lunarYear)
|
||||
}
|
||||
|
||||
const leapM = _leapMonth(lunarYear)
|
||||
let isLeapYear = false
|
||||
|
||||
for (lunarMonth = 1; lunarMonth < 13 && offset > 0; lunarMonth++) {
|
||||
if (leapM > 0 && lunarMonth === leapM + 1 && !isLeapYear) {
|
||||
--lunarMonth
|
||||
isLeapYear = true
|
||||
const leapDays = _leapDays(lunarYear)
|
||||
offset -= leapDays
|
||||
} else {
|
||||
offset -= _monthDays(lunarYear, lunarMonth)
|
||||
}
|
||||
if (isLeapYear && lunarMonth === leapM + 1) isLeapYear = false
|
||||
}
|
||||
|
||||
if (offset === 0 && leapM > 0 && lunarMonth === leapM + 1) {
|
||||
if (isLeapYear) {
|
||||
isLeapYear = false
|
||||
} else {
|
||||
isLeapYear = true
|
||||
--lunarMonth
|
||||
}
|
||||
}
|
||||
if (offset < 0) {
|
||||
offset += isLeapYear ? _leapDays(lunarYear) : _monthDays(lunarYear, lunarMonth)
|
||||
if (isLeapYear) isLeapYear = false
|
||||
else --lunarMonth
|
||||
}
|
||||
|
||||
lunarDay = offset + 1
|
||||
isLeap = isLeapYear
|
||||
|
||||
return {
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
lunarText: `${getLunarMonthName(month)}月${getLunarDayName(day)}`
|
||||
year: lunarYear,
|
||||
month: lunarMonth,
|
||||
day: lunarDay,
|
||||
isLeap,
|
||||
lunarText: `${isLeap ? '闰' : ''}${getLunarMonthName(lunarMonth)}月${getLunarDayName(lunarDay)}`
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 农历转公历
|
||||
* @param {Number} year - 农历年份
|
||||
* @param {Number} month - 农历月份
|
||||
* @param {Number} day - 农历日期
|
||||
* @returns {Date} 公历日期对象
|
||||
* 农历转公历(指定年份)
|
||||
* @param {Number} lunarYear
|
||||
* @param {Number} lunarMonth
|
||||
* @param {Number} lunarDay
|
||||
* @param {Boolean} isLeap 是否闰月
|
||||
* @returns {Date}
|
||||
*/
|
||||
function lunarToSolar(year, month, day) {
|
||||
// 简化版本,实际需要完整的农历转换算法
|
||||
// 这里返回当前日期作为示例
|
||||
// 实际项目中建议使用成熟的农历库
|
||||
|
||||
const now = new Date()
|
||||
return new Date(now.getFullYear(), month - 1, day)
|
||||
function lunarToSolar(lunarYear, lunarMonth, lunarDay, isLeap) {
|
||||
let offset = 0
|
||||
|
||||
for (let y = 1900; y < lunarYear; y++) {
|
||||
offset += _lunarYearDays(y)
|
||||
}
|
||||
|
||||
const leapM = _leapMonth(lunarYear)
|
||||
let hasLeap = false
|
||||
|
||||
for (let m = 1; m < lunarMonth; m++) {
|
||||
if (leapM > 0 && m === leapM && !hasLeap) {
|
||||
offset += _leapDays(lunarYear)
|
||||
hasLeap = true
|
||||
}
|
||||
offset += _monthDays(lunarYear, m)
|
||||
}
|
||||
|
||||
if (isLeap && lunarMonth === leapM) {
|
||||
offset += _monthDays(lunarYear, lunarMonth)
|
||||
}
|
||||
|
||||
offset += lunarDay - 1
|
||||
|
||||
const result = new Date(BASE_DATE.getTime() + offset * 86400000)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取农历年份的下一个相同农历日期的公历日期
|
||||
* @param {Number} lunarYear - 农历年份
|
||||
* @param {Number} lunarMonth - 农历月份
|
||||
* @param {Number} lunarDay - 农历日期
|
||||
* @returns {Date} 下一次该农历日期的公历日期
|
||||
* 获取指定农历月日在今年或明年的公历日期
|
||||
*/
|
||||
function getNextLunarDate(lunarYear, lunarMonth, lunarDay) {
|
||||
// 简化版本,返回明年同一天的日期
|
||||
function getNextLunarDate(lunarMonth, lunarDay) {
|
||||
const today = new Date()
|
||||
const nextYear = today.getFullYear() + 1
|
||||
return new Date(nextYear, today.getMonth(), today.getDate())
|
||||
const currentYear = today.getFullYear()
|
||||
const lunarToday = solarToLunar(today)
|
||||
|
||||
// 尝试今年
|
||||
let candidate = lunarToSolar(lunarToday.year, lunarMonth, lunarDay, false)
|
||||
if (candidate >= today) return candidate
|
||||
|
||||
// 明年
|
||||
return lunarToSolar(lunarToday.year + 1, lunarMonth, lunarDay, false)
|
||||
}
|
||||
|
||||
function getLunarMonthName(month) {
|
||||
return LUNAR_MONTHS[month - 1] || ''
|
||||
}
|
||||
|
||||
function getLunarDayName(day) {
|
||||
if (day === 10) return '初十'
|
||||
if (day === 20) return '二十'
|
||||
if (day === 30) return '三十'
|
||||
const tens = Math.floor(day / 10)
|
||||
const units = day % 10
|
||||
return LUNAR_DAYS_TENS[tens] + (units > 0 ? LUNAR_DAYS_UNITS[units - 1] : '')
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化农历显示文本
|
||||
*/
|
||||
function formatLunarText(lunarDate) {
|
||||
const lunar = solarToLunar(new Date(lunarDate.year, lunarDate.month - 1, lunarDate.day))
|
||||
return lunar.lunarText
|
||||
return solarToLunar(new Date(lunarDate.year, lunarDate.month - 1, lunarDate.day)).lunarText
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@@ -127,4 +208,3 @@ module.exports = {
|
||||
getLunarMonthName,
|
||||
getLunarDayName
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user