分支自 SuZhouGuanHong/TaiYuanTaiZhong

huanghongfeng
2024-07-08 32678f5c24c7681a198b517fa72db1de2e98646d
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<template>
    <el-dialog v-model="detialBox" title="上传DNC文件" :close-on-press-escape="false" :width="width"
      :fullscreen="fullscreen" :draggable="draggable" :modal="modal" :before-close="handleClose">
    <div class="upload-container">
        
      <a :href="template.url" ref="template"></a>
      <div class="button-group">
        <el-upload
          style="float: left"
          ref="uploadFile"
          :max-size="maxSize"
          :on-change="clearMsg"
          :before-upload="beforeUpload"
          :action="url"
          :change="handleFileChange"
        >
        <!-- <input type="file"  @change="handleFileChange"> -->
          <el-button size="small"
            ><i class="el-icon-folder-opened"></i>选择文件</el-button
          >
        </el-upload>
        <el-button
          v-if="template.url"
          style="margin-left: 10px"
          type="primary"
          size="small"
          @click="dowloadTemplate"
          :loading="loadingStatus"
        >
       <i class="el-icon-bottom"></i>
          下载模板</el-button
        >
        <el-button
          type="success"
          style="margin-left: 10px"
          size="small"
          @click="upload"
          :loading="loadingStatus"
        >
            <i class="el-icon-top"></i>
          上传文件</el-button
        >
      </div>
      <div class="alert">
        <el-alert title="上传说明" type="warning" :closable="false" show-icon
          >只能上传<!--excel文件,-->文件大小不超过{{ maxSize }}M的文件</el-alert
        >
      </div>
  
      <div v-if="file">
        <h3>文件列表</h3>
        <div class="file-info">
          <span>文件名:{{ file.name }}</span>
          <span>大小{{ (file.size / 1024).toFixed(2) }}KB</span>
        </div>
      </div>
      <div v-show="message" class="v-r-message">
        <h3 class="title">上传结果</h3>
        <div class="text" :class="resultClass" v-html="message"></div>
      </div>
      <slot></slot>
      
    </div>
    </el-dialog>
  </template>
  <script>
  //目前只支持单个Excel上传,其他功能开发中...
  export default {
    components: {},
    props: {
      url: {
        type: String,
        default: ''
      },
      template: {
        //下载模板配置
        type: Object,
        default: () => {
          return {
            url: '', //模板下载路径,如果没有模板路径,则不显示下载模板功能
            fileName: '未定义文件名' //下载模板的文件名
          };
        }
      },
      importExcelBefore: {
        type: Function,
        default: (file) => {
          return true;
        }
      }
    },
    data() {
      return {
        detialBox:false,
        maxSize: 10,
        model: true,
        file: null,
        loadingStatus: false,
        message: '',
        resultClass: ''
      };
    },
    methods: {
      handleFileChange(event) {
      const file = event.target.files[0]; // 获取第一个选定的文件对象
      console.log('文件路径:', file.path); // 输出文件路径到控制台
      
      // 如果想进行其他操作或者发送文件路径给服务器等,可以在这里编写相应逻辑
    },
      clearMsg() {
        this.message = '';
      },
      reset() {
        this.file = null;
        this.message = '';
        this.resultClass = '';
      },
      getFileType() {
        let fileName =
          this.file.name
            .split('.')
            .pop()
            .toLocaleLowerCase() || '';
        // if (['numbers', 'csv', 'xls', 'xlsx','SPF'].indexOf(fileName) == -1) {
        //   this.$Message.error('只能选择excel文件');
        //   return false;
        // }
        return true;
      },
      beforeUpload(file) {
        this.file = file;
        if (!this.getFileType()) {
          return false;
        }
        return false;
      },
      upload() {
        // let _url = this.url;
        let _url = "http://localhost:8099/api/dt_Machineinfo/Upload";
        if (!_url) {
          return this.$Message.error('没有配置好Url');
        }
   
        if (!this.file) {
          return this.$Message.error('请选择文件');
        }
        var formData = new FormData();
        formData.append('fileInput', this.file);
        // if (!this.importExcelBefore(formData)) {
        //   return;
        // }
        this.loadingStatus = true;
        this.http.post(_url, formData).then(
          (x) => {
            // this.$refs.uploadFile.clearFiles();
            this.loadingStatus = false;
            this.file = null;
            if (x.status) {
              this.$emit('importExcelAfter', x);
            }
  
            this.message = x.message;
            this.resultClass = x.status ? 'v-r-success' : 'v-r-error';
          },
          (error) => {
            this.loadingStatus = false;
          }
        );
      },
      dowloadTemplate() {
        let url = this.template.url;
        let xmlResquest = new XMLHttpRequest();
        xmlResquest.open('GET', url, true);
        xmlResquest.setRequestHeader('Content-type', 'application/json');
        xmlResquest.setRequestHeader(
          'Authorization',
          this.$store.getters.getToken()
        );
        let fileName = this.template.fileName + '.xlsx';
        let elink = this.$refs.template;
        xmlResquest.responseType = 'blob';
        let $_vue = this;
        this.loadingStatus = true;
        xmlResquest.onload = function(oEvent) {
          $_vue.loadingStatus = false;
          if (xmlResquest.response.type == 'application/json') {
            return $_vue.message.error('未找到下载文件');
          }
          let content = xmlResquest.response;
          elink.download = fileName;
          let blob = new Blob([content]);
          elink.href = URL.createObjectURL(blob);
          elink.click();
        };
        xmlResquest.send();
      }
    }
  };
  </script>
  <style lang="less" scoped>
  .upload-container {
    min-height: 270px !important;
    display: inline-block;
    width: 100%;
    padding: 10px;
    border: 1px dashed #989898;
    min-height: 250px;
    border-radius: 5px;
    .alert {
      margin-top: 12px;
    }
    .el-button-group > * {
      display: flex;
    }
    h3 {
      margin: 9px 0px;
    }
    .file-info > span {
      margin-right: 20px;
    }
    .v-r-message {
      margin-top: 10px;
      .title {
        margin-bottom: 2px;
      }
      > .text {
        font-size: 13px;
      }
      .v-r-success {
        color: #02b702;
      }
      .v-r-error {
        color: #dc0909;
      }
    }
  }
  </style>