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;
  }
}
相关推荐
迷藏4941 小时前
双阶段动态权重匹配系统:高效精准的工业级解决方案
java·junit
开源推荐官1 小时前
2026 三大国产优质开源商城深度测评:VortMall、Tigshop、Jinor 选型全解析
java·开源
We Just Keep growing1 小时前
【MySQL运维篇】——日志、主从复制、分库分表、读写分离
java·运维·数据库·windows·学习·mysql
change_fate1 小时前
ERR_PNPM_WORKSPACE_PKG_NOT_FOUND In ...
java·服务器·前端
做萤石二次开发的哈哈2 小时前
具备 ERTC 能力的萤石设备如何对接客户端通话?
音视频·实时音视频·萤石开放平台
电子元件小说家2 小时前
音频调音台直滑电位器选型:ALPS RK12L123000E 与国产同于科技替代方案评估
科技·音视频
kakawzw2 小时前
微服务组件源码2——Spring Ribbon原理(基于RibbonLoadBalancerClient)
java·微服务·ribbon
计算机安禾2 小时前
【算法分析与设计】第48篇:流算法与数据概要技术
java·服务器·网络·数据库·算法
hunterkkk(c++)2 小时前
SPFA最短路径算法(c++)
java·c++·算法