timer.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { Button, Col, Row, Sticky, Tag } from 'vant'
  2. import { defineComponent } from 'vue'
  3. import styles from './timer.module.less'
  4. import dayjs from 'dayjs'
  5. import isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
  6. import customParseFormat from 'dayjs/plugin/customParseFormat'
  7. dayjs.extend(customParseFormat)
  8. dayjs.extend(isSameOrBefore)
  9. export default defineComponent({
  10. name: 'timer',
  11. props: {
  12. onChoice: {
  13. type: Function,
  14. default: (item: any) => {}
  15. },
  16. courseMinutes: {
  17. type: Number,
  18. default: 25
  19. },
  20. freeMinutes: {
  21. type: Number,
  22. default: 5
  23. },
  24. startSetting: {
  25. type: String,
  26. default: '08:00'
  27. },
  28. endSetting: {
  29. type: String,
  30. default: '18:00'
  31. }
  32. },
  33. data() {
  34. return {
  35. timerList: [],
  36. list: [] as any,
  37. weekList: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  38. }
  39. },
  40. mounted() {
  41. this.list = this.timerInit(
  42. this.startSetting,
  43. this.endSetting,
  44. this.courseMinutes + this.freeMinutes || 30
  45. )
  46. },
  47. methods: {
  48. timerInit(startTime: string, endTime: string, space: number) {
  49. let start = dayjs(startTime, 'HH:mm')
  50. const end = dayjs(endTime, 'HH:mm')
  51. const timerList: any = []
  52. while (start.add(space, 'minute').isSameOrBefore(dayjs(end))) {
  53. const item = {
  54. startTime: start.format('HH:mm'),
  55. endTime: start.add(space, 'minute').format('HH:mm'),
  56. status: false
  57. }
  58. // 一周
  59. timerList.push(item)
  60. for (let i = 0; i < 7; i++) {}
  61. start = start.add(space, 'minute')
  62. }
  63. const list: any = []
  64. timerList.forEach((item: any) => {
  65. const weekList: any = []
  66. for (let i = 0; i < 7; i++) {
  67. weekList.push({
  68. ...item
  69. })
  70. }
  71. list.push(weekList)
  72. })
  73. return list
  74. },
  75. btnStatus(index: number, type: 'row' | 'col') {
  76. if (type === 'row') {
  77. return this.list.every((item: any) => {
  78. return item[index].status
  79. })
  80. }
  81. if (type == 'col') {
  82. return this.list[index].every((item: any) => item.status)
  83. }
  84. },
  85. choice(index: number, type: 'row' | 'col', status?: boolean) {
  86. if (type === 'row') {
  87. this.list.forEach((item: any, i: number) => {
  88. const type = !status ? true : false
  89. item[index].status = type
  90. })
  91. }
  92. if (type == 'col') {
  93. this.list[index].forEach((item: any, i: number) => {
  94. const type = !status ? true : false
  95. item.status = type
  96. })
  97. }
  98. },
  99. onSubmit() {
  100. const list = this.list
  101. const weekList = {
  102. monday: [],
  103. tuesday: [],
  104. wednesday: [],
  105. thursday: [],
  106. friday: [],
  107. saturday: [],
  108. sunday: []
  109. }
  110. const weekType = [
  111. 'monday',
  112. 'tuesday',
  113. 'wednesday',
  114. 'thursday',
  115. 'friday',
  116. 'saturday',
  117. 'sunday'
  118. ]
  119. let status = false
  120. list.forEach((item: any, i: number) => {
  121. item.forEach((times: any, j: number) => {
  122. if (times.status) {
  123. status = true
  124. weekList[weekType[j]].push({
  125. startTime: dayjs(times.startTime, 'HH:mm').format('HH:mm:ss'),
  126. endTime: dayjs(times.endTime, 'HH:mm').format('HH:mm:ss')
  127. })
  128. }
  129. })
  130. })
  131. this.onChoice && this.onChoice(weekList, status)
  132. }
  133. },
  134. render() {
  135. return (
  136. <div class={styles.timer}>
  137. <div class={styles.tips}>
  138. <div class={styles.tipsTitle}>请选择陪练开始时间</div>
  139. <div class={styles.tipsTime}>
  140. 陪练课单课时时长为 <span>{this.courseMinutes}</span> 分钟
  141. </div>
  142. </div>
  143. <div class={[styles.timerContainer, 'mb12']}>
  144. <Row gutter={5}>
  145. <Col span={3}></Col>
  146. {this.weekList.map((item: any) => (
  147. <Col span={3}>
  148. <span class={styles.tag}>{item}</span>
  149. </Col>
  150. ))}
  151. </Row>
  152. <Row gutter={5}>
  153. <Col span={3}></Col>
  154. {this.weekList.map((item: any, index: number) => (
  155. <Col span={3}>
  156. <span
  157. class={[
  158. styles.tag,
  159. this.btnStatus(index, 'row') && styles.active
  160. ]}
  161. onClick={() =>
  162. this.choice(index, 'row', this.btnStatus(index, 'row'))
  163. }
  164. title={item}
  165. >
  166. 全选
  167. </span>
  168. </Col>
  169. ))}
  170. </Row>
  171. {this.list.map((item: any, index: number) => (
  172. <Row gutter={5}>
  173. <Col span={3}>
  174. <span
  175. class={[
  176. styles.tag,
  177. this.btnStatus(index, 'col') && styles.active
  178. ]}
  179. onClick={() =>
  180. this.choice(index, 'col', this.btnStatus(index, 'col'))
  181. }
  182. >
  183. 全选
  184. </span>
  185. </Col>
  186. {item.map((week: any) => (
  187. <Col span={3}>
  188. <span
  189. class={[styles.tag, week.status && styles.select]}
  190. title={week}
  191. style={{ color: '#333333' }}
  192. onClick={() => (week.status = !week.status)}
  193. >
  194. {week.startTime}
  195. </span>
  196. </Col>
  197. ))}
  198. </Row>
  199. ))}
  200. </div>
  201. <Sticky offsetBottom={0} position="bottom">
  202. <div class={'btnGroup'}>
  203. <Button block round type="primary" onClick={this.onSubmit}>
  204. 确定
  205. </Button>
  206. </div>
  207. </Sticky>
  208. </div>
  209. )
  210. }
  211. })