<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2018-12-06 13:44:22
 * @LastEditors: 无尘
 * @LastEditTime: 2020-05-14 16:56:22
 -->
<!--
  单个上传组件
   <single-upload
    :imgSrc.sync=""
    :field="''">
  </single-upload>
 -->
<template>
  <div class="single-upload-wrap">
    <el-upload class="avatar-uploader" :action="uploadUrl()" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
      <img v-if="imgSrc" :src="imgSrc" class="avatar" @mouseover="showImage(imgSrc)" />
      <video v-if="videoSrc != '' && videoFlag == false" :src="videoSrc" class="avatar">您的浏览器不支持视频播放</video>
      <div v-if="!imgSrc && !videoSrc">
        <i class="el-icon-plus avatar-uploader-icon"></i>
        <p class="font-12 color-606266">{{ uploadType == 1 || uploadType == 2 ? '上传图片' : '上传视频' }}</p>
      </div>
      <!-- <el-progress v-if="videoFlag == true" type="circle" :percentage="videoUploadPercent" style="margin-top:30px;"></el-progress> -->
    </el-upload>

    <p v-if="uploadType == 1" class="upload-tip color-909399 font-12">
      图片支持png、jpg、jpeg格式，大小不超过2M，可上传1张
    </p>
    <p v-if="uploadType == 2" class="upload-tip color-909399 font-12">
      建议尺寸：160×160；支持jpg、jpeg、png格式，图片大小不超过1MB
    </p>
    <p v-if="uploadType == 3" class="upload-tip color-909399 font-12">
      视频支持MP4格式，大小不超过10M，上传企业微信后3天内有效
    </p>
    <!-- 图片预览 -->
    <!-- <vue-gic-img-preview :imgUrl="imgUrl" :imgShowFlag="imgShowFlag" @hideImage="hideImage"></vue-gic-img-preview> -->
  </div>
</template>
<script>
// import strLength from '@/common/js/strlen';
// import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
// import { _debounce } from '@/common/js/public';
// import { getRequest } from '@/api/api';
export default {
  name: 'single-upload',
  props: {
    videoSrc: {
      // 传入 input value
      type: String,
      default: ''
    },
    imgSrc: {
      // 传入 input value
      type: String,
      default: ''
    },
    field: {
      type: String,
      default: ''
    },
    uploadLimit: {
      type: Number,
      default: 2
    },
    uploadType: {
      type: [Number, String],
      default: 1 // 1.图片 2.网页 3.视频
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名
      imgShowFlag: false, // 是否弹框显示,true: 显示；false: 不显示
      imgUrl: '', // 传递的图片 src
      videoFlag: false,
      fileType: this.uploadType == 1 || this.uploadType == 2 ? 'jpg' : 'mp4'
    };
  },
  beforeMount() {
    const that = this;
    let host = window.location.origin;
    if (host.indexOf('localhost') != -1) {
      that.baseUrl = 'http://www.gicdev.com';
    } else {
      that.baseUrl = host;
    }
    // that.upUrl = that.baseUrl + '/api-plug/upload-img?wxFlag=1'
  },
  methods: {
    /**
     * 上传图片/视频
     */
    uploadUrl() {
      const that = this;
      let host = window.location.origin;
      let baseUrl;
      if (host.indexOf('localhost') != '-1') {
        baseUrl = 'http://www.gicdev.com';
      } else {
        baseUrl = host;
      }
      that.upUrl = baseUrl + `/haoban-manage3-web/upload-file?fileType=${that.fileType}`;
      return that.upUrl;
    },

    /**
     * 上传图片
     */
    handleAvatarSuccess(res, file, type) {
      const that = this;
      if (res.errorCode == 1) {
        console.log(res);
        that.videoFlag = false;
        if (that.uploadType == 3) {
          that.$emit('update:videoSrc', res.result.url);
        } else {
          that.$emit('update:imgSrc', res.result.url);
        }
      } else {
        errMsg.errorMsg(res);
      }
    },
    /* eslint-disable */
    beforeAvatarUpload(file) {
      const that = this;
      console.log(file);
      that.fileType = file.type === 'image/jpeg' ? 'jpeg' : file.type === 'image/jpg' ? 'jpg' : file.type === 'image/png' ? 'png' : file.type == 'video/mp4' ? 'mp4' : '';
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png';
      const isMP4 = file.type == 'video/mp4';
      const isLt2M = file.size / 1024 / 1024 < that.uploadLimit;
      if ((that.uploadType == 1 || that.uploadType == 2) && !isJPG) {
        that.$message.error('上传图片支持png、jpg、jpeg格式!');
      }
      if ((that.uploadType == 1 || that.uploadType == 2) && !isLt2M) {
        that.$message.error(`上传图片大小不能超过 ${that.uploadLimit}MB!`);
      }
      if (that.uploadType == 3 && !isMP4) {
        that.$message.error('上传视频支持MP4格式!');
      }
      if (that.uploadType == 3 && !isLt2M) {
        that.$message.error(`上传视频大小不能超过 ${that.uploadLimit}MB!`);
      }
      if (that.uploadType == 3) {
        return isMP4 && isLt2M;
      }
      return (
        isJPG &&
        isLt2M &&
        new Promise(function(resolve, reject) {
          let reader = new FileReader();
          reader.onload = function(event) {
            let image = new Image();
            image.onload = function() {
              /* eslint-disable */
              let width = this.width;
              let height = this.height;
              // if (that.uploadType == 2 && width !== 160 && height !== 160){
              //   that.$message.error('图片尺寸必须为160px*160px!');
              //   reject();
              // }
              resolve();
            };
            image.src = event.target.result;
            // that.$emit('update:imgSrc',event.target.result)
          };
          reader.readAsDataURL(file);
        })
      );
    },

    /**
     * 上传图片预览
     */
    showImage(src) {
      const that = this;
      if (!src || src == '') {
        return false;
      }
      that.imgShowFlag = true;
      that.imgUrl = src;
    },
    hideImage(val) {
      const that = this;
      that.imgShowFlag = val;
    }
  }
  // mounted() {}
};
</script>
<style lang="less" scoped>
.w-500 {
  width: 500px;
}
.avatar-uploader {
  /deep/ .el-upload {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-justify-content: center;
    justify-content: center;
    width: 100px;
    height: 100px;
    border: 1px dashed #c4c6cf;
    border-radius: 0px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
}

/* flex */
.flex {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
}

.flex-1 {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.flex-column {
  -webkit-flex-direction: column;
  -moz-flex-direction: column;
  -ms-flex-direction: column;
  -o-flex-direction: column;
  flex-direction: column;
}

.flex-row {
  -webkit-flex-direction: row;
  -moz-flex-direction: row;
  -ms-flex-direction: row;
  -o-flex-direction: row;
  flex-direction: row;
}

.flex-align-center {
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
}

.flex-pack-center {
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  // height: 100px;
  // line-height: 100px;
  text-align: center;
}
.avatar {
  max-width: 100px;
  max-height: 100px;
  display: block;
}

.upload-tip {
  margin-top: 8px;
  font-size: 12px;
  line-height: 20px;
}

.color-1890ff {
  color: #2f54eb;
}
</style>
