// pages/targetList/targetList.js const app = getApp() const util = require("../../utils/util") const urlDef = require("../../utils/urls") Page({ /** * 页面的初始数据 */ data: { source: null, target: null, dayScope: 7,// 默认 7 天 action: 0, list: [ ], beginDate: null, endDate: null, teacherLeaveDates: [], holidays: [] }, showChoose: function () { this.selectComponent("#classList").showChoose() }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.showLoading({ title: '正在加载数据 ...', mask: true }) let o = JSON.parse(options.data) let s = o.source let t = o.target if (t == null || t == undefined) { t = {} } let _dayScope = o.dayScope if (_dayScope) { this.setData({ dayScope: _dayScope }) } this.setData({ source: s, target: t, action: o.action }) let now = new Date(); // 从第2天开始,14天内的课程 let bdd = new Date(now.setDate(now.getDate() + 1)); let bd = util.formatTime(bdd).substring(0, 10).replace(/\//g, '-') let edd = new Date(now.setDate(now.getDate() + this.data.dayScope)); let ed = util.formatTime(edd).substring(0, 10).replace(/\//g, '-') this.setData({ beginDate: bd, endDate: ed, list: [] }); this.queryList() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, queryList: function () { const urls = urlDef.urls; let stu = wx.getStorageSync('student') // 先查询节假日数据,包含在节假日里面的所有课程,将不显示 util.apiPost(urls.get_holidays + '&q.companyId=' + stu.orgId).then((rs) => { this.holidays = rs; this.setData({ holidays: rs }) util.apiPost(urls.get_teacherLeave + '&q.orgId=' + stu.orgId).then((rs) => { this.setData({ teacherLeaveDates: rs }) this.queryClassList() }); }); }, queryClassList: function () { const urls = urlDef.urls; let stu = wx.getStorageSync('student') let classItem = []; let allWeekTask = {}; // 按星期分组 const dayTime = 86400000; let tbd = new Date(this.data.beginDate); let ted = new Date(this.data.endDate); // 算出两个日期一共有多少天; let days = Math.ceil((ted.getTime() - tbd.getTime()) / dayTime) + 1; let weeks = Math.ceil(days / 7); for (let i = 0; i < weeks; i++) { let tempDate = new Date(this.data.beginDate); tempDate.setDate(tempDate.getDate() + i * 7); let weekTask = {}; weekTask.complete = false; weekTask.beginDate = this.formateDate(tempDate); tempDate.setDate(tempDate.getDate() + 6); if (tempDate.getTime() > ted.getTime()) { tempDate = ted; } weekTask.endDate = this.formateDate(tempDate); weekTask.weekMap = this.initWeek(weekTask); allWeekTask['week' + i] = weekTask; } for (let weekKey in allWeekTask) { let weekTask = allWeekTask[weekKey]; // 查询 let item = this.data.source let params = { 'q.courseId': item.courseId, 'q.classesId': item.classesId, 'q.beginDate': weekTask.beginDate, 'q.endDate': weekTask.endDate, 'q.classromeId': item.classRoomId, 'q.studentId': stu.studentId, 'q.oldTeacherId': item.teacherId, 'q.kindId': item.kindId, 'q.level': (item.level ? item.level : ''), 'q.orgId': stu.orgId }; util.apiPost(urls.get_switch_course_list, params).then((rs) => { weekTask.data = rs; weekTask.complete = true; weekTask.list = this.filterList(weekTask); // 每次查询完,检查所有的请求执行完没有 let allTaskOver = true; for (let wk in allWeekTask) { if (allWeekTask[wk].complete) { } else { // 只要有一个没执行完,就都没执行完 allTaskOver = false; break; } } if (allTaskOver) { classItem = []; for (let weekKey in allWeekTask) { let weekTask = allWeekTask[weekKey]; classItem.push.apply(classItem, weekTask.list); } let data = []; classItem.forEach(item => { let day = item.newDate; let o; data.some(d => { if (d.day === day) { o = d; return true; } }); if (!o) { o = {}; o.day = day; data.push(o); } }); // 按日期排序 data.sort((a, b) => { return (parseInt(a.day.replace(/-/g, ''), 10) - parseInt(b.day.replace(/-/g, ''), 10)); }); data.forEach(item => { let items = []; classItem.forEach(i => { if (i.newDate === item.day) { i.bt = i.beginTime i.et = i.endTime items.push(i); } }); items.sort((a, b) => { return (parseInt(a.beginTime.replace(/:/g, ''), 10) - parseInt(b.beginTime.replace(/:/g, ''), 10)); }); item.items = items; item.date = item.day.substring(0, 10) item.week = this.getWeek(item.date) }); classItem = data; this.setData({ list: classItem }) wx.hideLoading() } }) } }, exceptHoliday: function (d) { let result = true; this.data.holidays.some(r => { if (r.holidayDate === d) { result = false; return true; } }); return result; }, exceptTeacheLeave: function (d) { // 老师请假中的课程不显示 let result = true; this.data.teacherLeaveDates.some(r => { let leaveBeginTime = this.dateToNumber(r.beginTime); let leaveEndTime = this.dateToNumber(r.endTime); let beginTime = this.dateToNumber(d.newDate + d.beginTime); let endTime = this.dateToNumber(d.newDate + d.endTime); if (r.teacherId === d.teacherId && ((beginTime >= leaveBeginTime && beginTime <= leaveEndTime) || (endTime >= leaveBeginTime && endTime <= leaveEndTime))) { result = false; return true; } }); return result; }, getDay: function (week, weekMap) { let result = null; weekMap.some(r => { if (r.week === week) { result = r.ymd; return true; } }); return result; }, formateDate: function (d) { return d.getFullYear() + '-' + this.addZero(d.getMonth() + 1) + '-' + this.addZero(d.getDate()); }, dateToNumber: function (d) { if (d) { return parseInt(d.replace(/-/g, '').replace(/:/g, '').replace(/\s+/g, ''), 10); } else { return 0; } }, addZero: function (n) { return (parseInt(n, 10) >= 10 ? '' : '0') + n; }, initWeek: function (weekTask) { let weekMap = []; const bd = new Date(weekTask.beginDate); const ed = new Date(weekTask.endDate); while (bd.getTime() <= ed.getTime()) { weekMap.push({ week: bd.getDay(), ymd: this.formateDate(bd) }); bd.setDate(bd.getDate() + 1); } return weekMap; }, len: function (o) { return (o == null || o === undefined) ? 0 : o.length; }, filterList: function (weekTask) { let items = []; weekTask.data.forEach((it) => { if (it.classes) { it.classes.forEach(c => { let item = c; item.classesName = c.name; item.address = c.address; item.roomName = c.roomName; let tempItems = []; if (this.len(c.beginTime) > 0) { let bt = c.beginTime.replace('T', ' ').substring(0, 16) item.newDate = bt.substring(0.10) item.beginTime = bt.substring(11); item.endTime = this.addZero(it.endHours) + ':' + this.addZero(it.endMinutes); tempItems.push(item); } else { // 没有真实排课 item.beginTime = this.addZero(it.beginHours) + ':' + this.addZero(it.beginMinutes); item.endTime = this.addZero(it.endHours) + ':' + this.addZero(it.endMinutes); // 服务端返回的2345671 表示 周一到周日 // 此处是 1234567 表示 周一到周日 let d = this.getDay(it.week - 1, weekTask.weekMap); if (d != null) { item.newDate = d; tempItems.push(item); } } tempItems.forEach(item => { if (item.classStatus === '1' && this.exceptHoliday(item.newDate) && this.exceptTeacheLeave(item)) { // 未满班的才允许调课 && 不在假节日中的课程 items.push(item); } }); }); } }); return items; }, getWeek: function (d) { return '星期' + '天一二三四五六'.charAt(new Date(d).getDay()); } })