targetList.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. //获取页面栈
  231. let pages = getCurrentPages();
  232. //获取所需页面
  233. let prevPage = pages[pages.length -2];//上一页
  234. that.data.item.leaveFlag = false
  235. prevPage.setData({
  236. newItem: that.data.item
  237. });
  238. wx.navigateBack({
  239. delta: 1,
  240. })
  241. }
  242. })
  243. }
  244. }
  245. })
  246. }
  247. },
  248. exceptHoliday: function (d) {
  249. let result = true;
  250. this.data.holidays.some(r => {
  251. if (r.holidayDate === d) {
  252. result = false;
  253. return true;
  254. }
  255. });
  256. return result;
  257. },
  258. exceptTeacheLeave: function (d) {
  259. // 老师请假中的课程不显示
  260. let result = true;
  261. this.data.teacherLeaveDates.some(r => {
  262. let leaveBeginTime = this.dateToNumber(r.beginTime);
  263. let leaveEndTime = this.dateToNumber(r.endTime);
  264. let beginTime = this.dateToNumber(d.newDate + d.beginTime);
  265. let endTime = this.dateToNumber(d.newDate + d.endTime);
  266. if (r.teacherId === d.teacherId && ((beginTime >= leaveBeginTime && beginTime <= leaveEndTime) || (endTime >= leaveBeginTime && endTime <= leaveEndTime))) {
  267. result = false;
  268. return true;
  269. }
  270. });
  271. return result;
  272. },
  273. getDay: function (week, weekMap) {
  274. let result = null;
  275. weekMap.some(r => {
  276. if (r.week === week) {
  277. result = r.ymd;
  278. return true;
  279. }
  280. });
  281. return result;
  282. },
  283. formateDate: function (d) {
  284. return d.getFullYear() + '-' + this.addZero(d.getMonth() + 1) + '-' + this.addZero(d.getDate());
  285. },
  286. dateToNumber: function (d) {
  287. if (d) {
  288. return parseInt(d.replace(/-/g, '').replace(/:/g, '').replace(/\s+/g, ''), 10);
  289. } else {
  290. return 0;
  291. }
  292. },
  293. addZero: function (n) {
  294. return (parseInt(n, 10) >= 10 ? '' : '0') + n;
  295. },
  296. initWeek: function (weekTask) {
  297. let weekMap = [];
  298. const bd = new Date(weekTask.beginDate);
  299. const ed = new Date(weekTask.endDate);
  300. while (bd.getTime() <= ed.getTime()) {
  301. weekMap.push({ week: bd.getDay(), ymd: this.formateDate(bd) });
  302. bd.setDate(bd.getDate() + 1);
  303. }
  304. return weekMap;
  305. },
  306. len: function (o) {
  307. return (o == null || o === undefined) ? 0 : o.length;
  308. },
  309. filterList: function (weekTask) {
  310. let items = [];
  311. weekTask.data.forEach((it) => {
  312. if (it.classes) {
  313. it.classes.forEach(c => {
  314. let item = c;
  315. item.classesName = c.name;
  316. item.address = c.address;
  317. item.roomName = c.roomName;
  318. let tempItems = [];
  319. if (this.len(c.beginTime) > 0) {
  320. let bt = c.beginTime.replace('T', ' ').substring(0, 16)
  321. item.newDate = bt.substring(0, 10)
  322. item.beginTime = bt.substring(11);
  323. item.endTime = this.addZero(it.endHours) + ':' + this.addZero(it.endMinutes);
  324. tempItems.push(item);
  325. } else { // 没有真实排课
  326. item.beginTime = this.addZero(it.beginHours) + ':' + this.addZero(it.beginMinutes);
  327. item.endTime = this.addZero(it.endHours) + ':' + this.addZero(it.endMinutes);
  328. // 服务端返回的2345671 表示 周一到周日
  329. // 此处是 1234567 表示 周一到周日
  330. let d = this.getDay(it.week - 1, weekTask.weekMap);
  331. if (d != null) {
  332. item.newDate = d;
  333. tempItems.push(item);
  334. }
  335. }
  336. tempItems.forEach(item => {
  337. if (item.classStatus === '1' && this.exceptHoliday(item.newDate) && this.exceptTeacheLeave(item)) { // 未满班的才允许调课 && 不在假节日中的课程
  338. items.push(item);
  339. }
  340. });
  341. });
  342. }
  343. });
  344. return items;
  345. },
  346. getWeek: function (d) {
  347. return '星期' + '天一二三四五六'.charAt(new Date(d).getDay());
  348. }
  349. })