122 lines
3.3 KiB
JavaScript
122 lines
3.3 KiB
JavaScript
App({
|
|
globalData: {
|
|
currentPersonId: null,
|
|
persons: [],
|
|
dataMap: {}
|
|
},
|
|
|
|
onLaunch() {
|
|
// 清除旧版存储,避免残留
|
|
wx.removeStorageSync("health_data")
|
|
var data = wx.getStorageSync("health_data_v2")
|
|
if (data) {
|
|
this.globalData.persons = data.persons || []
|
|
this.globalData.dataMap = data.dataMap || {}
|
|
this.globalData.currentPersonId = data.currentPersonId || (this.globalData.persons[0] && this.globalData.persons[0].id) || null
|
|
}
|
|
// 无存储或 persons 为空时,由首页弹窗引导创建首个用户
|
|
},
|
|
|
|
_defaultItems() {
|
|
return ["血红蛋白", "尿素", "肌酐", "尿酸", "血钾", "血钙", "血磷", "β2微球蛋白", "体重"]
|
|
},
|
|
|
|
saveData() {
|
|
wx.setStorageSync("health_data_v2", {
|
|
persons: this.globalData.persons,
|
|
dataMap: this.globalData.dataMap,
|
|
currentPersonId: this.globalData.currentPersonId
|
|
})
|
|
},
|
|
|
|
getCurrentData() {
|
|
var id = this.globalData.currentPersonId
|
|
if (!id || !this.globalData.dataMap[id]) return { items: [], records: [] }
|
|
return this.globalData.dataMap[id]
|
|
},
|
|
|
|
getCurrentPerson() {
|
|
var id = this.globalData.currentPersonId
|
|
var persons = this.globalData.persons
|
|
for (var i = 0; i < persons.length; i++) {
|
|
if (persons[i].id === id) return persons[i]
|
|
}
|
|
return null
|
|
},
|
|
|
|
switchPerson(id) {
|
|
if (!this.globalData.dataMap[id]) return false
|
|
this.globalData.currentPersonId = id
|
|
this.saveData()
|
|
return true
|
|
},
|
|
|
|
addPerson(name) {
|
|
var id = Date.now().toString(36) + Math.random().toString(36).slice(2, 6)
|
|
this.globalData.persons.push({ id: id, name: name })
|
|
// 新用户默认有一份默认项目列表
|
|
this.globalData.dataMap[id] = {
|
|
items: this._defaultItems(),
|
|
records: []
|
|
}
|
|
this.saveData()
|
|
return id
|
|
},
|
|
|
|
removePerson(id) {
|
|
var persons = this.globalData.persons
|
|
if (persons.findIndex(function(p){return p.id===id}) < 0) return false
|
|
this.globalData.persons = persons.filter(function (p) { return p.id !== id })
|
|
delete this.globalData.dataMap[id]
|
|
if (this.globalData.currentPersonId === id) {
|
|
this.globalData.currentPersonId = this.globalData.persons.length > 0 ? this.globalData.persons[0].id : null
|
|
}
|
|
this.saveData()
|
|
return true
|
|
},
|
|
|
|
renamePerson(id, newName) {
|
|
var persons = this.globalData.persons
|
|
for (var i = 0; i < persons.length; i++) {
|
|
if (persons[i].id === id) {
|
|
persons[i].name = newName
|
|
this.saveData()
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
},
|
|
|
|
addRecord(record) {
|
|
var data = this.getCurrentData()
|
|
data.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) {
|
|
var data = this.getCurrentData()
|
|
data.records = data.records.filter(function (r) { return r.id !== id })
|
|
this.saveData()
|
|
},
|
|
|
|
addItem(name) {
|
|
var data = this.getCurrentData()
|
|
if (data.items.indexOf(name) === -1) {
|
|
data.items.push(name)
|
|
this.saveData()
|
|
}
|
|
},
|
|
|
|
removeItem(name) {
|
|
var data = this.getCurrentData()
|
|
data.items = data.items.filter(function (i) { return i !== name })
|
|
data.records = data.records.filter(function (r) { return r.item !== name })
|
|
this.saveData()
|
|
}
|
|
})
|