123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- // 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, '-')
- if (o.action == '3') {
- // 根据curDate 算出前后七天, 其实时间晚于当天,已次日为其实时间
- // console.log('item.queryDate : ' + item.curDate);
- const temDate = new Date(s.attenceDate.replace(/-/g, '/') + ' 00:00:00')
- temDate.setDate(temDate.getDate() + 7); // 一周后
- ed = temDate.getFullYear() + '-' + this.addZero(temDate.getMonth() + 1) + '-' + this.addZero(temDate.getDate());
- // console.log('ed : ' + ed);
- temDate.setDate(temDate.getDate() - 14); // 往前推2周
- bd = temDate.getFullYear() + '-' + this.addZero(temDate.getMonth() + 1) + '-' + this.addZero(temDate.getDate());
- // console.log('bd : ' + bd);
- // 如果早于当前日期
- let nextDay = util.formatTime(new Date(new Date().getTime() + 86400000)).substring(0, 10).replace(/\//g, '-')
- // console.log('nextDay : ' + nextDay);
- if (parseInt(bd.replace(/-/g, '')) < parseInt(nextDay.replace(/-/g, ''))) {
- bd = nextDay;
- }
- }
- this.setData({ beginDate: bd, endDate: ed, list: [] });
- this.queryList()
- },
- addZero: function (n) {
- return (n < 10 ? '0' : '') + n;
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- 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()
- if (classItem.length <= 0) {
- wx.showModal({
- title: '提示',
- content: '暂无目标课程',
- showCancel: false, //是否显示取消按钮
- cancelText: "否", //默认是“取消”
- cancelColor: '#999999', //取消文字的颜色
- confirmText: "确定", //默认是“确定”
- // confirmColor: 'skyblue', //确定文字的颜色
- success() {
- wx.navigateBack({
- delta: 1,
- })
- }
- })
- }
- }
- })
- }
- },
- 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());
- }
- })
|