ZZJ
2022-07-15 b200eed05d5bff2e12a45331bdc062f4b00bebf7
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<template>
  <div class="file-uploader">
    <uploader
      v-if="single"
      ref="uploader"
      :options="options"
      :file-status-text="statusText"
      class="uploader-single"
      @file-added="onFileAdded"
      @complete="onComplete"
    >
      <!-- <uploader-drop v-if="isDrag == true">
        <div class="drag-txt">拖拽文件到这里</div>
        <span class="icon iconfont" @click.stop="showUpload = false"
          >&#xe70b;</span
        >
        <uploader-btn>选择文件</uploader-btn>
      </uploader-drop> -->
 
      <div class="up-bar" v-if="isDrag == true">
        <div class="name">{{ fileName || uploadPlaceholder }}</div>
        <uploader-btn slot="suffix">
          <el-tooltip :content="tipWords" placement="top" v-if="tip">
            <div class="open-file-btn">
              <span class="icon iconfont">&#xe712;</span>
            </div>
          </el-tooltip>
        </uploader-btn>
        <!-- <div class="open-file-btn">
          <span class="icon iconfont">&#xe712;</span>
        </div> -->
      </div>
      <el-input
        :placeholder="uploadPlaceholder"
        v-if="isDrag == false"
        size="small"
        :readonly="true"
        v-model="fileName"
      >
        <uploader-btn slot="suffix">
          <el-tooltip :content="tipWords" placement="top" v-if="tip">
            <i
              class="el-icon-upload2"
              style="font-size: 18px; color: #0088ff"
            ></i>
          </el-tooltip>
          <i
            v-else
            class="el-icon-upload2"
            style="font-size: 18px; color: #0088ff"
          ></i>
        </uploader-btn>
      </el-input>
      <uploader-list />
    </uploader>
 
    <uploader
      v-else
      ref="uploader"
      :options="options"
      :file-status-text="statusText"
      class="uploader-example"
      @file-added="onFileAdded"
      @file-complete="fileComplete"
      @complete="onComplete"
      @close="closeHandle"
    >
      <uploader-btn ref="button" :sourceType="sourceType">
        <i class="el-icon-upload2" style="font-size: 18px; color: #0088ff"></i>
        上传
      </uploader-btn>
      <uploader-list />
    </uploader>
  </div>
</template>
 
<script>
import uploader from "./uploader";
import SparkMD5 from "spark-md5";
import UploaderBtn from "./btn";
import UploaderList from "./list";
import UploaderDrop from "./drop";
 
export default {
  components: {
    uploader,
    UploaderBtn,
    UploaderList,
    UploaderDrop,
  },
  props: {
    sourceType: {
      type: Number,
    },
    tip: {
      type: Boolean,
      default: false,
    },
    tipWords: {
      type: String,
      default: "",
    },
    single: {
      type: Boolean,
      default: false,
    },
    uploadPlaceholder: {
      type: String,
      default: "",
    },
    isDrag: {
      type: Boolean,
      default: false,
    },
    url: {
      type: String,
      default: "/data/api-f/file/upload",
    },
    attrs: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      fileName: "",
      fileMd5: "",
      statusText: {
        success: "上传成功",
        error: "上传失败",
        uploading: "上传中",
        paused: "暂停中",
        waiting: "等待中",
      },
    };
  },
  computed: {
    uploader() {
      return this.$refs.uploader.uploader;
    },
    options() {
      return {
        target: this.url,
        testChunks: true,
        headers: {
          Authorization:
            sessionStorage.getItem("loginedInfo") &&
            JSON.parse(sessionStorage.getItem("loginedInfo")).access_token,
        },
      };
    },
  },
  methods: {
    onFileAdded(file) {
      if (this.single) {
        this.uploader.fileList = this.uploader.fileList.slice([-1]);
        this.$emit("file-added");
      }
      this.computeMD5(file);
    },
    computeMD5(file) {
      let fileReader = new FileReader();
      let time = new Date().getTime();
      let blobSlice =
        File.prototype.slice ||
        File.prototype.mozSlice ||
        File.prototype.webkitSlice;
      let currentChunk = 0;
      const chunkSize = 10 * 1024 * 1000;
      let chunks = Math.ceil(file.size / chunkSize);
      let spark = new SparkMD5.ArrayBuffer();
      // 文件状态设为"计算MD5"
      this.statusText.paused = "准备上传,正在检查文件";
      file.pause();
      loadNext();
      fileReader.onload = (e) => {
        spark.append(e.target.result);
        if (currentChunk < chunks) {
          currentChunk++;
          loadNext();
        } else {
          let md5 = spark.end();
          this.computeMD5Success(md5, file);
          this.fileName = file.name;
          this.fileMd5 = md5;
        }
      };
      fileReader.onerror = function () {
        this.error(`文件${file.name}读取出错,请检查该文件`);
        file.cancel();
      };
      function loadNext() {
        let start = currentChunk * chunkSize;
        let end =
          start + chunkSize >= file.size ? file.size : start + chunkSize;
        fileReader.readAsArrayBuffer(blobSlice.call(file.file, start, end));
      }
    },
    computeMD5Success(md5, file) {
      //将自定义参数直接加载uploader实例的opts上
      if (location.href.indexOf("dataStack") >= 0) {
        Object.assign(this.uploader.opts, {
          query: {
            stackId: this.DataStackPool.selectedDir.id,
            // ...this.params,
          },
        });
      }
      file.uniqueIdentifier = md5;
      file.resume();
      this.statusText.paused = "暂停中";
    },
    onComplete() {
      this.$emit("complete", {
        filename: this.fileName,
        identifier: this.fileMd5,
      });
    },
    fileComplete() {},
    closeHandle() {
      this.$emit("close");
    },
  },
  mounted() {
    this.isDrag;
    this.$nextTick(() => {
      window.uploader = this.$refs.uploader.uploader;
    });
  },
};
</script>
 
<style lang="scss">
.file-uploader {
  .el-input__suffix,
  .el-input__suffix-inner {
    outline: none !important;
  }
  .el-tooltip.focusing {
    outline: none;
  }
  .uploader-example {
    width: 99%;
    // padding: 15px;
    // margin: 40px auto 0;
    font-size: 12px;
    // box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
    background-color: #fff;
  }
  .uploader-example .uploader-btn {
    position: relative;
    display: none;
    // float: right;
    // top: -45px;
  }
  .uploader-example .uploader-list {
    max-height: 440px;
    overflow: auto;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .uploader-single {
    position: unset;
    .close {
      display: none;
    }
    .uploader-btn {
      border: 0px;
    }
    .uploader-file {
      height: 2px;
      .uploader-file-progress {
        background: #3d68e1;
      }
      .uploader-file-info {
        display: none;
      }
    }
 
    .up-bar {
      height: 30px;
      margin: 25px;
      background: #f2f2f7;
      border-radius: 2px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      box-sizing: border-box;
      padding: 0 20px;
      .iconfont {
        font-size: 16px;
        color: #333;
      }
      .name {
        color: #bdbdbd;
        font-size: 14px;
      }
    }
  }
}
</style>>