Newer
Older
qd_cnooc_front / src / views / dataManage / deviceData / waterHistory.vue
StephanieGitHub on 24 Apr 2022 4 KB fix<views>:优化细节,增加导出
<template>
  <el-dialog :visible.sync="dialogFormVisible" :title="this.listQuery.devCode+'历史数据'" width="1200px" append-to-body class="lineDialog">
    <div style="margin-bottom: 10px">
      <el-date-picker
        v-model="timeRange"
        type="datetimerange"
        range-separator="至"
        value-format="yyyy-MM-dd HH:mm:ss"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        size="small"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" size="small" @click="fetchData">搜索</el-button>
      <el-button class="filter-item" type="primary" size="small" @click="exportData">导出</el-button>
    </div>
    <div v-loading="loading1">
      <div>
        <el-table :data="data" size="mini" height="380px" >
          <el-table-column label="抄表时间" prop="uptime" width="140" align="center"/>
          <el-table-column label="日用水量(立方米)" prop="dayCount" align="center"/>
          <el-table-column label="总用水量(立方米)" prop="total" align="center"/>
<!--          <el-table-column label="阀门状态" align="center">-->
<!--            <template slot-scope="scope">{{ scope.status=='1'?'开':(scope.status=='0'?'关':'&#45;&#45;') }}</template>-->
<!--          </el-table-column>-->
        </el-table>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getDayTime } from '@/utils/dateutils'
import { totalData } from '@/api/device'
import { batchExportWaterHistory } from '@/api/data'

export default {
  name: 'WaterHistory',
  data() {
    return {
      loading1: true,
      timeRange: [],
      dialogFormVisible: false, // 对话框是否显示
      listQuery: {
        devCode: '',
        beginTime: '',
        endTime: ''
      },
      data:[]

    }
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.beginTime = val[0]
        this.listQuery.endTime = val[1]
      } else {
        this.listQuery.beginTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  methods: {
    initDialog: function(dialogFormVisible, row = null) {
      this.listQuery.devCode = row.devcode
      this.dialogFormVisible = dialogFormVisible
      const beginTime = getDayTime(new Date().getTime() - 24 * 7 * 60 * 60 * 1000).Format('yyyy-MM-dd hh:mm:ss')
      const endTime = new Date().Format('yyyy-MM-dd hh:mm:ss')
      this.timeRange = [beginTime, endTime]
      this.fetchData() // 传row
    },
    fetchData() {
      this.loading1 = true
      if(this.timeRange.length==2){
        this.listQuery.beginTime = this.timeRange[0]
        this.listQuery.endTime = this.timeRange[1]
      }
      totalData(this.listQuery).then(response => {
        this.data = response.data
        this.loading1 = false
      })
    },
    // 批量导出
    exportData() {
      console.log('批量导出液位配置')
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportWaterHistory(this.listQuery).then((res) => {
        loading.close() // 关闭加载动画
        console.log('download===', res)
        const blob = new Blob([res.data])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = `${this.listQuery.devCode}远传水表历史数据.xlsx` // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放blob对象
      })
      .catch((res) => {
        loading.close()
      })
    }
  }
}
</script>
<style>
  .el-dialog__header {
    padding: 10px !important;
  }
  .el-dialog__body {
    padding: 10px !important;
  }
  .lineDialog .el-table th{
    /*color: white !important;*/
    /*background-color: #488f7d !important;*/
  }

</style>