index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <div class="date-time-selector-content bg-white">
  3. <CheckboxGroup
  4. v-model="state"
  5. :config="checkboxConfig"
  6. :options="checkboxOptions"
  7. @change="onCheckboxChange"
  8. @item-select="onCheckboxSelect"
  9. :beforeChangeAction="beforeChangeAction"
  10. ref="checkboxGroup"
  11. />
  12. <DateTimeGroup
  13. v-show="dateTimeGroupActive"
  14. :highlight="dateTimeGroupActive"
  15. :startPlaceholder="startPlaceholder"
  16. :endPlaceholder="endPlaceholder"
  17. :endTimeFeature="endTimeFeature"
  18. @change="onChange"
  19. ref="dateTimeGroup"
  20. />
  21. </div>
  22. </template>
  23. <script>
  24. import DateTimeGroup from '@/components/selector/date-time-group'
  25. import { CheckboxGroup } from '@/ui'
  26. import { dateTimeKeys } from '@/data'
  27. import { dateFormatter, calcNotExactTime } from '@/utils'
  28. export default {
  29. name: 'DateTimeListSelector',
  30. components: {
  31. CheckboxGroup,
  32. DateTimeGroup
  33. },
  34. props: {
  35. /**
  36. * 显示哪些元素,传入key
  37. *
  38. * 'all','exact','yesterday','today','lately3','lately7','lately30','lately90','lately180','lastYear','thisYear','sinceLastYear','sinceYearBeforeLast'.....
  39. */
  40. useKeys: {
  41. type: Array,
  42. default() {
  43. return []
  44. }
  45. },
  46. endTimeFeature: {
  47. type: Boolean,
  48. default: false
  49. },
  50. options: {
  51. type: Array,
  52. default() {
  53. return [
  54. {
  55. label: '全部',
  56. key: 'all'
  57. },
  58. ...dateTimeKeys,
  59. {
  60. label: '自定义',
  61. key: 'exact'
  62. }
  63. ]
  64. }
  65. },
  66. startPlaceholder: {
  67. type: String,
  68. default: '开始时间'
  69. },
  70. endPlaceholder: {
  71. type: String,
  72. default: '结束时间'
  73. },
  74. beforeChangeAction: Function,
  75. defaultSelectedKey: {
  76. type: String,
  77. default: 'all' // all/lately30/lately90...
  78. }
  79. },
  80. data() {
  81. return {
  82. debug: import.meta.env.DEV,
  83. state: ['all'],
  84. checkboxConfig: {
  85. icon: false,
  86. multiple: false
  87. },
  88. checkboxOptions: []
  89. }
  90. },
  91. computed: {
  92. dateTimeGroupActive() {
  93. return this.state.includes('exact')
  94. }
  95. },
  96. created() {
  97. this.initUseOptions()
  98. },
  99. methods: {
  100. dateFormatter,
  101. initUseOptions() {
  102. if (Array.isArray(this.useKeys) && this.useKeys.length === 0) {
  103. this.checkboxOptions = this.options
  104. } else {
  105. this.checkboxOptions = this.options.filter((o) => {
  106. return this.useKeys.includes(o.key)
  107. })
  108. }
  109. console.log(this.checkboxOptions, 'checkboxOptions')
  110. this.calcLastTime()
  111. this.setDefaultSelected()
  112. },
  113. calcLastTime() {
  114. const renameList = [
  115. 'thisYear', // 今年全年
  116. 'sinceLastYear', // 去年至今
  117. 'sinceYearBeforeLast' // 前年至今
  118. ]
  119. const thisYear = new Date().getFullYear()
  120. this.checkboxOptions.forEach((item) => {
  121. const { key } = item
  122. if (renameList.includes(key)) {
  123. if (key === renameList[0]) {
  124. item.label = `${thisYear}年全年`
  125. } else if (key === renameList[1]) {
  126. item.label = `${thisYear - 1}年至今`
  127. } else if (key === renameList[2]) {
  128. item.label = `${thisYear - 2}年至今`
  129. }
  130. }
  131. })
  132. },
  133. setDefaultSelected() {
  134. // 初始化选中状态
  135. if (this.defaultSelectedKey) {
  136. // defaultSelectedKey选中,没有找到defaultSelectedKey则第一个选中
  137. const exist = this.useKeys.includes(this.defaultSelectedKey)
  138. if (exist) {
  139. this.setTimeListState(this.defaultSelectedKey)
  140. } else {
  141. this.setTimeListState(this.options[0].key)
  142. }
  143. }
  144. },
  145. onCheckboxChange(data) {
  146. const exact = data.includes('exact')
  147. if (exact) {
  148. this.$refs.dateTimeGroup.popupState('start', true)
  149. } else {
  150. this.onChange()
  151. }
  152. },
  153. setTimeListState(value) {
  154. this.state = [value]
  155. console.log(value, 'value')
  156. },
  157. clearDateTimePicker() {
  158. const { dateTimeGroup } = this.$refs
  159. dateTimeGroup.resetAll()
  160. },
  161. // 计算lately7/lately30/lastYear等的开始和结束时间
  162. // endTime传入一个时间戳
  163. calcNotExactTime,
  164. onChange() {
  165. const value = this.getState()
  166. if (this.debug) {
  167. console.table({
  168. start: this.dateFormatter(value.start),
  169. end: this.dateFormatter(value.end)
  170. })
  171. }
  172. const payload = {
  173. value
  174. }
  175. this.$emit('change', payload)
  176. },
  177. onCheckboxSelect(item) {
  178. this.$emit('checkboxClick', item)
  179. },
  180. setState(data) {
  181. // {
  182. // start: 1620230400000, // 2021-5-6
  183. // end: 1620489600000, // 2021-5-9
  184. // exact: 'all'
  185. // }
  186. if (!data || Object.keys(data).length === 0) {
  187. this.setTimeListState(this.defaultSelectedKey)
  188. } else {
  189. const { dateTimeGroup } = this.$refs
  190. this.setTimeListState(data.exact)
  191. if (data.exact === 'exact') {
  192. dateTimeGroup.setState({
  193. start: data.start ? data.start : '',
  194. end: data.end ? data.end : ''
  195. })
  196. } else {
  197. this.clearDateTimePicker()
  198. }
  199. }
  200. },
  201. getState() {
  202. const { dateTimeGroup } = this.$refs
  203. // timeState.exact: all/lately7/lately30/lastYear/exact等状态
  204. const timeState = {
  205. start: 0,
  206. end: 0,
  207. exact: this.state.join(',')
  208. }
  209. if (timeState.exact === 'exact') {
  210. const state = dateTimeGroup.getState()
  211. Object.assign(timeState, state)
  212. } else {
  213. Object.assign(timeState, this.calcNotExactTime(timeState.exact))
  214. }
  215. return timeState
  216. }
  217. }
  218. }
  219. </script>
  220. <style lang="scss" scoped>
  221. .date-time-button-group {
  222. margin: 8px 0;
  223. }
  224. </style>