/* eslint-disable */
// 阿里云上传方法
import OSS from 'ali-oss'
import md5 from './md5'
import $https from '../https/request'
import $API from '@/https/api'

let accessKeyId = 'STS.NJ65zv2Jt5KJFHMSFd1wy4CaW',
  accessKeySecret = '9EttMviN9FhXWRbPGWaVSF612t1qW9bK7SDG4Xe8mmSL',
  stsToken = 'CAIS+wF1q6Ft5B2yfSjIr4mDfsDC35VVgomhRG78t0YxPfhV24bKtTz2IHhIeHZoBOkfv/g0mW9V5/sblqR3TJBEXlDFZtA1tswOqbZE6SBR/57b16cNrbH4M0rxYkeJ8a2/SuH9S8ynCZXJQlvYlyh17KLnfDG5JTKMOoGIjpgVBbZ+HHPPD1x8CcxROxFppeIDKHLVLozNCBPxhXfKB0ca0WgVy0EfsPzknZPEtUrk4QekmrNPlePYOYO5asRgBpB7Xuqu0fZ+Hqi7i3YMt0cSqf4v3fAfpGia74zEGSpY5xSXLuzT6cZzKwx0YKc3HaNepeT7j+d/vuHDcUtmpxqAAWCZW7X2K/9T3H23Uo7ksEn3t2I3p/2XisqHJFfrRVk42YUNXrf/IrqW6mCE6s5qHu85YrpfCprgy4S+fP4SACGIdWpiAc5mEDcD+zV6vNN0sS+//EWsXtZL30eZaXgJ4Plal7Vt+WE5/xycm0KmUCH8TJM07EFq51Lq/9VTkbzW',
  endpoint = 'https://oss-cn-shenzhen.aliyuncs.com/',
  bucket = 'logistics-pic',
  url = 'https://logistics-pic.oss-cn-shenzhen.aliyuncs.com'

// 获取token
function retrieveToken () {
  let url = $API.public.upload.aliyunToken
  let param = {}
  return $https.post(url, param)
    .then(data => {
      accessKeyId = data.data.accessKeyId
      accessKeySecret = data.data.accessKeySecret
      stsToken = data.data.securityToken
    })
}

class Aliyun {
  constructor () {
    this.init = this.init.bind(this)
    this.upload = this.upload.bind(this)
    this.download = this.download.bind(this)
    this.delete = this.delete.bind(this)
    this.client = null
    this.init()
  }
  // 初始化
  init () {
    retrieveToken()
      .then(() => {
        this.client =
          new OSS({
            accessKeyId,
            accessKeySecret,
            stsToken,
            endpoint,
            bucket,
          })
      })
      .catch(err => {
        console.log('获取token错误, err =' + err)
      })
  }
  // 上传
  upload (file) {
    // 配置文件名
    const suffix = '.' + file.name.split('.').reverse()[0] // 文件后缀
    const strFormat = md5(String(new Date().getTime() + file.name)) + suffix // 加密加后缀创建独一无二文件名
    // 执行上传
    return new Promise((resolve, reject) => {
      this.client.multipartUpload(strFormat, file)
        .then((result) => {
          // 返回结果对象给业务代码
          resolve({
            ...result, // 返回对象
            // 业务可能关心的字段
            fileAddress: `${url}/${result.name}`,
            fileName: file.name,
            fileType: file.type,
            fileId: result.name,
          })
        })
        .catch(function (err) {
          console.log('阿里云上传文件错误: ' + err)
          reject({
            err,
            name: file.name
          })
        })
    })
  }
  // 下载
  download (file) {
    return this.client.signatureUrl(file.fileId)
    // return new Promise((resolve, reject) => {
    //   const dlUrl =
    //   if (dlUrl) {
    //     resolve(dlUrl)
    //   } else {
    //     reject(new Error({
    //       name: file.name
    //     }))
    //   }
    // })
  }
  // 删除
  delete (fileIds) {
    return new Promise((resolve, reject) => {
      this.client.deleteMulti(fileIds)
        .then(result => {
          resolve(result)
        })
        .catch(err => {
          reject(new Error({
            ...err,
          }))
        })
    })
  }
}

export default Aliyun
