DatePopup.vue 4.32 KB
<template>
  <van-popup
    v-model="show"
    position="bottom"
    class="date-popup"
    round
    @close="close"
  >
    <div class="date-title flex">
      时间选择
      <i
        class="van-icon van-icon-cross van-popup__close-icon van-popup__close-icon--top-right"
        @click="close"
      ></i>
    </div>
    <div class="date-box">
      <div class="date-subtitle">推送日期</div>
      <div class="date-list flex">
        <template v-for="(i,index) in dateList">
          <div
            :key="index"
            :class="['item flex',{'active':index == dateIndex}]"
            @click="chooseDate(index)"
          >
            {{ i }}
          </div>
        </template>
      </div>
      <div class="date-subtitle">自定义</div>
      <van-calendar
        ref="calendar"
        class="calendar"
        :poppable="false"
        :show-confirm="false"
        :show-title="false"
        :show-subtitle="false"
        type="range"
        color="#3975C6"
        :min-date="minDate"
        :max-date="maxDate"
        :allow-same-day="true"
        :lazy-render="false"
        row-height="48px"
        @confirm="confirmCalendar"
      />
      <van-button
        block
        round
        color="#3975C6"
        class="date-btn"
        :disabled="!dateValue.startTime && !dateValue.endTime"
        @click="confirmDate"
      >确定</van-button>
    </div>
  </van-popup>
</template>

<script type='text/ecmascript-6'>
import * as dayjs from 'dayjs'
export default {
  name: 'DatePopup',
  props: {
    value: Boolean
  },
  data () {
    return {
      show: false,
      dateList: ['上月', '近三月', '近一年'],
      dateValue: {
        startTime: '',
        endTime: ''
      },
      dateIndex: -1,
      minDate: new Date('2023/01/01'),
      maxDate: new Date(),
    }
  },
  watch: {
    value (n) {
      this.show = n
    }
  },
  mounted () {
  },
  methods: {
    chooseDate (index) {
      const monthNumber = {
        0: 1,
        1: 3,
        2: 12
      }[index]
      let startTime = dayjs().subtract(monthNumber, 'month'),
        endTime = dayjs()
      if (index == 0) {
        startTime = startTime.startOf('M')
        endTime = dayjs(startTime).endOf('month')
      }
      this.dateIndex = index
      this.setDateValue([startTime, endTime])
      this.$refs.calendar.reset()
    },
    confirmCalendar (val) {
      this.dateIndex = -1
      this.setDateValue(val)
    },
    setDateValue ([startTime, endTime]) {
      this.dateValue = {
        startTime: `${dayjs(startTime).format('YYYY-MM-DD')} 00:00:00`,
        endTime: `${dayjs(endTime).format('YYYY-MM-DD')} 23:59:59`
      }
      console.log(JSON.parse(JSON.stringify(this.dateValue)))
    },
    close () {
      this.$emit('update:value', false)
    },
    confirmDate () {
      let { dateValue } = this
      this.$emit('confirm', dateValue)
      this.close()
    }
  }
}
</script>

<style scoped lang='scss'>
.date-popup {
  max-height: 95%;
  /deep/.van-popup__close-icon {
    z-index: 3;
    margin-left: auto;
    position: static;
  }
  /deep/.van-calendar__day {
    font-size: 14px;
  }
  /deep/.van-calendar__month-title {
    height: 32px;
    line-height: 32px;
  }
  /deep/.van-calendar__bottom-info {
    font-size: 8px;
    bottom: 2px;
  }
  .date-title {
    height: 44px;
    padding: 0 16px;
    font-weight: 500;
    font-size: 16px;
    color: #222222;
    position: sticky;
    background-color: #fff;
    z-index: 2;
    top: 0;
  }
  .date-box {
    padding: 16px 16px calc(var(--constantSafeAreaInsetBottom) + 76px);
    padding: 16px 16px calc(var(--envSafeAreaInsetBottom) + 76px);
    .date-subtitle {
      font-size: 14px;
      color: #333333;
      margin-bottom: 8px;
    }
    .date-list {
      margin-bottom: 16px;
      gap: 8px;
      .item {
        width: 80px;
        height: 28px;
        border: 1px solid #dadde0;
        border-radius: 14px;
        font-size: 14px;
        color: #333333;
        justify-content: center;
      }
      .active {
        color: #3975c6;
        background: #3975c61a;
        border-color: #3975c61a;
      }
    }
    .calendar {
      height: 320px;
    }
  }
  .date-btn {
    position: fixed;
    left: 16px;
    bottom: calc(var(--constantSafeAreaInsetBottom) + 16px);
    bottom: calc(var(--envSafeAreaInsetBottom) + 16px);
    z-index: 2;
    width: calc(100% - 32px);
  }
}
</style>