import dayjs from 'dayjs'; import taskApi from '../../api/taskApi'; /** * 子任务列表模块 */ export const useSubTaskItem = Behavior({ data: { subTaskLoading: false, // 当日的子任务 subtaskList: [], }, methods: { statusType(status) { switch (status) { case '未执行': return { type: 'danger', icon: 'assignment-error-filled', }; case '正在执行': return { type: 'success', icon: 'time', }; case '已结束': return { type: '', icon: 'assignment-checked-filled', }; default: return { type: 'danger', icon: 'assignment-error-filled', }; } }, /** * 获取子任务 */ fetchSubtaskByDayTask() { this.setData({ subTaskLoading: true }); const { thisDate, daytasks } = this.data; const fdt = daytasks.find(d => { return dayjs(d.date).isSame(dayjs(thisDate), 'day'); }); if (fdt) { taskApi .fetchSubtaskByDayTask(fdt.guid) .then(res => { this.setData({ subtaskList: res .sort((a, b) => { if (a.sceneTypeId != b.sceneTypeId) { return a.sceneTypeId - b.sceneTypeId; } else { return dayjs(b.executionstarttime).unix() - dayjs(a.executionstarttime).unix(); } }) .map(stask => { const changePerType = () => { if (status.value.changeNum == 0) { if (status.value.proNum == 0) { return 'success'; } else { return 'danger'; } } else if (status.value.proNum == status.value.changeNum) { return 'success'; } else { return 'warning'; } }; const timeformat = date => { return date ? dayjs(date).format('YYYY-MM-DD HH:mm') : '----/--/-- --:--'; }; return { ...stask, startTime: timeformat(stask.executionstarttime), endTime: timeformat(stask.executionendtime), statusType: this.statusType(stask.status), }; }), }); }) .finally(() => this.setData({ subTaskLoading: false })); } else { this.setData({ subtaskList: [], subTaskLoading: false, }); } }, /** * 点击子任务事件处理函数 */ handleItemClick(e) { const { index } = e.currentTarget.dataset; const { subtaskList } = this.data; const subtask = subtaskList[index]; wx.navigateTo({ url: '/package_supervision/pages/inspection/index', success: result => { result.eventChannel.emit('acceptSubTaskData', { subtask, }); }, events: { // 任务状态变更事件 changeStatusEvent: data => { const { daytasks } = this.data; subtask.status = data?.subtask?.status; subtask.statusType = this.statusType(subtask.status); // 若任务结束,则更新日任务中的完成任务数量统计 if (subtask.status == '已结束') { let _index; const daytask = daytasks.find((d, i) => { if (d.guid == subtask.tsguid) { _index = i; return true; } }); daytask.completeTaskNum++; this.setData({ [`daytasks[${_index}]`]: daytask, }); } this.setData({ [`subtaskList[${index}]`]: subtask, }); this.selectThisDay(); }, }, }); }, }, });