import taskApi from '../../api/taskApi'; import dayjs from 'dayjs'; Page({ /** * 页面的初始数据 */ data: { minDate: new Date(2026, 0, 1).getTime(), maxDate: new Date(2026, 3, 30).getTime(), thisDate: new Date().getTime(), toptask: { options: [], value: '', }, daytasks: [], }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.fetchToptask(); }, /** * 用户点击右上角分享 */ onShareAppMessage() {}, /** * 修改总任务事件处理函数 */ onToptaskChange(e) { console.log('onToptaskChange', e); this.setData({ 'toptask.value': e.detail.value, }); this.fetchDayTasks(); }, /** * 切换月份或年份事件处理函数 */ handelMonthChange(e) { console.log('handelMonthChange', e); const { year, month } = e.detail; let thisDay = dayjs(this.data.thisDate); thisDay = thisDay.year(year); thisDay = thisDay.month(month - 1); this.setData({ thisDate: thisDay.toDate().getTime() }); this.fetchToptask(); }, /** * 根据日任务情况,格式化对应日期的样式和内容 * @param {TDate } day * TDate { date: Date; day: number; type: TDateType; className?: string; prefix?: string; suffix?: string;} * type TDateType = 'selected' | 'disabled' | 'start' | 'start-end' |'centre' | 'end' | '' */ _formatCalendarDay(day) { console.log('_formatCalendarDay', day); const { date } = day; const year = date.getFullYear(); const month = date.getMonth() + 1; const curDate = date.getDate(); const { daytasks } = this.data; // 查找当天是否有日任务 const fdt = daytasks.find(d => { return dayjs(d.date).isSame(dayjs(date), 'day'); }); if (fdt) { day.suffix = `${fdt.completeTaskNum}/${fdt.totalTaskNum}`; } return day; }, /** * 点击日期事件处理函数 */ handleSelectDay(e) { const date = new Date(e.detail.value); console.log('handleSelectDay', date); }, /** * 根据所选时段获取总任务 */ fetchToptask() { this.setData({ pageLoading: true }); const thisDay = dayjs(this.data.thisDate); const starttime = thisDay.startOf('month').format('YYYY-MM-DD HH:mm:ss'); const endtime = thisDay.endOf('month').format('YYYY-MM-DD HH:mm:ss'); taskApi.fetchTopTasks({ starttime, endtime }).then(res => { const options = res.data.map(r => { return { label: r.name, value: r.tguid, }; }); this.setData({ toptask: { options, value: options.length > 0 ? options[0].value : '', }, }); this.fetchDayTasks(); }); }, /** * 获取每日任务统计信息 */ fetchDayTasks() { this.setData({ pageLoading: true }); taskApi .fetchDayTasks(this.data.toptask.value) .then(res => { this.setData({ daytasks: res }); setTimeout(() => { const _formatCalendarDay = this._formatCalendarDay; this.setData({ formatCalendarDay: _formatCalendarDay }); }, 1000); }) .finally(() => this.setData({ pageLoading: false })); }, });