riku
3 小时以前 b09c7e7aefd41a62326ea56460092aa0db54c083
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import taskApi from '../../api/taskApi';
import dayjs from 'dayjs';
import { useSubTaskItem } from './subtaskitem-proxy.js';
 
const app = getApp();
 
Page({
  behaviors: [useSubTaskItem],
  /**
   * 页面的初始数据
   */
  data: {
    // 日历显示日期范围
    minDate: new Date(2026, 2, 1).getTime(),
    maxDate: new Date(2026, 3, 30).getTime(),
 
    // 选中的日期
    thisDate: new Date().getTime(),
    // 上次选中的日期
    lastDate: new Date().getTime(),
 
    // 当月总任务选项及选中的任务
    toptask: {
      options: [],
      value: '',
    },
    // 选中总任务下对应的日任务
    daytasks: [],
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchToptask();
    // 页面加载完成后,设定日历的格式化日期函数
    const _formatCalendarDay = this._formatCalendarDay;
    this.setData({
      formatCalendarDay: _formatCalendarDay,
    });
 
    // 设置日历起止时间
    this.setData({
      minDate: dayjs().startOf('month').add(-1, 'month').toDate().getTime(),
      maxDate: dayjs().endOf('month').toDate().getTime(),
    });
  },
 
  /**
   * 监听页面滚动
   */
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop,
    });
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
 
  /**
   * 修改总任务事件处理函数
   */
  onToptaskChange(e) {
    console.log('onToptaskChange', e);
    this.setData({
      'toptask.value': e.detail.value,
    });
    this.fetchDayTasks();
  },
 
  /**
   * 切换月份或年份事件处理函数
   */
  handelMonthChange(e) {
    // 选中日期同步切换一个月
    const { year, month } = e.detail;
    let thisDay = dayjs(this.data.thisDate);
    thisDay = thisDay.year(year);
    thisDay = thisDay.month(month - 1);
 
    // 如果切换后的月份为当前日历范围的起始月份,则修改起始月份减少一个月,确保用户可继续查看历史月份
    const _minDate = dayjs(this.data.minDate);
    if (thisDay.month() == _minDate.month()) {
      this.setData({
        minDate: _minDate.add(-1, 'month').toDate().getTime(),
      });
    }
 
    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) {
    if (!this) return;
    const { date } = day;
    const { daytasks } = this.data;
 
    // 查找当天是否有日任务
    const fdt = daytasks.find(d => {
      return dayjs(d.date).isSame(dayjs(date), 'day') && d.totalTaskNum > 0;
    });
 
    if (fdt) {
      day.suffix = `${fdt.completeTaskNum}/${fdt.totalTaskNum}`;
      day.className = 'has-task';
    }
    return day;
  },
 
  /**
   * 点击日期事件处理函数
   */
  handleSelectDay(e) {
    console.log('app', app);
    const date = new Date(e.detail.value);
    const { thisDate } = this.data;
    this.setData({
      lastDate: thisDate,
      thisDate: date.getTime(),
    });
    this.fetchSubtaskByDayTask();
  },
 
  /**
   * 根据所选时段获取总任务
   */
  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.length > 0
          ? res.data.map(r => {
              return {
                label: r.name,
                value: r.tguid,
              };
            })
          : [
              {
                label: '本月无监管任务',
                value: null,
              },
            ];
      this.setData({
        toptask: {
          options: options,
          value: options[0].value,
        },
      });
      this.fetchDayTasks();
    });
  },
 
  /**
   * 获取每日任务统计信息
   */
  fetchDayTasks() {
    this.setData({ pageLoading: true });
    taskApi
      .fetchDayTasks(this.data.toptask.value)
      .then(res => {
        if (res) {
          this.setData({ daytasks: res });
          this.selectThisDay();
        }
      })
      .finally(() => this.setData({ pageLoading: false }));
  },
 
  // 用于触发日历的日期格式化函数,显示每日任务完成情况
  selectThisDay() {
    setTimeout(() => {
      const { thisDate } = this.data;
      this.setData({
        thisDate,
      });
      this.fetchSubtaskByDayTask();
    }, 200);
  },
});