UploadHandler.vue 8.76 KB
<template>
  <div class="element">
    <label class="element_label">
      <span v-if="isRequire" class="require">*</span>
      {{ title }}:
    </label>
    <div class="wrapper">
      <div class="upload_wrap">
        <!-- 0 是默认上传类型 -->
        <template v-if="uploadType == 0">
          <template v-if="imagesList.length">
            <div v-for="(imgItem, i) in imagesList" :key="i" class="imagBox">
              <img class="imgItem" :src="imgItem" alt="" />
              <div class="img_mask">
                <i @click="handlePictureCardPreview(imgItem)" class="el-icon-zoom-in"></i>
                <i v-if="!disabled" @click="toDeleteImg(index, i)" class="el-icon-delete"></i>
              </div>
            </div>
          </template>
          <div v-if="!(imagesList.length == limitNum)" class="upload-value">
            <el-upload :disabled="disabled" list-type="picture" class="upload_item" action="#" :before-upload="e => beforeAvatarUpload(e)" :on-remove="e => handleRemove(e, index)" :http-request="e => httpRequest(e, index)">
              <img class="img" slot="default" :src="upload_img" alt="" />
            </el-upload>
          </div>
        </template>
        <!-- 1 代表的是身份证、驾驶证等类型 -->
        <template v-if="uploadType == 1">
          <template v-for="count in 2">
            <div v-if="(count == 1 && cardImg.font) || (count == 2 && cardImg.reverse)" :key="count" class="imagBox wid180">
              <img class="imgItem wid180" :src="count == 2 ? cardImg.reverse : cardImg.font" alt="" />
              <div class="img_mask wid180">
                <i @click="handlePictureCardPreview(count == 2 ? cardImg.reverse : cardImg.font)" class="el-icon-zoom-in"></i>
                <i v-if="!disabled" @click="removeCard(count)" class="el-icon-delete"></i>
              </div>
            </div>
          </template>
          <template v-for="count in 2">
            <div v-if="(count == 1 && !cardImg.font&&!disabled) || (count == 2 && !cardImg.reverse&&!disabled)" :key="count" class="upload-value upload-value-max marRight-13">
              <el-upload :disabled="disabled" list-type="picture" class="upload_item" action="#" :before-upload="e => beforeAvatarUpload(e)" :on-remove="e => handleRemove(e, index)" :http-request="e => httpRequest(e, count)">
                <img class="img" slot="default" :src="count == 2 ? idCardBack_img : idCard_img" alt="" />
              </el-upload>
            </div>
          </template>
        </template>
      </div>
      <div class="tip">
        图片格式为jpg,jpeg,png,大小10M以内
        <span v-if="limitNum > 8 && uploadType == 0">,最多可上传{{ limitNum }}张</span>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import upload_img from '@/assets/beforeSale/upload.png'
