- 新增 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:
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* 后端接口请求封装
|
||||
*
|
||||
* 开发版(开发者工具)走本地 docker,体验版/正式版走线上 HTTPS。
|
||||
*/
|
||||
|
||||
const DEV_BASE_URL = 'http://localhost:3000'
|
||||
const PROD_BASE_URL = 'https://wxserver.ymxixi.space'
|
||||
|
||||
function resolveBaseUrl() {
|
||||
try {
|
||||
const env = wx.getAccountInfoSync().miniProgram.envVersion
|
||||
return env === 'develop' ? DEV_BASE_URL : PROD_BASE_URL
|
||||
} catch (e) {
|
||||
return DEV_BASE_URL
|
||||
}
|
||||
}
|
||||
|
||||
const BASE_URL = resolveBaseUrl()
|
||||
|
||||
function request({ url, method = 'POST', data = {}, withOpenid = true }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const header = { 'Content-Type': 'application/json' }
|
||||
if (withOpenid) {
|
||||
const openid = wx.getStorageSync('openid')
|
||||
if (openid) header['x-openid'] = openid
|
||||
}
|
||||
wx.request({
|
||||
url: BASE_URL + url,
|
||||
method,
|
||||
header,
|
||||
data,
|
||||
timeout: 10000,
|
||||
success: (res) => {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) resolve(res.data)
|
||||
else reject(new Error(`HTTP ${res.statusCode}`))
|
||||
},
|
||||
fail: (err) => reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 登录:用 wx.login 拿 code 换 openid
|
||||
function login() {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success: async (loginRes) => {
|
||||
try {
|
||||
const res = await request({
|
||||
url: '/api/login',
|
||||
data: { code: loginRes.code },
|
||||
withOpenid: false
|
||||
})
|
||||
if (!res.success) return reject(new Error(res.error || '登录失败'))
|
||||
resolve(res)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
},
|
||||
fail: reject
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 纪念日操作(兼容原云函数 action 协议)
|
||||
function anniversary(action, data) {
|
||||
return request({ url: '/api/anniversary', data: { action, data } })
|
||||
}
|
||||
|
||||
module.exports = { request, login, anniversary, BASE_URL }
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* 公共常量
|
||||
*/
|
||||
|
||||
const ANNIVERSARY_TYPES = {
|
||||
BIRTHDAY: 'birthday',
|
||||
LUNAR_BIRTHDAY: 'lunar_birthday',
|
||||
WEDDING: 'wedding',
|
||||
ENGAGEMENT: 'engagement',
|
||||
OTHER: 'other'
|
||||
}
|
||||
|
||||
const TYPE_NAMES = {
|
||||
birthday: '公历生日',
|
||||
lunar_birthday: '农历生日',
|
||||
wedding: '结婚纪念日',
|
||||
engagement: '订婚纪念日',
|
||||
other: '其他纪念日'
|
||||
}
|
||||
|
||||
const TYPE_ICONS = {
|
||||
birthday: '🎂',
|
||||
lunar_birthday: '🌙',
|
||||
wedding: '💍',
|
||||
engagement: '💕',
|
||||
other: '📅'
|
||||
}
|
||||
|
||||
const IMPORTANCE_TEXTS = {
|
||||
high: '非常重要',
|
||||
medium: '重要',
|
||||
low: '一般'
|
||||
}
|
||||
|
||||
const IMPORTANCE_COLORS = {
|
||||
high: '#ff5722',
|
||||
medium: '#ff9800',
|
||||
low: '#07c160'
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ANNIVERSARY_TYPES,
|
||||
TYPE_NAMES,
|
||||
TYPE_ICONS,
|
||||
IMPORTANCE_TEXTS,
|
||||
IMPORTANCE_COLORS
|
||||
}
|
||||
@@ -103,9 +103,28 @@ function parseDate(dateString) {
|
||||
return new Date(parseInt(parts[0]), parseInt(parts[1]) - 1, parseInt(parts[2]))
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算指定月日的下一次发生日期(今年或明年)
|
||||
* @param {Number} month - 月份 (1-12)
|
||||
* @param {Number} day - 日 (1-31)
|
||||
* @returns {{ date: Date, daysUntil: Number }}
|
||||
*/
|
||||
function getNextOccurrence(month, day) {
|
||||
const today = new Date()
|
||||
const currentYear = today.getFullYear()
|
||||
let date = new Date(currentYear, month - 1, day)
|
||||
let daysUntil = getDaysUntil(date)
|
||||
if (daysUntil < 0) {
|
||||
date = new Date(currentYear + 1, month - 1, day)
|
||||
daysUntil = getDaysUntil(date)
|
||||
}
|
||||
return { date, daysUntil }
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatDate,
|
||||
getDaysUntil,
|
||||
getNextOccurrence,
|
||||
isToday,
|
||||
isPast,
|
||||
isUpcoming,
|
||||
|
||||
+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
|
||||
}
|
||||
|
||||
|
||||
+92
-39
@@ -1,17 +1,29 @@
|
||||
/**
|
||||
* 本地存储管理工具
|
||||
* 本地存储管理工具(含缓存层)
|
||||
*/
|
||||
|
||||
// 内存缓存
|
||||
const _cache = {
|
||||
persons: null,
|
||||
anniversaries: null
|
||||
}
|
||||
|
||||
function _invalidate() {
|
||||
_cache.persons = null
|
||||
_cache.anniversaries = null
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储人员数据
|
||||
*/
|
||||
function savePersons(persons) {
|
||||
try {
|
||||
wx.setStorageSync('persons', persons)
|
||||
return true
|
||||
_cache.persons = persons
|
||||
return { success: true }
|
||||
} catch (e) {
|
||||
console.error('保存人员数据失败', e)
|
||||
return false
|
||||
return { success: false, error: e.message }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +31,10 @@ function savePersons(persons) {
|
||||
* 获取人员数据
|
||||
*/
|
||||
function getPersons() {
|
||||
if (_cache.persons !== null) return _cache.persons
|
||||
try {
|
||||
return wx.getStorageSync('persons') || []
|
||||
_cache.persons = wx.getStorageSync('persons') || []
|
||||
return _cache.persons
|
||||
} catch (e) {
|
||||
console.error('获取人员数据失败', e)
|
||||
return []
|
||||
@@ -31,8 +45,7 @@ function getPersons() {
|
||||
* 根据ID获取人员
|
||||
*/
|
||||
function getPersonById(id) {
|
||||
const persons = getPersons()
|
||||
return persons.find(p => p.id === id) || null
|
||||
return getPersons().find(p => p.id === id) || null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,7 +60,8 @@ function addPerson(person) {
|
||||
updateTime: new Date().getTime()
|
||||
}
|
||||
persons.push(newPerson)
|
||||
return savePersons(persons) ? newPerson : null
|
||||
const result = savePersons(persons)
|
||||
return result.success ? newPerson : null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,12 +71,8 @@ function updatePerson(id, updates) {
|
||||
const persons = getPersons()
|
||||
const index = persons.findIndex(p => p.id === id)
|
||||
if (index !== -1) {
|
||||
persons[index] = {
|
||||
...persons[index],
|
||||
...updates,
|
||||
updateTime: new Date().getTime()
|
||||
}
|
||||
return savePersons(persons)
|
||||
persons[index] = { ...persons[index], ...updates, updateTime: new Date().getTime() }
|
||||
return savePersons(persons).success
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -72,8 +82,7 @@ function updatePerson(id, updates) {
|
||||
*/
|
||||
function deletePerson(id) {
|
||||
const persons = getPersons()
|
||||
const filtered = persons.filter(p => p.id !== id)
|
||||
return savePersons(filtered)
|
||||
return savePersons(persons.filter(p => p.id !== id)).success
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,10 +91,11 @@ function deletePerson(id) {
|
||||
function saveAnniversaries(anniversaries) {
|
||||
try {
|
||||
wx.setStorageSync('anniversaries', anniversaries)
|
||||
return true
|
||||
_cache.anniversaries = anniversaries
|
||||
return { success: true }
|
||||
} catch (e) {
|
||||
console.error('保存纪念日数据失败', e)
|
||||
return false
|
||||
return { success: false, error: e.message }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,8 +103,10 @@ function saveAnniversaries(anniversaries) {
|
||||
* 获取纪念日数据
|
||||
*/
|
||||
function getAnniversaries() {
|
||||
if (_cache.anniversaries !== null) return _cache.anniversaries
|
||||
try {
|
||||
return wx.getStorageSync('anniversaries') || []
|
||||
_cache.anniversaries = wx.getStorageSync('anniversaries') || []
|
||||
return _cache.anniversaries
|
||||
} catch (e) {
|
||||
console.error('获取纪念日数据失败', e)
|
||||
return []
|
||||
@@ -105,8 +117,7 @@ function getAnniversaries() {
|
||||
* 根据人员ID获取纪念日
|
||||
*/
|
||||
function getAnniversariesByPersonId(personId) {
|
||||
const anniversaries = getAnniversaries()
|
||||
return anniversaries.filter(a => a.personId === personId)
|
||||
return getAnniversaries().filter(a => a.personId === personId)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,7 +132,8 @@ function addAnniversary(anniversary) {
|
||||
updateTime: new Date().getTime()
|
||||
}
|
||||
anniversaries.push(newAnniversary)
|
||||
return saveAnniversaries(anniversaries) ? newAnniversary : null
|
||||
const result = saveAnniversaries(anniversaries)
|
||||
return result.success ? newAnniversary : null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,12 +143,8 @@ function updateAnniversary(id, updates) {
|
||||
const anniversaries = getAnniversaries()
|
||||
const index = anniversaries.findIndex(a => a.id === id)
|
||||
if (index !== -1) {
|
||||
anniversaries[index] = {
|
||||
...anniversaries[index],
|
||||
...updates,
|
||||
updateTime: new Date().getTime()
|
||||
}
|
||||
return saveAnniversaries(anniversaries)
|
||||
anniversaries[index] = { ...anniversaries[index], ...updates, updateTime: new Date().getTime() }
|
||||
return saveAnniversaries(anniversaries).success
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -146,8 +154,26 @@ function updateAnniversary(id, updates) {
|
||||
*/
|
||||
function deleteAnniversary(id) {
|
||||
const anniversaries = getAnniversaries()
|
||||
const filtered = anniversaries.filter(a => a.id !== id)
|
||||
return saveAnniversaries(filtered)
|
||||
return saveAnniversaries(anniversaries.filter(a => a.id !== id)).success
|
||||
}
|
||||
|
||||
/**
|
||||
* 原子性删除人员及其所有纪念日
|
||||
*/
|
||||
function deletePersonWithAnniversaries(personId) {
|
||||
try {
|
||||
const persons = getPersons().filter(p => p.id !== personId)
|
||||
const anniversaries = getAnniversaries().filter(a => a.personId !== personId)
|
||||
wx.setStorageSync('persons', persons)
|
||||
wx.setStorageSync('anniversaries', anniversaries)
|
||||
_cache.persons = persons
|
||||
_cache.anniversaries = anniversaries
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('删除人员及纪念日失败', e)
|
||||
_invalidate() // 缓存可能不一致,强制失效
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,20 +195,46 @@ function exportData() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入数据
|
||||
* 验证人员数据结构
|
||||
*/
|
||||
function _validatePerson(p) {
|
||||
return p && typeof p === 'object' && typeof p.id === 'string' && typeof p.name === 'string'
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证纪念日数据结构
|
||||
*/
|
||||
function _validateAnniversary(a) {
|
||||
return a && typeof a === 'object' && typeof a.id === 'string' &&
|
||||
typeof a.personId === 'string' &&
|
||||
typeof a.solarMonth === 'number' && typeof a.solarDay === 'number'
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入数据(含验证)
|
||||
*/
|
||||
function importData(data) {
|
||||
try {
|
||||
if (data.persons && Array.isArray(data.persons)) {
|
||||
wx.setStorageSync('persons', data.persons)
|
||||
if (!data || typeof data !== 'object') {
|
||||
return { success: false, error: '无效的数据格式' }
|
||||
}
|
||||
if (data.anniversaries && Array.isArray(data.anniversaries)) {
|
||||
wx.setStorageSync('anniversaries', data.anniversaries)
|
||||
if (!Array.isArray(data.persons) || !Array.isArray(data.anniversaries)) {
|
||||
return { success: false, error: 'persons 和 anniversaries 必须是数组' }
|
||||
}
|
||||
return true
|
||||
if (!data.persons.every(_validatePerson)) {
|
||||
return { success: false, error: '人员数据格式不正确' }
|
||||
}
|
||||
if (!data.anniversaries.every(_validateAnniversary)) {
|
||||
return { success: false, error: '纪念日数据格式不正确' }
|
||||
}
|
||||
wx.setStorageSync('persons', data.persons)
|
||||
wx.setStorageSync('anniversaries', data.anniversaries)
|
||||
_cache.persons = data.persons
|
||||
_cache.anniversaries = data.anniversaries
|
||||
return { success: true }
|
||||
} catch (e) {
|
||||
console.error('导入数据失败', e)
|
||||
return false
|
||||
return { success: false, error: e.message }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,6 +244,7 @@ function importData(data) {
|
||||
function clearAllData() {
|
||||
try {
|
||||
wx.clearStorageSync()
|
||||
_invalidate()
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('清空数据失败', e)
|
||||
@@ -207,7 +260,8 @@ module.exports = {
|
||||
addPerson,
|
||||
updatePerson,
|
||||
deletePerson,
|
||||
|
||||
deletePersonWithAnniversaries,
|
||||
|
||||
// 纪念日相关
|
||||
saveAnniversaries,
|
||||
getAnniversaries,
|
||||
@@ -215,10 +269,9 @@ module.exports = {
|
||||
addAnniversary,
|
||||
updateAnniversary,
|
||||
deleteAnniversary,
|
||||
|
||||
|
||||
// 工具函数
|
||||
exportData,
|
||||
importData,
|
||||
clearAllData
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user