import axios from 'axios'
import moment from 'moment'
export function downloadExcel(method = 'get', data, name = '') {
let baseURL = 'https://XXXX'
const token = sessionStorage.getItem('token')
const params = {
method: method,
headers: { 'token': token },
responseType: 'blob',
}
let url = '/api/detail/export'
if(method === 'get') {
params.url = url + '?' + Object.keys(data).map(i => `${i}=${(data[i]||'')}`).join('&')
} else {
params.data = data
}
const newDate = moment().format('YYYY-MM-DD HH:mm:ss')
axios(params).then((res) => {
const blob = new Blob([res.data], { type: res.headers['application/vnd.ms-excel'] })
const link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = name + '_' + newDate + '.xlsx'
document.body.appendChild(link)
link.click()
window.URL.revokeObjectURL(link.href)
document.body.removeChild(link)
})
}