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

291 lines
7.5 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-26 15:21:31 +08:00
<van-popup
v-model:show="listVisible"
position="bottom"
:style="{ height: '85%' }"
round
closeable
>
2025-12-25 11:20:56 +08:00
<div class="date-transactions">
<div class="popup-header">
<h3>{{ selectedDateText }}</h3>
2025-12-29 15:20:32 +08:00
<p v-if="dateTransactions.length">
{{ dateTransactions.length }} 笔交易
<span v-html="getBalance(dateTransactions)" />
</p>
2025-12-25 11:20:56 +08:00
</div>
2025-12-25 16:24:21 +08:00
2025-12-27 11:50:12 +08:00
<div class="bills-scroll-container">
2025-12-26 15:21:31 +08:00
<TransactionList
:transactions="dateTransactions"
:loading="listLoading"
:finished="true"
:show-delete="false"
@click="viewDetail"
/>
2025-12-25 11:20:56 +08:00
</div>
2025-12-27 11:50:12 +08:00
</div>
2025-12-25 11:20:56 +08:00
</van-popup>
<!-- 交易详情组件 -->
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>
2025-12-26 15:21:31 +08:00
import { ref, onMounted, nextTick } from "vue";
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";
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
// 获取指定日期的交易列表
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-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) {
return `结余<span style="color: var(--van-tag-success-color);">收入 ${balance.toFixed(1)} 元</span>`;
} else {
return `结余<span style="color: var(--van-tag-danger-color);">支出 ${(-balance).toFixed(1)} 元</span>`;
}
};
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
// 详情保存后的回调
const onDetailSave = () => {
// 重新加载当前日期的交易列表
if (selectedDate.value) {
2025-12-26 15:21:31 +08:00
fetchDateTransactions(selectedDate.value);
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
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);
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
}
.date-transactions {
height: 100%;
display: flex;
flex-direction: column;
}
.popup-header {
padding: 16px;
border-bottom: 1px solid #ebedf0;
}
.popup-header h3 {
margin: 0 0 8px 0;
font-size: 18px;
font-weight: 600;
}
.popup-header p {
margin: 0;
font-size: 14px;
color: #969799;
}
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>