targetList.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // pages/targetList/targetList.js
  2. const app = getApp()
  3. const util = require("../../utils/util")
  4. const urlDef = require("../../utils/urls")
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. source: null,
  11. target: null,
  12. dayScope: 7,// 默认 7 天
  13. action: 0,
  14. list: [
  15. ],
  16. beginDate: null,
  17. endDate: null,
  18. teacherLeaveDates: [],
  19. holidays: []
  20. },
  21. showChoose: function () {
  22. this.selectComponent("#classList").showChoose()
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function (options) {
  28. wx.showLoading({
  29. title: '正在加载数据 ...',
  30. mask: true
  31. })
  32. let o = JSON.parse(options.data)
  33. let s = o.source
  34. let t = o.target
  35. if (t == null || t == undefined) {
  36. t = {}
  37. }
  38. let _dayScope = o.dayScope
  39. if (_dayScope) {
  40. this.setData({ dayScope: _dayScope })
  41. }
  42. this.setData({ source: s, target: t, action: o.action })
  43. let now = new Date();
  44. // 从第2天开始,14天内的课程
  45. let bdd = new Date(now.setDate(now.getDate() + 1));
  46. let bd = util.formatTime(bdd).substring(0, 10).replace(/\//g, '-')
  47. let edd = new Date(now.setDate(now.getDate() + this.data.dayScope));
  48. let ed = util.formatTime(edd).substring(0, 10).replace(/\//g, '-')
  49. this.setData({ beginDate: bd, endDate: ed, list: [] });
  50. this.queryList()
  51. },
  52. /**
  53. * 生命周期函数--监听页面初次渲染完成
  54. */
  55. onReady: function () {
  56. },
  57. /**
  58. * 生命周期函数--监听页面显示
  59. */
  60. onShow: function () {
  61. },
  62. /**
  63. * 生命周期函数--监听页面隐藏
  64. */
  65. onHide: function () {
  66. },
  67. /**
  68. * 生命周期函数--监听页面卸载
  69. */
  70. onUnload: function () {
  71. },
  72. /**
  73. * 页面相关事件处理函数--监听用户下拉动作
  74. */
  75. onPullDownRefresh: function () {
  76. },
  77. /**
  78. * 页面上拉触底事件的处理函数
  79. */
  80. onReachBottom: function () {
  81. },
  82. /**
  83. * 用户点击右上角分享
  84. */
  85. onShareAppMessage: function () {
  86. },
  87. queryList: function () {
  88. const urls = urlDef.urls;
  89. let stu = wx.getStorageSync('student')
  90. // 先查询节假日数据,包含在节假日里面的所有课程,将不显示
  91. util.apiPost(urls.get_holidays + '&q.companyId=' + stu.orgId).then((rs) => {
  92. this.holidays = rs;
  93. this.setData({ holidays: rs })
  94. util.apiPost(urls.get_teacherLeave + '&q.orgId=' + stu.orgId).then((rs) => {
  95. this.setData({ teacherLeaveDates: rs })
  96. this.queryClassList()
  97. });
  98. });
  99. },
  100. queryClassList: function () {
  101. const urls = urlDef.urls;
  102. let stu = wx.getStorageSync('student')
  103. let classItem = [];
  104. let allWeekTask = {};
  105. // 按星期分组
  106. const dayTime = 86400000;
  107. let tbd = new Date(this.data.beginDate);
  108. let ted = new Date(this.data.endDate);
  109. // 算出两个日期一共有多少天;
  110. let days = Math.ceil((ted.getTime() - tbd.getTime()) / dayTime) + 1;
  111. let weeks = Math.ceil(days / 7);
  112. for (let i = 0; i < weeks; i++) {
  113. let tempDate = new Date(this.data.beginDate);
  114. tempDate.setDate(tempDate.getDate() + i * 7);
  115. let weekTask = {};
  116. weekTask.complete = false;
  117. weekTask.beginDate = this.formateDate(tempDate);
  118. tempDate.setDate(tempDate.getDate() + 6);
  119. if (tempDate.getTime() > ted.getTime()) {
  120. tempDate = ted;
  121. }
  122. weekTask.endDate = this.formateDate(tempDate);
  123. weekTask.weekMap = this.initWeek(weekTask);
  124. allWeekTask['week' + i] = weekTask;
  125. }
  126. for (let weekKey in allWeekTask) {
  127. let weekTask = allWeekTask[weekKey];
  128. // 查询
  129. let item = this.data.source
  130. let params = {
  131. 'q.courseId': item.courseId,
  132. 'q.classesId': item.classesId,
  133. 'q.beginDate': weekTask.beginDate,
  134. 'q.endDate': weekTask.endDate,
  135. 'q.classromeId': item.classRoomId,
  136. 'q.studentId': stu.studentId,
  137. 'q.oldTeacherId': item.teacherId,
  138. 'q.kindId': item.kindId,
  139. 'q.level': (item.level ? item.level : ''),
  140. 'q.orgId': stu.orgId
  141. };
  142. util.apiPost(urls.get_switch_course_list, params).then((rs) => {
  143. weekTask.data = rs;
  144. weekTask.complete = true;
  145. weekTask.list = this.filterList(weekTask);
  146. // 每次查询完,检查所有的请求执行完没有
  147. let allTaskOver = true;
  148. for (let wk in allWeekTask) {
  149. if (allWeekTask[wk].complete) {
  150. } else { // 只要有一个没执行完,就都没执行完
  151. allTaskOver = false;
  152. break;
  153. }
  154. }
  155. if (allTaskOver) {
  156. classItem = [];
  157. for (let weekKey in allWeekTask) {
  158. let weekTask = allWeekTask[weekKey];
  159. classItem.push.apply(classItem, weekTask.list);
  160. }
  161. let data = [];
  162. classItem.forEach(item => {
  163. let day = item.newDate;
  164. let o;
  165. data.some(d => {
  166. if (d.day === day) {
  167. o = d;
  168. return true;
  169. }
  170. });
  171. if (!o) {
  172. o = {};
  173. o.day = day;
  174. data.push(o);
  175. }
  176. });
  177. // 按日期排序
  178. data.sort((a, b) => {
  179. return (parseInt(a.day.replace(/-/g, ''), 10) - parseInt(b.day.replace(/-/g, ''), 10));
  180. });
  181. data.forEach(item => {
  182. let items = [];
  183. classItem.forEach(i => {
  184. if (i.newDate === item.day) {
  185. i.bt = i.beginTime
  186. i.et = i.endTime
  187. items.push(i);
  188. }
  189. });
  190. items.sort((a, b) => {
  191. return (parseInt(a.beginTime.replace(/:/g, ''), 10) - parseInt(b.beginTime.replace(/:/g, ''), 10));
  192. });
  193. item.items = items;
  194. item.date = item.day.substring(0, 10)
  195. item.week = this.getWeek(item.date)
  196. });
  197. classItem = data;
  198. this.setData({ list: classItem })
  199. wx.hideLoading()
  200. }
  201. })
  202. }
  203. },
  204. exceptHoliday: function (d) {
  205. let result = true;
  206. this.data.holidays.some(r => {
  207. if (r.holidayDate === d) {
  208. result = false;
  209. return true;
  210. }
  211. });
  212. return result;
  213. },
  214. exceptTeacheLeave: function (d) {
  215. // 老师请假中的课程不显示
  216. let result = true;
  217. this.data.teacherLeaveDates.some(r => {
  218. let leaveBeginTime = this.dateToNumber(r.beginTime);
  219. let leaveEndTime = this.dateToNumber(r.endTime);
  220. let beginTime = this.dateToNumber(d.newDate + d.beginTime);
  221. let endTime = this.dateToNumber(d.newDate + d.endTime);
  222. if (r.teacherId === d.teacherId && ((beginTime >= leaveBeginTime && beginTime <= leaveEndTime) || (endTime >= leaveBeginTime && endTime <= leaveEndTime))) {
  223. result = false;
  224. return true;
  225. }
  226. });
  227. return result;
  228. },
  229. getDay: function (week, weekMap) {
  230. let result = null;
  231. weekMap.some(r => {
  232. if (r.week === week) {
  233. result = r.ymd;
  234. return true;
  235. }
  236. });
  237. return result;
  238. },
  239. formateDate: function (d) {
  240. return d.getFullYear() + '-' + this.addZero(d.getMonth() + 1) + '-' + this.addZero(d.getDate());
  241. },
  242. dateToNumber: function (d) {
  243. if (d) {
  244. return parseInt(d.replace(/-/g, '').replace(/:/g, '').replace(/\s+/g, ''), 10);
  245. } else {
  246. return 0;
  247. }
  248. },
  249. addZero: function (n) {
  250. return (parseInt(n, 10) >= 10 ? '' : '0') + n;
  251. },
  252. initWeek: function (weekTask) {
  253. let weekMap = [];
  254. const bd = new Date(weekTask.beginDate);
  255. const ed = new Date(weekTask.endDate);
  256. while (bd.getTime() <= ed.getTime()) {
  257. weekMap.push({ week: bd.getDay(), ymd: this.formateDate(bd) });
  258. bd.setDate(bd.getDate() + 1);
  259. }
  260. return weekMap;
  261. },
  262. len: function (o) {
  263. return (o == null || o === undefined) ? 0 : o.length;
  264. },
  265. filterList: function (weekTask) {
  266. let items = [];
  267. weekTask.data.forEach((it) => {
  268. if (it.classes) {
  269. it.classes.forEach(c => {
  270. let item = c;
  271. item.classesName = c.name;
  272. item.address = c.address;
  273. item.roomName = c.roomName;
  274. let tempItems = [];
  275. if (this.len(c.beginTime) > 0) {
  276. let bt = c.beginTime.replace('T', ' ').substring(0, 16)
  277. item.newDate = bt.substring(0.10)
  278. item.beginTime = bt.substring(11);
  279. item.endTime = this.addZero(it.endHours) + ':' + this.addZero(it.endMinutes);
  280. tempItems.push(item);
  281. } else { // 没有真实排课
  282. item.beginTime = this.addZero(it.beginHours) + ':' + this.addZero(it.beginMinutes);
  283. item.endTime = this.addZero(it.endHours) + ':' + this.addZero(it.endMinutes);
  284. // 服务端返回的2345671 表示 周一到周日
  285. // 此处是 1234567 表示 周一到周日
  286. let d = this.getDay(it.week - 1, weekTask.weekMap);
  287. if (d != null) {
  288. item.newDate = d;
  289. tempItems.push(item);
  290. }
  291. }
  292. tempItems.forEach(item => {
  293. if (item.classStatus === '1' && this.exceptHoliday(item.newDate) && this.exceptTeacheLeave(item)) { // 未满班的才允许调课 && 不在假节日中的课程
  294. items.push(item);
  295. }
  296. });
  297. });
  298. }
  299. });
  300. return items;
  301. },
  302. getWeek: function (d) {
  303. return '星期' + '天一二三四五六'.charAt(new Date(d).getDay());
  304. }
  305. })