Blame view

admin-web-master/src/components/fujianUpload/fujianList.vue 11 KB
3f535f30   杨鑫   '初始'
1
  <template>
a182f238   wesley88   1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    <div class="upload-container">
      <div class="card-list-box">
  
        <div style="position: relative;width: 100%;">
          <input :accept="options.accept" style="opacity: 0;position: absolute;" :id="inputtype" ref="fileInput" type="file" :multiple="true"
          @change="handleUploadFile" />
          <div v-if="localValue && localValue.length < options.limit && !options.disabled" style="display: flex;align-items: center;width: 100%;">
            <el-button  @click="handleUpload" style="background-color: #fff;color: #000;border: 1px solid #dcdfe6;" class="buttonHover">
              <div style="display: flex;align-items: center;">
                <img src="@/assets/images/upload.png" style="width: 16px;height: 16px;margin-right: 4px;" alt="">
                <span style="">上传附件</span>
              </div>
            </el-button>
            <div class="tips" style="margin-left: 10px;">不超过{{ options.limitSize }}MB</div>
          </div>
        </div>
        <div v-for="(file, i) in localValue" :key="'upload-BaseImage' + i" style="display: flex;justify-content: space-between;width: 100%;">
          <div>文件{{i+1}}</div>
          <div title="删除" class="delete-box"
            @click="handleDeleteImg(file, i)">
            <i class="el-icon-close"></i>
118fc86d   wesley88   1
23
          </div>
a182f238   wesley88   1
24
        </div>
118fc86d   wesley88   1
25
      </div>
a182f238   wesley88   1
26
    </div>
3f535f30   杨鑫   '初始'
27
  </template>
a182f238   wesley88   1
28
  
3f535f30   杨鑫   '初始'
29
  <script>
a182f238   wesley88   1
30
    //   import { uploadFiles, uploadStraightFile } from '@/api/commonApi/file';
3f535f30   杨鑫   '初始'
31
    import {
d64cd58f   wesley88   上传验收小程序
32
33
      miniioupload
    } from '@/api/commodityLease.js'
a182f238   wesley88   1
34
35
    import BaseImage from '@/components/BaseImage/index.vue';
  
