index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div>
  3. <el-date-picker
  4. v-model="dateRange"
  5. type="daterange"
  6. unlink-panels
  7. range-separator="To"
  8. start-placeholder="开始日期"
  9. end-placeholder="结束日期"
  10. :shortcuts="shortcuts"
  11. size="default"
  12. value-format="YYYY-MM-DD"
  13. :disabled-date="disabledDate"
  14. :clearable="false"
  15. :popper-options="{placement: props.popperPlacement}"
  16. @change="changedValue"
  17. style="border-radius: 0;"
  18. />
  19. </div>
  20. </template>
  21. <script lang="ts">
  22. import { useShopInfo } from '/@/stores/shopInfo'
  23. const timezoneDefault = useShopInfo().profile.time_zone
  24. export default {}
  25. </script>
  26. <script setup lang="ts">
  27. import { ref, onMounted } from 'vue'
  28. import dayjs, { Dayjs } from 'dayjs'
  29. defineOptions({
  30. name: 'DateRangePicker'
  31. })
  32. const props = defineProps({
  33. modelValue: {type: Array, required: true },
  34. timezone: { type: String, default: timezoneDefault },
  35. popperPlacement: { type: String, default: 'bottom-start' }
  36. })
  37. const dateRange = ref(props.modelValue)
  38. const emits = defineEmits(['update:modelValue', 'change'])
  39. const changedValue = (newVal: string[]) => {
  40. emits('update:modelValue', newVal)
  41. emits('change', newVal)
  42. }
  43. function disabledDate(datetime: Date) {
  44. const now = dayjs(new Date()).tz(props.timezone)
  45. const now_tz = now.startOf("day")
  46. return now_tz.isBefore(dayjs(datetime).tz(props.timezone))
  47. }
  48. function tzFormat(dt_tz: Dayjs) {
  49. return dt_tz.format("YYYY-MM-DD HH:mm:ss")
  50. }
  51. function recentDays(days: number):string[] {
  52. const now_tz = dayjs(new Date()).tz(props.timezone)
  53. const start = now_tz.subtract(days, 'day')
  54. const end = now_tz.subtract(1, 'day')
  55. return [tzFormat(start), tzFormat(end)]
  56. }
  57. function currentWeekMonthYear(dim: 'month'|'week'|'year'): string[] {
  58. const end = dayjs(new Date()).tz(props.timezone)
  59. const start = end.startOf(dim)
  60. return [tzFormat(start), tzFormat(end)]
  61. }
  62. function lastWeekMonthYear(dim: 'month'|'week'|'year'): string[] {
  63. const last = dayjs(new Date()).tz(props.timezone).subtract(1, dim)
  64. return [tzFormat(last.startOf(dim)), tzFormat(last.endOf(dim))]
  65. }
  66. const shortcuts = [
  67. {
  68. text: '今天',
  69. value: () => {
  70. const now_tz = dayjs(new Date()).tz(props.timezone)
  71. return [tzFormat(now_tz), tzFormat(now_tz)]
  72. },
  73. },
  74. {
  75. text: '昨天',
  76. value: () => recentDays(1)
  77. },
  78. {
  79. text: '最近7天',
  80. value: () => recentDays(7)
  81. },
  82. {
  83. text: '最近15天',
  84. value: () => recentDays(15)
  85. },
  86. {
  87. text: '最近30天',
  88. value: () => recentDays(30)
  89. },
  90. {
  91. text: '本周',
  92. value: () => currentWeekMonthYear('week')
  93. },
  94. {
  95. text: '上周',
  96. value: () => lastWeekMonthYear('week')
  97. },
  98. {
  99. text: '本月',
  100. value: () => currentWeekMonthYear('month')
  101. },
  102. {
  103. text: '上月',
  104. value: () => lastWeekMonthYear('month')
  105. },
  106. {
  107. text: '本年',
  108. value: () => currentWeekMonthYear('year')
  109. }
  110. ]
  111. </script>
  112. <style scoped>
  113. </style>