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.
70 lines
1.8 KiB
70 lines
1.8 KiB
1 week ago
|
const request = require('../utils/request')
|
||
|
Page({
|
||
|
data: {
|
||
|
currentMonth: '',
|
||
|
totalBalance: '0.00',
|
||
|
totalIncome: '0.00',
|
||
|
totalExpense: '0.00',
|
||
|
recentRecords: []
|
||
|
},
|
||
|
|
||
|
onShow() {
|
||
|
this.updateData();
|
||
|
},
|
||
|
|
||
|
async updateData() {
|
||
|
// 更新当前月份
|
||
|
const date = new Date();
|
||
|
const year = date.getFullYear();
|
||
|
const month = date.getMonth() + 1;
|
||
|
this.setData({
|
||
|
currentMonth: `${year}年${month}月`
|
||
|
});
|
||
|
|
||
|
// 获取所有记录
|
||
|
let records = [];
|
||
|
|
||
|
// 筛选本月记录
|
||
|
const currentMonthRecords = records.filter(record => {
|
||
|
const recordDate = new Date(record.date);
|
||
|
return recordDate.getFullYear() === year && recordDate.getMonth() + 1 === month;
|
||
|
});
|
||
|
|
||
|
// 计算收支和余额
|
||
|
let totalIncome = 0;
|
||
|
let totalExpense = 0;
|
||
|
|
||
|
currentMonthRecords.forEach(record => {
|
||
|
if (record.type === 'income') {
|
||
|
totalIncome += parseFloat(record.amount);
|
||
|
} else {
|
||
|
totalExpense += parseFloat(record.amount);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const totalBalance = totalIncome - totalExpense;
|
||
|
|
||
|
try {
|
||
|
// 自动携带token
|
||
|
const res = await request.get('/admin-api/book/inout/myList?pageNo=1&pageSize=10')
|
||
|
console.log('获取数据成功:', res)
|
||
|
if(res.code==0){
|
||
|
records=res.data.list
|
||
|
}
|
||
|
// 处理数据...
|
||
|
} catch (error) {
|
||
|
console.error('加载失败:', error)
|
||
|
}
|
||
|
console.log('records:',records)
|
||
|
this.setData({
|
||
|
totalBalance: totalBalance.toFixed(2),
|
||
|
totalIncome: totalIncome.toFixed(2),
|
||
|
totalExpense: totalExpense.toFixed(2),
|
||
|
// 按日期排序,取最近10条
|
||
|
recentRecords: [...records]
|
||
|
// .sort((a, b) => new Date(b.date) - new Date(a.date))
|
||
|
.slice(0, 10)
|
||
|
});
|
||
|
}
|
||
|
})
|