// 压缩图片
// eslint-disable-next-line no-unused-vars
export function compressImage (file, config, read) {
  // eslint-disable-next-line no-unused-vars
  let src
  // eslint-disable-next-line no-unused-vars
  let files
  // let fileSize = parseFloat(parseInt(file['size']) / 1024 / 1024).toFixed(2)
  read.readAsDataURL(file)
  return new Promise(function (resolve, reject) {
    read.onload = function (e) {
      const img = new Image()
      img.src = e.target.result
      img.onload = function () {
        // 默认按比例压缩
        const w = config.width
        const h = config.height
        // 生成canvas
        const canvas = document.createElement('canvas')
        const ctx = canvas.getContext('2d')
        let base64 = null
        // 创建属性节点
        canvas.setAttribute('width', w)
        canvas.setAttribute('height', h)
        ctx.drawImage(this, 0, 0, w, h)

        base64 = canvas.toDataURL(file.type, config.quality)

        // 回调函数返回file的值（将base64编码转成file）
        // files = dataURLtoFile(base64) // 如果后台接收类型为base64的话这一步可以省略
        // 回调函数返回file的值（将base64转为二进制）
        // let fileBinary = dataURLtoBlob(base64)

        resolve(base64)
      }
    }
  })
};
// 将base64转为二进制  此方法我也没用到
function dataURLtoBlob (dataurl) {
  const arr = dataurl.split(',')
  const mime = arr[0].match(/:(.*?);/)[1]
  const bstr = atob(arr[1])
  let n = bstr.length
  const u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new Blob([u8arr], { type: mime })
}

// base64转码（将base64编码转回file文件）  此方法我没用到
// eslint-disable-next-line no-unused-vars
function dataURLtoFile (dataurl) {
  const arr = dataurl.split(',')
  const mime = arr[0].match(/:(.*?);/)[1]
  const bstr = atob(arr[1])
  let n = bstr.length
  const u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new File([u8arr], { type: mime })
}
