uniapp-图片,视频上传组件封装

新建 uploadImage.vue 文件

javascript 复制代码
<template>
  <uni-file-picker ref="files" v-model="fileLists" :title="title" :limit="limit" :auto-upload="false"
    @select="selectFile" @delete="deleteFile">
    <view class="add line-d-E5 radius-16 f-24 t-gray80 bg-ed flex a-center j-center">
      <image class="img" :src="baseImgUrl + '/recycle/20260514-upload-img-icon.png'" />
    </view>
  </uni-file-picker>
</template>

<script>
  import {
    upload
  } from '@/api/system/upload';
  import util, {
    getExtname
  } from '@/utils';

  export default {
    props: {
      value: [String, Array],
      limit: {
        type: [String, Number],
        default: 5,
      },
      fileSize: {
        type: Number,
        default: 1,
      },
      title: {
        type: String,
        default: '',
      },
      pathSuffix: {
        type: String,
        default: '',
      }
    },
    data() {
      return {
        fileLists: [
          // {
          // 	url: 'https://xxx.xxx.xxx.com/VKCEYUGU-dc-site/b7c7f970-517d-11eb-97b7-0dc4655d6e68.jpg',
          // 	name: 'shuijiao.png',
          // 	extname: 'png'
          // }
        ]
      }
    },
    mounted() {
      if (this.value) {
        let imgs = this.value;
        if (typeof this.value === "string") {
          imgs = this.value.split(',');
        }
        this.fileLists = imgs.map(item => {
          return {
            url: item,
            name: item,
            extname: getExtname(item)
          };
        });
      }
    },
    methods: {
      selectFile(e) {
        let index;
        for (index in e.tempFiles) {
          this.fileLists.push(e.tempFiles[index]);
          // 先不限制图片上传大小
          if (e.tempFiles[index].size / 1024 / 1024 > this.fileSize) {
            this.$nextTick(() => {
              this.fileLists.splice(this.fileLists.length - 1, 1);
            });
            util.toast(`上传头像图片大小不能超过 ${this.fileSize} MB!`)
            break;
          }

          this.uploadImg(e.tempFilePaths[index], this.fileLists.length - 1);
        }
      },
      uploadImg(path, index) {
        upload({
            filePath: path,
            name: 'file',
            custom: {
              loading: true
            },
            // #ifdef MP-ALIPAY
            fileType: 'image/video/audio',
            // #endif
          },
          this.pathSuffix
        ).then(res => {
          this.fileLists.splice(
            index,
            1, {
              url: res.data.url,
              name: res.data.fileName,
              extname: getExtname(res.data.fileName)
            }
          );
          this.setValue();
        }).catch(err => {
          this.deleteFile({
            tempFilePath: path
          })
        });
      },
      deleteFile(e) {
        this.fileLists.forEach((item, index) => {
          if (e.tempFilePath == item.url) {
            this.fileLists.splice(index, 1);
            this.setValue();
            return;
          }
        })
      },
      setValue() {
        let imgs = this.fileLists.map(item => item.url);
        if (typeof this.value === "string" || !this.value) {
          return this.$emit('input', imgs.join(','));
        }
        this.$emit('input', imgs);
      }
    }
  }
</script>

<style lang="scss">
  .add {
    width: 160rpx;
    height: 160rpx;

    .img {
      width: 64rpx;
      height: 64rpx;
    }
  }
</style>

调用

javascript 复制代码
<com-uploadImage-index limit="3" v-model="form.urls" pathSuffix="/work_order"              
    class="upload-box flex-1 m-b-25"/>



// 自定义 上传图标
.upload-box{
 ::v-deep .icon-del-box {
   width: 40rpx;
   height: 40rpx;
  }
}
相关推荐
0x5327 分钟前
Java网络编程(二)
java·服务器·网络
小柯南敲键盘1 小时前
跨马翻译:跨境电商批量图片翻译与视频字幕一站式工具
人工智能·python·音视频
Flittly1 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(三):输出校验与治理闭环实战
java·spring boot·spring
Poo_Chai1 小时前
QT emit信号后完整处理流程,包括槽函数响应流程
java·开发语言·数据库
瑞码空间1 小时前
Java 图形界面(GUI)完整知识点手册
java·开发语言·图形界面·swing
树码小子1 小时前
开启 IDEA 中的断言功能
android·java·intellij-idea
茶本无香1 小时前
Java调用Shell脚本执行SQL数据库操作:从入门到实战
java·sql·shell
风流 少年2 小时前
Spring AI 2.0:MCP
java·后端·spring
天疆说2 小时前
01 硬件选型与 llama.cpp 部署:4× RTX 5880 Ada 跑 DeepSeek-V4-Flash
java·redis·llama