index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import React, { useCallback, useEffect, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import ZexhiBtn from '@/components/ZexhiBtn'
  4. import dayjs from 'dayjs'
  5. import getWeekList, { TimeChageResType } from '@/utils/timeChange'
  6. import classNames from 'classnames'
  7. import history from '@/utils/history'
  8. import { B1_APIgetInfoByDay } from '@/store/action/all'
  9. import { B3TimeArrType } from './type'
  10. import { useParams } from 'react-router-dom'
  11. function B3start() {
  12. // 获取路由参数key,1:个人/2:团体
  13. const urlObjTemp: any = useParams()
  14. const [list, setList] = useState<B3TimeArrType[]>([])
  15. const [loding, setLoding] = useState(false)
  16. // 获取日期信息(过滤掉不可预约日期)
  17. const getTimeRes = useCallback(async () => {
  18. const res = await B1_APIgetInfoByDay(dayjs().format('YYYY-MM-DD'))
  19. if (res.code === 0) {
  20. let noTime: string = res.data.stopDate
  21. let noTimeArr = noTime
  22. .replaceAll('年', '.')
  23. .replaceAll('月', '.')
  24. .replaceAll('日', '')
  25. .split(',')
  26. // noTimeArr = noTimeArr.map(v => dayjs().format('YYYY') + '.' + v)
  27. // 过了晚上8点 之后 列表显示8个
  28. const nowXiaoShi = dayjs().hour()
  29. let xiaoShi8 = nowXiaoShi >= 20 ? 8 : 7
  30. const arr = getWeekList(undefined, undefined, xiaoShi8)
  31. let arrRes: TimeChageResType[] = []
  32. arr.forEach(v => {
  33. if (!noTimeArr.includes(v.date)) {
  34. arrRes.push(v)
  35. // 只有周六日可以约
  36. // const num = dayjs(v.timeStamp).day()
  37. // if ([6, 0].includes(num)) arrRes.push(v)
  38. }
  39. })
  40. if (arrRes && arrRes.length) setNowTime(arrRes[0].date)
  41. // 设置顶部数组
  42. setZhouArr(arrRes)
  43. setLoding(true)
  44. // 设置入馆时间数组
  45. setList(res.data.time)
  46. }
  47. }, [])
  48. useEffect(() => {
  49. getTimeRes()
  50. }, [getTimeRes])
  51. useEffect(() => {
  52. setTimeout(() => {
  53. const dom: HTMLDivElement = document.querySelector('.B3selDay')!
  54. if (dom) {
  55. dom.scrollTo({
  56. left: 1000,
  57. behavior: 'smooth'
  58. })
  59. setTimeout(() => {
  60. dom.scrollTo({
  61. left: 0,
  62. behavior: 'smooth'
  63. })
  64. }, 500)
  65. }
  66. }, 500)
  67. }, [])
  68. // 顶部日期选中
  69. const [nowTime, setNowTime] = useState('')
  70. // 顶部一共七天日期数组
  71. const [zhouArr, setZhouArr] = useState<TimeChageResType[]>([])
  72. // 选中的顶部索引
  73. const [acInd, setAcInd] = useState(-1)
  74. // 判断当天时间是否已经超时
  75. const nowDayFlag = useCallback(
  76. (time: string) => {
  77. // true 表示当前时间已经超过了可预约时间
  78. let flag = true
  79. // 不是当天 直接为 false(表示全部可选择)
  80. if (nowTime !== dayjs().format('YYYY.MM.DD')) flag = false
  81. else {
  82. const lastTime = Number(time.split('-')[1].split(':')[0])
  83. const noeXisoShi = Number(dayjs(Date.now()).format('HH'))
  84. if (lastTime > noeXisoShi) flag = false
  85. // console.log(123, lastTime, noeXisoShi)
  86. }
  87. return flag
  88. },
  89. [nowTime]
  90. )
  91. // 点击顶部的切换日期
  92. const cutTopFu = useCallback(async (val: string) => {
  93. const varRes = val.replaceAll('.', '-')
  94. const res = await B1_APIgetInfoByDay(varRes)
  95. if (res.code === 0) {
  96. // 切换顶部选中
  97. setNowTime(val)
  98. // 清空底部索引选中
  99. setAcInd(-1)
  100. // 设置入馆时间数组
  101. setList(res.data.time)
  102. }
  103. }, [])
  104. return (
  105. <div className={styles.B3start}>
  106. <div className='B3main'>
  107. <div className='B3tit'>选择日期({urlObjTemp.key === '1' ? '个人预约' : '团队预约'})</div>
  108. <div className='B3nowTime'>{nowTime}</div>
  109. {zhouArr.length ? (
  110. <div className='B3selDay'>
  111. {zhouArr.map(item => (
  112. <div
  113. key={item.name + item.date}
  114. className={classNames('B3dayRow', nowTime === item.date ? 'B3dayRowAc' : '')}
  115. onClick={() => cutTopFu(item.date)}
  116. >
  117. <div>{item.name}</div>
  118. <div>{item.date.slice(-2)}</div>
  119. </div>
  120. ))}
  121. </div>
  122. ) : (
  123. <div hidden={!loding} style={{ fontSize: '20px', marginBottom: '30px' }}>
  124. 无可预约日期
  125. </div>
  126. )}
  127. <div className='B3tit'>入馆时间</div>
  128. {list.length ? (
  129. <div className='B3time'>
  130. {list.map((item, index) => (
  131. <div
  132. key={item.id}
  133. onClick={() => setAcInd(index)}
  134. className={classNames(
  135. 'B3timeRow',
  136. acInd === index ? 'B3timeRowAc' : '',
  137. item.pcs <= 0 || nowDayFlag(item.time) ? 'myBtnNo' : ''
  138. )}
  139. >
  140. {item.time}
  141. {item.pcs <= 0 ? <p>预约已满</p> : ''}
  142. </div>
  143. ))}
  144. </div>
  145. ) : (
  146. <span hidden={!loding} style={{ fontSize: '20px' }}>
  147. 请先选择日期
  148. </span>
  149. )}
  150. </div>
  151. <ZexhiBtn
  152. nextOk={acInd > -1}
  153. nextFu={() =>
  154. history.push(`/exhiForm/${nowTime.replaceAll('.', '_')}/${acInd}/${urlObjTemp.key}`)
  155. }
  156. />
  157. </div>
  158. )
  159. }
  160. const MemoB3start = React.memo(B3start)
  161. export default MemoB3start