index.vue 4.49 KB
<template>
  <div class="upload-ctnr">
      <label v-if="name" :class="['label',{'required-field': required}]" for="">{{name}}</label>
      <div class="upload-wrap"  :style="defaultStyle">
        <img v-if="url" class="image" :src="url" alt="" />
        <div v-else class="add-img">
          <input type="file" class="file" id="file" ref="input" @change="upload" accept="image/png, image/jpeg, image/gif, image/jpg" />
          <i class="el-icon-plus add"></i>
          <p>上传照片</p>
        </div>
        <div class="operate" v-if="url">
          <div class="btn-wrap">
            <i class="el-icon-view" @click="handleLook"></i>
            <i v-if="!disabled" class="el-icon-delete del-icon" @click="del"></i>
          </div>
        </div>
      </div>
      <el-dialog class="image-dialog" :visible.sync="dialogVisibleImg" width="30%">
        <big-view ref="viewForm" v-if="dialogVisibleImg" :dialogSrc="url" dialogType="img"></big-view>
      </el-dialog>
  </div>
</template>

<script>
import bigView from '@/components/big-view'
export default {
  name: 'Upload',
  components: {
    bigView
  },
  props: {
    required: {
      type: Boolean,
      default: false
    },
    name: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    url: {
      type: String,
      default: ''
    },
    attr: {
      type: String,
      default: ''
    },
    defaultStyle: {
      type: Object,
      default: ()=>{
        return {}
      }
    },
    checkObj: { // 校验规则
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      dialogVisibleImg: false
    }
  },
  methods: {
    handleLook() {
      this.dialogVisibleImg = true
    },
    upload(e) {
      const file = e.target.files[0]
      if (this.checkObj && Object.keys(this.checkObj).length) {
        const AllImgExt = this.checkObj.fileTypes
        let extName = file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
        if (AllImgExt.indexOf(extName) === -1) {
          return this.$message.error('上传格式不正确')
        }
        const size = file.size / 1024 / 1024
        const maxSize = this.checkObj.maxSize
        if (size > maxSize) {
          return this.$message.error(`上传文件不可超过${maxSize}M`)
        }
      }
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = event => {
        const imgFile = event.target.result
        const arrBase64 = imgFile.split(',')
        const base64 = file.name.split('.').pop() + '@' + arrBase64[1]
        this.$apis.claims.uploadImgApi({ file: base64, fileType: 'image' }).then(res => {
          this.$emit('uploadSuccess', { attr: this.attr, url: res })
        })
      }
    },
    del() {
      this.$emit('delImage', { attr: this.attr })
    }
  }
}
</script>
<style lang="scss" scoped>
.upload-ctnr {
  display: flex;
  .label {
    display: inline-block;
    vertical-align: top;
    width: 154px;
    height: 20px;
    font-size: 14px;
    color: #333333;
    text-align: right;
    line-height: 20px;
    box-sizing: border-box;
  }
  .upload-wrap {
    display: inline-flex;
    border: 1px dashed #00000026;
    box-sizing: border-box;
    width: 104px;
    height: 104px;
    background: #0000000a;
    border-radius: 4px;
    position: relative;
    &:hover {
      .operate {
        display: block;
      }
    }
    .image {
      width: 100%;
      height: 100%;
      z-index: 2;
      position: relative;
    }
    .add-img {
      width: 100%;
      height: 100%;
      cursor: pointer;
      display: inline-flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      color: #666666;
      position: relative;
      .file {
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 1;
        position: absolute;
        opacity: 0;
      }
      .add {
        font-size: 18px;
      }
      p {
        height: 32px;
        line-height: 32px;
      }
    }
    .operate {
      display: none;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0.4);
      border-radius: 4px;
      z-index: 5;
      .btn-wrap {
        color: #fff;
        position: absolute;
        transform: translate(-50%, -50%);
        top: 50%;
        left: 50%;
        .del-icon {
          margin-left: 12px;
        }
        i {
          font-size: 16px;
          cursor: pointer;
        }
      }
    }
  }
}
</style>