202 lines
5.9 KiB
JavaScript
202 lines
5.9 KiB
JavaScript
var app = getApp()
|
||
|
||
Page({
|
||
data: {
|
||
persons: [],
|
||
personNames: [],
|
||
currentPersonName: "",
|
||
currentPersonIndex: 0,
|
||
showAddInput: false,
|
||
newPersonName: "",
|
||
showForceModal: false,
|
||
forceName: "",
|
||
showPersonModal: false,
|
||
editingPersonId: "",
|
||
editingPersonName: ""
|
||
},
|
||
|
||
onShow: function () {
|
||
this.refreshView()
|
||
},
|
||
|
||
refreshView: function () {
|
||
var persons = app.globalData.persons
|
||
var names = persons.map(function (p) { return p.name })
|
||
var person = app.getCurrentPerson()
|
||
var idx = persons.findIndex(function (p) { return p && person && p.id === person.id })
|
||
this.setData({
|
||
persons: persons,
|
||
personNames: names,
|
||
currentPersonName: person ? person.name : "",
|
||
currentPersonIndex: idx >= 0 ? idx : 0
|
||
})
|
||
if (persons.length === 0) {
|
||
this.setData({ showForceModal: true })
|
||
} else {
|
||
this.setData({ showForceModal: false })
|
||
}
|
||
},
|
||
|
||
noop: function () {},
|
||
|
||
goEntry: function () {
|
||
wx.navigateTo({ url: '/pages/entry/entry' })
|
||
},
|
||
goItems: function () {
|
||
wx.navigateTo({ url: '/pages/items/items' })
|
||
},
|
||
goTrend: function () {
|
||
wx.navigateTo({ url: '/pages/trend/trend' })
|
||
},
|
||
goDetails: function () {
|
||
wx.navigateTo({ url: '/pages/details/details' })
|
||
},
|
||
|
||
onClearAll: function () {
|
||
var that = this
|
||
wx.showModal({
|
||
title: "⚠️ 危险操作",
|
||
content: "确定要清除「" + this.data.currentPersonName + "」的所有检查记录吗?\n此操作将删除所有历史数据,但保留检查项目,且不可恢复!",
|
||
confirmText: "确认清除",
|
||
confirmColor: "#e53e3e",
|
||
success: function (res) {
|
||
if (!res.confirm) return
|
||
var data = app.getCurrentData()
|
||
data.records = []
|
||
app.saveData()
|
||
wx.showToast({ title: "已清除全部记录", icon: "success", duration: 1500 })
|
||
}
|
||
})
|
||
},
|
||
|
||
// ===== 切换用户 =====
|
||
onSwitchPerson: function (e) {
|
||
var idx = parseInt(e.detail.value)
|
||
var persons = this.data.persons
|
||
if (idx === this.data.currentPersonIndex) return
|
||
var person = persons[idx]
|
||
if (!person) return
|
||
app.switchPerson(person.id)
|
||
this.refreshView()
|
||
wx.showToast({ title: "已切换至" + person.name, icon: "success", duration: 1000 })
|
||
},
|
||
|
||
// ===== 新增用户(首页行内) =====
|
||
onShowAddInput: function () {
|
||
this.setData({ showAddInput: true, newPersonName: "" })
|
||
},
|
||
|
||
onCancelAddPerson: function () {
|
||
this.setData({ showAddInput: false, newPersonName: "" })
|
||
},
|
||
|
||
onAddNameInput: function (e) {
|
||
this.setData({ newPersonName: e.detail.value })
|
||
},
|
||
|
||
onConfirmAddPerson: function () {
|
||
var name = this.data.newPersonName.trim()
|
||
if (!name) {
|
||
wx.showToast({ title: "请输入用户名", icon: "none" })
|
||
return
|
||
}
|
||
if (name.length > 10) {
|
||
wx.showToast({ title: "用户名最多10个字符", icon: "none" })
|
||
return
|
||
}
|
||
app.addPerson(name)
|
||
app.switchPerson(app.globalData.persons[app.globalData.persons.length - 1].id)
|
||
this.setData({ showAddInput: false, newPersonName: "" })
|
||
this.refreshView()
|
||
wx.showToast({ title: "已添加" + name, icon: "success", duration: 1000 })
|
||
},
|
||
|
||
// ===== 强制创建首个用户(无用户弹窗) =====
|
||
onForceNameInput: function (e) {
|
||
this.setData({ forceName: e.detail.value })
|
||
},
|
||
|
||
onConfirmForceCreate: function () {
|
||
var name = this.data.forceName.trim()
|
||
if (!name) {
|
||
wx.showToast({ title: "请先创建用户", icon: "none" })
|
||
return
|
||
}
|
||
if (name.length > 10) {
|
||
wx.showToast({ title: "用户名最多10个字符", icon: "none" })
|
||
return
|
||
}
|
||
var id = app.addPerson(name)
|
||
app.switchPerson(id)
|
||
this.setData({ showForceModal: false, forceName: "" })
|
||
this.refreshView()
|
||
wx.showToast({ title: "欢迎," + name, icon: "success", duration: 1200 })
|
||
},
|
||
|
||
onCancelForceCreate: function () {
|
||
// 取消了,但仍无用户,弹窗不退
|
||
wx.showToast({ title: "请先创建用户", icon: "none" })
|
||
},
|
||
|
||
// ===== 用户管理弹窗(重命名/删除) =====
|
||
onTogglePersonModal: function () {
|
||
this.setData({
|
||
showPersonModal: !this.data.showPersonModal,
|
||
editingPersonId: "",
|
||
editingPersonName: ""
|
||
})
|
||
},
|
||
|
||
onStartRename: function (e) {
|
||
var id = e.currentTarget.dataset.id
|
||
var person = this.data.persons.find(function (p) { return p.id === id })
|
||
this.setData({
|
||
editingPersonId: id,
|
||
editingPersonName: person ? person.name : ""
|
||
})
|
||
},
|
||
|
||
onEditNameInput: function (e) {
|
||
this.setData({ editingPersonName: e.detail.value })
|
||
},
|
||
|
||
onConfirmRename: function () {
|
||
var name = this.data.editingPersonName.trim()
|
||
if (!name) {
|
||
wx.showToast({ title: "请输入新名称", icon: "none" })
|
||
return
|
||
}
|
||
if (name.length > 10) {
|
||
wx.showToast({ title: "用户名最多10个字符", icon: "none" })
|
||
return
|
||
}
|
||
app.renamePerson(this.data.editingPersonId, name)
|
||
this.setData({ editingPersonId: "", editingPersonName: "" })
|
||
this.refreshView()
|
||
wx.showToast({ title: "已重命名", icon: "success", duration: 1000 })
|
||
},
|
||
|
||
onDeletePerson: function (e) {
|
||
var id = e.currentTarget.dataset.id
|
||
var person = this.data.persons.find(function (p) { return p.id === id })
|
||
if (!person) return
|
||
var that = this
|
||
wx.showModal({
|
||
title: "确认删除",
|
||
content: "确定删除用户「" + person.name + "」吗?\n该用户的所有数据将被永久删除。",
|
||
confirmColor: "#e53e3e",
|
||
success: function (res) {
|
||
if (!res.confirm) return
|
||
app.removePerson(id)
|
||
that.setData({ editingPersonId: "", editingPersonName: "" })
|
||
// 删完后如无用户,关闭管理弹窗并强制展示创建弹窗
|
||
if (app.globalData.persons.length === 0) {
|
||
that.setData({ showPersonModal: false })
|
||
}
|
||
that.refreshView()
|
||
wx.showToast({ title: "已删除", icon: "success", duration: 1000 })
|
||
}
|
||
})
|
||
}
|
||
})
|