targetList.js 9.1 KB

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