import idCard_img from '@/assets/beforeSale/idCard.png'
import idCardBack_img from '@/assets/beforeSale/idCardBack.png'
import { uploadFileApi } from '@/api/modules/common'
export default {
  name: 'uploadHandler',
  props: {
    title: {
      type: String,
      default: '其他'
    },
    imagesList: Array,
    uploadType: {
      type: Number,
      default: 0
    },
    limitNum: {
      type: Number,
      default: 100
    },
    cardImg: Object,
    disabled:Boolean,
    isRequire:Boolean
  },
  data() {
    return {
      upload_img,
      idCard_img,
      idCardBack_img,
      dialogVisible: false,
      hideUpload: false
    }
  },
  mounted() {},

  methods: {
    toDeleteImg(index, i) {
      this.imagesList.splice(i, 1)
      this.$emit('getImages', this.imagesList)
    },
    removeCard(count) {
      if (count == 1) {
        this.cardImg.font = ''
      } else {
        this.cardImg.reverse = ''
      }
      this.$emit('getCardImg', {detail:this.cardImg,type:count})
    },
    handlePictureCardPreview(url) {
      this.dialogImageUrl = url
      this.dialogVisible = true
    },
    beforeAvatarUpload(e) {
      return new Promise((resolve, reject) => {
        let AllImgExt = '.png|.jpg|.jpeg'
        let extName = e.name.substring(e.name.lastIndexOf('.')).toLowerCase()
        if (AllImgExt.indexOf(extName) === -1) {
          this.$message.error('上传格式不正确')
          return reject(false)
        }
        const size = e.size / 1024 / 1024
        let maxSize = 10
        let filetype = this.getFileType(e.type)
        if (filetype == 'image') {
          maxSize = 10
        } else {
          maxSize = 30
        }
        if (size > maxSize) {
          this.$message.error(`上传文件不可超过${maxSize}M`)
          return reject(false)
        }
        return resolve(true)
      })
    },
    httpRequest({ file: e }, count) {
      let size = e.size / 1024 / 1024
      let loading = null
      // 大于2给loading
      if (size > 2) {
        loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
      }
      // // 转码base64
      const reader = new FileReader()
      reader.readAsDataURL(e)
      reader.onload = event => {
        const imgFile = event.target.result
        const arrBase64 = imgFile.split(',')
        const targetNameList = e.name.split('.')
        const fileInfo = targetNameList[targetNameList.length - 1] + '@' + arrBase64[1]
        const fileType = this.getFileType(e.type)
        const paramObj = { file: fileInfo, fileType: fileType }
        let param = {}
        param.fileList = [paramObj]
        uploadFileApi(param, res => {
          if (loading != null) {
            loading.close()
            loading = null
          }
          // let progress = ((res.loaded / res.total) * 100).toFixed(2)
          // if (progress > 90) {
          //   progress = 90
          // }
        }).then(res => {
          if (res.code == 0) {
            const url = res.data[0]
            if (this.uploadType == 0) {
              this.imagesList.push(url)
              this.$emit('getImages', this.imagesList)
            } else if (this.uploadType == 1) {
              if (count == 1) {
                this.cardImg.font = url
              } else {
                this.cardImg.reverse = url
              }
              this.$emit('getCardImg', {detail:this.cardImg,type:count})
            }
          }
        })
      }
    },
    handleRemove(e, fieldName) {
      if (e.status == 'ready') return
    },
    getFileType(type) {
      if (!type) return 'doc'
      if (type.indexOf('image') > -1) {
        return 'image'
      } else {
        return 'doc'
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.element {
  display: flex;
  margin-bottom: 20px;
  .element_label {
    width: 164px;
    font-weight: 400;
    font-size: 14px;
    color: #333333;
    text-align: right;
    .require {
      color: red;
    }
  }
  .wrapper {
    width: 650px;
    .upload_wrap {
      display: flex;
      flex-wrap: wrap;
      .imagBox {
        position: relative;
        .imgItem {
          width: 104px;
          height: 104px;
          border-radius: 4px;
          margin-right: 10px;
          margin-bottom: 10px;
          border: 1px dashed #00000026;
        }
        .img_mask {
          display: none;
        }
      }
      .imagBox:hover {
        .img_mask {
          display: block;
          width: 104px;
          height: 104px;
          border-radius: 4px;
          position: absolute;
          left: 0;
          top: 0;
          right: 0;
          bottom: 0;
          background: rgba(0, 0, 0, 0.4);
          transition: opacity 0.3s;
          display: flex;
          align-items: center;
          justify-content: center;
          i {
            color: white;
            cursor: pointer;
            font-weight: 400;
            font-size: 21px;
          }
          i:first-child {
            margin-right: 12px;
          }
          i:last-child {
            margin-right: 0;
          }
        }
      }
    }
    .upload-value {
      width: 104px;
      height: 104px;
      border: 1px dashed #00000026;
      border-radius: 4px;
      .upload_item {
        width: 104px;
        height: 104px;
        .img {
          width: 104px;
          height: 104px;
          border-radius: 4px;
        }
        /deep/.el-upload-list--picture {
          display: none;
        }
      }
    }
    .upload-value-max {
      width: 180px;
      .upload_item {
        width: 180px;
        .img {
          width: 180px;
        }
      }
    }
    .wid180 {
      width: 180px !important;
      margin-right: 13px;
    }
    .marRight-13 {
      margin-right: 13px;
    }
    .tip {
      font-weight: 400;
      font-size: 14px;
      color: #999999;
      margin-top: 8px;
    }
  }
}
</style>