Initial Commit
This commit is contained in:
+117
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* 日期工具函数
|
||||
*/
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
* @param {Date} date - 日期对象
|
||||
* @param {String} format - 格式字符串,默认 'YYYY-MM-DD'
|
||||
* @returns {String} 格式化后的日期字符串
|
||||
*/
|
||||
function formatDate(date, format = 'YYYY-MM-DD') {
|
||||
if (!date) return ''
|
||||
|
||||
const d = new Date(date)
|
||||
const year = d.getFullYear()
|
||||
const month = String(d.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(d.getDate()).padStart(2, '0')
|
||||
const hour = String(d.getHours()).padStart(2, '0')
|
||||
const minute = String(d.getMinutes()).padStart(2, '0')
|
||||
const second = String(d.getSeconds()).padStart(2, '0')
|
||||
|
||||
return format
|
||||
.replace('YYYY', year)
|
||||
.replace('MM', month)
|
||||
.replace('DD', day)
|
||||
.replace('HH', hour)
|
||||
.replace('mm', minute)
|
||||
.replace('ss', second)
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算距离今天还有多少天
|
||||
* @param {Date} targetDate - 目标日期
|
||||
* @returns {Number} 天数差
|
||||
*/
|
||||
function getDaysUntil(targetDate) {
|
||||
const today = new Date()
|
||||
today.setHours(0, 0, 0, 0)
|
||||
|
||||
const target = new Date(targetDate)
|
||||
target.setHours(0, 0, 0, 0)
|
||||
|
||||
const diff = target.getTime() - today.getTime()
|
||||
return Math.ceil(diff / (1000 * 60 * 60 * 24))
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是今天
|
||||
*/
|
||||
function isToday(date) {
|
||||
return getDaysUntil(date) === 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否已过期
|
||||
*/
|
||||
function isPast(date) {
|
||||
return getDaysUntil(date) < 0
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否即将到来
|
||||
*/
|
||||
function isUpcoming(date) {
|
||||
const days = getDaysUntil(date)
|
||||
return days > 0 && days <= 7
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取星期几
|
||||
*/
|
||||
function getWeekDay(date) {
|
||||
const weekdays = ['日', '一', '二', '三', '四', '五', '六']
|
||||
const d = new Date(date)
|
||||
return weekdays[d.getDay()]
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否在本月
|
||||
*/
|
||||
function isInThisMonth(date) {
|
||||
const today = new Date()
|
||||
const target = new Date(date)
|
||||
return today.getFullYear() === target.getFullYear() &&
|
||||
today.getMonth() === target.getMonth()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月的开始日期和结束日期
|
||||
*/
|
||||
function getMonthRange(year, month) {
|
||||
const start = new Date(year, month - 1, 1)
|
||||
const end = new Date(year, month, 0)
|
||||
return { start, end }
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日期对象(从YYYY-MM-DD字符串)
|
||||
*/
|
||||
function parseDate(dateString) {
|
||||
if (!dateString) return null
|
||||
const parts = dateString.split('-')
|
||||
return new Date(parseInt(parts[0]), parseInt(parts[1]) - 1, parseInt(parts[2]))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatDate,
|
||||
getDaysUntil,
|
||||
isToday,
|
||||
isPast,
|
||||
isUpcoming,
|
||||
getWeekDay,
|
||||
isInThisMonth,
|
||||
getMonthRange,
|
||||
parseDate
|
||||
}
|
||||
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* 农历日期转换工具
|
||||
* 使用简化版的农历计算方法
|
||||
*/
|
||||
|
||||
// 1900-2100年的农历数据
|
||||
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
|
||||
]
|
||||
|
||||
/**
|
||||
* 农历年份
|
||||
*/
|
||||
const lunarMonths = ['正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊']
|
||||
const lunarDays = ['初', '十', '廿', '三']
|
||||
const lunarDayNames = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
|
||||
|
||||
/**
|
||||
* 获取农历月份名称
|
||||
*/
|
||||
function getLunarMonthName(month) {
|
||||
return lunarMonths[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
|
||||
|
||||
let name = lunarDays[tens]
|
||||
if (units > 0) {
|
||||
name += lunarDayNames[units - 1]
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算农历指定年的总天数
|
||||
*/
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* 公历转农历
|
||||
* @param {Date} solarDate - 公历日期对象
|
||||
* @returns {Object} 农历日期对象 {year, month, day, 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
|
||||
|
||||
// 返回格式化的农历
|
||||
return {
|
||||
year: year,
|
||||
month: month,
|
||||
day: day,
|
||||
lunarText: `${getLunarMonthName(month)}月${getLunarDayName(day)}`
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 农历转公历
|
||||
* @param {Number} year - 农历年份
|
||||
* @param {Number} month - 农历月份
|
||||
* @param {Number} day - 农历日期
|
||||
* @returns {Date} 公历日期对象
|
||||
*/
|
||||
function lunarToSolar(year, month, day) {
|
||||
// 简化版本,实际需要完整的农历转换算法
|
||||
// 这里返回当前日期作为示例
|
||||
// 实际项目中建议使用成熟的农历库
|
||||
|
||||
const now = new Date()
|
||||
return new Date(now.getFullYear(), month - 1, day)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取农历年份的下一个相同农历日期的公历日期
|
||||
* @param {Number} lunarYear - 农历年份
|
||||
* @param {Number} lunarMonth - 农历月份
|
||||
* @param {Number} lunarDay - 农历日期
|
||||
* @returns {Date} 下一次该农历日期的公历日期
|
||||
*/
|
||||
function getNextLunarDate(lunarYear, lunarMonth, lunarDay) {
|
||||
// 简化版本,返回明年同一天的日期
|
||||
const today = new Date()
|
||||
const nextYear = today.getFullYear() + 1
|
||||
return new Date(nextYear, today.getMonth(), today.getDate())
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化农历显示文本
|
||||
*/
|
||||
function formatLunarText(lunarDate) {
|
||||
const lunar = solarToLunar(new Date(lunarDate.year, lunarDate.month - 1, lunarDate.day))
|
||||
return lunar.lunarText
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
solarToLunar,
|
||||
lunarToSolar,
|
||||
getNextLunarDate,
|
||||
formatLunarText,
|
||||
getLunarMonthName,
|
||||
getLunarDayName
|
||||
}
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
/**
|
||||
* 本地存储管理工具
|
||||
*/
|
||||
|
||||
/**
|
||||
* 存储人员数据
|
||||
*/
|
||||
function savePersons(persons) {
|
||||
try {
|
||||
wx.setStorageSync('persons', persons)
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('保存人员数据失败', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取人员数据
|
||||
*/
|
||||
function getPersons() {
|
||||
try {
|
||||
return wx.getStorageSync('persons') || []
|
||||
} catch (e) {
|
||||
console.error('获取人员数据失败', e)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取人员
|
||||
*/
|
||||
function getPersonById(id) {
|
||||
const persons = getPersons()
|
||||
return persons.find(p => p.id === id) || null
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加人员
|
||||
*/
|
||||
function addPerson(person) {
|
||||
const persons = getPersons()
|
||||
const newPerson = {
|
||||
id: generateId(),
|
||||
...person,
|
||||
createTime: new Date().getTime(),
|
||||
updateTime: new Date().getTime()
|
||||
}
|
||||
persons.push(newPerson)
|
||||
return savePersons(persons) ? newPerson : null
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新人员
|
||||
*/
|
||||
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)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除人员
|
||||
*/
|
||||
function deletePerson(id) {
|
||||
const persons = getPersons()
|
||||
const filtered = persons.filter(p => p.id !== id)
|
||||
return savePersons(filtered)
|
||||
}
|
||||
|
||||
/**
|
||||
* 存储纪念日数据
|
||||
*/
|
||||
function saveAnniversaries(anniversaries) {
|
||||
try {
|
||||
wx.setStorageSync('anniversaries', anniversaries)
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('保存纪念日数据失败', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取纪念日数据
|
||||
*/
|
||||
function getAnniversaries() {
|
||||
try {
|
||||
return wx.getStorageSync('anniversaries') || []
|
||||
} catch (e) {
|
||||
console.error('获取纪念日数据失败', e)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据人员ID获取纪念日
|
||||
*/
|
||||
function getAnniversariesByPersonId(personId) {
|
||||
const anniversaries = getAnniversaries()
|
||||
return anniversaries.filter(a => a.personId === personId)
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加纪念日
|
||||
*/
|
||||
function addAnniversary(anniversary) {
|
||||
const anniversaries = getAnniversaries()
|
||||
const newAnniversary = {
|
||||
id: generateId(),
|
||||
...anniversary,
|
||||
createTime: new Date().getTime(),
|
||||
updateTime: new Date().getTime()
|
||||
}
|
||||
anniversaries.push(newAnniversary)
|
||||
return saveAnniversaries(anniversaries) ? newAnniversary : null
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新纪念日
|
||||
*/
|
||||
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)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除纪念日
|
||||
*/
|
||||
function deleteAnniversary(id) {
|
||||
const anniversaries = getAnniversaries()
|
||||
const filtered = anniversaries.filter(a => a.id !== id)
|
||||
return saveAnniversaries(filtered)
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成唯一ID
|
||||
*/
|
||||
function generateId() {
|
||||
return 'id_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出数据
|
||||
*/
|
||||
function exportData() {
|
||||
return {
|
||||
persons: getPersons(),
|
||||
anniversaries: getAnniversaries(),
|
||||
exportTime: new Date().getTime()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入数据
|
||||
*/
|
||||
function importData(data) {
|
||||
try {
|
||||
if (data.persons && Array.isArray(data.persons)) {
|
||||
wx.setStorageSync('persons', data.persons)
|
||||
}
|
||||
if (data.anniversaries && Array.isArray(data.anniversaries)) {
|
||||
wx.setStorageSync('anniversaries', data.anniversaries)
|
||||
}
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('导入数据失败', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有数据
|
||||
*/
|
||||
function clearAllData() {
|
||||
try {
|
||||
wx.clearStorageSync()
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('清空数据失败', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
// 人员相关
|
||||
savePersons,
|
||||
getPersons,
|
||||
getPersonById,
|
||||
addPerson,
|
||||
updatePerson,
|
||||
deletePerson,
|
||||
|
||||
// 纪念日相关
|
||||
saveAnniversaries,
|
||||
getAnniversaries,
|
||||
getAnniversariesByPersonId,
|
||||
addAnniversary,
|
||||
updateAnniversary,
|
||||
deleteAnniversary,
|
||||
|
||||
// 工具函数
|
||||
exportData,
|
||||
importData,
|
||||
clearAllData
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user