change_course.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // pages/change_course/change_course.js
  2. const { formatNumber } = require('../../utils/util.js')
  3. const { thisMonthLenth } = require('../../utils/changeCourseTime.js')
  4. const app = getApp()
  5. const DATE = new Date()
  6. const yearList = []
  7. const monthList = []
  8. const dayList = []
  9. const nowYear = DATE.getFullYear()
  10. const nowMonth = DATE.getMonth() + 1
  11. const nowDay = DATE.getDate()
  12. const nowDate = `${formatNumber(nowYear)}/${formatNumber(nowMonth)}/${formatNumber(nowDay)}`
  13. for (let i = nowYear; i <= nowYear + 10; i++) {
  14. yearList.push(i)
  15. }
  16. for (let i = 1; i <= 12; i++) {
  17. monthList.push(i)
  18. }
  19. for (let i = 1; i <= 31; i++) {
  20. dayList.push(i)
  21. }
  22. Page({
  23. /**
  24. * 页面的初始数据
  25. */
  26. data: {
  27. isShowModal: false,
  28. yearList,
  29. monthList,
  30. dayList,
  31. dateValue: [0, 0, 0],
  32. adjustReason: '',
  33. oldPlanTime: '',
  34. currentPlanTime: '',
  35. nowYear,
  36. nowMonth,
  37. nowDay,
  38. nowDate,
  39. year: nowYear,
  40. month: nowMonth,
  41. day: nowDay,
  42. weeks: [
  43. {
  44. text: '周一'
  45. },
  46. {
  47. text: '周二'
  48. },
  49. {
  50. text: '周三'
  51. },
  52. {
  53. text: '周四'
  54. },
  55. {
  56. text: '周五'
  57. },
  58. {
  59. text: '周六'
  60. },
  61. {
  62. text: '周日'
  63. }
  64. ],
  65. daysIndex: -1,
  66. days: [],
  67. oldDate: '',
  68. oldTime: '',
  69. newDate: '',
  70. newTime: '',
  71. items: [
  72. {
  73. text: '临时有其他安排',
  74. style: ''
  75. },
  76. {
  77. text: '假期旅游',
  78. style: ''
  79. },
  80. {
  81. text: '学校调课',
  82. style: ''
  83. },
  84. {
  85. text: '考前冲刺',
  86. style: ''
  87. },
  88. {
  89. text: '期末复习',
  90. style: ''
  91. },
  92. {
  93. text: '临时加课',
  94. style: ''
  95. },
  96. {
  97. text: '学生状态不好',
  98. style: ''
  99. },
  100. {
  101. text: '家长要求',
  102. style: ''
  103. },
  104. {
  105. text: '老师个人原因',
  106. style: ''
  107. }
  108. ],
  109. timeValue: [0],
  110. timeList: [
  111. // {
  112. // "startTime": "08:00",
  113. // "endTime": "10:00"
  114. // },
  115. // {
  116. // "startTime": "10:00",
  117. // "endTime": "12:00"
  118. // },
  119. // {
  120. // "startTime": "15:30",
  121. // "endTime": "17:30"
  122. // }
  123. ],
  124. courseList: [
  125. // {
  126. // "date": "2018-09-10",
  127. // "type": 1,
  128. // "courses": [
  129. // {
  130. // "courseId": "12131313132",
  131. // "startTime": "09:00",
  132. // "endTime": "11:00"
  133. // },
  134. // {
  135. // "courseId": "12131313132",
  136. // "startTime": "09:00",
  137. // "endTime": "11:00"
  138. // }
  139. // ]
  140. // },
  141. // {
  142. // "date": "2018-10-01",
  143. // "type": 1,
  144. // "courses": [
  145. // {
  146. // "courseId": "12131313132",
  147. // "startTime": "09:00",
  148. // "endTime": "11:00"
  149. // },
  150. // {
  151. // "courseId": "12131313132",
  152. // "startTime": "09:00",
  153. // "endTime": "11:00"
  154. // }
  155. // ]
  156. // },
  157. // {
  158. // "date": "2018-10-02",
  159. // "type": 2,
  160. // "courses": [
  161. // {
  162. // "courseId": "12131313132",
  163. // "startTime": "09:00",
  164. // "endTime": "11:00"
  165. // },
  166. // {
  167. // "courseId": "12131313132",
  168. // "startTime": "09:00",
  169. // "endTime": "11:00"
  170. // },
  171. // {
  172. // "courseId": "12131313132",
  173. // "startTime": "09:00",
  174. // "endTime": "11:00"
  175. // },
  176. // {
  177. // "courseId": "12131313132",
  178. // "startTime": "09:00",
  179. // "endTime": "11:00"
  180. // },
  181. // {
  182. // "courseId": "12131313132",
  183. // "startTime": "09:00",
  184. // "endTime": "11:00"
  185. // },
  186. // {
  187. // "courseId": "12131313132",
  188. // "startTime": "09:00",
  189. // "endTime": "11:00"
  190. // },
  191. // {
  192. // "courseId": "12131313132",
  193. // "startTime": "09:00",
  194. // "endTime": "11:00"
  195. // },
  196. // {
  197. // "courseId": "12131313132",
  198. // "startTime": "09:00",
  199. // "endTime": "11:00"
  200. // }
  201. // ]
  202. // },
  203. // {
  204. // "date": "2018-10-10",
  205. // "type": 3,
  206. // "courses": [
  207. // {
  208. // "courseId": "12131313132",
  209. // "startTime": "09:00",
  210. // "endTime": "11:00"
  211. // },
  212. // {
  213. // "courseId": "12131313132",
  214. // "startTime": "09:00",
  215. // "endTime": "11:00"
  216. // }
  217. // ]
  218. // },
  219. // {
  220. // "date": "2018-10-15",
  221. // "type": 3,
  222. // "courses": [
  223. // {
  224. // "courseId": "12131313132",
  225. // "startTime": "09:00",
  226. // "endTime": "11:00"
  227. // },
  228. // {
  229. // "courseId": "12131313132",
  230. // "startTime": "09:00",
  231. // "endTime": "11:00"
  232. // }
  233. // ]
  234. // }
  235. ]
  236. },
  237. /**
  238. * 生命周期函数--监听页面加载
  239. */
  240. onLoad: function (options) {
  241. this.setData({ options })
  242. console.log(options)
  243. this.getCourseList()
  244. },
  245. /**
  246. * 生命周期函数--监听页面初次渲染完成
  247. */
  248. onReady: function () {
  249. },
  250. /**
  251. * 生命周期函数--监听页面显示
  252. */
  253. onShow: function () {
  254. },
  255. /**
  256. * 生命周期函数--监听页面隐藏
  257. */
  258. onHide: function () {
  259. },
  260. /**
  261. * 生命周期函数--监听页面卸载
  262. */
  263. onUnload: function () {
  264. },
  265. /**
  266. * 页面相关事件处理函数--监听用户下拉动作
  267. */
  268. onPullDownRefresh: function () {
  269. this.getCourseList(res => {
  270. wx.stopPullDownRefresh()
  271. })
  272. },
  273. /**
  274. * 页面上拉触底事件的处理函数
  275. */
  276. onReachBottom: function () {
  277. },
  278. true:function(){},
  279. /**
  280. * 用户点击右上角分享
  281. */
  282. onShareAppMessage: function () {
  283. if (app.sharePageDefaultCtl) {
  284. return app.sharePageDefaultCtl()
  285. }
  286. },
  287. /**
  288. * 获取所有排课列表
  289. */
  290. getCourseList: function (cb) {
  291. const postData = { fn: this.getCourseList, param: { ...arguments } }
  292. const { year, month, options } = this.data
  293. const time = `${formatNumber(year)}-${formatNumber(month)}-01`
  294. const { bizId } = options
  295. app.post('/api/parents/parents/V2/getStudentCourseData', { bizId, time }, 0, postData).then(res => {
  296. this.setData({ courseList: res.data.list || [] })
  297. this.getCalendar()
  298. cb && cb()
  299. }).catch(res => {
  300. cb && cb()
  301. })
  302. },
  303. /**
  304. * 获取新日期下所有新时间段
  305. */
  306. getTimeList: function () {
  307. const { options, newDate } = this.data
  308. const { teacherId } = options
  309. const queryTime = newDate
  310. const postData = { fn: this.getTimeList, param: { ...arguments } }
  311. app.post('/api/parents/parents/V2/getTeacherCourseTimeDetail', { teacherId, queryTime }, 0, postData).then(res => {
  312. const { list } = res.data
  313. if (list.length > 0) {
  314. this.setData({ timeList: list, timeValue: [0], newTime: list[0]['startTime'] + ':00', currentPlanTime: newDate + ' ' + list[0]['startTime'] + ':00' })
  315. } else {
  316. this.setData({ timeList: [], newTime: '', currentPlanTime: ''})
  317. wx.showToast({
  318. title: '当天无空挡,请重选日期',
  319. icon: 'none'
  320. })
  321. }
  322. })
  323. },
  324. /**
  325. * 获取日历
  326. */
  327. getCalendar: function () {
  328. const { year, month, courseList, nowDate, oldDate } = this.data
  329. const week = new Date(`${year}/${month}/01`).getDay() || 7
  330. const days = []
  331. for (let i = 1; i < week; i++) {
  332. days.push({
  333. text: '',
  334. isActive: 0,
  335. })
  336. }
  337. for (let i = 1; i < thisMonthLenth(year, month) + 1; i++) {
  338. const dateTemp = `${formatNumber(year)}/${formatNumber(month)}/${formatNumber(i)}`
  339. const date = `${formatNumber(year)}-${formatNumber(month)}-${formatNumber(i)}`
  340. const isLess = new Date(dateTemp) < new Date(nowDate)
  341. days.push({
  342. date,
  343. text: formatNumber(i),
  344. isLess,
  345. type: 0,
  346. isActive: 0,
  347. courses: [],
  348. coursesIndex: -1
  349. })
  350. }
  351. let isActive = false
  352. let daysIndex = -1
  353. let date = oldDate
  354. for (let i in days) {
  355. for (let j in courseList) {
  356. if (days[i]['date'] == courseList[j]['date']) {
  357. days[i]['type'] = courseList[j]['type']
  358. days[i]['courses'] = courseList[j]['courses']
  359. if (!isActive && (courseList[j]['type'] == 2 || courseList[j]['type'] == 3)) {
  360. days[i]['isActive'] = 1
  361. daysIndex = i
  362. date = days[i]['date']
  363. isActive = true
  364. }
  365. }
  366. }
  367. }
  368. this.setData({ days, daysIndex, oldDate: date })
  369. },
  370. /**
  371. * 切换年月
  372. */
  373. checkMonthCtl: function (e) {
  374. const { type } = e.currentTarget.dataset
  375. let { year, month } = this.data
  376. if (type == 1) {
  377. if (month < 2) {
  378. month = 12
  379. year--
  380. } else {
  381. month--
  382. }
  383. } else if (type == 2) {
  384. if (month > 11) {
  385. month = 1
  386. year++
  387. } else {
  388. month++
  389. }
  390. }
  391. this.setData({ year, month })
  392. this.getCourseList()
  393. },
  394. /**
  395. * 选择需要调课日期
  396. */
  397. checkDayCtl: function (e) {
  398. const { index } = e.currentTarget.dataset
  399. const { days, daysIndex } = this.data
  400. const { courses, type, isActive, date, text } = days[index]
  401. if (daysIndex == index || isActive) {
  402. return
  403. }
  404. if (!(type == 2 || type == 3)) {
  405. wx.showToast({
  406. title: '只能选择未上过课日期',
  407. icon: 'none'
  408. })
  409. return
  410. }
  411. const temp = {}
  412. for (let i in days) {
  413. if (i == index) {
  414. days[i]['isActive'] = 1
  415. } else {
  416. days[i]['isActive'] = 0
  417. }
  418. }
  419. this.setData({ days, daysIndex: index, oldDate: date, day: text })
  420. },
  421. /**
  422. * 选择需要调课时间段
  423. */
  424. checkTimeCtl: function (e) {
  425. const { index } = e.currentTarget.dataset
  426. const { days, daysIndex, yearList, monthList, dayList, oldDate } = this.data
  427. const { courses, coursesIndex, date } = days[daysIndex]
  428. const { courseId, startTime, endTime } = courses[index]
  429. const dateValue = ((v, a) => {
  430. const arr = []
  431. for (let i in v) {
  432. for (let j in a[i]) {
  433. if (v[i] == a[i][j]) {
  434. arr.push(j)
  435. break
  436. }
  437. }
  438. }
  439. return arr
  440. })(date.split('-'), [yearList, monthList, dayList])
  441. days[daysIndex].coursesIndex = index
  442. this.setData({ days, dateValue, bizId: courseId, oldTime: startTime + ':00', oldPlanTime: oldDate + ' ' + startTime + ':00', newDate: oldDate, isShowModal: true})
  443. this.getTimeList()
  444. },
  445. /**
  446. * 获取新调课日期
  447. */
  448. changeDateCtl: function (e) {
  449. const { value } = e.detail
  450. const { yearList, monthList, dayList, dateValue, nowDate } = this.data
  451. const { date, year, month, day} = ((v, a) => {
  452. const date = [],
  453. year = formatNumber(a[0][v[0]]),
  454. month = formatNumber(a[1][v[1]]),
  455. day = formatNumber(a[2][v[2]])
  456. for (let i in v) {
  457. date.push(formatNumber(a[i][v[i]]))
  458. }
  459. return { date: date.join('-'), year, month, day }
  460. })(value, [yearList, monthList, dayList])
  461. if (thisMonthLenth(year, month) < day) {
  462. wx.showToast({
  463. title: '当前月份没有该天,请重新选择',
  464. icon: 'none'
  465. })
  466. this.setData({ dateValue })
  467. return
  468. }
  469. if (new Date(date.replace(/\-/g, '/')) < new Date(nowDate)) {
  470. wx.showToast({
  471. title: '不能选择今天之前的日期',
  472. icon: 'none'
  473. })
  474. this.setData({ dateValue })
  475. return
  476. }
  477. this.setData({ newDate: date, dateValue: value })
  478. this.getTimeList()
  479. },
  480. /**
  481. * 获取新调课时间段
  482. */
  483. changeTimeCtl: function (e) {
  484. const { value } = e.detail
  485. const { timeList, newDate } = this.data
  486. this.setData({ timeValue: value, newTime: timeList[value[0]]['startTime'] + ':00', currentPlanTime: newDate + ' ' + timeList[value[0]]['startTime'] + ':00'})
  487. },
  488. /**
  489. * 选择调课事由
  490. */
  491. checkReasonCtl: function (e) {
  492. const { index } = e.currentTarget.dataset
  493. let { items, adjustReason } = this.data
  494. if (items[index]['style'] == 'active') {
  495. return
  496. }
  497. for (let i in items) {
  498. if (i == index) {
  499. items[i]['style'] = 'active'
  500. adjustReason = items[i]['text']
  501. } else {
  502. items[i]['style'] = ''
  503. }
  504. }
  505. this.setData({ items, adjustReason })
  506. },
  507. /**
  508. * 确定调课
  509. */
  510. submitCtl: function (e) {
  511. const { type } = e.currentTarget.dataset
  512. this.setData({ isShowModal: false })
  513. if (type == 1) {
  514. const { oldPlanTime, currentPlanTime, oldDate, oldTime, newDate, newTime, adjustReason, bizId } = this.data
  515. if (!newTime) {
  516. wx.showToast({
  517. title: '请选择调课时间段',
  518. icon: 'none'
  519. })
  520. return
  521. }
  522. if (!adjustReason) {
  523. wx.showToast({
  524. title: '请选择调课事由',
  525. icon: 'none'
  526. })
  527. return
  528. }
  529. console.log(JSON.stringify({ bizId, oldPlanTime, adjustReason, currentPlanTime }))
  530. const postData = { fn: this.submitCtl, param: { ...arguments } }
  531. app.post('/api/parents/parents/V2/applyCourseTime', { courseId:bizId, oldPlanTime, adjustReason, currentPlanTime }, 0, postData).then(res => {
  532. wx.showToast({
  533. title: '调课成功'
  534. })
  535. this.getCourseList()
  536. setTimeout(() => {
  537. wx.showModal({
  538. title: '',
  539. content: '是否继续调课?',
  540. success: res => {
  541. if (res.cancel) {
  542. wx.navigateBack({
  543. delta: 1
  544. })
  545. }
  546. }
  547. })
  548. }, 800)
  549. }).catch(res => {
  550. wx.showToast({
  551. title: res.msg,
  552. icon: 'none'
  553. })
  554. })
  555. }
  556. }
  557. })