imgList.vue 10 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" />
      <template v-for="(imgItem, imgKey) in list">
        <div class="wrap" :key="imgKey" v-if="(imgItem.key == curKey && curKey != 'detail') || (curKey == 'detail' && imgItem.serviceItemCode == curCode && hasLossType(imgItem))">
          <span class="title">
            <i v-if="imgItem.imgObj.auditStatus === 1" class="el-icon-success success"></i>
            <i v-if="imgItem.imgObj.auditStatus === 0" class="el-icon-error error"></i>
            <el-tooltip class="item" effect="dark" :content="imgItem.secondClassifyName" placement="top">
              <span class="name">{{ imgItem.secondClassifyName }}</span>
            </el-tooltip>
            <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.imgObj.attachmentFile, 'error-img': imgItem.status == 2, 'success-img': imgItem.status == 1 }]">
            <img v-if="!imgItem.imgObj.attachmentFile && imgItem.exampleUrl" class="example-img" fit="cover" :src="imgItem.exampleUrl" alt="" />
            <span class="example" v-if="!imgItem.imgObj.attachmentFile && imgItem.exampleUrl" @click.stop="viewExample(imgItem.exampleUrl)">示例</span>
            <img v-if="!imgItem.imgObj.attachmentFile" :class="['add-btn', { disabled: disabled }]" :src="cameraUrl" alt="" @click="clickImg(imgItem, imgKey)" />
            <img v-else class="need-img" fit="cover" :src="imgItem.imgObj.attachmentFile" alt="" />
            <div class="mask">
              <i class="el-icon-view" @click="handlePreview(imgItem.imgObj.attachmentFile)"></i>
              <!-- 定损不通过&不是其他资料&不是不通过的,则隐藏 -->
              <template v-if="!(curRouteInfo.status == 7 && imgItem.imgObj.auditStatus !== 0 && curKey != 'other')">
                <!-- 如果是现场照片&客户提交的,则隐藏 -->
                <template v-if="!(curKey == 'onSite' && isCustomerCommit)">
                  <i v-if="!disabled" class="el-icon-delete" @click="handleDel(imgItem, imgKey)"></i>
                </template>
              </template>
            </div>
            <div class="error-tip" v-if="imgItem.imgObj.auditStatus === 0">
              <el-popover placement="top-start" width="200" trigger="hover" :content="imgItem.imgObj.reasonForFailure || '--'">
                <span slot="reference">{{ imgItem.imgObj.reasonForFailure || '--' }}</span>
              </el-popover>
            </div>
          </div>
          <slot name="footer" :scope="{ imgItem, imgKey }"></slot>
        </div>
      </template>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
    <image-viewer :z-index="10000" :initial-index="initIdx" v-if="showViewer" :on-close="closeViewer" :url-list="reviewList" />
    <!-- <pre-img ref="preImgRef"></pre-img> -->
  </div>
