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

355 lines
9.8 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"
/>
2026-01-16 11:15:44 +08:00
<ContributionHeatmap ref="heatmapRef" />
2025-12-25 11:20:56 +08:00
2026-01-15 18:39:04 +08:00
<!-- 底部安全距离 -->
2026-01-16 11:15:44 +08:00
<div style="height: calc(60px + env(safe-area-inset-bottom, 0px))" />
2026-01-15 17:55:37 +08:00
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="75%"
2025-12-26 15:21:31 +08:00
>
2025-12-30 17:02:30 +08:00
<template #header-actions>
2026-01-16 11:15:44 +08:00
<SmartClassifyButton
2025-12-30 17:02:30 +08:00
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-16 11:15:44 +08:00
import { ref, onMounted, nextTick, onBeforeUnmount } 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'
import SmartClassifyButton from '@/components/SmartClassifyButton.vue'
import PopupContainer from '@/components/PopupContainer.vue'
import ContributionHeatmap from '@/components/ContributionHeatmap.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('')
const heatmapRef = ref(null)
2025-12-25 11:20:56 +08:00
// 设置日历可选范围例如过去2年到未来1年
2026-01-16 11:15:44 +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 () => {
2026-01-16 11:15:44 +08:00
await nextTick()
2025-12-25 16:24:21 +08:00
setTimeout(() => {
// 计算页面高度滚动3/4高度以显示更多日期
2026-01-16 11:15:44 +08:00
const height = document.querySelector('.calendar-container').clientHeight * 0.43
document.querySelector('.van-calendar__body').scrollBy({
2025-12-26 15:21:31 +08:00
top: -height,
2026-01-16 11:15:44 +08:00
behavior: 'smooth'
})
}, 300)
})
2025-12-25 16:24:21 +08:00
2025-12-25 11:20:56 +08:00
// 获取日历统计数据
const fetchDailyStatistics = async (year, month) => {
try {
2026-01-16 11:15:44 +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为日期
2026-01-16 11:15:44 +08:00
const statsMap = {}
2025-12-26 15:21:31 +08:00
response.data.forEach((item) => {
2026-01-17 12:33:16 +08:00
console.warn(item)
2025-12-25 11:20:56 +08:00
statsMap[item.date] = {
count: item.count,
2026-01-17 12:33:16 +08:00
amount: (item.income - item.expense).toFixed(1)
2026-01-16 11:15:44 +08:00
}
})
2025-12-25 11:20:56 +08:00
dailyStatistics.value = {
...dailyStatistics.value,
2026-01-16 11:15:44 +08:00
...statsMap
}
2025-12-25 11:20:56 +08:00
}
} catch (error) {
2026-01-16 11:15:44 +08:00
console.error('获取日历统计数据失败:', error)
2025-12-25 11:20:56 +08:00
}
2026-01-16 11:15:44 +08:00
}
2025-12-25 11:20:56 +08:00
2026-01-16 11:15:44 +08:00
const smartClassifyButtonRef = ref(null)
2025-12-25 11:20:56 +08:00
// 获取指定日期的交易列表
const fetchDateTransactions = async (date) => {
try {
2026-01-16 11:15:44 +08:00
listLoading.value = true
2025-12-26 15:21:31 +08:00
const dateStr = date
2026-01-16 11:15:44 +08:00
.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
})
.replace(/\//g, '-')
2025-12-25 16:24:21 +08:00
2026-01-16 11:15:44 +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
// 根据金额从大到小排序
2026-01-16 11:15:44 +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 {
2026-01-16 11:15:44 +08:00
dateTransactions.value = []
showToast(response.message || '获取交易列表失败')
2025-12-25 11:20:56 +08:00
}
} catch (error) {
2026-01-16 11:15:44 +08:00
console.error('获取日期交易列表失败:', error)
dateTransactions.value = []
showToast('获取交易列表失败')
2025-12-25 11:20:56 +08:00
} finally {
2026-01-16 11:15:44 +08:00
listLoading.value = false
2025-12-25 11:20:56 +08:00
}
2026-01-16 11:15:44 +08:00
}
2025-12-25 11:20:56 +08:00
2025-12-29 15:20:32 +08:00
const getBalance = (transactions) => {
2026-01-16 11:15:44 +08:00
let balance = 0
transactions.forEach((tx) => {
if (tx.type === 1) {
balance += tx.amount
} else if (tx.type === 0) {
balance -= tx.amount
2025-12-29 15:20:32 +08:00
}
2026-01-16 11:15:44 +08:00
})
2025-12-29 15:20:32 +08:00
2026-01-16 11:15:44 +08:00
if (balance >= 0) {
return `结余收入 ${balance.toFixed(1)}`
2025-12-29 15:20:32 +08:00
} else {
2026-01-16 11:15:44 +08:00
return `结余支出 ${(-balance).toFixed(1)}`
2025-12-29 15:20:32 +08:00
}
2026-01-16 11:15:44 +08:00
}
2025-12-29 15:20:32 +08:00
2025-12-25 11:20:56 +08:00
// 当月份显示时触发
const onMonthShow = ({ date }) => {
2026-01-16 11:15:44 +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) => {
2026-01-16 11:15:44 +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) => {
2026-01-16 11:15:44 +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 {
2026-01-16 11:15:44 +08:00
const response = await getTransactionDetail(transaction.id)
2025-12-25 11:20:56 +08:00
if (response.success) {
2026-01-16 11:15:44 +08:00
currentTransaction.value = response.data
detailVisible.value = true
2025-12-25 11:20:56 +08:00
} else {
2026-01-16 11:15:44 +08:00
showToast(response.message || '获取详情失败')
2025-12-25 11:20:56 +08:00
}
} catch (error) {
2026-01-16 11:15:44 +08:00
console.error('获取详情出错:', error)
showToast('获取详情失败')
2025-12-25 11:20:56 +08:00
}
2026-01-16 11:15:44 +08:00
}
2025-12-25 11:20:56 +08:00
// 详情保存后的回调
2025-12-29 20:51:20 +08:00
const onDetailSave = async (saveData) => {
2026-01-16 11:15:44 +08:00
const item = dateTransactions.value.find((tx) => tx.id === saveData.id)
if (!item) {
return
}
// 如果分类发生了变化 移除智能分类的内容,防止被智能分类覆盖
2026-01-16 11:15:44 +08:00
if (item.classify !== saveData.classify) {
// 通知智能分类按钮组件移除指定项
smartClassifyButtonRef.value?.removeClassifiedTransaction(saveData.id)
item.upsetedClassify = ''
2025-12-25 11:20:56 +08:00
}
2025-12-25 16:24:21 +08:00
// 更新当前日期交易列表中的数据
2026-01-16 11:15:44 +08:00
Object.assign(item, saveData)
2025-12-25 11:20:56 +08:00
// 重新加载当前月份的统计数据
2026-01-16 11:15:44 +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) => {
2026-01-16 11:15:44 +08:00
dateTransactions.value = dateTransactions.value.filter((t) => t.id !== transactionId)
2026-01-01 11:58:21 +08:00
// 刷新当前日期以及当月的统计数据
2026-01-16 11:15:44 +08:00
const now = selectedDate.value || new Date()
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1)
}
2026-01-01 11:58:21 +08:00
2025-12-29 20:30:15 +08:00
// 智能分类保存回调
const onSmartClassifySave = async () => {
// 保存完成后重新加载数据
if (selectedDate.value) {
2026-01-16 11:15:44 +08:00
await fetchDateTransactions(selectedDate.value)
2025-12-29 20:30:15 +08:00
}
// 重新加载统计数据
2026-01-16 11:15:44 +08:00
const now = selectedDate.value || new Date()
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1)
}
2025-12-29 20:30:15 +08:00
2025-12-25 11:20:56 +08:00
const formatterCalendar = (day) => {
2026-01-16 11:15:44 +08:00
const dayCopy = { ...day }
2025-12-25 11:20:56 +08:00
if (dayCopy.date.toDateString() === new Date().toDateString()) {
2026-01-16 11:15:44 +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
2026-01-16 11:15:44 +08:00
.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) {
2026-01-16 11:15:44 +08:00
dayCopy.topInfo = `${stats.count}` // 展示消费笔数
2026-01-17 12:33:16 +08:00
dayCopy.bottomInfo = `${stats.amount}` // 展示消费金额
2025-12-25 11:20:56 +08:00
}
2026-01-16 11:15:44 +08:00
return dayCopy
}
2025-12-25 11:20:56 +08:00
// 初始加载当前月份数据
2026-01-16 11:15:44 +08:00
const now = new Date()
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1)
2026-01-01 11:58:21 +08:00
// 全局删除事件监听,确保日历页面数据一致
const onGlobalTransactionDeleted = () => {
2026-01-01 11:58:21 +08:00
if (selectedDate.value) {
fetchDateTransactions(selectedDate.value)
}
const now = selectedDate.value || new Date()
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1)
heatmapRef.value?.refresh()
2026-01-01 11:58:21 +08:00
}
2026-01-16 11:15:44 +08:00
window.addEventListener &&
window.addEventListener('transaction-deleted', onGlobalTransactionDeleted)
2026-01-01 11:58:21 +08:00
onBeforeUnmount(() => {
2026-01-16 11:15:44 +08:00
window.removeEventListener &&
window.removeEventListener('transaction-deleted', onGlobalTransactionDeleted)
2026-01-01 11:58:21 +08:00
})
// 当有交易被新增/修改/批量更新时刷新
const onGlobalTransactionsChanged = () => {
2026-01-01 11:58:21 +08:00
if (selectedDate.value) {
fetchDateTransactions(selectedDate.value)
}
const now = selectedDate.value || new Date()
fetchDailyStatistics(now.getFullYear(), now.getMonth() + 1)
heatmapRef.value?.refresh()
2026-01-01 11:58:21 +08:00
}
2026-01-16 11:15:44 +08:00
window.addEventListener &&
window.addEventListener('transactions-changed', onGlobalTransactionsChanged)
2026-01-01 11:58:21 +08:00
onBeforeUnmount(() => {
2026-01-16 11:15:44 +08:00
window.removeEventListener &&
window.removeEventListener('transactions-changed', onGlobalTransactionsChanged)
2026-01-01 11:58:21 +08:00
})
2025-12-25 11:20:56 +08:00
</script>
<style scoped>
2026-01-16 11:15:44 +08:00
.van-calendar {
background: transparent !important;
}
2025-12-25 11:20:56 +08:00
.calendar-container {
2025-12-25 17:20:50 +08:00
/* 使用准确的视口高度减去 TabBar 高度50px和安全区域 */
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;
background-color: var(--van-background);
2025-12-25 11:20:56 +08:00
}
.calendar-container :deep(.van-calendar) {
2026-01-15 18:54:08 +08:00
height: calc(auto + 40px) !important;
2025-12-25 17:28:06 +08:00
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
2026-01-15 18:29:53 +08:00
/* Add margin to bottom of heatmap to separate from tabbar */
:deep(.heatmap-card) {
flex-shrink: 0; /* Prevent heatmap from shrinking */
}
2025-12-26 15:21:31 +08:00
</style>