fix daily summary

This commit is contained in:
Felix
2025-11-09 11:43:58 +08:00
parent c2c8b9a02a
commit 0682577d53
4 changed files with 126 additions and 86 deletions

View File

@@ -26,10 +26,12 @@ Page({
// 移除模拟数据改为从API获取
groupedHistory: [] as any[],
// 添加分页相关数据
page: 1,
size: 10,
total: 0,
hasMore: true,
dailyPage: 1,
dailySize: 10,
dailyTotal: 0,
dailyHasMore: true,
todayPage: 1,
todaySize: 10,
// 添加加载状态
isLoading: true,
// 添加文件基础URL
@@ -47,6 +49,7 @@ Page({
// 只有登录成功后才加载历史数据
if (this.data.isLoggedIn) {
this.loadDailySummary()
this.loadTodaySummary() // 加载今日摘要数据
this.checkDayType()
}
}).catch((error) => {
@@ -62,12 +65,6 @@ Page({
<path d="M 486 70 C 486 71.7614 483.761 74 481 74 C 478.239 74 476 71.7614 476 70 C 476 69 477 69 477 70 C 477 71 479 73 481 73 C 483 73 485 71 485 70 C 485 69 486 69 486 70 M 490 64 C 488.7 64 488.7 66 490 66 C 491.3 66 491.3 64 490 64 M 472 64 C 470.7 64 470.7 66 472 66 C 473.3 66 473.3 64 472 64" stroke="#001858" stroke-width="1.5" stroke-linecap="round"></path>
</svg>
`.trim();
// const svgString = `
// <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="470 50 20 40">
// <path d="M 486 70 C 486 71.7614 483.761 74 481 74 C 478.239 74 476 71.7614 476 70 C 476 69 476 69 476 70 C 476 72 478 74 481 74 C 484 74 486 72 486 70 C 486 69 486 69 486 70 M 490 64 C 488.7 64 488.7 66 490 66 C 491.3 66 491.3 64 490 64 M 472 64 C 470.7 64 470.7 66 472 66 C 473.3 66 473.3 64 472 64" stroke="#001858" stroke-width="3" stroke-linecap="round"></path>
// </svg>
// `.trim();
const encodedSvg = encodeURIComponent(svgString);
const dataUrl = `data:image/svg+xml;utf8,${encodedSvg}`;
@@ -116,9 +113,9 @@ Page({
checkDayType() {
const currentHour = new Date().getHours();
const currentDate = new Date().toLocaleDateString('zh-CN');
// const dayType = currentHour >= 6 && currentHour < 12 ? 'morning' :
// currentHour >= 12 && currentHour < 18 ? 'afternoon' : 'night';
const dayType = 'morning';
const dayType = currentHour >= 6 && currentHour < 12 ? 'morning' :
currentHour >= 12 && currentHour < 18 ? 'afternoon' : 'night';
// const dayType = 'morning';
this.setData({
current_date: currentDate,
day_type: dayType as IDayType
@@ -135,7 +132,7 @@ Page({
});
},
// 加载每日摘要数据
// 加载每日摘要数据(历史记录,支持分页)
async loadDailySummary(page: number = 1) {
try {
// 只有在第一页时才显示加载状态(下拉刷新)
@@ -143,11 +140,10 @@ Page({
this.setData({ isLoading: true });
}
const result = await apiManager.getDailySummary(page, this.data.size);
const todaySummary = await apiManager.getTodaySummary(page, this.data.size);
const result = await apiManager.getDailySummary(page, this.data.dailySize);
// 处理数据,按年份分组
const processedItems = [...todaySummary.items, ...result.items].map(item => ({
const processedItems = result.items.map(item => ({
...item,
images: item.image_ids && item.thumbnail_ids ?
item.image_ids.map((imageId: string, index: number) => ({
@@ -158,43 +154,87 @@ Page({
})) : []
}));
// 如果是第一页,替换数据;否则追加数据
const newData = page === 1 ? processedItems : [...this.data.groupedHistory, ...processedItems];
let groupedArray;
// 按年份分组
const currentYear = new Date().getFullYear();
const grouped: any = {};
newData.forEach(item => {
const date = new Date(item.summary_time);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
if (page === 1) {
// 第一页:替换所有数据
const currentYear = new Date().getFullYear();
const grouped: any = {};
if (!grouped[year]) {
grouped[year] = [];
}
grouped[year].push({
...item,
monthDay: `${month}-${day}`
processedItems.forEach(item => {
const date = new Date(item.summary_time);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
if (!grouped[year]) {
grouped[year] = [];
}
grouped[year].push({
...item,
monthDay: `${month}-${day}`
});
});
});
// 转换为数组形式并按年份排序
const groupedArray = Object.keys(grouped)
.map(year => ({
year: parseInt(year),
isCurrentYear: parseInt(year) === currentYear,
items: grouped[year]
}))
.sort((a, b) => b.year - a.year);
// 转换为数组形式并按年份排序
groupedArray = Object.keys(grouped)
.map(year => ({
year: parseInt(year),
isCurrentYear: parseInt(year) === currentYear,
items: grouped[year]
}))
.sort((a, b) => b.year - a.year);
} else {
// 后续页面:只处理新数据并追加到现有数据
const currentYear = new Date().getFullYear();
const existingData = [...this.data.groupedHistory];
// 处理新数据的分组
const newGrouped: any = {};
processedItems.forEach(item => {
const date = new Date(item.summary_time);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
if (!newGrouped[year]) {
newGrouped[year] = [];
}
newGrouped[year].push({
...item,
monthDay: `${month}-${day}`
});
});
// 将新数据合并到现有数据中
Object.keys(newGrouped).forEach(year => {
const yearInt = parseInt(year);
const existingYearGroup = existingData.find(group => group.year === yearInt);
if (existingYearGroup) {
// 如果年份已存在,追加到该年份的数据中
existingYearGroup.items = [...existingYearGroup.items, ...newGrouped[year]];
} else {
// 如果年份不存在,添加新的年份组
existingData.push({
year: yearInt,
isCurrentYear: yearInt === currentYear,
items: newGrouped[year]
});
}
});
// 重新排序年份
groupedArray = existingData.sort((a, b) => b.year - a.year);
}
this.setData({
groupedHistory: groupedArray,
page: page,
total: result.total,
hasMore: newData.length < result.total,
dailyPage: page,
dailyTotal: result.total,
dailyHasMore: page * this.data.dailySize < result.total,
isLoading: false,
});
} catch (error) {
@@ -207,10 +247,42 @@ Page({
}
},
// 触底加载更多数据
// 加载今日摘要数据(当天记录,只加载第一页)
async loadTodaySummary(page: number = 1) {
try {
const result = await apiManager.getTodaySummary(page, this.data.todaySize);
// 处理数据,按年份分组
const processedItems = result.items.map(item => ({
...item,
images: item.image_ids && item.thumbnail_ids ?
item.image_ids.map((imageId: string, index: number) => ({
image_id: imageId,
thumbnail_id: item.thumbnail_ids![index],
thumbnail_url: `${this.data.fileBaseUrl}/${item.thumbnail_ids![index]}`,
image_url: `${this.data.fileBaseUrl}/${imageId}`
})) : []
}));
// 注意today summary 不需要追加到 groupedHistory 中,它有独立的用途
// 这里只处理数据但不更新页面显示,保留翻页能力但暂不实现
console.log('今日摘要数据加载完成:', processedItems);
return processedItems;
} catch (error) {
console.error('加载今日摘要失败:', error);
wx.showToast({
title: '加载今日数据失败',
icon: 'none'
});
throw error;
}
},
// 触底加载更多数据(只加载每日摘要的下一页)
onReachBottom() {
if (this.data.hasMore && !this.data.isLoading) {
const nextPage = this.data.page + 1;
if (this.data.dailyHasMore && !this.data.isLoading) {
const nextPage = this.data.dailyPage + 1;
this.loadDailySummary(nextPage);
}
},
@@ -218,7 +290,8 @@ Page({
// 下拉刷新
onPullDownRefresh() {
this.setData({
page: 1,
dailyPage: 1,
todayPage: 1
});
this.loadDailySummary(1).then(() => {
wx.stopPullDownRefresh();
@@ -428,37 +501,4 @@ Page({
})
}
// 退出登录
// handleLogout() {
// wx.showModal({
// title: '确认退出',
// content: '确定要退出登录吗?',
// success: (res) => {
// if (res.confirm) {
// // 清理登录状态
// app.globalData.isLoggedIn = false
// app.globalData.token = undefined
// app.globalData.userInfo = undefined
// wx.removeStorageSync('token')
// wx.removeStorageSync('userInfo')
// wx.removeStorageSync('tokenExpiry')
// this.updateLoginStatus()
// wx.showToast({
// title: '已退出登录',
// icon: 'success'
// })
// // 跳转到登录页
// setTimeout(() => {
// wx.reLaunch({
// url: '/pages/index/index'
// })
// }, 1500)
// }
// }
// })
// }
})

Binary file not shown.

Before

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 281 KiB