glUploadImg.vue 3.92 KB
<!-- 图片上传 -->
<template>
  <div
    class="gl-upload-img"
    v-if="show">
    <el-upload
      action=""
      list-type="picture-card"
      accept=".jpg,.png"
      :auto-upload="false"
      :show-file-list="false"
      :disabled="!!picObj[formObj[attrName]].url"
      :on-change="e => handleChange(e, formObj[attrName], formObj)"
      :on-preview="e => handlePreview(e, formObj[attrName])"
      :on-remove="e => handleRemove(e, formObj[attrName])">
      <span class="img-btn">
        <img
          v-if="picObj[formObj[attrName]].url"
          :src="picObj[formObj[attrName]].url"
          class="rectImg">
        <span class="img-btns">
          <span class="btn-item">
            <i
              @click.stop="handleRemove(formObj[attrName], formObj)"
              class="el-icon-delete" />
            <i
              @click.stop="handleImgLook"
              class="el-icon-zoom-in" />
          </span>
        </span>
      </span>
      <i
        v-if="!picObj[formObj[attrName]].url && formObj.useDefaultImg"
        class="el-icon-plus avatar-uploader-icon"
        style="color: #8c939d;" />
      <img
        v-if="!picObj[formObj[attrName]].url && !formObj.useDefaultImg"
        :src="examplePicUrl"
        class="rectImg">
    </el-upload>
    <span v-if="txtDescript">{{ txtDescript }}</span>
    <p
      v-show="!picObj[formObj[attrName]].url"
      slot="errorTips"
      class="car-error-txt">
      请上传{{ txtDescript }}
    </p>
    <el-dialog :visible.sync="lookImgVisible">
      <img
        width="100%"
        :src="picObj[formObj[attrName]].url"
        alt="图片">
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'GlUploadImg',
  data() {
    return {
      examplePicUrl: '',
      lookImgVisible: false
    }
  },

  props: {
    // 表单对象
    formObj: {
      type: Object,
      default: () => { }
    },
    // 标记拿绑定哪个属性
    attrName: {
      type: String,
      default: 'slotName'
    },
    // 图片绑定的属性
    picObj: {
      type: Object,
      default: () => { }
    },
    // 文字说明
    txtDescript: {
      type: String,
      default: ''
    },
    // 显示/隐藏
    show: {
      type: Boolean,
      default: true
    }
  },

  mounted() {
    // public 文件需要使用绝对路径引入
    this.examplePicUrl = `${process.env.VUE_APP_PUBLIC_PATH}account/` + this.formObj[this.attrName] + '.png'
    // console.log(this.picObj[this.formObj[this.attrName]])
    // console.log(this.formObj[this.attrName])
  },

  methods: {
    handleImgLook() {
      this.lookImgVisible = !this.lookImgVisible
    },
    handleChange(file, attrName, targetItem) {
      if (!(file.size / 1024 / 1024 < 1)) {
        this.$message.error('请上传小于1M的图片')
        return false
      }
      this.$emit('handleImgChange', { file: file, attrName: attrName, targetItem: targetItem })
    },
    handlePreview() { },
    handleRemove(attrName, targetItem) {
      this.$emit('handleImgRemove', { attrName: attrName, targetItem: targetItem })
    }
  }
}

</script>
<style>
.gl-upload-img .disabled .el-upload--picture-card {
  display: none;
}
.gl-upload-img .el-upload--picture-card {
  width: 140px;
  height: 92px;
  line-height: 98px;
}
.gl-upload-img .rectImg {
  width: 137px;
  height: 90px;
}

.gl-upload-img .img-btn {
  display: inline-block;
  position: relative;
}
.img-btn .img-btns {
  display: none;
  position: absolute;
  width: 140px;
  height: 92px;
  left: 0;
  top: 0;
  z-index: 2;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
}
.img-btn .img-btns .btn-item {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.img-btn .img-btns .btn-item {
  display: flex;
  width: 70%;
  justify-content: space-around;
}

.img-btn:hover .img-btns {
  display: block;
}
.gl-upload-img .el-upload--picture-card i {
  color: #fff;
}
.gl-upload-img .car-error-txt {
  font-size: 12px;
  line-height: 6px;
  margin: 0 0;
}
</style>