008 封装附件上传组件示例

36 阅读1分钟
<template>
  <el-upload
    ref="uploadRef"
    :class="card ? 'card-box' : 'img-box'"
    accept="image/jpeg,image/jpg,image/png,image/gif"
    action="#"
    v-bind="$attrs"
    :multiple="multiple"
    list-type="picture-card"
    :show-file-list="true"
    :http-request="handleUpload"
    :file-list="imgList"
    :before-upload="beforeUpload"
  >
    <template slot="default">
      <i v-if="(!multiple && !imgList.length) || multiple" class="el-icon-plus avatar-uploader-icon" />
      <slot v-if="card" name="card-img" />
    </template>
    <div slot="file" slot-scope="{ file }">
      <div class="el-upload-list__item-thumbnail">
        <show-img :file="file.url" @no-img="filterImg"/>
      </div>
      <span class="el-upload-list__item-actions">
        <span class="el-upload-list__item-delete" @click="handleDel(file)">
          <i class="el-icon-delete" />
        </span>
      </span>
    </div>
  </el-upload>
</template>
<script>
import showImg from '@/views/common/show-img'
import { getFileFtp, delFileFtp } from "@/api/authentication"

export default {
  components: { showImg },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    multiple: {
      type: Boolean,
      default: false
    },
    card: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      imgList: []
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val) {
          this.imgList = this.value.split(',').map(item => {
            return {
              name: item,
              url: item
            }
          })
        } else {
          this.imgList = []
        }
      },
      immediate: true
    },
    imgList: {
      handler(val) {
        if (!this.multiple) {
          this.$nextTick(() => {
            const targetEl = this.$refs.uploadRef.$el.querySelector('.el-upload--picture-card')
            if (val.length) {
              targetEl.style.display = 'none'
            } else {
              targetEl.style.display = 'block'
            }
          })
        }
      },
      immediate: true
    }
  },
  methods: {
    filterImg(val) {
      const newList = this.imgList.filter(item => item.url != val).map(item => item.url)
      this.$emit('change', newList.join(','))
    },
    handleDel(file) {
      delFileFtp({ fileId: file.url }).then(res => {
        if (res.success) {
          this.filterImg(file.url)
        }
      }).catch(() => {
        this.$error('删除失败!')
      })
    },
    handleUpload(res) {
      const { file } = res
      const formData = new FormData()
      formData.append('file', file, file.name)
      getFileFtp(formData).then(res => {
        if (res.success && res.data.filePath) {
          this.emitValue(res.data.filePath)
        }
      }).catch(() => {
        this.$error('上传失败')
      })
    },
    emitValue(fileId = '') {
      let oldUrl = this.imgList.length ? this.imgList.map(item => item.url).join(',') + ',' : ''
      if (!this.multiple) oldUrl = ''
      const newUrl = oldUrl + fileId
      this.$emit('change', newUrl)
    },
    beforeUpload(file) {
      let boo = file.size / 1024 / 1024 < 2
      if (!boo) {
        this.$message.error("上传图片大小不能超过2M")
      }
      return boo
    },
  }
}
</script>
<style scoped lang="scss">
.img-box {
  ::v-deep .el-upload-list--picture-card {
    .el-upload-list__item {
      width: 100px;
      height: 100px;
    }
  }
  ::v-deep .el-upload--picture-card {
    width: 100px;
    height: 100px;
    line-height: 100px;
  }
}

.card-box {
  ::v-deep .el-upload-list--picture-card {
    .el-upload-list__item {
      width: 100%;
      height: auto;
      border: 0;
      box-shadow: 3px 4px 8px rgba(0, 0, 0, 0.1);
      border-radius: 3px;
    }
  }
  ::v-deep .el-upload--picture-card {
    width: 100%;
    height: 100%;
    line-height: 100%;
    border: 0;
    position: relative;
    i {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    img {
      width: 100%;
    }
  }
}
</style>