<template>
  <div class="add_img">
    <div class="add_pic">
      <i class="el-icon-plus add_icon"></i>
      <input
        type="file"
        id="uploads"
        class="up_input"
        accept="image/png, image/jpeg, image/gif, image/jpg"
        @change="uploadImg($event,1)"
      />
    </div>
    <!-- 弹出层-裁剪 -->
    <el-dialog
      title="编辑图片"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :close-on-click-modal="false"
      class="cropper_dia"
    >
      <div style="width； 100%">
        <div class="cropper_container">
          <!-- 裁剪 -->
          <div class="croppers">
            <vueCropper
              style="width:100%;height:300px"
              ref="cropper"
              :img="attach.customaryUrl"
              :autoCrop="options.autoCrop"
              :fixedBox="options.fixedBox"
              :canMoveBox="options.canMoveBox"
              :autoCropWidth="options.autoCropWidth"
              :autoCropHeight="options.autoCropHeight"
              :centerBox="options.centerBox"
              @realTime="realTime"
            ></vueCropper>
          </div>
          <!-- 实时裁剪结果 -->
          <div :style="previews.div" class="preview">
            <img :src="previews.url" :style="previews.img" />
          </div>
        </div>
        <el-row class="footerBtn" align="center">
          <el-button type="primary" size="small" round @click="cut('blob')">确认</el-button>
          <el-button type="primary" size="small" round @click="handleClose">取消</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { VueCropper } from "vue-cropper";
export default {
  components: {
    VueCropper
  },
  data() {
    return {
      dialogVisible: false,
      options: {
        autoCrop: true, //默认生成截图框
        fixedBox: true, //固定截图框大小
        canMoveBox: true, //截图框不能拖动
        autoCropWidth: 200, //截图框宽度
        autoCropHeight: 200, //截图框高度
        centerBox: false //截图框被限制在图片里面
      },
      previews: {}, //实时预览图数据
      attach: {
        id: "",
        userId: "",
        customaryUrl: "", //原图片路径
        laterUrl: "", //裁剪后图片路径
        attachType: "photo" //附件类型
      },
      fileName: "", //本机文件地址
      uploadImgRelaPath: "" //上传后图片地址
    };
  },
  methods: {
    //控制弹出层关闭
    handleClose() {
      this.dialogVisible = false;
    },
    //实时预览
    realTime(data) {
      this.previews = data;
    },
    //加载图片信息
    find() {
      this.userId = sessionStorage.getItem("userId");
      this.$http.post("ssapi", this.attach).then(res => {
        console.log(res);
      });
    },
    //选择本地图片
    uploadImg(e, num) {
      var file = e.target.files[0];
      if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
        this.$message.error("图片类型必须是.gif,jpeg,jpg,png,bmp中的一种");
        return false;
      }
      // console.log(e.target);
      //fileReader 接口，用于异步读取文件数据
      var reader = new FileReader();
      reader.readAsDataURL(file); //重要 以dataURL形式读取文件
      reader.onload = e => {
        // data = window.URL.createObjectURL(new Blob([e.target.result])) 转化为blob格式

        let data = e.target.result;

        this.attach.customaryUrl = data;
        // 转化为base64
        // reader.readAsDataURL(file)
        // 转化为blob
      };
      this.dialogVisible = true;
    },
    //确认截图,上传
    cut(type) {
      var formData = new FormData();
      this.$refs.cropper.getCropBlob(res => {
        //res是裁剪后图片的bolb对象
        formData.append("file", res, this.userId);
        formData.append("directory", "image");
        let url = "/awecloud/static/image/upload";
        this.$http
          .post(url, formData, {
            contentType: false,
            processData: false,
            headers: { "Content-Type": "multipart/form-data" }
          })
          .then(res => {
            // 上传图片后服务器返回访问路径
            this.$emit("getNewUrl", res.data.data);
            this.handleClose();
          });
      });
    }
  }
};
</script>

<style scoped>
.add_img {
  display: inline-block;
  margin-left: 10px;
}
.add_pic {
  width: 140px;
  height: 140px;
  border: 3px #f6f7fb solid;
  border-radius: 5px;
  position: relative;
}
.add_icon {
  position: absolute;
  top: calc((100% - 70px) / 2);
  left: calc((100% - 70px) / 2);
  color: #e3e6f0;
  font-size: 70px;
}
.up_input {
  display: inline-block;
  width: 100%;
  height: 100%;
  opacity: 0;
}
.cropper_container {
  width: 100%;
}
.croppers {
  display: inline-block;
  width: 400px;
}
.preview {
  display: inline-block;
  overflow: hidden;
  border: 1px solid #cccccc;
  background: #cccccc;
  margin-left: 20px;
}
.footerBtn {
  display: flex;
  justify-content: center;
  margin-top: 15px;
}
</style>
<style>
.cropper_dia .el-dialog {
  width: 860px;
}
</style>