arrange.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.singleCourseMinutes,
  61. teacherId: state.user.data?.userId
  62. }
  63. }
  64. )
  65. const result = res.data || []
  66. let tempObj = {}
  67. result.forEach((item: any) => {
  68. tempObj[item.date] = item
  69. })
  70. this.calendarList = tempObj
  71. } catch {}
  72. },
  73. onSelectDay(obj: any) {
  74. const result = obj || []
  75. let list = [...createState.selectCourseList]
  76. result.forEach((item: any) => {
  77. const isExist = list.some(
  78. (course: any) => course.startTime === item.startTime
  79. )
  80. !isExist && list.push({ ...item })
  81. })
  82. // 对数组进行排序
  83. list.sort((first: any, second: any) => {
  84. if (first.startTime > second.startTime) return 1
  85. if (first.startTime < second.startTime) return -1
  86. return 0
  87. })
  88. createState.selectCourseList = [...list]
  89. },
  90. onCloseTag(item: any) {
  91. Dialog.confirm({
  92. title: '提示',
  93. message: '您是否要删除该选择的课程?',
  94. confirmButtonColor: 'var(--van-primary)'
  95. }).then(() => {
  96. const index = createState.selectCourseList.findIndex(
  97. (course: any) => course.startTime === item.startTime
  98. )
  99. createState.selectCourseList.splice(index, 1)
  100. })
  101. },
  102. async onSubmit() {
  103. if (createState.selectCourseList.length <= 0) {
  104. Toast('请选择课程时间')
  105. return
  106. }
  107. if (createState.selectCourseList.length < createState.live.courseNum) {
  108. this.selectStatus = true
  109. return
  110. }
  111. await this._lookCourse()
  112. },
  113. async _lookCourse(callBack?: Function) {
  114. try {
  115. let times = [] as any
  116. createState.selectCourseList.forEach((item: any) => {
  117. times.push({
  118. startTime: item.startTime,
  119. endTime: item.endTime
  120. })
  121. })
  122. const res = await request.post(
  123. '/api-teacher/courseGroup/lockCourseToCache',
  124. {
  125. data: {
  126. courseNum: createState.live.courseNum,
  127. courseType: 'LIVE',
  128. loop: this.selectType === 'noEnough' ? 1 : 0,
  129. teacherId: state.user.data?.userId,
  130. timeList: [...times]
  131. }
  132. }
  133. )
  134. const result = res.data || []
  135. result.forEach((item: any, index: number) => {
  136. createState.live.coursePlanList[index] = {
  137. ...createState.live.coursePlanList[index],
  138. startTime: item.startTime,
  139. endTime: item.endTime,
  140. classNum: index + 1
  141. }
  142. })
  143. createState.coursePlanStatus = true
  144. this.selectStatus = true
  145. callBack && callBack()
  146. } catch {}
  147. },
  148. async _unLookCourse() {
  149. try {
  150. await request.get('/api-teacher/courseGroup/unlockCourseToCache', {
  151. params: {
  152. teacherId: state.user.data?.userId
  153. }
  154. })
  155. this.selectStatus = false
  156. createState.live.coursePlanList.forEach((item: any) => {
  157. item.startTime = ''
  158. item.endTime = ''
  159. })
  160. } catch {}
  161. },
  162. async onReset() {
  163. if (this.selectType === 'noEnough') {
  164. this.selectStatus = false
  165. } else if (this.selectType === 'enough') {
  166. await this._unLookCourse()
  167. }
  168. createState.coursePlanStatus = false
  169. },
  170. async onSure() {
  171. await this._lookCourse(() => {
  172. this.selectStatus = false
  173. createState.active = 4
  174. })
  175. }
  176. },
  177. render() {
  178. return (
  179. <div class={styles.arrange}>
  180. <Calendar
  181. selectList={createState.selectCourseList}
  182. list={this.calendarList}
  183. maxDays={createState.live.courseNum || 0}
  184. nextMonth={(date: Date) => this.getList(date)}
  185. prevMonth={(date: Date) => this.getList(date)}
  186. selectDay={this.onSelectDay}
  187. />
  188. <Cell
  189. class={[styles.arrangeCell, 'mb12']}
  190. v-slots={{
  191. title: () => (
  192. <div class={styles.rTitle}>
  193. <span>已选择课程时间</span>
  194. </div>
  195. ),
  196. label: () => (
  197. <div class={styles.rTag}>
  198. {this.showSelectList.map((item: any) => (
  199. <>
  200. <Tag
  201. plain
  202. round
  203. closeable
  204. size="large"
  205. type="primary"
  206. class={styles.tag}
  207. onClose={() => this.onCloseTag(item)}
  208. >
  209. {item.title}
  210. </Tag>
  211. <br />
  212. </>
  213. ))}
  214. </div>
  215. )
  216. }}
  217. ></Cell>
  218. <Sticky offsetBottom={0} position="bottom">
  219. <div class={['btnGroup', 'btnMore']}>
  220. <Button
  221. block
  222. round
  223. type="primary"
  224. plain
  225. onClick={() => {
  226. createState.active = 2
  227. }}
  228. >
  229. 上一步
  230. </Button>
  231. <Button block round type="primary" onClick={this.onSubmit}>
  232. 下一步
  233. </Button>
  234. </div>
  235. </Sticky>
  236. <Popup show={this.selectStatus} class={styles.selectPopup}>
  237. <div class={styles.selectContainer}>
  238. <div class={styles.rTitle}>
  239. <span>提示</span>
  240. </div>
  241. <div class={styles.selectPopupContent}>
  242. <p class={styles.desc}>
  243. {this.selectType === 'noEnough'
  244. ? '您所选择的上课时间未达到您输入的课时数,系统根据已选时间将自动按周顺延排课。'
  245. : '您已选择以下上课时间,请确认后点击确认按'}
  246. </p>
  247. {createState.live.coursePlanList &&
  248. createState.live.coursePlanList.length > 0 &&
  249. this.selectType === 'enough' && (
  250. <p class={styles.times}>
  251. {createState.live.coursePlanList.map((item: any) => (
  252. <span>
  253. {dayjs(item.startTime || new Date()).format(
  254. 'YYYY-MM-DD'
  255. )}{' '}
  256. {dayjs(item.startTime || new Date()).format('HH:mm')}~
  257. {dayjs(item.endTime || new Date()).format('HH:mm')}
  258. </span>
  259. ))}
  260. </p>
  261. )}
  262. </div>
  263. <div class={styles.selectBtn}>
  264. <Button
  265. class={styles.btn}
  266. type="primary"
  267. round
  268. block
  269. plain
  270. onClick={this.onReset}
  271. >
  272. {this.selectType === 'noEnough' ? '继续选择' : '重新选择'}
  273. </Button>
  274. <Button
  275. class={styles.btn}
  276. type="primary"
  277. round
  278. block
  279. onClick={this.onSure}
  280. >
  281. 确认
  282. </Button>
  283. </div>
  284. </div>
  285. </Popup>
  286. </div>
  287. )
  288. }
  289. })