</template>
<script type="text/ecmascript-6">
import cameraUrl from '@/assets/camera.png'
import url from '../../image/code.png'
import imageViewer from 'element-ui/packages/image/src/image-viewer';
// import preImg from './preImg'
// import glsxCombiForm from '@/components/glsx-combi-form'
export default {
  name: 'imgList',
  props:{
    list:{
        type: Array
    },
    id: {
      type: Number
    },
    curKey:{
      default:'base'
    },
    curCode:{
      default:''
    },
    curRouteInfo: Object,
    disabled:{
      type: Boolean,
      default: false
    },
    curLossType:{
      type: Array,
      default:function(){
        return []
      }
    },
    isCustomerCommit:{
      type: Boolean,
      default: false
    },
    isWriteOff:{
      type: Boolean,
      default: false
    }

  },
  computed:{
    hasLossType(){
      return function(item){
        if(this.isWriteOff) return true;
        // if(item.secondClassifyName.indexOf('钥匙:')!=0 || item.secondClassifyName.indexOf('车牌:')!=0) return true;
        return item.serviceItemCode!='SC0006' || (item.serviceItemCode=='SC0006' && ((item.secondClassifyName.indexOf('钥匙:')===0 && this.curLossType?.includes('3')) || (item.secondClassifyName.indexOf('车牌:')===0 && this.curLossType?.includes('4'))))
      }
    },
    reviewList(){
      return this.list?.map(item => {
          if(this.curKey == item.key || (this.isWriteOff && item.serviceItemCode == this.curCode)) return item?.imgObj?.attachmentFile
        })?.filter(item => !!item)
    }
  },
  data() {
    return {
      url,
      initIdx: 0,
      showViewer: false,
      cameraUrl,
      dialogImageUrl: '',
      dialogVisible: false,
      curID:'',
      curItem: {},
    }
  },
  components: {
    imageViewer
    // glsxCombiForm
    // preImg
  },
  mounted() {
  },
  methods: {
    closeViewer(){
      this.showViewer = false
    },
    handlePreview(e) {
      this.initIdx = this.reviewList?.indexOf(e) || 0
      this.showViewer = true
    },
    viewExample(url){
      // this.$refs.preImgRef.preImage(url)
      this.dialogImageUrl = url;
      this.dialogVisible = true;
    },
    handleDel(item,idx){
      if(item.secondClassifyName == '其他资料'){
        this.list.splice(idx, 1)
        this.$forceUpdate();
      }else{
        this.$set(this.list[idx], 'imgObj', {})
      }
    },
    clickImg(item,idx) {
      if(this.disabled) return;
      this.curID = idx
      this.curItem =  item
      this.$refs.input.click();
    },
    upload(e){
      // let url = 'http://qcimg01.glsx.com.cn:7060/group1/M00/02/34/wKgD3mMlXICAbufiAABNO42zIwY122.png'
      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.uploadImageApi({ file: base64, fileType: 'image' }).then(res => {
          if(res.code == '0' && res.data){
            const {id, attachmentCode } = this.curItem
            if(this.list[this.curID]?.imgObj?.id){
              this.$set(this.list[this.curID].imgObj, 'attachmentFile',  url)
            }else{
              let obj = {
                claimsId : this.id,
                attachmentCode,
                attachmentType: file.name.split('.').pop(),
                attachmentFile : res.data,
                classifyId : id,
              }
              this.$set(this.list[this.curID],'imgObj', obj)
            }
            if(this.curItem.firstClassifyName.indexOf('其他资料')>-1 && this.curItem.secondClassifyName=='其他资料'){
              this.list.push({...this.curItem,imgObj:{}})
              this.$forceUpdate()
            }
          }
        }).finally(()=>{
          this.$refs.input.value = ''
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.disabled {
  cursor: not-allowed !important;
}
.container {
  display: flex;
  flex-wrap: wrap;
  position: relative;
  .file {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
  }
  .wrap {
    margin: 0 16px 16px 0;
    .title {
      display: flex;
      align-items: center;
      font-family: PingFangSC-Regular;
      font-size: 14px;
      color: #333333;
      .name {
        width: 190px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        display: inline-block;
        height: 20px;
      }
      .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: 5px;
      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;
        top: 90px;
        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: 2;
        overflow: hidden;
      }
      .need-img,
      .mask {
        position: absolute;
        left: 0;
        bottom: 0;
        top: 0;
        right: 0;
        z-index: 6;
      }
      .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;
          &:last-child {
            margin-left: 12px;
          }
        }
      }
      .example {
        position: absolute;
        display: inline-block;
        width: 36px;
        height: 18px;
        background-color: #e5e5e5;
        font-size: 12px;
        text-align: center;
        right: 8px;
        top: 8px;
        color: #666;
        cursor: pointer;
        border-radius: 2px;
        z-index: 4;
      }
      .example-img {
        z-index: 3;
      }
      .add-btn {
        position: absolute;
        width: 60px;
        height: 60px;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 5;
        cursor: pointer;
      }
      img {
        width: 100%;
        height: 100%;
        border-radius: 4px;
      }
    }
  }
}
</style>