<template>
  <div>
    <upload-to-ali
      v-model="url"
      :customDomain="OSS_CUSTOMDOMAIN()"
      preview
      :region="OSS_REGION()"
      :bucket="OSS_BUCKET()"
      :multiple="true"
      :accept="OSS_ACCEPT()"
      :size="102400"
      :accessKeyId="OSS_ACCESSKEYID()"
      :accessKeySecret="OSS_ACCESSKEYSECRET()"
      :before-upload="file"
      :dir="dir"
      @loaded="overUpload"
    ></upload-to-ali>
  </div>
</template>
<script>
import UploadToAli from '@femessage/upload-to-ali'
import BMF from 'browser-md5-file'
import { fileCheck, fileInsert } from '@/api/actor'
export default {
  data() {
    return {
      url: '',
      accessKeyId: 'LTAIbNbqWzSOklQV',
      accessKeySecret: '1rkPz7JNoXk8sJevPaeYHWqfkQXBGh',
      bucket: 'ctop-media',
      region: 'oss-cn-beijing',
      dir: 'img/2019-10-30/',
      md5: ''
    }
  },
  components: {
    UploadToAli
  },
  methods: {
    OSS_CUSTOMDOMAIN() {
      return ''
    },
    OSS_REGION() {
      return this.region
    },
    OSS_BUCKET() {
      return this.bucket
    },
    OSS_ACCESSKEYID() {
      return this.accessKeyId
    },
    OSS_ACCESSKEYSECRET() {
      return this.accessKeySecret
    },
    OSS_ACCEPT() {
      return 'image/jpeg,image/png,video/mpeg,video/mp4'
    },
    file(file) {
      console.log(file)
      var bmf = new BMF()
      var that = this
      return new Promise(function(resolve, reject) {
        bmf.md5(file[0], (err, md5) => {
          that.md5 = md5
          fileCheck({ code: md5 }).then(res => {
            console.log(res)
            if (res.code == 0) {
              reject()
            } else {
              resolve()
            }
          })
        })
      })
    },
    overUpload(url) {
      console.log(url, this.md5)
      fileInsert({ code: this.md5, url: url[0] }).then(res => {
        console.log(res)
      })
    }
  }
}
</script>