index.vue 5.76 KB
<template>
  <div class="activity-img-box">
    <template v-if="imageUrlArr && imageUrlArr.length > 0">
      <div v-for="(item, index) in imageUrlArr" :key="index">
        <div class="pic-box" :style="{ width: picOptions.width, height: picOptions.height }">
          <img class="photo-activity" :src="item" alt="" />
          <div class="mask" :style="{ width: picOptions.width, height: picOptions.height }">
            <div class="pic-pane-close" @click="handleEnlarge(item)">
              <img src="../../assets/images/view.png" alt="" />
            </div>
            <template v-if="picOptions.isClose">
              <div class="pic-pane-close" @click="close('img', index)">
                <img src="../../assets/images/dele.png" alt="" />
              </div>
            </template>
          </div>
        </div>
      </div>
    </template>
    <template v-if="(imageUrlArr && imageUrlArr.length < 9 && limit > 1) || (imageUrlArr && imageUrlArr.length == 0 && limit == 1)">
      <div class="upload-img">
        <i class="el-icon-plus avatar-uploader-desc-icon"></i>
        <div class="avatar-uploader-desc-word">上传照片</div>
        <input type="file" ref="uploadImg" class="upload-img" style="opacity: 0" accept=".jpg, .jpeg, .png" @change="e => handleImgChange(e)" />
      </div>
    </template>
    <el-dialog class="image-dialog" :visible.sync="dialogVisibleImg" :append-to-body="true" width="50%">
      <img :src="dialogSrc" width="100%" v-if="dialogType == 'img'" />
      <!-- <video width="100%" autoplay controls :src="dialogSrc" v-else></video> -->
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    picOptions: {
      type: Object,
      default: () => {
        return {
          width: '104px',
          height: '104px',
          isClose: true
        }
      }
    },
    imageUrlArr: {
      type: Array,
      default: () => []
    },
    limit: {
      type: String,
      default: '1'
    },
    index: {
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      dialogVisibleImg: false
    }
  },
  methods: {
    // 上传图片
    handleImgChange(e) {
      this.$notify({
        title: '提示',
        message: '图片上传中,请稍后'
      })
      var file = e.srcElement.files[0]
      var reader = new FileReader()
      // 默认1MB
      const limitSize = 10
      const isLt = file.size / 1024 / 1024 < limitSize
      if (!isLt) {
        this.$message.error('上传图片大小不能超过' + limitSize + 'MB!')
        return true
      }
      if (this.imageUrlArr.length > 9) {
        this.$message.error('最多上传9张图片')
        return
      }
      this.$emit('beforeUpload')
      reader.readAsDataURL(file)
      reader.onload = e => {
        const imgFile = e.target.result
        const arrBase64 = imgFile.split(',')
        const splitStr = file.name.split('.')
        const fileUrl = splitStr[splitStr.length - 1] + '@' + arrBase64[1]
        this.$apis.common
          .pictureUpload({ file: fileUrl, compress: this.picOptions.compress || 0 })
          .then(res => {
            if (res) {
              this.$notify({
                title: '提示',
                message: '图片上传成功',
                type: 'success'
              })
              this.imageUrlArr.push(res)
              this.$emit('change', { arr: this.imageUrlArr, key: this.picOptions.key, index: this.index })
            }
            e.target.value = ''
            this.$refs.uploadImg.value = ''
          })
          .finally(end => {
            this.$emit('afterUpload', end)
          })
      }
    },
    // 预览图文
    handleEnlarge(src, type) {
      this.dialogVisibleImg = true
      this.dialogSrc = src
      this.dialogType = type ? type : 'img'
    },
    // 删除图片/视频
    close(type, index) {
      switch (type) {
        case 'radio':
          this.videoUrl = ''
          this.videoImage = ''
          break
        case 'img':
          this.imageUrlArr.splice(index, 1)
          this.$emit('change', { arr: this.imageUrlArr, key: this.picOptions.key, id: 1, index: this.index })
          break
        default:
          this.videoUrl = ''
      }
    }
  }
}
</script>
<style scoped lang="scss">
.activity-img-box {
  display: flex;
  flex-wrap: wrap;
  max-width: 600px;
  .pic-box {
    position: relative;
    width: 104px;
    height: 104px;
    border-radius: 4px;
    margin-right: 8px;
    margin-bottom: 5px;
    &:hover .mask {
      opacity: 1;
    }
    .pic {
      position: absolute;
      object-fit: fill;
    }
    .photo-activity {
      position: absolute;
      width: 100%;
      height: 100%;
    }
    .mask {
      display: flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      background: rgba(0, 0, 0, 0.5);
      color: #ffffff;
      opacity: 0;
      top: 0;
      right: 0;
      width: 104px;
      height: 104px;
      z-index: 999;
      .pic-pane-close {
        width: 16px;
        height: 16px;
        cursor: pointer;
        margin-left: 24px;
        &:first-child {
          margin-left: 0;
        }
        img {
          width: 100%;
          display: block;
        }
      }
    }
  }
  .upload-img {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 104px;
    height: 104px;
    background: rgba(0, 0, 0, 0.04);
    border: 1px dashed rgba(0, 0, 0, 0.15);
    border-radius: 4px;

    input {
      position: absolute;
      // top: 0;
      // left: 0;
      width: 104px;
      height: 104px;
    }
  }
  .avatar-uploader-desc-word {
    font-weight: 400;
    font-size: 28rpx;
    color: #666666;
    margin-top: 8px;
  }
  .image-dialog {
    /deep/ {
      .el-dialog__header {
        border-bottom: 0px;
        padding: 0px;
      }
    }
  }
}
</style>