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.
215 lines
4.8 KiB
215 lines
4.8 KiB
const request = require('../utils/request')
|
|
Page({
|
|
data: {
|
|
type: 'out', // 默认为支出
|
|
amount: '',
|
|
selectedCategoryId: null,
|
|
categories: [],
|
|
filteredCategories: [],
|
|
note: '',
|
|
useDate: '',
|
|
maxDate: '',
|
|
// 控制弹窗显示
|
|
showPopup: false,
|
|
|
|
// 一级分类数据
|
|
firstLevelCategories: [
|
|
|
|
],
|
|
|
|
// 二级分类数据
|
|
secondLevelCategories: [
|
|
|
|
],
|
|
|
|
// 选中状态
|
|
selectedFirstLevelId: null,
|
|
selectedSecondLevelId: null,
|
|
selectedCategoryName: '',
|
|
filteredSecondLevelCategories: []
|
|
},
|
|
// 显示分类选择弹窗
|
|
showCategoryPopup() {
|
|
this.setData({
|
|
showPopup: true
|
|
});
|
|
},
|
|
|
|
// 隐藏分类选择弹窗
|
|
hideCategoryPopup() {
|
|
this.setData({
|
|
showPopup: false
|
|
});
|
|
},
|
|
|
|
// 选择一级分类
|
|
selectFirstLevel(e) {
|
|
const parentId = e.currentTarget.dataset.id;
|
|
|
|
// 筛选对应的二级分类
|
|
const filteredSeconds = this.data.secondLevelCategories.filter(
|
|
item => item.parentId == parentId
|
|
);
|
|
|
|
this.setData({
|
|
selectedFirstLevelId: parentId,
|
|
filteredSecondLevelCategories: filteredSeconds,
|
|
// 重置二级分类选中状态
|
|
selectedSecondLevelId: null
|
|
});
|
|
},
|
|
|
|
// 选择二级分类
|
|
selectSecondLevel(e) {
|
|
const secondLevelId = e.currentTarget.dataset.id;
|
|
const secondLevelName = e.currentTarget.dataset.name;
|
|
|
|
// 如果点击的是已选中的,则取消选中
|
|
if (this.data.selectedSecondLevelId === secondLevelId) {
|
|
this.setData({
|
|
selectedSecondLevelId: null
|
|
});
|
|
} else {
|
|
this.setData({
|
|
selectedSecondLevelId: secondLevelId,
|
|
selectedCategoryName: secondLevelName
|
|
});
|
|
}
|
|
},
|
|
|
|
// 确认选择
|
|
confirmSelection() {
|
|
if (this.data.selectedSecondLevelId) {
|
|
// 这里可以处理选中后的逻辑
|
|
console.log('选中的二级分类ID:', this.data.selectedSecondLevelId);
|
|
console.log('选中的二级分类名称:', this.data.selectedCategoryName);
|
|
|
|
// 关闭弹窗
|
|
this.hideCategoryPopup();
|
|
}
|
|
},
|
|
async getClassification() {
|
|
const res=await request.get('/admin-api/book/classification/oneTwoLevelList',{})
|
|
console.log('resss:',res)
|
|
if(res.code==0){
|
|
this.setData(
|
|
{
|
|
firstLevelCategories:res.data.outOne,
|
|
secondLevelCategories:res.data.outTwo,
|
|
categories:res.data.inOne,
|
|
filteredCategories: res.data.inOne
|
|
}
|
|
)
|
|
}
|
|
},
|
|
onShow(){
|
|
this.getClassification()
|
|
},
|
|
onLoad() {
|
|
// 获取当前日期
|
|
const today = new Date();
|
|
const year = today.getFullYear();
|
|
const month = String(today.getMonth() + 1).padStart(2, '0');
|
|
const day = String(today.getDate()).padStart(2, '0');
|
|
const currentDate = `${year}-${month}-${day}`;
|
|
|
|
this.setData({
|
|
useDate: currentDate,
|
|
maxDate: currentDate
|
|
});
|
|
|
|
|
|
},
|
|
|
|
setType(e) {
|
|
const type = e.currentTarget.dataset.type;
|
|
this.setData({
|
|
type,
|
|
selectedCategoryId: null
|
|
});
|
|
console.log('type:',type)
|
|
},
|
|
|
|
onAmountChange(e) {
|
|
let amount = e.detail.value;
|
|
|
|
// 处理金额输入格式
|
|
if (amount) {
|
|
// 只保留数字和一个小数点
|
|
amount = amount.replace(/[^\d.]/g, '');
|
|
// 确保只有一个小数点
|
|
const dotIndex = amount.indexOf('.');
|
|
if (dotIndex !== -1) {
|
|
amount = amount.substring(0, dotIndex + 3); // 限制两位小数
|
|
}
|
|
}
|
|
|
|
this.setData({ amount });
|
|
},
|
|
|
|
selectCategory(e) {
|
|
const categoryId = e.currentTarget.dataset.id;
|
|
this.setData({ selectedCategoryId: categoryId });
|
|
},
|
|
|
|
onNoteChange(e) {
|
|
this.setData({ note: e.detail.value });
|
|
},
|
|
|
|
onDateChange(e) {
|
|
this.setData({ useDate: e.detail.value });
|
|
},
|
|
|
|
async saveRecord() {
|
|
if (!this.data.amount ) {
|
|
wx.showToast({
|
|
title: '请填写金额和选择分类',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
// 创建新记录
|
|
const newRecord = {
|
|
type: this.data.type,
|
|
money: parseFloat(parseFloat(this.data.amount).toFixed(2)),
|
|
classId: this.data.type=='out'?this.data.selectedSecondLevelId:this.data.selectedCategoryId,
|
|
|
|
remark: this.data.note,
|
|
useDate: this.data.useDate
|
|
};
|
|
// 自动携带token
|
|
const res = await request.post('/admin-api/book/inout/create',newRecord)
|
|
console.log("新增结果:",res)
|
|
if(res.code==0){
|
|
// 显示成功提示
|
|
wx.showToast({
|
|
title: '记录保存成功',
|
|
icon: 'success',
|
|
duration: 1500
|
|
});
|
|
this.setData({
|
|
amount: '',
|
|
selectedCategoryId: '',
|
|
note: '',
|
|
selectedSecondLevelId: null,
|
|
});
|
|
console.log('this.data:',this.data)
|
|
}else{
|
|
wx.showToast({
|
|
title: '记录保存失败',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
}
|
|
// 返回首页
|
|
setTimeout(() => {
|
|
wx.navigateBack({
|
|
delta: 1
|
|
});
|
|
}, 1500);
|
|
}
|
|
})
|
|
|