weixinxiaochengxu/app.js

60 lines
1.6 KiB
JavaScript

App({
globalData: {
items: ["血红蛋白", "尿素", "肌酐", "尿酸", "血钾", "血钙", "血磷", "β2微球蛋白", "体重"],
records: []
},
onLaunch() {
const defaults = ["血红蛋白", "尿素", "肌酐", "尿酸", "血钾", "血钙", "血磷", "β2微球蛋白", "体重"]
const data = wx.getStorageSync("health_data")
if (data) {
// 合并:保留用户自定义项目,同时补上新增的默认项目
const storedItems = data.items || []
const merged = [...defaults]
storedItems.forEach(item => {
if (!merged.includes(item)) merged.push(item)
})
this.globalData.items = merged
this.globalData.records = data.records || []
} else {
this.globalData.items = [...defaults]
}
this.saveData()
},
saveData() {
wx.setStorageSync("health_data", {
items: this.globalData.items,
records: this.globalData.records
})
},
addRecord(record) {
this.globalData.records.push({
id: Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
item: record.item,
date: record.date,
value: parseFloat(record.value)
})
this.saveData()
},
deleteRecord(id) {
this.globalData.records = this.globalData.records.filter(r => r.id !== id)
this.saveData()
},
addItem(name) {
if (!this.globalData.items.includes(name)) {
this.globalData.items.push(name)
this.saveData()
}
},
removeItem(name) {
this.globalData.items = this.globalData.items.filter(i => i !== name)
this.globalData.records = this.globalData.records.filter(r => r.item !== name)
this.saveData()
}
})