You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
3.6 KiB
157 lines
3.6 KiB
Page({
|
|
data: {
|
|
activeType: 'expense',
|
|
categories: [],
|
|
filteredCategories: [],
|
|
showModal: false,
|
|
isEditing: false,
|
|
editingId: null,
|
|
categoryName: '',
|
|
selectedIcon: '',
|
|
icons: [
|
|
'cutlery', 'car', 'shopping-cart', 'home', 'gift', 'money',
|
|
'book', 'film', 'plane', 'medkit', 'coffee', 'shirt',
|
|
'graduation-cap', 'briefcase', 'gamepad', 'heart', 'pet'
|
|
]
|
|
},
|
|
|
|
onLoad() {
|
|
this.loadCategories();
|
|
},
|
|
|
|
loadCategories() {
|
|
const categories = wx.getStorageSync('categories') || [];
|
|
this.setData({
|
|
categories,
|
|
filteredCategories: categories.filter(cat => cat.type === this.data.activeType)
|
|
});
|
|
},
|
|
|
|
setActiveType(e) {
|
|
const type = e.currentTarget.dataset.type;
|
|
this.setData({
|
|
activeType: type,
|
|
filteredCategories: this.data.categories.filter(cat => cat.type === type)
|
|
});
|
|
},
|
|
|
|
addCategory() {
|
|
this.setData({
|
|
showModal: true,
|
|
isEditing: false,
|
|
editingId: null,
|
|
categoryName: '',
|
|
selectedIcon: this.data.icons[0]
|
|
});
|
|
},
|
|
|
|
editCategory(e) {
|
|
const categoryId = e.currentTarget.dataset.id;
|
|
const category = this.data.categories.find(cat => cat.id === categoryId);
|
|
|
|
if (category) {
|
|
this.setData({
|
|
showModal: true,
|
|
isEditing: true,
|
|
editingId: categoryId,
|
|
categoryName: category.name,
|
|
selectedIcon: category.icon
|
|
});
|
|
}
|
|
},
|
|
|
|
deleteCategory(e) {
|
|
const categoryId = e.currentTarget.dataset.id;
|
|
|
|
wx.showModal({
|
|
title: '确认删除',
|
|
content: '删除分类后,该分类下的记录将不受影响,是否继续?',
|
|
cancelText: '取消',
|
|
confirmText: '删除',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
let categories = this.data.categories;
|
|
categories = categories.filter(cat => cat.id !== categoryId);
|
|
|
|
wx.setStorageSync('categories', categories);
|
|
this.loadCategories();
|
|
|
|
wx.showToast({
|
|
title: '分类已删除',
|
|
icon: 'success',
|
|
duration: 1500
|
|
});
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
hideModal() {
|
|
this.setData({ showModal: false });
|
|
},
|
|
|
|
onNameChange(e) {
|
|
this.setData({ categoryName: e.detail.value });
|
|
},
|
|
|
|
selectIcon(e) {
|
|
const icon = e.currentTarget.dataset.icon;
|
|
this.setData({ selectedIcon: icon });
|
|
},
|
|
|
|
saveCategory() {
|
|
const { categoryName, selectedIcon, isEditing, editingId, activeType, categories } = this.data;
|
|
|
|
if (!categoryName.trim()) {
|
|
wx.showToast({
|
|
title: '请输入分类名称',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (!selectedIcon) {
|
|
wx.showToast({
|
|
title: '请选择图标',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
return;
|
|
}
|
|
|
|
let updatedCategories = [...categories];
|
|
|
|
if (isEditing) {
|
|
// 编辑现有分类
|
|
const index = updatedCategories.findIndex(cat => cat.id === editingId);
|
|
if (index !== -1) {
|
|
updatedCategories[index] = {
|
|
...updatedCategories[index],
|
|
name: categoryName,
|
|
icon: selectedIcon
|
|
};
|
|
}
|
|
} else {
|
|
// 添加新分类
|
|
const newId = Date.now();
|
|
updatedCategories.push({
|
|
id: newId,
|
|
name: categoryName,
|
|
type: activeType,
|
|
icon: selectedIcon
|
|
});
|
|
}
|
|
|
|
// 保存分类
|
|
wx.setStorageSync('categories', updatedCategories);
|
|
this.loadCategories();
|
|
this.hideModal();
|
|
|
|
wx.showToast({
|
|
title: isEditing ? '分类已更新' : '分类已添加',
|
|
icon: 'success',
|
|
duration: 1500
|
|
});
|
|
}
|
|
})
|
|
|