d64cd58f   wesley88   上传验收小程序
36
    export default {
a182f238   wesley88   1
37
38
39
40
      components: {
        BaseImage,
      },
      name: 'BaseUpload',
d64cd58f   wesley88   上传验收小程序
41
      props: {
a182f238   wesley88   1
42
43
44
45
46
47
48
49
50
51
        value: {
          type: String,
          default: '',
        },
        cmpOption: {
          type: Object,
          default: () => ({
            disabled: false,
            isSetCover: true,
          }),
d64cd58f   wesley88   上传验收小程序
52
53
54
        },
        filePath: {
          type: String,
a182f238   wesley88   1
55
          default: 'other',
d64cd58f   wesley88   上传验收小程序
56
        },
a182f238   wesley88   1
57
        inputtype: {
d64cd58f   wesley88   上传验收小程序
58
          type: String,
a182f238   wesley88   1
59
60
61
62
63
          default: '',
        },
        limit: {
          type: Number,
          default: 1,
d64cd58f   wesley88   上传验收小程序
64
        },
d64cd58f   wesley88   上传验收小程序
65
      },
3f535f30   杨鑫   '初始'
66
67
      data() {
        return {
a182f238   wesley88   1
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
          fileList:[],
          onforIndex:0,
          localValue: [],
          options: {
            limitSize: 10,
            accept: '.mp4',
            multiple: false,
            limit: 1,
            disabled: false,
            field: 'files',
            reqParams: {
              code: 2000
            },
          },
          reactiveData: {
            files: [],
            filesIng: [],
            status: 'uploading',
            message: '上传中...',
            total: 0,
            loaded: 0,
            lengthComputable: false,
          },
        };
d64cd58f   wesley88   上传验收小程序
92
93
      },
      watch: {
a182f238   wesley88   1
94
95
96
97
98
99
100
101
102
103
104
105
        limit: {
          handler() {
            this.options.limit= this.limit
          },
          immediate: true,
        },
        cmpOption: {
          handler() {
            this.setOptions();
          },
          immediate: true,
        },
d64cd58f   wesley88   上传验收小程序
106
107
        value: {
          handler(val) {
a182f238   wesley88   1
108
            if (val && val !='') {
d64cd58f   wesley88   上传验收小程序
109
              // 首先将值转为数组
d64cd58f   wesley88   上传验收小程序
110
111
              const list = Array.isArray(val) ? val : this.value.split(',')
              // 然后将数组转为对象数组
a182f238   wesley88   1
112
113
              this.fileList = list
              this.localValue = list.map(item => {
d64cd58f   wesley88   上传验收小程序
114
                if (typeof item === 'string') {
a182f238   wesley88   1
115
116
                  item = {
                    address: item,
d64cd58f   wesley88   上传验收小程序
117
                  }
d64cd58f   wesley88   上传验收小程序
118
                }
a182f238   wesley88   1
119
120
                return item
              })
d64cd58f   wesley88   上传验收小程序
121
            } else {
a182f238   wesley88   1
122
              this.localValue = []
d64cd58f   wesley88   上传验收小程序
123
124
125
126
              this.fileList = []
              return []
            }
          },
a182f238   wesley88   1
127
128
          immediate: true,
        },
3f535f30   杨鑫   '初始'
129
130
      },
      methods: {
a182f238   wesley88   1
131
132
133
134
        async handleUploadFile(value) {
          const files = value.target.files
          if (!this.filesCheck(files)) {
            return;
d64cd58f   wesley88   上传验收小程序
135
          }
a182f238   wesley88   1
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
          this.reactiveData.filesIng = new Array(files.length);
          this.reactiveData.lengthComputable = false;
          this.reactiveData.status = 'uploading';
          this.reactiveData.total = 0;
          this.reactiveData.loaded = 0;
          let c1 =  await this.asyncFileUpload(Array.from(files));
          console.error('-----------')
          console.error(this.localValue)
          console.error(c1)
          let list = []
          for (let i = 0; i < c1.length; i++) {
            let info = {
              address:c1[i]
            }
            list.push(info)
          }
          console.error(list)
          this.localValue = [...this.localValue,...list]
          this.reactiveData.filesIng = []
          this.fileList = []
          for (let i = 0; i < this.localValue.length; i++) {
            this.fileList.push(this.localValue[i].address)
          }
          let urlstr = this.fileList.join(',')
          this.$emit('changimg',urlstr)
          
3f535f30   杨鑫   '初始'
162
        },
a182f238   wesley88   1
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
        setOptions() {
          this.options = {
            ...this.options,
            ...this.cmpOption
          };
        },
        uploadPercentage() {
          return parseInt(String(this.reactiveData.loaded / (this.reactiveData.total / 100)));
        },
        handleUpload() {
          const input = document.querySelector('#'+this.inputtype)
          input.click()
        },
        async asyncFileUpload(formDataArray) {
          console.error(formDataArray)
          try {
            // 初始化上传状态
            this.reactiveData.status = 'uploading';
            this.reactiveData.filesIng = formDataArray.map(() => ({
              status: 'pending',
              loaded: 0,
              total: 0
            }));
            this.reactiveData.lengthComputable = false;
            this.reactiveData.total = 0;
            this.reactiveData.loaded = 0;
            let list = []
            // 循环遍历每个文件并进行上传
            for (let i = 0; i < formDataArray.length; i++) {
              const fileData = formDataArray[i];
  
              // 创建一个新的 FormData 对象
              let fd = new FormData();
              fd.append('file', fileData); // 假设 formData 包含文件
              fd.append('filePath', this.filePath);
  
              // 更新当前文件的上传状态
              this.reactiveData.filesIng[i].status = 'uploading';
  
              // 调用 miniioupload 方法并等待响应
              const res = await miniioupload(fd);
              list.push(res.data)
              // 处理成功响应
              this.reactiveData.filesIng[i].status = 'success';
         
            }
  
            // 所有文件上传完成
            this.reactiveData.status = 'success';
            this.$message.success('文件上传成功');
            console.error(list)
            return list
          } catch (error) {
            // 处理错误
            console.error('上传失败:', error);
            this.reactiveData.status = 'error';
            this.reactiveData.filesIng = formDataArray.map(() => ({
              status: 'error',
              loaded: 0,
              total: 0
            }));
            this.$message.error('上传失败');
            return []
          }
        },
        async handleDeleteImg(file, i) {
          let modelValue_ = this.localValue.filter((value, index) => index != i);
          this.localValue = modelValue_
          this.fileList = []
          for (let i = 0; i < this.localValue.length; i++) {
            this.fileList.push(this.localValue[i].address)
          }
          let urlstr = this.fileList.join(',')
          this.$emit('changimg',urlstr)
        },
        async handleSetCover(file, i) {
          console.error(file, i)
          this.onforIndex = i
d64cd58f   wesley88   上传验收小程序
241
242
        },
        filesCheck(files) {
a182f238   wesley88   1
243
244
245
246
247
248
          const {
            limitSize,
            accept,
            limit
          } = this.options;
          if (this.reactiveData.files.length + files.length > limit) {
d64cd58f   wesley88   上传验收小程序
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
            this.$message.error(`最多上传${limit}个文件`);
            return false;
          }
          for (let index = 0; index < files.length; index++) {
            const file = files[index];
            if (!this.checkFileType(file)) {
              this.$message.error(`请上传${accept}等格式`);
              return false;
            }
            if (!this.checkSize(file)) {
              this.$message.error(`文件大小不能超过 ${limitSize}MB!`);
              return false;
            }
          }
  
          return true;
        },
a182f238   wesley88   1
266
267
268
269
        checkSize({
          size
        }) {
          return size <= this.options.limitSize * 1024 * 1024;
3f535f30   杨鑫   '初始'
270
        },
a182f238   wesley88   1
271
272
273
274
275
276
277
        checkFileType(file) {
          console.error(file)
          let {
            accept
          } = this.options;
          if (!accept || accept === '*') {
            return true;
3f535f30   杨鑫   '初始'
278
          }
a182f238   wesley88   1
279
280
281
282
          accept = accept.toLowerCase().replace(/\s/g, '');
          const acceptTypeList = accept.split(',');
          const fileType = file.name.match(/\.\w*$/)?.shift() ?? '';
          return acceptTypeList.includes(fileType.toLowerCase());
3f535f30   杨鑫   '初始'
283
        },
a182f238   wesley88   1
284
285
      },
    };
