完善每日签到功能
This commit is contained in:
@@ -1,189 +1,178 @@
|
||||
// 定义日历日期类型
|
||||
// pages/checkin/checkin.ts
|
||||
|
||||
interface CalendarDay {
|
||||
type: 'current' | 'empty'; // current:当月日期, empty:空占位(上月/下月)
|
||||
day: number; // 日期数字
|
||||
date: string; // 完整日期(YYYY-MM-DD)
|
||||
isChecked: boolean; // 是否已签到
|
||||
isToday: boolean; // 是否是今天
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
currentYear: new Date().getFullYear(), // 当前年
|
||||
currentMonth: new Date().getMonth(), // 当前月(0-11)
|
||||
calendarDays: [] as CalendarDay[], // 日历日期列表
|
||||
checkinHistory: [] as string[], // 签到历史(存储YYYY-MM-DD格式日期)
|
||||
continuousCheckinDays: 0, // 连续签到天数
|
||||
totalCheckinDays: 0, // 本月总签到天数
|
||||
isCheckedToday: false, // 今日是否已签到
|
||||
isAnimating: false // 是否正在执行签到动画
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
// 从本地缓存加载签到历史
|
||||
this.loadCheckinHistory();
|
||||
// 生成当月日历数据
|
||||
this.generateCalendarDays();
|
||||
// 计算签到统计数据
|
||||
this.calculateCheckinStats();
|
||||
},
|
||||
|
||||
/**
|
||||
* 从本地缓存加载签到历史
|
||||
*/
|
||||
loadCheckinHistory() {
|
||||
const history = wx.getStorageSync('checkinHistory') || [];
|
||||
this.setData({ checkinHistory: history });
|
||||
|
||||
// 检查今日是否已签到
|
||||
const today = this.formatDate(new Date());
|
||||
const isCheckedToday = history.includes(today);
|
||||
this.setData({ isCheckedToday });
|
||||
},
|
||||
|
||||
/**
|
||||
* 生成当月日历数据
|
||||
*/
|
||||
generateCalendarDays() {
|
||||
const { currentYear, currentMonth, checkinHistory } = this.data;
|
||||
const calendarDays: CalendarDay[] = [];
|
||||
|
||||
// 获取当月第一天是星期几(0=周日,1=周一...6=周六)
|
||||
const firstDayOfMonth = new Date(currentYear, currentMonth, 1).getDay();
|
||||
// 获取当月总天数
|
||||
const totalDaysOfMonth = new Date(currentYear, currentMonth + 1, 0).getDate();
|
||||
// 获取今日日期(YYYY-MM-DD)
|
||||
const today = this.formatDate(new Date());
|
||||
|
||||
// 添加上月残留日期(空占位)
|
||||
for (let i = 0; i < firstDayOfMonth; i++) {
|
||||
calendarDays.push({
|
||||
type: 'empty',
|
||||
day: 0,
|
||||
date: '',
|
||||
isChecked: false,
|
||||
isToday: false
|
||||
});
|
||||
}
|
||||
|
||||
// 添加当月日期
|
||||
for (let day = 1; day <= totalDaysOfMonth; day++) {
|
||||
const date = new Date(currentYear, currentMonth, day);
|
||||
const dateStr = this.formatDate(date);
|
||||
const isChecked = checkinHistory.includes(dateStr);
|
||||
const isToday = dateStr === today;
|
||||
|
||||
calendarDays.push({
|
||||
type: 'current',
|
||||
day,
|
||||
date: dateStr,
|
||||
isChecked,
|
||||
isToday
|
||||
});
|
||||
}
|
||||
|
||||
// 添加下月占位日期(确保日历满6行,共42个格子)
|
||||
const totalCells = 42; // 6行×7列=42个格子
|
||||
const remainingCells = totalCells - calendarDays.length;
|
||||
for (let i = 0; i < remainingCells; i++) {
|
||||
calendarDays.push({
|
||||
type: 'empty',
|
||||
day: 0,
|
||||
date: '',
|
||||
isChecked: false,
|
||||
isToday: false
|
||||
});
|
||||
}
|
||||
|
||||
this.setData({ calendarDays });
|
||||
},
|
||||
|
||||
/**
|
||||
* 计算签到统计数据
|
||||
*/
|
||||
calculateCheckinStats() {
|
||||
const { checkinHistory, currentYear, currentMonth } = this.data;
|
||||
const today = new Date();
|
||||
|
||||
// 计算本月总签到天数
|
||||
const monthStart = new Date(currentYear, currentMonth, 1);
|
||||
const monthEnd = new Date(currentYear, currentMonth + 1, 0);
|
||||
const monthStartStr = this.formatDate(monthStart);
|
||||
const monthEndStr = this.formatDate(monthEnd);
|
||||
|
||||
const monthlyCheckins = checkinHistory.filter(date => {
|
||||
return date >= monthStartStr && date <= monthEndStr;
|
||||
});
|
||||
|
||||
// 计算连续签到天数
|
||||
let continuousDays = 0;
|
||||
const sortedHistory = [...checkinHistory].sort();
|
||||
|
||||
// 从今天开始往前查连续签到
|
||||
const checkDate = new Date(today);
|
||||
while (true) {
|
||||
const dateStr = this.formatDate(checkDate);
|
||||
if (sortedHistory.includes(dateStr)) {
|
||||
continuousDays++;
|
||||
// 往前推一天
|
||||
checkDate.setDate(checkDate.getDate() - 1);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.setData({
|
||||
totalCheckinDays: monthlyCheckins.length,
|
||||
continuousCheckinDays: continuousDays
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理签到逻辑
|
||||
*/
|
||||
handleCheckin() {
|
||||
if (this.data.isCheckedToday || this.data.isAnimating) return;
|
||||
|
||||
// 开始动画
|
||||
this.setData({ isAnimating: true });
|
||||
|
||||
// 记录当前日期
|
||||
const today = this.formatDate(new Date());
|
||||
const { checkinHistory } = this.data;
|
||||
|
||||
// 添加到签到历史
|
||||
const newHistory = [...checkinHistory, today];
|
||||
wx.setStorageSync('checkinHistory', newHistory);
|
||||
|
||||
// 更新数据
|
||||
setTimeout(() => {
|
||||
this.setData({
|
||||
checkinHistory: newHistory,
|
||||
isCheckedToday: true,
|
||||
isAnimating: false
|
||||
});
|
||||
|
||||
// 更新日历和统计
|
||||
this.generateCalendarDays();
|
||||
this.calculateCheckinStats();
|
||||
|
||||
// 显示成功提示
|
||||
wx.showToast({
|
||||
title: '签到成功!',
|
||||
icon: 'success',
|
||||
duration: 1500
|
||||
});
|
||||
}, 1000); // 动画持续1秒
|
||||
},
|
||||
|
||||
/**
|
||||
* 格式化日期为YYYY-MM-DD
|
||||
*/
|
||||
formatDate(date: Date): string {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
day: number | string; // 日期数字或空字符串
|
||||
fullDate?: string; // 完整日期字符串,如 'YYYY-MM-DD'
|
||||
isPrevMonth?: boolean; // 是否上月日期
|
||||
isNextMonth?: boolean; // 是否下月日期
|
||||
isToday?: boolean; // 是否今天
|
||||
isCheckedIn?: boolean; // 是否已签到
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Page({
|
||||
data: {
|
||||
weekdays: ['日', '一', '二', '三', '四', '五', '六'],
|
||||
currentYear: 0,
|
||||
currentMonth: 0, // 1-12月
|
||||
calendarDays: [] as CalendarDay[], // 日历日期数据
|
||||
|
||||
consecutiveCheckInDays: 0, // 连续签到天数
|
||||
monthCheckInDays: 0, // 本月签到天数
|
||||
todayCheckedIn: false, // 今日是否已签到
|
||||
|
||||
// 假设这些是用户已签到的日期(仅日期数字),在真实项目中从后端获取
|
||||
checkedInDates: [] as number[], // [1, 5, 18, 29] 表示当月哪些天已签到
|
||||
|
||||
// 用于保存当前显示的月份的Date对象,方便计算
|
||||
displayMonthDate: new Date(),
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.initCalendar();
|
||||
this.fetchCheckInStats(); // 模拟从后端获取签到数据
|
||||
},
|
||||
|
||||
/**
|
||||
* 初始化日历,生成当前月份的日期
|
||||
*/
|
||||
initCalendar() {
|
||||
const today = new Date();
|
||||
this.data.displayMonthDate = new Date(today.getFullYear(), today.getMonth(), 1); // 设置为当前月的第一天
|
||||
|
||||
this.renderCalendar(this.data.displayMonthDate);
|
||||
},
|
||||
|
||||
/**
|
||||
* 渲染指定月份的日历
|
||||
* @param date 当前月份的任意一天,例如 new Date(2025, 8, 1) 表示 2025年9月
|
||||
*/
|
||||
renderCalendar(date: Date) {
|
||||
const year = date.getFullYear();
|
||||
const month = date.getMonth(); // 0-11
|
||||
|
||||
this.setData({
|
||||
currentYear: year,
|
||||
currentMonth: month + 1, // 显示给用户是1-12月
|
||||
});
|
||||
|
||||
const calendarDays: CalendarDay[] = [];
|
||||
const firstDayOfMonth = new Date(year, month, 1);
|
||||
const lastDayOfMonth = new Date(year, month + 1, 0); // 当月最后一天
|
||||
|
||||
// 补充上个月的日期(如果需要)
|
||||
const startDayOfWeek = firstDayOfMonth.getDay(); // 0-6,0是周日
|
||||
for (let i = 0; i < startDayOfWeek; i++) {
|
||||
calendarDays.push({ day: '', isPrevMonth: true }); // 用空字符串占位或显示上月日期
|
||||
}
|
||||
|
||||
// 填充当前月份的日期
|
||||
for (let i = 1; i <= lastDayOfMonth.getDate(); i++) {
|
||||
const currentDay = new Date(year, month, i);
|
||||
const isToday = this.isSameDay(currentDay, new Date());
|
||||
const isCheckedIn = this.data.checkedInDates.includes(i); // 判断是否已签到
|
||||
|
||||
calendarDays.push({
|
||||
day: i,
|
||||
fullDate: this.formatDate(currentDay),
|
||||
isToday: isToday,
|
||||
isCheckedIn: isCheckedIn,
|
||||
});
|
||||
}
|
||||
|
||||
// 补充下个月的日期(如果需要,凑够6行或7的倍数)
|
||||
const totalDays = calendarDays.length;
|
||||
const remainingDays = 42 - totalDays; // 假设日历固定6行7列,共42个格子
|
||||
for (let i = 0; i < remainingDays; i++) {
|
||||
calendarDays.push({ day: '', isNextMonth: true }); // 用空字符串占位
|
||||
}
|
||||
|
||||
this.setData({
|
||||
calendarDays: calendarDays,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 模拟从后端获取签到统计数据和已签到日期
|
||||
*/
|
||||
fetchCheckInStats() {
|
||||
// 真实项目中这里会发起 wx.request 到您的后端接口
|
||||
// 假设后端返回的数据格式如下:
|
||||
const mockApiResponse = {
|
||||
consecutiveCheckInDays: 29, // 连续签到天数
|
||||
monthCheckInDays: 15, // 本月签到天数
|
||||
todayCheckedIn: false, // 假设今天未签到
|
||||
checkedInDaysInMonth: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 29], // 当月已签到的日期数字
|
||||
};
|
||||
|
||||
this.setData({
|
||||
consecutiveCheckInDays: mockApiResponse.consecutiveCheckInDays,
|
||||
monthCheckInDays: mockApiResponse.monthCheckInDays,
|
||||
todayCheckedIn: mockApiResponse.todayCheckedIn,
|
||||
checkedInDates: mockApiResponse.checkedInDaysInMonth,
|
||||
});
|
||||
|
||||
// 重新渲染日历以应用新的签到状态
|
||||
this.renderCalendar(this.data.displayMonthDate);
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理签到按钮点击
|
||||
*/
|
||||
handleCheckIn() {
|
||||
if (this.data.todayCheckedIn) {
|
||||
wx.showToast({ title: '今日已签到', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
// 模拟后端签到逻辑
|
||||
wx.showLoading({ title: '签到中...' });
|
||||
setTimeout(() => {
|
||||
// 假设后端返回签到成功
|
||||
const today = new Date().getDate();
|
||||
const newCheckedInDates = [...this.data.checkedInDates, today];
|
||||
|
||||
this.setData({
|
||||
todayCheckedIn: true,
|
||||
consecutiveCheckInDays: this.data.consecutiveCheckInDays + 1, // 假设连续+1
|
||||
monthCheckInDays: this.data.monthCheckInDays + 1, // 假设当月+1
|
||||
checkedInDates: newCheckedInDates,
|
||||
});
|
||||
|
||||
// 重新渲染日历,标记今天为已签到
|
||||
this.renderCalendar(this.data.displayMonthDate);
|
||||
|
||||
wx.hideLoading();
|
||||
wx.showToast({ title: '签到成功!', icon: 'success' });
|
||||
}, 1000); // 模拟网络请求
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理日历中的日期点击(可选,如果需要点击日期查看历史签到等)
|
||||
*/
|
||||
handleDayClick(event: WechatMiniprogram.TouchEvent) {
|
||||
const fullDate = event.currentTarget.dataset.date;
|
||||
if (fullDate) {
|
||||
console.log('点击了日期:', fullDate);
|
||||
// 可在此处实现点击日期查看历史签到详情等逻辑
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 辅助函数:判断两个日期是否是同一天 (忽略时间)
|
||||
*/
|
||||
isSameDay(date1: Date, date2: Date): boolean {
|
||||
return date1.getFullYear() === date2.getFullYear() &&
|
||||
date1.getMonth() === date2.getMonth() &&
|
||||
date1.getDate() === date2.getDate();
|
||||
},
|
||||
|
||||
/**
|
||||
* 辅助函数:格式化日期为 YYYY-MM-DD
|
||||
*/
|
||||
formatDate(date: Date): string {
|
||||
const year = date.getFullYear();
|
||||
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
||||
const day = date.getDate().toString().padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user