SearchBox.vue 7.33 KB
<template>
  <div class="search-box">
    <el-form
      inline
      size="small"
      label-width="97px"
    >
      <template v-if="hasStatisticalPeriod">
        <el-form-item label="统计周期:">
          <el-select
            v-model="statisticalPeriod"
            @change="changeStatisticalPeriod"
            :popper-append-to-body="false"
          >
            <el-option
              v-for="item in statisticalPeriodOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </template>
      <el-form-item class="radio-group">
        <el-radio-group
          v-model="form.dateType"
          @input="search('dateType')"
        >
          <el-radio-button
            v-for="item in dateGroup"
            :key="item.value"
            :label="item.value"
          >{{item.name}}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          class="date-picker"
          v-model="date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="changeDate"
          :picker-options="beforeRangePickerOptions"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div
      class="data-config"
      v-if="hasConfig"
    >
      <el-button
        size="small"
        @click="dialogShow = true"
      ><i class="el-icon-setting"></i>数据配置</el-button>
    </div>
    <config-dialog
      :show.sync="dialogShow"
      @update="$emit('update')"
    />
  </div>
</template>

<script type='text/ecmascript-6'>
import dayjs from 'dayjs'
import ConfigDialog from './ConfigDialog.vue'
const statisticalPeriodOptions = {
  day: { label: '按日', value: 'day' },
  week: { label: '按周', value: 'week' },
  month: { label: '按月', value: 'month' },
},
  DATE_TYPES = {
    WEEK: 'week',
    MONTH: 'month',
    DEFAULT: 'default'
  }, dateTypeMapping = {
    [DATE_TYPES.WEEK]: {
      options: [statisticalPeriodOptions.day],
      period: 'day'
    },
    [DATE_TYPES.MONTH]: {
      options: [statisticalPeriodOptions.day, statisticalPeriodOptions.week],
      period: 'day'
    },
    [DATE_TYPES.DEFAULT]: {
      options: [statisticalPeriodOptions.month],
      period: 'month'
    }
  }
export default {
  components: {
    ConfigDialog
  },
  name: '',
  props: {
    hasConfig: {
      type: Boolean,
      default: true
    },
    hasStatisticalPeriod: Boolean
  },
  data () {
    return {
      beforeRangePickerOptions: {
        disabledDate: date => date > new Date()
      },
      form: {
        dateType: 'week'
      },
      date: [],
      dateGroup: [
        // { name: '今日', value: 'date' },
        { name: '本周', value: 'week' },
        { name: '本月', value: 'month' },
        { name: '全年', value: 'year' },
      ],
      dialogShow: false,
      statisticalPeriodOptions: [statisticalPeriodOptions.day],
      statisticalPeriod: 'day'
    }
  },
  watch: {
    date: {
      handler (val) {
        if (val && val.length) {
          let [startDate, endDate] = val
          let dayNum = dayjs(endDate).diff(dayjs(startDate), 'day') + 1
          if (dayNum <= 7) {
            this.statisticalPeriodOptions = [statisticalPeriodOptions.day]
            this.statisticalPeriod = 'day'
          }
          if (dayNum >= 8 && dayNum <= 93) {
            this.statisticalPeriodOptions = [statisticalPeriodOptions.day, statisticalPeriodOptions.week]
            this.statisticalPeriod = 'day'
          }
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted () {
    this.initDate()
  },
  methods: {
    initDate () {
      this.dateGroup.map(e => {
        let {
          startDate,
          endDate
        } = this.getDateRange(e.value)
        e.startDate = startDate
        e.endDate = endDate
      })
      this.search()
    },
    getDateRange (type) {
      let now = new Date();
      let startDate, endDate;
      if (type === 'week') {
        let dayOfWeek = now.getDay();
        if (dayOfWeek == 0) dayOfWeek = 7
        startDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() - dayOfWeek + 1);
        endDate = new Date(now.getFullYear(), now.getMonth(), now.getDate() + (7 - dayOfWeek));
      } else {
        startDate = dayjs().startOf(type);
        endDate = dayjs().endOf(type)
      }
      return {
        startDate: this.formatDate(startDate, 'start'),
        endDate: this.formatDate(endDate)
      };
    },
    formatDate (date, type) {
      return dayjs(date).format("YYYY-MM-DD")
      // + (type == 'start' ? ' 00:00:00' : ' 23:59:59')
    },
    changeDate (e) {
      if (e && e.length) {
        let [startDate, endDate] = e
        const date1 = dayjs(startDate),
          date2 = dayjs(endDate),
          day = date2.diff(date1, 'day') + 1
        if (day > 93) {
          this.$message.error('自定义时间范围限制为93天')
          return
        }
        this.form.dateType = ''
        this.$emit('search', this.getSearchTime())
      } else {
        this.form = this.$options.data.call(this).form
        this.search()
      }

    },
    search (e) {
      let { form: {
        dateType
      } } = this
      if (e == 'dateType') this.date = []
      if (this.hasStatisticalPeriod) {
        const selectedDateType = dateType && Object.keys(dateTypeMapping).includes(dateType) ? dateType : DATE_TYPES.DEFAULT
        this.statisticalPeriodOptions = dateTypeMapping[selectedDateType].options
        this.statisticalPeriod = dateTypeMapping[selectedDateType].period
      }
      // return this.$emit('search', {
      //   allYear: false,
      //   startDate: '2024-10-01',
      //   endDate: '2024-12-31'
      // })
      this.$emit('search', this.getSearchTime())
    },
    changeStatisticalPeriod (e) {
      this.$emit('search', this.getSearchTime())
    },
    getSearchTime () {
      let { dateGroup, date, form: {
        dateType
      } } = this,
        params = {
          allYear: dateType == 'year'
        },
        startDate = '',
        endDate = ''
      if (date && date.length) {
        startDate = this.formatDate(date[0])
        endDate = this.formatDate(date[1])
      } else {
        let dateItem = dateGroup.find(i => i.value == dateType)
        if (dateItem) {
          startDate = dateItem.startDate
          endDate = dateItem.endDate
        }
      }
      if (this.hasStatisticalPeriod) {
        params.dateType = {
          'day':0,
          'week':1,
          'month':2,
        }[this.statisticalPeriod]
      }
      params.startDate = startDate
      params.endDate = endDate
      return params
    },
    handleReset () {
      this.form = this.$options.data.call(this).form
      this.search('dateType')
    }
  }
}
</script>

<style scoped lang='scss'>
.search-box {
  padding: 15.25px 20px;
  position: relative;
  ::v-deep.el-form-item__label {
    width: fit-content;
  }
  .el-form-item--small.el-form-item {
    margin-bottom: 0 !important;
  }
  .el-icon-setting {
    margin-right: 4px;
  }
  .select {
    width: 233px;
  }
  .radio-group {
    transform: translateY(-2px);
  }
  .date-picker {
    width: 255px;
  }
  .data-config {
    position: absolute;
    top: 50%;
    right: 20px;
    transform: translateY(-50%);
  }
}
</style>