3f535f30   杨鑫   '初始'
286
  </script>
a182f238   wesley88   1
287
  
d64cd58f   wesley88   上传验收小程序
288
  <style lang="scss" scoped>
a182f238   wesley88   1
289
290
291
292
    .upload-container {
      width: 100%;
  
      .tips {
118fc86d   wesley88   1
293
294
295
296
        color: #0006;
        font: 14px 'Alibaba PuHuiTi 2.0-55 Regular';
        line-height: 38px;
      }
a182f238   wesley88   1
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  
      .action-box {
        cursor: pointer;
        padding-left: 10px;
  
        .default-action {
          color: #3f9b6a;
        }
      }
  
      .card-list-box {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
  
        .card-item-box {
          position: relative;
          width: 112px;
          height: 112px;
          border: 1px dotted #dcdfe6;
          border-radius: 2px;
          overflow: hidden;
          margin: 0 10px 10px 0;
  
          .delete-box {
            padding: 2px 5px;
            position: absolute;
            right: 0;
            top: 0;
            line-height: normal;
            background-color: #000;
            color: #fff;
            border-radius: 50%;
            transform: translate(40%, -30%);
  
            .el-icon-close {
              font-size: 12px;
              transform: translate(-30%, 20%);
            }
          }
        }
  
        .card-action-box {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          width: 112px;
          height: 112px;
          border: 1px dashed #cdd0d6;
          border-radius: 2px;
          overflow: hidden;
          // margin-bottom: 10px;
          background-color: #fafafa;
          cursor: pointer;
          font-size: 12px;
          color: #00000066;
  
          .el-icon-plus {
            font-size: 20px;
          }
  
          &:hover {
            border-color: #3f9b6a;
            color: #3f9b6a;
          }
        }
      }
  
      .text-list-box {
        .text-item-box {
          padding: 0 5px;
          border-radius: 2px;
          overflow: hidden;
  
          .file-name {
            flex: 1;
          }
  
          .text-icon {
            flex-shrink: 0;
            padding-right: 10px;
          }
  
          .el-icon-close {
            cursor: pointer;
            padding-left: 10px;
            flex-shrink: 0;
          }
  
          &:hover {
            background-color: rgba(63, 155, 106, 0.2);
  
            .file-name {
              color: #3f9b6a;
            }
          }
  
          &:nth-child(n + 2) {
            margin-top: 10px;
          }
        }
      }
  
      .uploading-box {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        color: #fff;
        background-color: #c0c4cc;
      }
  
      .progress-box {
        width: 100%;
        height: 100%;
        position: relative;
  
        .el-progress {
          position: absolute;
          right: 10px;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  </style>