targetList.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. if (o.action == '3') {
  50. // 根据curDate 算出前后七天, 其实时间晚于当天,已次日为其实时间
  51. // console.log('item.queryDate : ' + item.curDate);
  52. const temDate = new Date(s.attenceDate.replace(/-/g, '/') + ' 00:00:00')
  53. temDate.setDate(temDate.getDate() + 7); // 一周后
  54. ed = temDate.getFullYear() + '-' + this.addZero(temDate.getMonth() + 1) + '-' + this.addZero(temDate.getDate());
  55. // console.log('ed : ' + ed);
  56. temDate.setDate(temDate.getDate() - 14); // 往前推2周
  57. bd = temDate.getFullYear() + '-' + this.addZero(temDate.getMonth() + 1) + '-' + this.addZero(temDate.getDate());
  58. // console.log('bd : ' + bd);
  59. // 如果早于当前日期
  60. let nextDay = util.formatTime(new Date(new Date().getTime() + 86400000)).substring(0, 10).replace(/\//g, '-')
  61. // console.log('nextDay : ' + nextDay);
  62. if (parseInt(bd.replace(/-/g, '')) < parseInt(nextDay.replace(/-/g, ''))) {
  63. bd = nextDay;
  64. }
  65. }
  66. this.setData({ beginDate: bd, endDate: ed, list: [] });
  67. this.queryList()
  68. },
  69. addZero: function (n) {
  70. return (n < 10 ? '0' : '') + n;
  71. },
  72. /**
  73. * 生命周期函数--监听页面初次渲染完成
  74. */
  75. onReady: function () {
  76. },
  77. /**
  78. * 生命周期函数--监听页面显示
  79. */
  80. onShow: function () {
  81. },
  82. /**
  83. * 生命周期函数--监听页面隐藏
  84. */
  85. onHide: function () {
  86. },
  87. /**
  88. * 生命周期函数--监听页面卸载
  89. */
  90. onUnload: function () {
  91. },
  92. /**
  93. * 页面相关事件处理函数--监听用户下拉动作
  94. */
  95. onPullDownRefresh: function () {
  96. },
  97. /**
  98. * 页面上拉触底事件的处理函数
  99. */
  100. onReachBottom: function () {
  101. },
  102. /**
  103. * 用户点击右上角分享
  104. */
  105. onShareAppMessage: function () {
  106. },
  107. queryList: function () {
  108. const urls = urlDef.urls;
  109. let stu = wx.getStorageSync('student')
  110. // 先查询节假日数据,包含在节假日里面的所有课程,将不显示
  111. util.apiPost(urls.get_holidays + '&q.companyId=' + stu.orgId).then((rs) => {
  112. this.holidays = rs;
  113. this.setData({ holidays: rs })
  114. util.apiPost(urls.get_teacherLeave + '&q.orgId=' + stu.orgId).then((rs) => {
  115. this.setData({ teacherLeaveDates: rs })
  116. this.queryClassList()
  117. });
  118. });
  119. },
  120. queryClassList: function () {
  121. const urls = urlDef.urls;
  122. let stu = wx.getStorageSync('student')
  123. let classItem = [];
  124. let allWeekTask = {};
  125. // 按星期分组
  126. const dayTime = 86400000;
  127. let tbd = new Date(this.data.beginDate);
  128. let ted = new Date(this.data.endDate);
  129. // 算出两个日期一共有多少天;
  130. let days = Math.ceil((ted.getTime() - tbd.getTime()) / dayTime) + 1;
  131. let weeks = Math.ceil(days / 7);
  132. for (let i = 0; i < weeks; i++) {
  133. let tempDate = new Date(this.data.beginDate);
  134. tempDate.setDate(tempDate.getDate() + i * 7);
  135. let weekTask = {};
  136. weekTask.complete = false;
  137. weekTask.beginDate = this.formateDate(tempDate);
  138. tempDate.setDate(tempDate.getDate() + 6);
  139. if (tempDate.getTime() > ted.getTime()) {
  140. tempDate = ted;
  141. }
  142. weekTask.endDate = this.formateDate(tempDate);
  143. weekTask.weekMap = this.initWeek(weekTask);
  144. allWeekTask['week' + i] = weekTask;
  145. }
  146. for (let weekKey in allWeekTask) {
  147. let weekTask = allWeekTask[weekKey];
  148. // 查询
  149. let item = this.data.source
  150. let params = {
  151. 'q.courseId': item.courseId,
  152. 'q.classesId': item.classesId,
  153. 'q.beginDate': weekTask.beginDate,
  154. 'q.endDate': weekTask.endDate,
  155. 'q.classromeId': item.classRoomId,
  156. 'q.studentId': stu.studentId,
  157. 'q.oldTeacherId': item.teacherId,
  158. 'q.kindId': item.kindId,
  159. 'q.level': (item.level ? item.level : ''),
  160. 'q.orgId': stu.orgId
  161. };
  162. util.apiPost(urls.get_switch_course_list, params).then((rs) => {
  163. weekTask.data = rs;
  164. weekTask.complete = true;
  165. weekTask.list = this.filterList(weekTask);
  166. // 每次查询完,检查所有的请求执行完没有
  167. let allTaskOver = true;
  168. for (let wk in allWeekTask) {
  169. if (allWeekTask[wk].complete) {
  170. } else { // 只要有一个没执行完,就都没执行完
  171. allTaskOver = false;
  172. break;
  173. }
  174. }
  175. if (allTaskOver) {
  176. classItem = [];
  177. for (let weekKey in allWeekTask) {
  178. let weekTask = allWeekTask[weekKey];
  179. classItem.push.apply(classItem, weekTask.list);
  180. }
  181. let data = [];
  182. classItem.forEach(item => {
  183. let day = item.newDate;
  184. let o;
  185. data.some(d => {
  186. if (d.day === day) {
  187. o = d;
  188. return true;
  189. }
  190. });
  191. if (!o) {
  192. o = {};
  193. o.day = day;
  194. data.push(o);
  195. }
  196. });
  197. // 按日期排序
  198. data.sort((a, b) => {
  199. return (parseInt(a.day.replace(/-/g, ''), 10) - parseInt(b.day.replace(/-/g, ''), 10));
  200. });
  201. data.forEach(item => {
  202. let items = [];
  203. classItem.forEach(i => {
  204. if (i.newDate === item.day) {
  205. i.bt = i.beginTime
  206. i.et = i.endTime
  207. items.push(i);
  208. }
  209. });
  210. items.sort((a, b) => {
  211. return (parseInt(a.beginTime.replace(/:/g, ''), 10) - parseInt(b.beginTime.replace(/:/g, ''), 10));
  212. });
  213. item.items = items;
  214. item.date = item.day.substring(0, 10)
  215. item.week = this.getWeek(item.date)
  216. });
  217. classItem = data;
  218. this.setData({ list: classItem })
  219. wx.hideLoading()
  220. if (classItem.length <= 0) {
  221. wx.showModal({
  222. title: '提示',
  223. content: '暂无目标课程',
  224. showCancel: false, //是否显示取消按钮
  225. cancelText: "否", //默认是“取消”
  226. cancelColor: '#999999', //取消文字的颜色
  227. confirmText: "确定", //默认是“确定”
  228. // confirmColor: 'skyblue', //确定文字的颜色
  229. success() {
  230. wx.navigateBack({
  231. delta: 1,
  232. })
  233. }
  234. })
  235. }
  236. }
  237. })
  238. }
  239. },
  240. exceptHoliday: function (d) {
  241. let result = true;
  242. this.data.holidays.some(r => {
  243. if (r.holidayDate === d) {
  244. result = false;
  245. return true;
  246. }
  247. });
  248. return result;
  249. },
  250. exceptTeacheLeave: function (d) {
  251. // 老师请假中的课程不显示
  252. let result = true;
  253. this.data.teacherLeaveDates.some(r => {
  254. let leaveBeginTime = this.dateToNumber(r.beginTime);
  255. let leaveEndTime = this.dateToNumber(r.endTime);
  256. let beginTime = this.dateToNumber(d.newDate + d.beginTime);
  257. let endTime = this.dateToNumber(d.newDate + d.endTime);
  258. if (r.teacherId === d.teacherId && ((beginTime >= leaveBeginTime && beginTime <= leaveEndTime) || (endTime >= leaveBeginTime && endTime <= leaveEndTime))) {
  259. result = false;
  260. return true;
  261. }
  262. });
  263. return result;
  264. },
  265. getDay: function (week, weekMap) {
  266. let result = null;
  267. weekMap.some(r => {
  268. if (r.week === week) {
  269. result = r.ymd;
  270. return true;
  271. }
  272. });
  273. return result;
  274. },
  275. formateDate: function (d) {
  276. return d.getFullYear() + '-' + this.addZero(d.getMonth() + 1) + '-' + this.addZero(d.getDate());
  277. },
  278. dateToNumber: function (d) {
  279. if (d) {
  280. return parseInt(d.replace(/-/g, '').replace(/:/g, '').replace(/\s+/g, ''), 10);
  281. } else {
  282. return 0;
  283. }
  284. },
  285. addZero: function (n) {
  286. return (parseInt(n, 10) >= 10 ? '' : '0') + n;
  287. },
  288. initWeek: function (weekTask) {
  289. let weekMap = [];
  290. const bd = new Date(weekTask.beginDate);
  291. const ed = new Date(weekTask.endDate);
  292. while (bd.getTime() <= ed.getTime()) {
  293. weekMap.push({ week: bd.getDay(), ymd: this.formateDate(bd) });
  294. bd.setDate(bd.getDate() + 1);
  295. }
  296. return weekMap;
  297. },
  298. len: function (o) {
  299. return (o == null || o === undefined) ? 0 : o.length;
  300. },
  301. filterList: function (weekTask) {
  302. let items = [];
  303. weekTask.data.forEach((it) => {
  304. if (it.classes) {
  305. it.classes.forEach(c => {
  306. let item = c;
  307. item.classesName = c.name;
  308. item.address = c.address;
  309. item.roomName = c.roomName;
  310. let tempItems = [];
  311. if (this.len(c.beginTime) > 0) {
  312. let bt = c.beginTime.replace('T', ' ').substring(0, 16)
  313. item.newDate = bt.substring(0, 10)
  314. item.beginTime = bt.substring(11);
  315. item.endTime = this.addZero(it.endHours) + ':' + this.addZero(it.endMinutes);
  316. tempItems.push(item);
  317. } else { // 没有真实排课
  318. item.beginTime = this.addZero(it.beginHours) + ':' + this.addZero(it.beginMinutes);
  319. item.endTime = this.addZero(it.endHours) + ':' + this.addZero(it.endMinutes);
  320. // 服务端返回的2345671 表示 周一到周日
  321. // 此处是 1234567 表示 周一到周日
  322. let d = this.getDay(it.week - 1, weekTask.weekMap);
  323. if (d != null) {
  324. item.newDate = d;
  325. tempItems.push(item);
  326. }
  327. }
  328. tempItems.forEach(item => {
  329. if (item.classStatus === '1' && this.exceptHoliday(item.newDate) && this.exceptTeacheLeave(item)) { // 未满班的才允许调课 && 不在假节日中的课程
  330. items.push(item);
  331. }
  332. });
  333. });
  334. }
  335. });
  336. return items;
  337. },
  338. getWeek: function (d) {
  339. return '星期' + '天一二三四五六'.charAt(new Date(d).getDay());
  340. }
  341. })