arrange.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import Calendar from '@/business-components/calendar'
  2. import request from '@/helpers/request'
  3. import { Button, Cell, Dialog, Popup, Sticky, Tag, Toast } from 'vant'
  4. import { defineComponent } from 'vue'
  5. import styles from './arrange.module.less'
  6. import dayjs from 'dayjs'
  7. import { createState } from './createState'
  8. import { state } from '@/state'
  9. import { getWeekCh } from '@/helpers/utils'
  10. export default defineComponent({
  11. name: 'arrange',
  12. data() {
  13. return {
  14. selectStatus: false,
  15. calendarList: {}
  16. }
  17. },
  18. computed: {
  19. showSelectList() {
  20. let list = [...createState.selectCourseList]
  21. list.forEach((item: any) => {
  22. item.title =
  23. dayjs(item.startTime).format('YYYY-MM-DD') +
  24. ' ' +
  25. getWeekCh(dayjs(item.startTime).day()) +
  26. ' ' +
  27. item.start +
  28. '~' +
  29. item.end
  30. })
  31. return list
  32. },
  33. selectType() {
  34. // 循环次数是否足够
  35. return createState.selectCourseList.length < createState.live.courseNum
  36. ? 'noEnough'
  37. : 'enough'
  38. }
  39. },
  40. async mounted() {
  41. const initDate = dayjs().add(1, 'day').toDate()
  42. await this.getList(initDate)
  43. if (createState.coursePlanStatus) {
  44. this.selectStatus = true
  45. }
  46. },
  47. methods: {
  48. async getList(date?: Date) {
  49. let params = {
  50. day: dayjs(date || new Date()).format('DD'),
  51. month: dayjs(date || new Date()).format('MM'),
  52. year: dayjs(date || new Date()).format('YYYY')
  53. }
  54. try {
  55. let res = await request.post(
  56. '/api-teacher/courseSchedule/createLiveCourseCalendar',
  57. {
  58. data: {
  59. ...params,
  60. singleCourseMinutes: createState.live.singleMins,
  61. freeCourseMinutes: createState.live.freeMinutes,
  62. teacherId: state.user.data?.userId
  63. }
  64. }
  65. )
  66. const result = res.data || []
  67. let tempObj = {}
  68. result.forEach((item: any) => {
  69. tempObj[item.date] = item
  70. })
  71. this.calendarList = tempObj
  72. } catch {}
  73. },
  74. onSelectDay(obj: any) {
  75. const result = obj || []
  76. let list = [...createState.selectCourseList]
  77. result.forEach((item: any) => {
  78. const isExist = list.some(
  79. (course: any) => course.startTime === item.startTime
  80. )
  81. !isExist && list.push({ ...item })
  82. })
  83. // 去掉不在
  84. list.forEach((item: any) => {
  85. const isExist = result.some(
  86. (course: any) => course.startTime === item.startTime
  87. )
  88. !isExist && list.splice(list.indexOf(item), 1)
  89. })
  90. // 对数组进行排序
  91. list.sort((first: any, second: any) => {
  92. if (first.startTime > second.startTime) return 1
  93. if (first.startTime < second.startTime) return -1
  94. return 0
  95. })
  96. createState.selectCourseList = [...list]
  97. },
  98. onCloseTag(item: any) {
  99. Dialog.confirm({
  100. title: '提示',
  101. message: '您是否要删除该选择的课程?',
  102. confirmButtonColor: 'var(--van-primary)'
  103. }).then(() => {
  104. const index = createState.selectCourseList.findIndex(
  105. (course: any) => course.startTime === item.startTime
  106. )
  107. createState.selectCourseList.splice(index, 1)
  108. })
  109. },
  110. async onSubmit() {
  111. if (createState.selectCourseList.length <= 0) {
  112. Toast('请选择课程时间')
  113. return
  114. }
  115. if (createState.selectCourseList.length < createState.live.courseNum) {
  116. this.selectStatus = true
  117. return
  118. }
  119. await this._lookCourse()
  120. },
  121. async _lookCourse(callBack?: Function) {
  122. try {
  123. let times = [] as any
  124. createState.selectCourseList.forEach((item: any) => {
  125. times.push({
  126. startTime: item.startTime,
  127. endTime: item.endTime
  128. })
  129. })
  130. const res = await request.post(
  131. '/api-teacher/courseGroup/lockCourseToCache',
  132. {
  133. data: {
  134. courseNum: createState.live.courseNum,
  135. courseType: 'LIVE',
  136. loop: this.selectType === 'noEnough' ? 1 : 0,
  137. teacherId: state.user.data?.userId,
  138. timeList: [...times]
  139. }
  140. }
  141. )
  142. const result = res.data || []
  143. result.forEach((item: any, index: number) => {
  144. createState.live.coursePlanList[index] = {
  145. ...createState.live.coursePlanList[index],
  146. startTime: item.startTime,
  147. endTime: item.endTime,
  148. classNum: index + 1
  149. }
  150. })
  151. createState.coursePlanStatus = true
  152. this.selectStatus = true
  153. callBack && callBack()
  154. } catch {}
  155. },
  156. async _unLookCourse() {
  157. try {
  158. await request.get('/api-teacher/courseGroup/unlockCourseToCache', {
  159. params: {
  160. teacherId: state.user.data?.userId
  161. }
  162. })
  163. this.selectStatus = false
  164. createState.live.coursePlanList.forEach((item: any) => {
  165. item.startTime = ''
  166. item.endTime = ''
  167. })
  168. } catch {}
  169. },
  170. async onReset() {
  171. // 是否有锁课状态 或 是锁课类型的
  172. if (createState.coursePlanStatus || this.selectType === 'enough') {
  173. await this._unLookCourse()
  174. } else if (this.selectType === 'noEnough') {
  175. this.selectStatus = false
  176. }
  177. createState.coursePlanStatus = false
  178. },
  179. async onSure() {
  180. const status = createState.coursePlanStatus
  181. await this._lookCourse(() => {
  182. if (status) {
  183. this.selectStatus = false
  184. createState.active = 4
  185. }
  186. })
  187. }
  188. },
  189. render() {
  190. return (
  191. <div class={styles.arrange}>
  192. <Calendar
  193. selectList={createState.selectCourseList}
  194. list={this.calendarList}
  195. maxDays={createState.live.courseNum || 0}
  196. nextMonth={(date: Date) => this.getList(date)}
  197. prevMonth={(date: Date) => this.getList(date)}
  198. selectDay={this.onSelectDay}
  199. />
  200. <Cell
  201. class={[styles.arrangeCell, 'mb12']}
  202. v-slots={{
  203. title: () => (
  204. <div class={styles.rTitle}>
  205. <span>已选择课程时间</span>
  206. </div>
  207. ),
  208. label: () => (
  209. <div class={styles.rTag}>
  210. {this.showSelectList.map((item: any) => (
  211. <>
  212. <Tag
  213. plain
  214. round
  215. closeable
  216. size="large"
  217. type="primary"
  218. class={styles.tag}
  219. onClose={() => this.onCloseTag(item)}
  220. >
  221. {item.title}
  222. </Tag>
  223. <br />
  224. </>
  225. ))}
  226. </div>
  227. )
  228. }}
  229. ></Cell>
  230. <Sticky offsetBottom={0} position="bottom">
  231. <div class={['btnGroup', 'btnMore']}>
  232. <Button
  233. block
  234. round
  235. type="primary"
  236. plain
  237. onClick={() => {
  238. createState.active = 2
  239. // 重置选择的课次
  240. createState.selectCourseList = []
  241. }}
  242. >
  243. 上一步
  244. </Button>
  245. <Button block round type="primary" onClick={this.onSubmit}>
  246. 下一步
  247. </Button>
  248. </div>
  249. </Sticky>
  250. <Popup show={this.selectStatus} class={styles.selectPopup}>
  251. <div class={styles.selectContainer}>
  252. <div class={styles.rTitle}>
  253. <span>提示</span>
  254. </div>
  255. <div class={styles.selectPopupContent}>
  256. <p class={styles.desc}>
  257. {this.selectType === 'noEnough' && !createState.coursePlanStatus
  258. ? '您所选择的上课时间未达到您输入的课时数,系统根据已选时间将自动按周顺延排课。'
  259. : '您已选择以下上课时间段,时间段会暂时锁定,锁定期间学员不可购买该时间段课程。'}
  260. </p>
  261. {createState.live.coursePlanList &&
  262. createState.live.coursePlanList.length > 0 &&
  263. createState.coursePlanStatus && (
  264. <p class={styles.times}>
  265. {createState.live.coursePlanList.map((item: any) => (
  266. <span>
  267. {dayjs(item.startTime || new Date()).format(
  268. 'YYYY-MM-DD'
  269. )}{' '}
  270. {dayjs(item.startTime || new Date()).format('HH:mm')}~
  271. {dayjs(item.endTime || new Date()).format('HH:mm')}
  272. </span>
  273. ))}
  274. </p>
  275. )}
  276. </div>
  277. <div class={styles.selectBtn}>
  278. <Button
  279. class={styles.btn}
  280. type="primary"
  281. round
  282. block
  283. plain
  284. onClick={this.onReset}
  285. >
  286. {this.selectType === 'noEnough' ? '继续选择' : '重新选择'}
  287. </Button>
  288. <Button
  289. class={styles.btn}
  290. type="primary"
  291. round
  292. block
  293. onClick={this.onSure}
  294. >
  295. 确认
  296. </Button>
  297. </div>
  298. </div>
  299. </Popup>
  300. </div>
  301. )
  302. }
  303. })