imgList.vue 5.42 KB
<template>
  <div class="">
    <div class="container">
      <input type="file" class="file" id="file" ref="input" @change="upload" accept="image/png, image/jpeg, image/gif, image/jpg" />
      <div class="wrap" v-for="(imgItem, imgKey) in list" :key="imgKey">
        <span class="title">
          <i v-if="imgItem.status == 1" class="el-icon-success success"></i>
          <i v-if="imgItem.status == 2" class="el-icon-error error"></i>
          <span>{{ imgItem.title }}</span>
          <el-tooltip v-if="imgItem.tip" class="item" effect="dark" :content="imgItem.tip" placement="top">
            <i v-if="imgItem.tip" class="el-icon-question question"></i>
          </el-tooltip>
        </span>
        <div :class="['img-box', { 'has-img': imgItem.url, 'error-img': imgItem.status == 2, 'success-img': imgItem.status == 1 }]">
          <!-- <img v-if="!imgItem.url" class="back-img" fit="cover" :src="url" alt="" @click="clickImg(imgKey)" /> -->
          <img v-if="!imgItem.url" class="add-btn" :src="cameraUrl" alt="" @click="clickImg(imgKey)" />
          <img v-else class="need-img" fit="cover" :src="imgItem.url" alt="" />
          <div class="mask">
            <i class="el-icon-view" @click="handlePreview(imgItem.url)"></i>
            <i class="el-icon-delete" @click="handleDel(imgKey)"></i>
          </div>
          <div class="error-tip" v-if="imgItem.status == 2">
            <el-popover placement="top-start" width="200" trigger="hover" :content="imgItem.reason || '--'">
              <span slot="reference">{{ imgItem.reason || '--' }}</span>
            </el-popover>
          </div>
        </div>
        <slot name="footer" :scope="{ imgItem, imgKey }"></slot>
      </div>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import cameraUrl from '@/assets/camera.png'
import url from '@/assets/images/idCard.png'
// import glsxCombiForm from '@/components/glsx-combi-form'
export default {
  name: 'imgList',
  props:{
    list:{
        type: Array
    }
  },
  data() {
    return {
      url,
      cameraUrl,
      dialogImageUrl: '',
      dialogVisible: false,
      activeName: 'base'
    }
  },
  components: {
    // glsxCombiForm
  },
  mounted() {

  },
  methods: {
    handlePreview(url) {
      this.dialogImageUrl = url;
      this.dialogVisible = true;
    },
    handleDel(key){
        this.list[key].url = ''
        this.list[key].status = ''
    },
    clickImg(type) {
      this.$refs.input.click();
    },
    upload(e){
      const file = e.target.files[0]
      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.beforeSale.uploadImgApi({ file: base64, fileType: 'image' }).then(res => {
          console.log(res,'resss')
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  display: flex;
  flex-wrap: wrap;
  position: relative;
  .file {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
  }
  .wrap {
    margin: 0 16px 16px 0;
    .title {
      font-family: PingFangSC-Regular;
      font-size: 14px;
      color: #333333;
      .success {
        color: #2cca74;
        margin-right: 5px;
      }
      .error {
        color: #eb4e4e;
        margin-right: 5px;
      }
      .question {
        color: #999;
        cursor: pointer;
        margin-left: 5px;
      }
    }
    .has-img:hover {
      .mask {
        display: block;
      }
    }
    .error-img {
      border: 1px solid #ff5257 !important;
    }
    .success-img {
      border: 1px solid rgba(0, 0, 0, 0.15) !important;
    }
    .img-box {
      margin-top: 8px;
      height: 135px;
      width: 216px;
      border: 1px dashed rgba(0, 0, 0, 0.15);
      border-radius: 4px;
      position: relative;
      background-color: #f5f5f5;
      .error-tip {
        width: 100%;
        background: rgba(255, 82, 87, 0.7);
        position: absolute;
        height: 54px;
        line-height: 16px;
        bottom: 0;
        border-radius: 0 0 4px 4px;
        z-index: 10;
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        font-family: PingFangSC-Regular, PingFang SC;
        font-size: 14px;
        padding: 5px 10px;
        display: -webkit-box;
        box-sizing: border-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
        overflow: hidden;
      }
      .need-img,
      .mask {
        position: absolute;
        left: 0;
        bottom: 0;
        top: 0;
        right: 0;
        z-index: 2;
      }
      .mask {
        display: none;
        background: rgba(0, 0, 0, 0.5);
        z-index: 9;
        text-align: center;
        line-height: 135px;
        border-radius: 4px;
        i {
          color: #c6c6c6;
          cursor: pointer;
          font-size: 16px;
          &:first-child {
            margin-right: 12px;
          }
        }
      }
      .add-btn {
        position: absolute;
        width: 60px;
        height: 60px;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 1;
        cursor: pointer;
      }
      img {
        width: 100%;
        height: 100%;
        border-radius: 4px;
      }
    }
  }
}
</style>