225 lines
4.1 KiB
JavaScript
225 lines
4.1 KiB
JavaScript
/**
|
|
* 本地存储管理工具
|
|
*/
|
|
|
|
/**
|
|
* 存储人员数据
|
|
*/
|
|
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
|
|
}
|
|
|