import axios from 'axios'

const exportExcel = (url, params) => {
  console.log('我是公共的方法' + url)
  const apiVersion = process.env.VUE_APP_API_VERSION.replace(/\./g, '_')
  axios.post(process.env.VUE_APP_API_BASEURL + apiVersion + url, params, {
    responseType: 'blob',
    headers: {
      // 'X-Token': getToken(),
      'Content-Type': 'application/json;charset=utf-8'
    }
  }).then(res => {
    if (!res) {
      this.$message.error('下载模板文件失败')
      return false
    }
    const stream = res.data // 后端用stream返回Excel文件
    const blob = new Blob([stream])
    // 前端获取业务码，成功执行正常业务
    const downloadElement = document.createElement('a')
    const href = window.URL.createObjectURL(blob) // 创建下载的链接
    downloadElement.href = href
    const timeStamp = new Date().toString()
    const nameStr = decodeURI(escape(JSON.parse(res.headers['content-disposition'].split(';')[1].split('=')[1])))
    downloadElement.download = nameStr // 下载后文件名
    document.body.appendChild(downloadElement)
    downloadElement.click() // 点击下载
    document.body.removeChild(downloadElement) // 下载完成移除元素
    window.URL.revokeObjectURL(href) // 释放掉blob对象
  })
}
export {
  exportExcel
}