timeselect.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div>
  3. <div v-if="isRadioGroup">
  4. <a-radio-group class="mr-3" v-model="selected" @change="handleChange">
  5. <a-radio-button v-for="m of options" :key="m.label" :value="m.value">
  6. {{ m.label }}
  7. </a-radio-button>
  8. </a-radio-group>
  9. </div>
  10. <div v-else>
  11. <a-select :value="selected" :dropdownMatchSelectWidth="false" @change="handleChange">
  12. <a-select-option v-for="m of options" :key="m.label" :value="m.value">
  13. {{ m.label }}
  14. </a-select-option>
  15. </a-select>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. import i18n from '@/locales'
  21. const options = [
  22. { label: i18n.t('timeselect.hour', [1]), value: 60 },
  23. { label: i18n.t('timeselect.hours', [6]), value: 360 },
  24. { label: i18n.t('timeselect.hours', [12]), value: 12 * 60 },
  25. { label: i18n.t('timeselect.days', [1]), value: 24 * 60 },
  26. { label: i18n.t('timeselect.days', [7]), value: 7 * 24 * 60 },
  27. { label: i18n.t('timeselect.days', [14]), value: 14 * 24 * 60 },
  28. { label: i18n.t('timeselect.months', [1]), value: 30 * 24 * 60 },
  29. { label: i18n.t('common.last_month'), value: 'last_month' },
  30. ]
  31. export default {
  32. name: 'TimeSelect',
  33. props: {
  34. defaultValue: {
  35. type: Number,
  36. default: 7 * 24 * 60,
  37. },
  38. value: {
  39. type: Number, // minutes
  40. },
  41. isRadioGroup: {
  42. type: Boolean,
  43. default: false,
  44. },
  45. },
  46. data () {
  47. const selected = this.value || this.defaultValue
  48. return {
  49. selected: selected,
  50. options: options,
  51. }
  52. },
  53. watch: {
  54. value (v = '') {
  55. this.selected = v || this.defaultValue
  56. },
  57. },
  58. methods: {
  59. handleChange (v) {
  60. if (this.isRadioGroup) {
  61. this.$emit('change', v.target.value)
  62. } else {
  63. this.$emit('change', v)
  64. }
  65. },
  66. parseTimeRange (v) {
  67. if (v === 'last_month') {
  68. const now = this.$moment()
  69. const lastMonthStart = this.$moment().subtract(1, 'month').startOf('month') // 上个月1号0点
  70. const lastMonthEnd = this.$moment().subtract(1, 'month').endOf('month') // 上个月最后一天最后一刻
  71. const fromHours = now.diff(lastMonthStart, 'hours')
  72. const toHours = now.diff(lastMonthEnd, 'hours')
  73. return { from: fromHours, to: toHours, value: v }
  74. }
  75. const to = new Date()
  76. const from = new Date(to - v * 60 * 1000)
  77. return { from: from, to: to, value: v }
  78. },
  79. },
  80. }
  81. </script>