repeatCustomerStore.vue 5.55 KB
<template>
  <div class="repeat-customer-container" v-loading="loading">
    <div class="analysis-date-condition">
      <div class="condition-item-option">
        <el-date-picker
          class="date-input analysis-date-input times-box"
          v-model="customTime"
          value-format="yyyy-MM-dd"
          align="right"
          type="daterange"
          size="mini"
          :picker-options="pickerOptions1"
          range-separator="-"
          :start-placeholder="$t('table.startTime')"
          :end-placeholder="$t('table.endTime')">
        </el-date-picker>
      </div>
      <div class="condition-item btn-condition-item">
        <el-button type="primary" class="primary-btn analysis-collapse-btn" size="samll" @click="clickHandle">{{$t('button.confirm')}}</el-button>
        <el-button type="primary" class="primary-btn analysis-collapse-btn" size="samll" @click="downloadHandle">{{$t('allPages.loadData')}}</el-button>
      </div>
    </div>
    <el-table :data="tableData" class="asis-table baseasis-table" :max-height="tableHeight">
      <el-table-column prop="orderNum" align="center" label="序号"></el-table-column>
      <el-table-column prop="mallId" align="center" label="门店ID"></el-table-column>
      <el-table-column prop="name" align="center" label="门店名称" show-overflow-tooltip></el-table-column>
      <el-table-column prop="countdate" align="center" label="日期" :formatter="dateFormatter"></el-table-column>
      <el-table-column prop="count" align="center" label="多次到店人数"></el-table-column>
    </el-table>
    <el-pagination 
      class="page-wrapper"
      background
      tooltip-effect="light"
      :current-page="currentPage" 
      :page-sizes="[10, 20, 50, 100]" 
      :page-size="pageSize" 
      @size-change="sizeChange" 
      @current-change="currentChange" 
      layout="sizes, prev, pager, next, slot, jumper" 
      :total="total">
      <span class="slot-ele-total">{{$t('table.pageHead')}} {{ total }} {{$t('table.pageTail')}}</span>
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      customTime: [],
      loading: false,
      allData: [],
      tableData: [],
      downloadData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  computed: {
    tableHeight() {
      // 浏览器内部高度 - 条件容器高 - 页码容器 - 底部距离
      return window.innerHeight - 54 - 58 - 40
    }
  },
  watch: {
  },
  created() {
    this.customTime = [
      this.dayReduce(new Date(), 7),
      dateUnit.dateFormat(new Date(), 'yyyy-MM-dd')
    ]
  },
  mounted() {
    // this.getData()
  },
  methods: {
    getData() {
      let { customTime } = this
        , props = ['mallId', 'name', 'countdate', 'count']

      this.currentPage = 1
      this.total = 0
      this.tableData = []
      this.downloadData = []
      this.$api.analysisReport.historyArrival({
        startDate: customTime[0],
        endDate: customTime[1]
      })
      .then(res => {
        let result = res.data
        if (result.msg_code === 200) {
          if (!result.data.length) return
          let resultData = result.data.map(item => {
            return this.sortProp(props, item)
          })
          this.allData = resultData.map((item, index) => {
            this.downloadData.push(Object.values(item))
            return {
              ...item,
              orderNum: ++index
            }
          })
          this.total = resultData.length
          this.tableData = this.pagination()
        } 
        else {
          this.$message({
            message: result.msg_info,
            type: 'error'
          })
        }
        this.loading = false
      })
      .catch(error => {
        this.loading = false
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    sortProp(props, sortObj) {
      let obj = {}
      props.forEach(k => {
        obj[k] = sortObj[k]
      })
      return obj
    },
    clickHandle() {
      this.loading = true
      this.getData()
    },
    pagination() {
      let 
        { currentPage, pageSize, allData } = this
        , offset = (currentPage - 1) * pageSize
      return (offset + pageSize >= allData.length)
        ? this.allData.slice(offset, allData.length)
        : this.allData.slice(offset, offset + pageSize)
    },
    currentChange(val) {
      if (this.currentPage != val) {
				this.currentPage = val
				this.tableData = this.pagination()
			}
    },
    sizeChange(val) {
      this.pageSize = val
			this.tableData = this.pagination()
    },
    dateFormatter(row, column, cellValue) {
      return cellValue.replace(/^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/, '$1-$2-$3')
    },
    downloadHandle() {
      import('@/utils/Export2Excel').then(excel => {
        excel.export_json_to_excel({
            header: ['门店ID', '门店名称', '日期', '多次到店人数'],
            data: this.downloadData,
            filename: '多次到店人数报表',
            autoWidth: true,
            bookType: 'xlsx'
        })
      })
    }
  }
}
</script>

<style scoped>
.repeat-customer-container {
  width: 100%;
}

.repeat-customer-container .analysis-date-condition {
  padding: 15px 0;
  margin: 0 auto;
  background: #fff;
  box-sizing: border-box;
  overflow: hidden;
}

.repeat-customer-container .condition-item-option {
  float: left;
  width: 50%;
  text-align: right;
}

.repeat-customer-container .page-wrapper {
  padding-top: 20px;
}
</style>