luoyb
2021-03-12 4e6b24f719a8a44eb8efd9c47e846cf86137e205
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" icon="el-icon-download" type="primary" plain @click="templateDownload">
        {{ $t('table.templateDownload') }}
      </el-button>
      <el-upload
        class="upload"
        :action="uploadAction"
        :headers="headers"
        list-type="picture"
        :show-file-list="false"
        :before-upload="beforeUpload"
        :on-success="uploadSuccess"
        :on-error="uploadError"
        :on-progress="uploadProgress"
      >
        <el-button class="filter-item" icon="el-icon-upload2" type="success" plain>
          {{ $t('table.import') }}
        </el-button>
      </el-upload>
      <el-button class="filter-item" icon="el-icon-download" type="info" plain @click="exportExcel">
        {{ $t('table.export') }}
      </el-button>
    </div>
    <el-table
      ref="table"
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column :label="$t('table.eximport.field1')" prop="field1" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.field1 }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.eximport.field2')" prop="field2" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.field2 }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.eximport.field3')" prop="field3" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.field3 }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.eximport.createTime')" prop="createTime" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="pagination.num" :limit.sync="pagination.size" @pagination="search" />
    <result
      :dialog-visible="dialogVisible"
      :data="data"
      :error="error"
      :time="time"
      @close="closeDialog"
    />
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import Result from './Result'
import { getToken } from '@/utils/auth'
import { getFileType } from '@/utils'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
 
export default {
  name: 'ExImport',
  components: { Pagination, Result },
  data() {
    return {
      uploadAction: `${process.env.VUE_APP_BASE_API}system/eximport/import`,
      dialogVisible: false,
      tableKey: 0,
      loading: false,
      list: null,
      total: 0,
      pagination: {
        size: 10,
        num: 1
      },
      fileList: [],
      headers: {
        Authorization: `bearer ${getToken()}`
      },
      data: [],
      error: [],
      time: '0 ms'
    }
  },
  mounted() {
    this.fetch()
  },
  methods: {
    closeDialog() {
      this.dialogVisible = false
    },
    templateDownload() {
      this.$download('system/eximport/template', {}, 'excel_import_template.xlsx')
    },
    exportExcel() {
      this.$download('system/eximport/excel', {
        pageSize: this.pagination.size,
        pageNum: this.pagination.num
      }, `export_${new Date().getTime()}.xlsx`)
    },
    beforeUpload(file) {
      const type = getFileType(file.name)
      if (type !== 'xlsx') {
        this.$message({
          message: this.$t('tips.onlySupportXlsx'),
          type: 'error'
        })
        return false
      } else {
        return true
      }
    },
    uploadError() {
      this.$message({
        message: this.$t('tips.uploadFailed'),
        type: 'error'
      })
      NProgress.done()
    },
    uploadSuccess(response) {
      const data = response.data
      this.data = data.data
      this.error = data.error
      this.time = data.time
      NProgress.done()
      this.fetch()
      this.dialogVisible = true
    },
    uploadProgress() {
      NProgress.start()
    },
    fetch(params = {}) {
      this.loading = true
      params.pageSize = this.pagination.size
      params.pageNum = this.pagination.num
      this.$get('system/eximport', { ...params }).then((r) => {
        const data = r.data.data
        this.total = data.total
        this.list = data.rows
        this.loading = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .upload {
    display: inline-block;
  }
</style>