index.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import OHeader from '@/components/o-header'
  2. import item from '@/student/coupons/item'
  3. import dayjs from 'dayjs'
  4. // import isBetween from 'dayjs/plugin/isBetween'
  5. // dayjs.extend(isBetween)
  6. import { Button, Cell, CellGroup, Popup, showToast, Sticky, TimePicker } from 'vant'
  7. import { defineComponent, onMounted, reactive, watch } from 'vue'
  8. import styles from './index.module.less'
  9. export default defineComponent({
  10. name: 'timer',
  11. props: {
  12. timerList: {
  13. type: Object,
  14. default: () => {}
  15. },
  16. times: {
  17. // 默认时长
  18. type: Number,
  19. default: 120
  20. }
  21. },
  22. emits: ['close', 'confirm'],
  23. setup(props, { slots, attrs, emit }) {
  24. const defaultTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
  25. const state = reactive({
  26. calendarDate: null as any,
  27. selectTimeStatus: false,
  28. selectTime: null as any,
  29. useTimer: [] as any, // 可排课时间段
  30. useTimerFormat: [] as any,
  31. usedTimer: [] as any, // 不可排课时间段
  32. minMinute: 0,
  33. maxMinute: 59
  34. })
  35. const onFormatter = (type: any, option: any) => {
  36. if (type === 'hour') {
  37. option.text += '时'
  38. }
  39. if (type === 'minute') {
  40. option.text += '分'
  41. }
  42. return option
  43. }
  44. const onFilter = (type: any, option: any) => {
  45. // 时间
  46. if (type === 'hour') {
  47. const hour: any = []
  48. option.forEach((o: any) => {
  49. state.useTimerFormat.forEach((time: any) => {
  50. if (
  51. o.value >= time.startHour &&
  52. o.value <= time.endHour &&
  53. o.value != hour[hour.length - 1]?.value
  54. ) {
  55. hour.push(o)
  56. }
  57. })
  58. })
  59. return hour
  60. }
  61. return option
  62. }
  63. // 时间切换时
  64. // [6:30, 12:00] [15:00, 18:00]
  65. const onChange = (val: any) => {
  66. // 判断是否选择小时
  67. if (val.columnIndex === 1) return
  68. // 选择时间
  69. const selectHour = Number(val.selectedValues[0])
  70. let minute = 0 // 获取开始的分钟数
  71. state.useTimerFormat.forEach((item: any) => {
  72. if (selectHour === item.startHour) {
  73. minute = item.startMinute
  74. } else if (selectHour === item.endHour) {
  75. minute = item.endMinute
  76. }
  77. })
  78. state.minMinute = minute
  79. state.maxMinute = 59
  80. }
  81. //
  82. const onFormatTimer = (val: Array<any>) => {
  83. const format: any = []
  84. val.forEach((item: any) => {
  85. format.push({
  86. startHour: Number(dayjs(item.startTime).format('HH')),
  87. startMinute: Number(dayjs(item.startTime).format('mm')),
  88. endHour: Number(dayjs(item.endTime).format('HH')),
  89. endMinute: Number(dayjs(item.endTime).format('mm'))
  90. })
  91. })
  92. return format
  93. }
  94. // 确认时间
  95. const onConfirm = (val: any) => {
  96. // console.log(val, 'val')
  97. const selectedValues = val.selectedValues
  98. const tempDate = dayjs(state.calendarDate)
  99. .hour(selectedValues[0])
  100. .minute(selectedValues[1])
  101. .second(0)
  102. // console.log(dayjs(tempDate).format('YYYY-MM-DD HH:mm:ss'), 'first', dayjs(tempDate).minute())
  103. // 时间加上每节课的时间,
  104. const lastDate = dayjs(tempDate).minute(props.times + dayjs(tempDate).minute())
  105. // console.log(dayjs(lastDate).format('YYYY-MM-DD HH:mm:ss'), 'second')
  106. // console.log(
  107. // dayjs(tempDate).format('YYYY-MM-DD HH:mm:ss'),
  108. // tempDate.toDate(),
  109. // 'second tempDate',
  110. // state.useTimer
  111. // )
  112. let isActive = false
  113. state.useTimer.forEach((item: any) => {
  114. // if (dayjs(lastDate).isBetween(item.startTime, item.endTime, null, '[]')) {
  115. // isActive = true
  116. // }
  117. // 判断课程的时间范围在不在可排课时间范围内
  118. if (
  119. dayjs(tempDate).valueOf() >= dayjs(item.startTime).valueOf() &&
  120. dayjs(lastDate).valueOf() <= dayjs(item.endTime).valueOf()
  121. ) {
  122. isActive = true
  123. }
  124. })
  125. // console.log(isActive, 'isActive')
  126. if (!isActive) {
  127. showToast('您选择的时间超过可排课时间范围')
  128. return
  129. }
  130. state.selectTime = tempDate.toDate()
  131. state.selectTimeStatus = false
  132. }
  133. watch(
  134. () => props.timerList,
  135. () => {
  136. init()
  137. }
  138. )
  139. const init = () => {
  140. console.log(props.timerList, 'timerList')
  141. state.calendarDate = props.timerList?.calendarDate
  142. const timeDetailList = props.timerList?.timeDetailList || []
  143. const useTimer: any = [] // 可排课时间段
  144. const usedTimer: any = [] // 不可排课时间段
  145. timeDetailList.forEach((time: any) => {
  146. if (time.enable) {
  147. useTimer.push(time)
  148. } else {
  149. usedTimer.push(time)
  150. }
  151. })
  152. // 初始化时间
  153. state.useTimer = [...useTimer]
  154. const useFormat = onFormatTimer(useTimer)
  155. state.useTimerFormat = useFormat
  156. state.usedTimer = [...usedTimer]
  157. console.log(onFormatTimer(useTimer), 'onFormatTimer')
  158. console.log(state.useTimer, state.usedTimer, 'onUseTimer')
  159. // 判断有可排课数据
  160. if (useFormat.length > 0) {
  161. const temp = useFormat[0]
  162. state.minMinute = temp.startMinute
  163. state.maxMinute = 59
  164. }
  165. }
  166. onMounted(() => {
  167. init()
  168. })
  169. return () => (
  170. <div class={styles.timer}>
  171. <OHeader title="训练时间" desotry={false} />
  172. {/* <div class={styles.selectTimer}>{dayjs(state.calendarDate).format('YYYY年MM月DD日')}</div> */}
  173. <CellGroup inset class={styles.cellGroup} style={{ marginTop: '12px' }}>
  174. {state.useTimer.map((item: any) => (
  175. <Cell
  176. center
  177. value={`${dayjs(item.startTime).format('HH:mm')}~${dayjs(item.endTime).format(
  178. 'HH:mm'
  179. )}`}
  180. title="可选时间"
  181. ></Cell>
  182. ))}
  183. {state.usedTimer.map((item: any) => (
  184. <Cell
  185. center
  186. title={`${dayjs(item.startTime).format('HH:mm')}~${dayjs(item.endTime).format(
  187. 'HH:mm'
  188. )}`}
  189. value="冲突时间"
  190. class={styles.noTime}
  191. ></Cell>
  192. ))}
  193. </CellGroup>
  194. <CellGroup inset class={styles.cellGroup}>
  195. <Cell
  196. center
  197. title={'训练开始时间'}
  198. value={state.selectTime ? dayjs(state.selectTime).format('HH:mm') : ''}
  199. isLink
  200. onClick={() => (state.selectTimeStatus = true)}
  201. ></Cell>
  202. </CellGroup>
  203. <Sticky position="bottom">
  204. <div class={'btnGroup'}>
  205. <Button
  206. round
  207. block
  208. type="primary"
  209. onClick={() => {
  210. if (!state.selectTime) {
  211. showToast('请选择训练开始时间')
  212. return
  213. }
  214. emit('confirm', state.selectTime)
  215. emit('close')
  216. }}
  217. >
  218. 确认
  219. </Button>
  220. </div>
  221. </Sticky>
  222. <Popup v-model:show={state.selectTimeStatus} position="bottom" round>
  223. <TimePicker
  224. minMinute={state.minMinute}
  225. maxMinute={state.maxMinute}
  226. formatter={onFormatter}
  227. filter={onFilter}
  228. onChange={onChange}
  229. onConfirm={onConfirm}
  230. onCancel={() => (state.selectTimeStatus = false)}
  231. />
  232. </Popup>
  233. </div>
  234. )
  235. }
  236. })