Blame view

admin-web-master/src/components/ImageUpload/index.vue 11.7 KB
3f535f30   杨鑫   '初始'
1
  <template>
d64cd58f   wesley88   上传验收小程序
2
3
4
5
6
    <div class="upload-container">
      <div class="tips" v-if="!options.disabled">
        请上传小于{{ options.limitSize }}M,格式为{{
          options.accept
        }}格式的图片,最多可上传{{ options.limit }}张
3f535f30   杨鑫   '初始'
7
      </div>
d64cd58f   wesley88   上传验收小程序
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
      <div class="card-list-box">
        <div v-for="(file, i) in localValue" :key="'upload-BaseImage' + i" class="card-item-box">
          <BaseImage :src="file.address" fit="contain" class="card-img" :code="options.reqParams.code"
            :filePath="filePath" @handleDelete="handleDeleteImg(file, i)"
            @handleSetCover="handleSetCover(file, i)" :isSetCover="cmpOption.isSetCover" :forIndex="i" :onforIndex="onforIndex"/>
          <div v-show="!options.disabled && !cmpOption.isSetCover" title="删除" class="delete-box"
            @click="handleDeleteImg(file, i)">
            <i class="el-icon-close"></i>
          </div>
        </div>
        <div v-for="(file, i) in reactiveData.filesIng" :key="i" class="card-item-box">
          <div v-show="reactiveData.status === 'uploading'" class="uploading-box flex-col-center-center">
            <el-progress v-if="reactiveData.lengthComputable" type="circle" :width="50"
              :percentage="uploadPercentage()" />
            <template v-else>
              <i class="el-icon-loading"></i>
              {{ reactiveData.message }}
            </template>
          </div>
        </div>
        <div style="position: relative;">
a182f238   wesley88   1
29
          <input :accept="options.accept" style="opacity: 0;position: absolute;" :id="inputtype" ref="fileInput" type="file" :multiple="true"
d64cd58f   wesley88   上传验收小程序
30
31
32
33
34
35
36
          @change="handleUploadFile" />
          <div v-if="
              localValue && localValue.length < options.limit && !options.disabled
            " title="点击上传" class="card-action-box" @click="handleUpload">
            <i class="el-icon-plus"></i>
            <span class="card-action-text">点击上传图片</span>
          </div>
3f535f30   杨鑫   '初始'
37
  
d64cd58f   wesley88   上传验收小程序
38
        </div>
3f535f30   杨鑫   '初始'
39
      </div>
3f535f30   杨鑫   '初始'
40
41
42
43
    </div>
  </template>
  
  <script>
d64cd58f   wesley88   上传验收小程序
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
    //   import { uploadFiles, uploadStraightFile } from '@/api/commonApi/file';
    import {
      miniioupload
    } from '@/api/commodityLease.js'
    import BaseImage from '@/components/BaseImage/index.vue';
  
    export default {
      components: {
        BaseImage,
      },
      name: 'BaseUpload',
      props: {
        value: {
          type: String,
          default: '',
        },
        cmpOption: {
          type: Object,
          default: () => ({
            disabled: false,
            isSetCover: true,
          }),
        },
        filePath: {
          type: String,
          default: 'other',
        },
        inputtype: {
          type: String,
          default: '',
        },
        limit: {
          type: Number,
cf438595   wesley88   1
77
          default: 9,
d64cd58f   wesley88   上传验收小程序
78
        },
3f535f30   杨鑫   '初始'
79
      },
d64cd58f   wesley88   上传验收小程序
80
81
82
83
84
85
      data() {
        return {
          fileList:[],
          onforIndex:0,
          localValue: [],
          options: {
cf438595   wesley88   1
86
            limitSize: 9,
d64cd58f   wesley88   上传验收小程序
87
88
            accept: '.bmp,.jpg,.jpeg,.png',
            multiple: false,
cf438595   wesley88   1
89
            limit: 9,
d64cd58f   wesley88   上传验收小程序
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
            disabled: false,
            field: 'files',
            reqParams: {
              code: 2000
            },
          },
          reactiveData: {
            files: [],
            filesIng: [],
            status: 'uploading',
            message: '上传中...',
            total: 0,
            loaded: 0,
            lengthComputable: false,
          },
        };
3f535f30   杨鑫   '初始'
106
      },
d64cd58f   wesley88   上传验收小程序
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
      watch: {
        limit: {
          handler() {
            this.options.limit= this.limit
          },
          immediate: true,
        },
        cmpOption: {
          handler() {
            this.setOptions();
          },
          immediate: true,
        },
        value: {
          handler(val) {
            if (val && val !='') {
              // 首先将值转为数组
              const list = Array.isArray(val) ? val : this.value.split(',')
              // 然后将数组转为对象数组
              this.fileList = list
              this.localValue = list.map(item => {
                if (typeof item === 'string') {
                  item = {
                    address: item,
                  }
                }
                return item
              })
            } else {
              this.localValue = []
              this.fileList = []
              return []
            }
          },
          immediate: true,
        },
3f535f30   杨鑫   '初始'
143
      },
d64cd58f   wesley88   上传验收小程序
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
      methods: {
        async handleUploadFile(value) {
          const files = value.target.files
          if (!this.filesCheck(files)) {
            return;
          }
          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)
          
        },
        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';
c3f8e431   wesley88   1
215
              console.error(fd)
d64cd58f   wesley88   上传验收小程序
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
              // 调用 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('上传失败');
3f535f30   杨鑫   '初始'
239
240
241
            return []
          }
        },
