<style>
  .koreaUpload {
    width: 100%;
    background-color: #fff;
    height: 100%;
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
    color: #000;
  }

  .koreaUpload .box {
    width: 90%;
    height: 200px;
    margin-top: 40px;
    border-radius: 8px;
    margin-left: 5%;
    border: 1px dashed #e2e2e2;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .koreaUpload .avatar-uploader {}

</style>
<template>
  <div class="koreaUpload">

    <div class="box">
      <el-upload class="avatar-uploader " action="" :before-upload="uploadFile" :show-file-list="false">
        <i class="el-icon-upload" style="font-size: 40px;color: #e2e2e2;"></i>
        <div class="el-upload__text" style="font-size: 18px;">上传文件</em></div>
      </el-upload>
    </div>
    <div>{{fliename}}</div>
  </div>
</template>
<script>
  import wx from 'weixin-js-sdk'

  import {
    UploadViittoFile
  } from "../api/common/common";
  export default {
    meta: {
      title: "文件上传"
    },
    data() {
      return {
        data: '',
        fliename: '',
      };
    },
    created() {
      if (this.$route.query) {
        this.data = JSON.parse(this.$route.query.data)
      }
    },
    methods: {
      uploadFile(files) {
        let ft = files.name.substring(files.name.lastIndexOf('.') + 1, files.name.length)
        let stringArr = this.data.FileType
        if (stringArr.indexOf(ft) == '-1') { //判断文件格式
          this.$q.notify({
            type: 'negative',
            message: `请上传正确的文件格式!`,
            position: 'top'
          })
          return false;
        }
        if (this.data.FileSizeLimit > 0) { //判断文件大小
          const size = files.size / 1024 / 1024
          this.FileName = size
          if (size > this.data.FileSizeLimit) {
            this.$q.notify({
              type: 'negative',
              message: "文件大小不能超过" + this.data.FileSizeLimit + "MB哦!",
              position: 'top'
            })
            return false;
          }
        }
        UploadViittoFile('korea/Test/Upload', files, res => {
          if (res.Code == 1) {
            res.data = this.data;
            wx.miniProgram.navigateBack({
              delta: 1
            })
            wx.miniProgram.postMessage({
              data: res
            })
          } else {
            this.$q.notify({
              type: 'negative',
              message: "上传失败！",
              position: 'top'
            })
            return false;
          }
        }).catch(() => {
          this.$q.notify({
            type: 'negative',
            message: "上传失败！",
            position: 'top'
          })
          return false;
        })
      },
    },
    mounted() {}
  };

</script>
