<template>
  <div class="ui-update">
	  <!-- 文件上传 -->
	<el-upload
	  v-if="uploadType=='file'"
	  class="upload-demo"
	  :action="upUrl"
	  :data="updata"
	  :show-file-list="false"
	  :headers="headers"
	  :on-success="handleAvatarSuccess"
	  :on-error="handleAvatarError"
	  :before-upload="beforeAvatarUpload">
	  <el-button size="small" type="primary">点击上传</el-button>
	</el-upload>
	<!-- 图片上传 -->
	<el-upload
	  v-else
	  class="avatar-uploader fx-r fx-ac fx-h"
	  :action="upUrl"
	  :data="updata"
	  :show-file-list="false"
	  :headers="headers"
	  :on-success="handleAvatarSuccess"
	  :on-error="handleAvatarError"
	  :before-upload="beforeAvatarUpload">
	  <div v-if="img"  class="update-img fx-h fx-ac fx-bc">
	    <img  :src="img" class="avatar">
	  </div>
	  <div v-if="!img" class="">{{fileName}}</div>
	</el-upload>
  </div>
</template>


<script>
    export default {
        name: "ui-upload",
        computed: {},
        props:{
            upUrl:{
                default:'',
                type:String
            },
            headers:{
                default: ()=> { return {};},
                type:Object
            },
			uploadType:{
				default:'',
				type:String
			},
            updata:{
                default: ()=> { return {};},
                type:Object
            },
            img:{
                default:'',
                type:String
            },
            fileName:{
                default:'',
                type:String
            },
            type:{
                default:'img',
                type:String
            },
        },
        data() {
            return {
            }
        },

        created() {
        },
        mounted(){

        },
        methods: {
            beforeAvatarUpload(file) {
                const array = ['image/jpeg','image/png','image/gif'];
				var maxSize = 2;
				var utitle = "图片";
				//文件上传
				if(file.type==''){
					maxSize = 3;
					utitle = "文件";
				}
                const isLt2M = file.size / 1024 / 1024 < maxSize;
                var isImg = true;
                if (file.type!='' && array.indexOf(file.type) == -1) {
                    this.$message.error('上传头像图片只能支持JPG,PNG,GI格式!');
                    isImg = false;
                }
                if (!isLt2M) {
                    this.$message.error('上传'+utitle+'大小不能超过 '+maxSize+'MB!');
                }
                this.$utils.loading(this,"上传中..");
                return isImg && isLt2M;
            },
            /**
             * 上传成功以后
             */
            handleAvatarSuccess(res, file) {
                this.$utils.loadingClose();
                // this.imageUrl = URL.createObjectURL(file.raw);
				res.imginfo = file;
                this.$emit("onUpload",res);
            },

            handleAvatarError:function (err, file, fileList) {
                this.$utils.loadingClose();
              //  this.$message.error(err);
                console.log(err);
            }

        }
    }
</script>
<style scoped>
  .avatar-uploader{
    border: 1px dashed #d9d9d9;
    width: 82px;
    height: 82px;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .file-uploader{
    border: 1px dashed #d9d9d9;
    width: 100%;
    height: 42px;
    border-radius: 2px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
  }
  .update-img{ height: 80px; width: 80px; overflow:hidden;}
  .avatar {width: 80px;display: block;}
</style>
