1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<template>
  <j-modal :visible="visible" title="批量导出" :confirmLoading="confirmLoading" @ok="handleSubmit('设备日志')"
           @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validateRules" :labelCol="{span:4}" :wrapperCol="{span:20}">
        <a-form-model-item label="日期" prop="dates">
          <a-range-picker v-model="model.dates" value-format="YYYY-MM-DD" :disabledDate="disabledDate"
                          @calendarChange="handleCalendarChange" @openChange="handleCalendarOpenChange"
                          :allow-clear="false"/>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
 
  </j-modal>
</template>
 
<script>
  import moment from 'moment'
  import { downFile } from '@/api/manage'
 
  export default {
    name: 'DeviceLogBatchExportModal',
    props: {
      equipmentId: {
        type: String
      }
    },
    data() {
      return {
        visible: false,
        confirmLoading: false,
        model: {},
        validateRules: {
          dates: [{ required: true, message: '请选择日期!' }]
        },
        preSelectFirstDate: null,
        url: {
          batchExportXlsUrl: '/mdc/mdcEquipmentRunningSection/batchExportLogXls'
        }
      }
    },
    computed: {
      disabledDate() {
        // current为需要禁用的时间段
        return current => {
          if (!this.preSelectFirstDate) {
            return current > moment().endOf('days')
          } else {
            return current < moment(this.preSelectFirstDate).subtract(30, 'days').startOf('days')
              || current > moment().endOf('days')
              || current > moment(this.preSelectFirstDate).add(30, 'days').endOf('days')
          }
        }
      }
    },
    methods: {
      handleSubmit(fileName) {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.confirmLoading = true
            if (!fileName || typeof fileName != 'string') {
              fileName = '导出文件'
            }
            const param = {
              equipmentId: this.equipmentId,
              startTime: this.model.dates[0],
              endTime: this.model.dates[1]
            }
            console.log('导出参数', param)
            downFile(this.url.batchExportXlsUrl, param)
              .then((data) => {
                if (!data) {
                  // this.$message.warning("文件下载失败")
                  this.$notification.warning({
                    message: '消息',
                    description: '文件下载失败'
                  })
                  return
                }
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                  window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
                } else {
                  let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
                  let link = document.createElement('a')
                  link.style.display = 'none'
                  link.href = url
                  link.setAttribute('download', fileName + '.xls')
                  document.body.appendChild(link)
                  link.click()
                  document.body.removeChild(link) //下载完成移除元素
                  window.URL.revokeObjectURL(url) //释放掉blob对象
                }
                this.handleCancel()
              })
              .finally(() => {
                this.confirmLoading = false
              })
          } else {
            return false
          }
        })
      },
 
      handleCalendarChange(dates) {
        console.log('dates', dates)
        if (dates.length === 1) this.preSelectFirstDate = dates[0]
      },
 
      handleCalendarOpenChange(status) {
        if (status) {
          if (this.model.dates && this.model.dates.length > 0) {
            this.preSelectFirstDate = this.model.dates[0]
          } else {
            this.preSelectFirstDate = null
          }
        }
      },
 
      handleCancel() {
        this.$refs.form.clearValidate()
        this.model = {}
        this.visible = false
      }
    }
  }
</script>
 
<style scoped>
 
</style>