Files
EmailBill/Web/src/views/CalendarView.vue

328 lines
9.6 KiB
Vue
Raw Normal View History

2025-12-25 11:20:56 +08:00
<template>
2025-12-26 17:29:17 +08:00
<div class="page-container calendar-container">
2025-12-26 15:21:31 +08:00
<van-calendar
title="日历"
:poppable="false"
:show-confirm="false"
:formatter="formatterCalendar"
2025-12-25 17:41:36 +08:00
:min-date="minDate"
2025-12-26 15:21:31 +08:00
:max-date="maxDate"
@month-show="onMonthShow"
@select="onDateSelect"
/>
2025-12-25 11:20:56 +08:00
<!-- 日期交易列表弹出层 -->
2025-12-30 17:02:30 +08:00
<PopupContainer
v-model="listVisible"
:title="selectedDateText"
:subtitle="getBalance(dateTransactions)"
height="85%"
2025-12-26 15:21:31 +08:00
>
2025-12-30 17:02:30 +08:00
<template #header-actions>
<SmartClassifyButton
ref="smartClassifyButtonRef"
2025-12-26 15:21:31 +08:00
:transactions="dateTransactions"
2025-12-30 17:02:30 +08:00
@save="onSmartClassifySave"
2025-12-26 15:21:31 +08:00
/>
2025-12-30 17:02:30 +08:00
</template>
<TransactionList
:transactions="dateTransactions"
:loading="listLoading"
:finished="true"
2025-12-30 18:49:46 +08:00
:show-delete="true"
2025-12-30 17:02:30 +08:00
@click="viewDetail"
2026-01-01 11:58:21 +08:00
@delete="handleDateTransactionDelete"
2025-12-30 17:02:30 +08:00
/>
</PopupContainer>
2025-12-25 11:20:56 +08:00
<!-- 交易详情组件 -->
2025-12-26 15:21:31 +08:00
<TransactionDetail
v-model:show="detailVisible"
:transaction="currentTransaction"
@save="onDetailSave"
/>
2025-12-25 11:20:56 +08:00
</div>
</template>
<script setup>
2026-01-01 11:58:21 +08:00
import { ref, onMounted, nextTick, onBeforeUnmount } from "vue";
2025-12-26 15:21:31 +08:00
import { showToast } from "vant";
import request from "@/api/request";
import { getTransactionDetail, getTransactionsByDate } from "@/api/transactionRecord";
import TransactionList from "@/components/TransactionList.vue";
import TransactionDetail from "@/components/TransactionDetail.vue";
2025-12-29 20:30:15 +08:00
import SmartClassifyButton from "@/components/SmartClassifyButton.vue";
2025-12-30 17:02:30 +08:00
import PopupContainer from "@/components/PopupContainer.vue";
2025-12-26 15:21:31 +08:00
const dailyStatistics = ref({});
const listVisible = ref(false);
const detailVisible = ref(false);
const dateTransactions = ref([]);
const currentTransaction = ref(null);
const listLoading = ref(false);
const selectedDate = ref(null);
const selectedDateText = ref("");
2025-12-25 11:20:56 +08:00
// 设置日历可选范围例如过去2年到未来1年
2025-12-26 15:21:31 +08:00
const minDate = new Date(new Date().getFullYear() - 2, 0, 1); // 2年前的1月1日
const maxDate = new Date(new Date().getFullYear() + 1, 11, 31); // 明年12月31日
2025-12-25 11:20:56 +08:00
2025-12-25 16:24:21 +08:00
onMounted(async () => {
2025-12-26 15:21:31 +08:00
await nextTick();
2025-12-25 16:24:21 +08:00
setTimeout(() => {
// 计算页面高度滚动3/4高度以显示更多日期
2025-12-26 15:21:31 +08:00
const height = document.querySelector(".calendar-container").clientHeight * 0.45;
document.querySelector(".van-calendar__body").scrollBy({
top: -height,
behavior: "smooth",
});
2025-12-25 16:24:21 +08:00
}, 300);
2025-12-26 15:21:31 +08:00
});
2025-12-25 16:24:21 +08:00
2025-12-25 11:20:56 +08:00
// 获取日历统计数据
const fetchDailyStatistics = async (year, month) => {
try {
2025-12-26 15:21:31 +08:00
const response = await request.get("/TransactionRecord/GetDailyStatistics", {
params: { year, month },
});
2025-12-25 11:20:56 +08:00
if (response.success && response.data) {
// 将数组转换为对象key为日期
2025-12-26 15:21:31 +08:00
const statsMap = {};
response.data.forEach((item) => {
2025-12-25 11:20:56 +08:00
statsMap[item.date] = {
count: item.count,
2025-12-26 15:21:31 +08:00
amount: item.amount,
};
});
2025-12-25 11:20:56 +08:00
dailyStatistics.value = {
...dailyStatistics.value,
2025-12-26 15:21:31 +08:00
...statsMap,
};
2025-12-25 11:20:56 +08:00
}
} catch (error) {
2025-12-26 15:21:31 +08:00
console.error("获取日历统计数据失败:", error);
2025-12-25 11:20:56 +08:00
}
2025-12-26 15:21:31 +08:00
};
2025-12-25 11:20:56 +08:00
2025-12-29 20:30:15 +08:00
const smartClassifyButtonRef = ref(null);
2025-12-25 11:20:56 +08:00
// 获取指定日期的交易列表
const fetchDateTransactions = async (date) => {
try {
2025-12-26 15:21:31 +08:00
listLoading.value = true;
const dateStr = date
.toLocaleString("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit" })
.replace(/\//g, "-");
2025-12-25 16:24:21 +08:00
2025-12-26 15:21:31 +08:00
const response = await getTransactionsByDate(dateStr);
2025-12-25 16:24:21 +08:00
2025-12-25 11:20:56 +08:00
if (response.success && response.data) {
2025-12-26 15:21:31 +08:00
// 根据金额从大到小排序
dateTransactions.value = response
.data
.sort((a, b) => b.amount - a.amount);
2025-12-29 20:30:15 +08:00
// 重置智能分类按钮
2025-12-30 18:49:46 +08:00
smartClassifyButtonRef.value?.reset()
2025-12-25 11:20:56 +08:00
} else {
2025-12-26 15:21:31 +08:00
dateTransactions.value = [];
showToast(response.message || "获取交易列表失败");
2025-12-25 11:20:56 +08:00
}
} catch (error) {
2025-12-26 15:21:31 +08:00
console.error("获取日期交易列表失败:", error);
dateTransactions.value = [];
showToast("获取交易列表失败");
2025-12-25 11:20:56 +08:00
} finally {
2025-12-26 15:21:31 +08:00
listLoading.value = false;
2025-12-25 11:20:56 +08:00
}
2025-12-26 15:21:31 +08:00
};
2025-12-25 11:20:56 +08:00
2025-12-29 15:20:32 +08:00
const getBalance = (transactions) => {
let balance = 0;
transactions.forEach(tx => {
if(tx.type === 1) {
balance += tx.amount;
} else if(tx.type === 0) {
balance -= tx.amount;
}
});
if(balance >= 0) {
2025-12-30 17:02:30 +08:00
return `结余收入 ${balance.toFixed(1)}`;
2025-12-29 15:20:32 +08:00
} else {
2025-12-30 17:02:30 +08:00
return `结余支出 ${(-balance).toFixed(1)}`;
2025-12-29 15:20:32 +08:00
}
};
2025-12-25 11:20:56 +08:00
// 当月份显示时触发
const onMonthShow = ({ date }) => {
2025-12-26 15:21:31 +08:00
const year = date.getFullYear();
const month = date.getMonth() + 1;
fetchDailyStatistics(year, month);
};
2025-12-25 11:20:56 +08:00
// 日期选择事件
const onDateSelect = (date) => {
2025-12-26 15:21:31 +08:00
selectedDate.value = date;
selectedDateText.value = formatSelectedDate(date);
fetchDateTransactions(date);
listVisible.value = true;
};
2025-12-25 11:20:56 +08:00
// 格式化选中的日期
const formatSelectedDate = (date) => {
2025-12-26 15:21:31 +08:00
return date.toLocaleDateString("zh-CN", {
year: "numeric",
month: "long",
day: "numeric",
weekday: "long",
});
};
2025-12-25 11:20:56 +08:00
// 查看详情
const viewDetail = async (transaction) => {
try {
2025-12-26 15:21:31 +08:00
const response = await getTransactionDetail(transaction.id);
2025-12-25 11:20:56 +08:00
if (response.success) {
2025-12-26 15:21:31 +08:00
currentTransaction.value = response.data;
detailVisible.value = true;
2025-12-25 11:20:56 +08:00
} else {
2025-12-26 15:21:31 +08:00
showToast(response.message || "获取详情失败");
2025-12-25 11:20:56 +08:00
}
} catch (error) {
2025-12-26 15:21:31 +08:00
console.error("获取详情出错:", error);
showToast("获取详情失败");
2025-12-25 11:20:56 +08:00
}
2025-12-26 15:21:31 +08:00
};
2025-12-25 11:20:56 +08:00
// 详情保存后的回调
2025-12-29 20:51:20 +08:00
const onDetailSave = async (saveData) => {
2025-12-25 11:20:56 +08:00
// 重新加载当前日期的交易列表
2025-12-29 20:51:20 +08:00
if (saveData && dateTransactions.value) {
var updatedIndex = dateTransactions.value.findIndex(tx => tx.id === saveData.id);
if (updatedIndex !== -1) {
// 更新已有记录
dateTransactions.value[updatedIndex].amount = saveData.amount;
dateTransactions.value[updatedIndex].balance = saveData.balance;
dateTransactions.value[updatedIndex].type = saveData.type;
dateTransactions.value[updatedIndex].upsetedType = '';
dateTransactions.value[updatedIndex].classify = saveData.classify;
dateTransactions.value[updatedIndex].upsetedClassify = '';
dateTransactions.value[updatedIndex].reason = saveData.reason;
}
2025-12-25 11:20:56 +08:00
}
2025-12-25 16:24:21 +08:00
2025-12-25 11:20:56 +08:00
// 重新加载当前月份的统计数据
2025-12-26 15:21:31 +08:00
const now = selectedDate.value || new Date();
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1);
};
2025-12-25 11:20:56 +08:00
2026-01-01 11:58:21 +08:00
// 处理删除事件:从当前日期交易列表中移除,并刷新当日和当月统计
const handleDateTransactionDelete = async (transactionId) => {
dateTransactions.value = dateTransactions.value.filter(t => t.id !== transactionId)
// 刷新当前日期以及当月的统计数据
const now = selectedDate.value || new Date();
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1);
};
2025-12-29 20:30:15 +08:00
// 智能分类保存回调
const onSmartClassifySave = async () => {
// 保存完成后重新加载数据
if (selectedDate.value) {
await fetchDateTransactions(selectedDate.value);
}
// 重新加载统计数据
const now = selectedDate.value || new Date();
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1);
};
2025-12-25 11:20:56 +08:00
const formatterCalendar = (day) => {
const dayCopy = { ...day };
if (dayCopy.date.toDateString() === new Date().toDateString()) {
2025-12-26 15:21:31 +08:00
dayCopy.text = "今天";
2025-12-25 11:20:56 +08:00
}
// 格式化日期为 yyyy-MM-dd
2025-12-26 15:21:31 +08:00
const dateKey = dayCopy.date
.toLocaleString("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit" })
.replace(/\//g, "-");
const stats = dailyStatistics.value[dateKey];
2025-12-25 11:20:56 +08:00
if (stats) {
2025-12-26 15:21:31 +08:00
dayCopy.topInfo = `${stats.count}`; // 展示消费笔数
dayCopy.bottomInfo = `${stats.amount.toFixed(1)}`; // 展示消费金额
2025-12-25 11:20:56 +08:00
}
return dayCopy;
};
// 初始加载当前月份数据
2025-12-26 15:21:31 +08:00
const now = new Date();
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1);
2026-01-01 11:58:21 +08:00
// 全局删除事件监听,确保日历页面数据一致
const onGlobalTransactionDeleted = (e) => {
if (selectedDate.value) {
fetchDateTransactions(selectedDate.value)
}
const now = selectedDate.value || new Date()
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1)
}
window.addEventListener && window.addEventListener('transaction-deleted', onGlobalTransactionDeleted)
onBeforeUnmount(() => {
window.removeEventListener && window.removeEventListener('transaction-deleted', onGlobalTransactionDeleted)
})
// 当有交易被新增/修改/批量更新时刷新
const onGlobalTransactionsChanged = (e) => {
if (selectedDate.value) {
fetchDateTransactions(selectedDate.value)
}
const now = selectedDate.value || new Date()
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1)
}
window.addEventListener && window.addEventListener('transactions-changed', onGlobalTransactionsChanged)
onBeforeUnmount(() => {
window.removeEventListener && window.removeEventListener('transactions-changed', onGlobalTransactionsChanged)
})
2025-12-25 11:20:56 +08:00
</script>
<style scoped>
.calendar-container {
2025-12-25 17:20:50 +08:00
/* 使用准确的视口高度减去 TabBar 高度50px和安全区域 */
height: calc(var(--vh, 100vh) - 50px - env(safe-area-inset-bottom, 0px));
2025-12-25 17:28:06 +08:00
max-height: calc(var(--vh, 100vh) - 50px - env(safe-area-inset-bottom, 0px));
2025-12-25 11:20:56 +08:00
display: flex;
flex-direction: column;
2025-12-25 17:20:50 +08:00
overflow: hidden;
2025-12-25 17:28:06 +08:00
margin: 0;
padding: 0;
2025-12-25 11:20:56 +08:00
}
.calendar-container :deep(.van-calendar) {
2025-12-25 17:28:06 +08:00
height: 100% !important;
flex: 1;
2025-12-25 17:20:50 +08:00
overflow: auto;
2025-12-25 17:28:06 +08:00
margin: 0;
padding: 0;
}
/* 移除日历组件可能的底部 padding */
.calendar-container :deep(.van-calendar__body) {
padding-bottom: 0 !important;
}
.calendar-container :deep(.van-calendar__months) {
padding-bottom: 0 !important;
2025-12-25 11:20:56 +08:00
}
2025-12-26 18:03:52 +08:00
/* 设置页面容器背景色 */
2025-12-28 10:23:57 +08:00
:deep(.van-calendar__header-title) {
2025-12-26 18:03:52 +08:00
background: transparent !important;
}
2025-12-28 10:23:57 +08:00
2025-12-26 15:21:31 +08:00
</style>