|
|
|
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('/app-api/book/inout/allList')
|
|
|
|
console.log('获取数据成功:', res)
|
|
|
|
if(res.code==0){
|
|
|
|
records=res.data
|
|
|
|
}
|
|
|
|
const res1 = await request.get('/app-api/book/inout/myList-tol',{type:"in"})
|
|
|
|
console.log('获取数据成功:', res1)
|
|
|
|
if(res1.code==0){
|
|
|
|
totalIncome=res1.data
|
|
|
|
}
|
|
|
|
const res2 = await request.get('/app-api/book/inout/myList-tol',{type:"out"})
|
|
|
|
console.log('获取数据成功:', res2)
|
|
|
|
if(res2.code==0){
|
|
|
|
totalExpense=res2.data
|
|
|
|
}
|
|
|
|
// 处理数据...
|
|
|
|
} 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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|