d64cd58f   wesley88   上传验收小程序
242
243
244
245
246
247
248
249
        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(',')
a182f238   wesley88   1
250
          console.error(urlstr)
d64cd58f   wesley88   上传验收小程序
251
252
253
254
255
          this.$emit('changimg',urlstr)
        },
        async handleSetCover(file, i) {
          console.error(file, i)
          this.onforIndex = i
98bc35a2   wesley88   1
256
          this.$emit('changfmindex',this.onforIndex)
d64cd58f   wesley88   上传验收小程序
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
        },
        filesCheck(files) {
          const {
            limitSize,
            accept,
            limit
          } = this.options;
          if (this.reactiveData.files.length + files.length > limit) {
            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;
        },
        checkSize({
          size
        }) {
          return size <= this.options.limitSize * 1024 * 1024;
        },
        checkFileType(file) {
          console.error(file)
          let {
            accept
          } = this.options;
          if (!accept || accept === '*') {
            return true;
          }
          accept = accept.toLowerCase().replace(/\s/g, '');
          const acceptTypeList = accept.split(',');
          const fileType = file.name.match(/\.\w*$/)?.shift() ?? '';
          return acceptTypeList.includes(fileType.toLowerCase());
        },
3f535f30   杨鑫   '初始'
300
      },
d64cd58f   wesley88   上传验收小程序
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
    };
  </script>
  
  <style lang="scss" scoped>
    .upload-container {
      width: 100%;
  
      .tips {
        color: #0006;
        font: 14px 'Alibaba PuHuiTi 2.0-55 Regular';
        line-height: 38px;
      }
  
      .action-box {
        cursor: pointer;
        padding-left: 10px;
  
        .default-action {
          color: #3f9b6a;
3f535f30   杨鑫   '初始'
320
        }
d64cd58f   wesley88   上传验收小程序
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
      }
  
      .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%);
            }
3f535f30   杨鑫   '初始'
352
          }
3f535f30   杨鑫   '初始'
353
354
        }
  
d64cd58f   wesley88   上传验收小程序
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
        .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;
3f535f30   杨鑫   '初始'
378
379
          }
        }
3f535f30   杨鑫   '初始'
380
      }
3f535f30   杨鑫   '初始'
381
  
d64cd58f   wesley88   上传验收小程序
382
383
384
385
386
      .text-list-box {
        .text-item-box {
          padding: 0 5px;
          border-radius: 2px;
          overflow: hidden;
3f535f30   杨鑫   '初始'
387
  
d64cd58f   wesley88   上传验收小程序
388
389
390
          .file-name {
            flex: 1;
          }
3f535f30   杨鑫   '初始'
391
  
d64cd58f   wesley88   上传验收小程序
392
393
394
395
          .text-icon {
            flex-shrink: 0;
            padding-right: 10px;
          }
3f535f30   杨鑫   '初始'
396
  
d64cd58f   wesley88   上传验收小程序
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
          .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 {
3f535f30   杨鑫   '初始'
418
        position: absolute;
d64cd58f   wesley88   上传验收小程序
419
420
421
422
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
3f535f30   杨鑫   '初始'
423
        color: #fff;
d64cd58f   wesley88   上传验收小程序
424
        background-color: #c0c4cc;
3f535f30   杨鑫   '初始'
425
426
      }
  
d64cd58f   wesley88   上传验收小程序
427
      .progress-box {
3f535f30   杨鑫   '初始'
428
429
        width: 100%;
        height: 100%;
d64cd58f   wesley88   上传验收小程序
430
431
432
433
434
435
436
437
        position: relative;
  
        .el-progress {
          position: absolute;
          right: 10px;
          top: 50%;
          transform: translateY(-50%);
        }
3f535f30   杨鑫   '初始'
438
439
      }
    }
3f535f30   杨鑫   '初始'
440
  </style>