绿满眶商城微信小程序-uniapp
zxl
2025-07-11 01439f14432ee731be561d193c88e3ea31399345
components/uview-components/uview-ui/components/u-upload/u-upload.vue
@@ -100,7 +100,7 @@
      // 后端地址
      action: {
         type: String,
         default: ''
         default: 'http://127.0.0.1:8890/common/lmk/file/upload'
      },
      // 最大上传数量
      maxCount: {
@@ -394,12 +394,13 @@
         this.lists[index].error = false;
         this.uploading = true;
         // 创建上传对象
         const task = uni.uploadFile({
            url: this.action,
            filePath: this.lists[index].url,
            name: this.name,
            formData: this.formData,
            header: this.header,
            name: 'file',
            // file: this.formData,
            // header: this.header,
            success: res => {
               // 判断是否json字符串,将其转为json格式
               let data = this.toJson && this.$u.test.jsonString(res.data) ? JSON.parse(res.data) : res.data;
@@ -407,10 +408,11 @@
                  this.uploadError(index, data);
               } else {
                  // 上传成功
                  const fileType = this.getFileType(this.lists[index].url);
                  this.lists[index].response = data;
                  this.lists[index].progress = 100;
                  this.lists[index].error = false;
                  this.$emit('on-success', data, index, this.lists);
                  this.$emit('success', data, index, this.lists,fileType);
               }
            },
            fail: e => {
@@ -430,6 +432,21 @@
            }
         });
      },
      getFileType(filePath) {
         const extension = filePath.split('.').pop().toLowerCase();
       // 图片类型
           const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'];
           // 视频类型
           const videoTypes = ['mp4', 'mov', 'avi', 'wmv', 'flv', 'm3u8'];
           if (imageTypes.includes(extension)) {
             return 'image';
           } else if (videoTypes.includes(extension)) {
             return 'video';
           } else {
             return 'text'; // 其他所有类型归为text
           }
      },
      // 上传失败
      uploadError(index, err) {
         this.lists[index].progress = 0;