index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import dayjs from 'dayjs'
  2. import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
  3. import customParseFormat from 'dayjs/plugin/customParseFormat'
  4. dayjs.extend(customParseFormat)
  5. dayjs.extend(isSameOrBefore)
  6. import { ElButton, ElCol, ElRow } from 'element-plus'
  7. import { defineComponent } from 'vue'
  8. import styles from './index.module.less'
  9. export default defineComponent({
  10. name: 'practice-timer',
  11. props: {
  12. timerObject: {
  13. // 传入的数据
  14. type: Object,
  15. default: {}
  16. },
  17. onClose: {
  18. type: Function,
  19. default: () => {}
  20. },
  21. onChoice: {
  22. // 点击选择时间
  23. type: Function,
  24. default: (item: any) => {}
  25. },
  26. courseMinutes: {
  27. // 课程时长
  28. type: Number,
  29. default: 25
  30. },
  31. freeMinutes: {
  32. // 空余时长
  33. type: Number,
  34. default: 5
  35. },
  36. startSetting: {
  37. // 开始设置时间
  38. type: String,
  39. default: '08:00'
  40. },
  41. endSetting: {
  42. // 结束设置时间
  43. type: String,
  44. default: '18:00'
  45. }
  46. },
  47. data() {
  48. return {
  49. timerList: [],
  50. list: [] as any,
  51. weekList: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  52. weekType: [
  53. 'monday',
  54. 'tuesday',
  55. 'wednesday',
  56. 'thursday',
  57. 'friday',
  58. 'saturday',
  59. 'sunday'
  60. ]
  61. }
  62. },
  63. mounted() {
  64. this.list = this.timerInit(
  65. this.startSetting,
  66. this.endSetting,
  67. this.courseMinutes + this.freeMinutes || 30
  68. )
  69. console.log(this.endSetting)
  70. },
  71. methods: {
  72. timerInit(startTime: string, endTime: string, space: number) {
  73. let start = dayjs(startTime, 'HH:mm')
  74. const end = dayjs(endTime, 'HH:mm')
  75. const timerList: any = []
  76. // 生成一天的时间段
  77. while (start.add(space, 'minute').isSameOrBefore(dayjs(end))) {
  78. const item = {
  79. startTime: start.format('HH:mm'),
  80. endTime: start.add(space, 'minute').format('HH:mm'),
  81. status: false
  82. }
  83. // 一周
  84. timerList.push(item)
  85. start = start.add(space, 'minute')
  86. }
  87. const list: any = []
  88. // 生成一周的时间段
  89. timerList.forEach((item: any) => {
  90. const weekList: any = []
  91. for (let i = 0; i < 7; i++) {
  92. weekList.push({
  93. ...item
  94. })
  95. }
  96. list.push(weekList)
  97. })
  98. const tempList = this._initData(list)
  99. return tempList
  100. },
  101. _initData(list: Array<any>) {
  102. // 回显数据
  103. const weekType = this.weekType
  104. const timerObject = this.timerObject
  105. list.forEach((item: any) => {
  106. item.forEach((slot: any, slotIndex: number) => {
  107. const dayList = timerObject[weekType[slotIndex]]
  108. const startTime = dayjs(slot.startTime, 'HH:mm').format('HH:mm:ss')
  109. const isExist = dayList?.some(
  110. (course: any) => course.startTime === startTime
  111. )
  112. isExist && (slot.status = true)
  113. })
  114. })
  115. return list
  116. },
  117. btnStatus(index: number, type: 'row' | 'col') {
  118. if (type === 'row') {
  119. return this.list.every((item: any) => {
  120. return item[index].status
  121. })
  122. }
  123. if (type == 'col') {
  124. return this.list[index].every((item: any) => item.status)
  125. }
  126. },
  127. choice(index: number, type: 'row' | 'col', status?: boolean) {
  128. if (type === 'row') {
  129. this.list.forEach((item: any, i: number) => {
  130. const type = !status ? true : false
  131. item[index].status = type
  132. })
  133. }
  134. if (type == 'col') {
  135. this.list[index].forEach((item: any, i: number) => {
  136. const type = !status ? true : false
  137. item.status = type
  138. })
  139. }
  140. },
  141. onSubmit() {
  142. const list = this.list
  143. const weekList = {
  144. monday: [],
  145. tuesday: [],
  146. wednesday: [],
  147. thursday: [],
  148. friday: [],
  149. saturday: [],
  150. sunday: []
  151. }
  152. const weekType = this.weekType
  153. let status = false
  154. list.forEach((item: any, i: number) => {
  155. item.forEach((times: any, j: number) => {
  156. if (times.status) {
  157. status = true
  158. weekList[weekType[j]].push({
  159. startTime: dayjs(times.startTime, 'HH:mm').format('HH:mm:ss'),
  160. endTime: dayjs(times.endTime, 'HH:mm')
  161. .subtract(this.freeMinutes, 'minute')
  162. .format('HH:mm:ss')
  163. })
  164. }
  165. })
  166. })
  167. this.onChoice && this.onChoice(weekList, status)
  168. }
  169. },
  170. render() {
  171. return (
  172. <div class={styles.timer}>
  173. <div class={styles.tips}>
  174. <div class={styles.tipsTitle}>请选择陪练开始时间</div>
  175. <div class={styles.tipsTime}>
  176. 陪练课单课时时长为 <span>{this.courseMinutes}</span> 分钟
  177. </div>
  178. </div>
  179. <div class={[styles.timerContainer, 'mb12']}>
  180. <ElRow gutter={5}>
  181. <ElCol span={3} class={[styles.tag]}></ElCol>
  182. {this.weekList.map((item: any) => (
  183. <ElCol span={3}>
  184. <span class={styles.tag}>{item}</span>
  185. </ElCol>
  186. ))}
  187. </ElRow>
  188. <ElRow gutter={5} class="pt-1">
  189. <ElCol span={3} class={[styles.tag]}></ElCol>
  190. {this.weekList.map((item: any, index: number) => (
  191. <ElCol span={3}>
  192. <span
  193. class={[
  194. styles.tag,
  195. 'cursor-pointer',
  196. this.btnStatus(index, 'row') && styles.active
  197. ]}
  198. onClick={() =>
  199. this.choice(index, 'row', this.btnStatus(index, 'row'))
  200. }
  201. >
  202. 全选
  203. </span>
  204. </ElCol>
  205. ))}
  206. </ElRow>
  207. <div class="h-72 overflow-y-auto overflow-x-hidden">
  208. {this.list.map((item: any, index: number) => (
  209. <ElRow gutter={5} class="pt-1">
  210. <ElCol span={3}>
  211. <span
  212. class={[
  213. styles.tag,
  214. 'cursor-pointer',
  215. this.btnStatus(index, 'col') && styles.active
  216. ]}
  217. onClick={() =>
  218. this.choice(index, 'col', this.btnStatus(index, 'col'))
  219. }
  220. >
  221. 全选
  222. </span>
  223. </ElCol>
  224. {item.map((week: any) => (
  225. <ElCol span={3}>
  226. <span
  227. class={[
  228. styles.tag,
  229. 'cursor-pointer',
  230. week.status && styles.select
  231. ]}
  232. style={{ color: '#333333' }}
  233. onClick={() => (week.status = !week.status)}
  234. >
  235. {week.startTime}
  236. </span>
  237. </ElCol>
  238. ))}
  239. </ElRow>
  240. ))}
  241. </div>
  242. </div>
  243. {/* <Sticky offsetBottom={0} position="bottom"> */}
  244. <div class="text-center pt-3 pb-5">
  245. <ElButton
  246. class="!w-40 !h-[48px] !text-base"
  247. round
  248. onClick={() => {
  249. this.onClose()
  250. }}
  251. >
  252. 取消
  253. </ElButton>
  254. <ElButton
  255. type="primary"
  256. class="!w-40 !h-[48px] !text-base"
  257. onClick={this.onSubmit}
  258. round
  259. >
  260. 保存设置
  261. </ElButton>
  262. </div>
  263. {/* </Sticky> */}
  264. </div>
  265. )
  266. }
  267. })