index.vue 3.65 KB
<template>
  <div class="upload_img_wrap">
    <el-upload
			ref="pictures"
			v-if="pictures.length<maxNum"
			list-type="picture-card"
			class="image-upload image-header"
			:action='WEBURL+"/carservice/publicservice/common/uploadFile"'
			header='Access-Control-Allow-Origin'
			:show-file-list="false"
			:on-success="uploadHeaderImageSuccess"
			:on-error=" (value) => uploadError(value,'附件')"
			:before-upload=" (value) => uploadImageChange(value,'附件')"
			>
			<span>
        <i class="el-icon-plus image-upload-icon"></i>
			</span>
		</el-upload>
		<template v-for="(item,index) in pictures">
			<span
				style="position:relative;display: inline-block;"
				:key="index">
		
				<i class="el-icon-remove red-cl" style="position:absolute;top:1px;right:5px;cursor:pointer;" @click="deleteImage(index)"></i>
				<el-image
          class="image-header-item"
          :src="item"
          :preview-src-list="pictures"
          fit="contain">
				</el-image>
			</span>
		</template>
  </div>
</template>

<script>
import {
  getBussinessApi,
  getDictDataListByKey,
} from '@/api/system'

export default {
  props: {
    maxNum: Number,
    beforeUpload: Function, // eslint-disable-line
    onSuccess: Function// eslint-disable-line
  },
  data() {
    return {
      WEBURL:process.env.BASE_API,
      pictures: [],
    }
  },
  methods: {
    uploadError(file,imageName){
      this.$message.error(imageName+'上传失败!');
    },
    uploadImageChange(file,imageName) {
      const regx = ["image/png","image/jpeg"]
      const isJPG = regx.includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传的'+imageName+'只能是 JPG/PNG 格式!');
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传的'+imageName+'大小不能超过 2MB!');
        return false
      }
      return isJPG && isLt2M;
    },
    //业务
    uploadHeaderImageSuccess(res, file) {
      this.pictures.push(res.data);
      this.$emit('update', this.pictures)
    },
    deleteImage(index){
      getBussinessApi({'fileUrl':this.pictures[index]},'deleteFile','post').then(response => {
        if(response&&response.code == 200){ //添加成功
          this.pictures.splice(index,1)
          this.$emit('update', this.pictures)
        }else{
          this.$message.error('照片删除失败,请稍后重试');
        }
      }).catch(error => {})
    },
    reset(){
      this.pictures = []
    }
  }
}
</script>

<style lang="scss" scoped>
.upload_img_wrap{
  .image-upload {
    .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 5px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      width: 100%;
      height: 100%;
    }
    .el-upload:hover {
      border-color: #409EFF;
    }
    .image-upload-icon {
      font-size: 28px;
      color: #8c939d;
      width: 100%;
      height: 100%;
      text-align: center;
    }
    .image {
      width: 100%;
      height: 100%;
      display: block;
    }
    p{
      position: absolute;
      top: 0px;
      width: 100%;
      color: #999;
      margin: 0;
      padding-top: 30px;
    }
  }
  .image-header {
    padding: 10px 0 0 0;
    float: left;
    margin-right: 10px;
    .el-upload {
      border-radius: 5px;
      width: 100px;
      height: 100px;
      line-height: 100px;
    }
  }
  .image-header-item{
    border:1px dashed rgba(0,0,0,.1);
    padding: 5px;
    border-radius: 5px;
    width: 100px; 
    height: 100px; 
    margin:10px 10px 0 0;
  }
  
}


</style>
<style lang="scss" >
.upload_img_wrap{
  .el-upload--picture-card{
    width: 100px;
    height: 100px;
    line-height: 102px;
  }
}
</style>