Blame view

ant-design-vue-jeecg/src/components/jeecg/JImportModal.vue 4.08 KB
肖超群 authored
1
2
3
4
5
6
7
8
9
10
11
<template>
  <a-modal
    title="导入EXCEL"
    :width="600"
    :visible="visible"
    :confirmLoading="uploading"
    @cancel="handleClose">

    <div style="margin: 0px 0px 5px 1px" v-if="online">
      <span style="display: inline-block;height: 32px;line-height: 32px;vertical-align: middle;">是否开启校验:</span>
      <span style="display: inline-block;height: 32px;margin-left: 6px">
肖超群 authored
12
13
         <a-switch :checked="validateStatus==1" @change="handleChangeValidateStatus" checked-children="是"
                   un-checked-children="否" size="small"/>
肖超群 authored
14
15
16
17
18
19
20
21
22
23
24
      </span>
    </div>

    <a-upload
      name="file"
      :multiple="true"
      accept=".xls,.xlsx"
      :fileList="fileList"
      :remove="handleRemove"
      :beforeUpload="beforeUpload">
      <a-button>
肖超群 authored
25
        <a-icon type="upload"/>
肖超群 authored
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        选择导入文件
      </a-button>
    </a-upload>

    <template slot="footer">
      <a-button @click="handleClose">关闭</a-button>
      <a-button
        type="primary"
        @click="handleImport"
        :disabled="fileList.length === 0"
        :loading="uploading">
        {{ uploading ? '上传中...' : '开始上传' }}
      </a-button>
    </template>

  </a-modal>
</template>

<script>
肖超群 authored
45
46
47
48
49
50
51
52
53
import {postAction} from '@/api/manage'

export default {
  name: 'JImportModal',
  props: {
    url: {
      type: String,
      default: '',
      required: false
肖超群 authored
54
    },
肖超群 authored
55
56
57
58
    biz: {
      type: String,
      default: '',
      required: false
肖超群 authored
59
    },
肖超群 authored
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    //是否online导入
    online: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      visible: false,
      uploading: false,
      fileList: [],
      uploadAction: '',
      foreignKeys: '',
      validateStatus: 0
    }
  },
  watch: {
    url(val) {
      if (val) {
        this.uploadAction = window._CONFIG['domianURL'] + val
肖超群 authored
81
      }
肖超群 authored
82
83
84
85
86
87
88
89
90
    }
  },
  created() {
    this.uploadAction = window._CONFIG['domianURL'] + this.url
  },

  methods: {
    handleClose() {
      this.visible = false
肖超群 authored
91
    },
肖超群 authored
92
93
94
95
96
97
    show(arg) {
      this.fileList = []
      this.uploading = false
      this.visible = true
      this.foreignKeys = arg;
      this.validateStatus = 0
肖超群 authored
98
    },
肖超群 authored
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
    handleRemove(file) {
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList
    },
    beforeUpload(file) {
      this.fileList = [...this.fileList, file]
      return false;
    },
    handleImport() {
      const {fileList} = this;
      const formData = new FormData();
      if (this.biz) {
        formData.append('isSingleTableImport', this.biz);
      }
      if (this.foreignKeys && this.foreignKeys.length > 0) {
        formData.append('foreignKeys', this.foreignKeys);
      }
      if (this.online == true) {
        formData.append('validateStatus', this.validateStatus);
      }
      fileList.forEach((file) => {
        formData.append('files[]', file);
      });
      this.uploading = true
      postAction(this.uploadAction, formData).then((res) => {
肖超群 authored
126
        this.uploading = false
肖超群 authored
127
128
129
130
131
        if (res.success) {
          if (res.code == 201) {
            this.errorTip(res.message, res.result)
          } else {
            this.$message.success(res.message)
肖超群 authored
132
          }
肖超群 authored
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
          this.visible = false
          this.$emit('ok')
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    // 是否开启校验 开关改变事件
    handleChangeValidateStatus(checked) {
      this.validateStatus = checked == true ? 1 : 0
    },
    // 错误信息提示
    errorTip(tipMessage, fileUrl) {
      const h = this.$createElement;
      let href = window._CONFIG['domianURL'] + fileUrl
      this.$warning({
        title: '导入成功,但是有错误数据!',
        content: h('div', {}, [
          h('div', tipMessage),
          h('span', '具体详情请 '),
          h('a', {
            attrs: {
              href: href,
              target: '_blank'
            },
          }, '点击下载'),
        ]),
        onOk() {
        },
      });
    },
肖超群 authored
164
165

  }
肖超群 authored
166
}
肖超群 authored
167
168
169
170
171
</script>

<style scoped>

</style>