import { uget, get, post, parmasPost, put, deletes } from '@/http/index'
import axios from 'axios'

// 通过ID删除业务表关联的附件信息
export const deleteDocumentById = async (formData) => {
  return await post('/system/document/deleteDocumentById', formData)
}

const baseUrl = process.env.VUE_APP_BASE_API
export function downLoadFile(id) {
  const url = baseUrl + '/system/document/downLoadFile'
  axios({
    method: 'post',
    url: url,
    data: id,
    responseType: 'blob'
  }).then((res) => {
    resolveBlob(res)
  })
}

/**
 * 解析blob响应内容并下载
 * @param {*} res blob响应内容
 * @param {String} mimeType MIME类型
 */
export function resolveBlob(res) {
  const aLink = document.createElement('a')
  var blob = new Blob([res.data])
  // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
  var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
  var contentDisposition = decodeURI(res.headers['content-disposition'])
  var result = patt.exec(contentDisposition)
  var fileName = result[1]
  fileName = fileName.replace(/\"/g, '')
  aLink.href = URL.createObjectURL(blob)
  aLink.setAttribute('download', fileName) // 设置下载文件名称
  document.body.appendChild(aLink)
  aLink.click()
  document.body.removeChild(aLink)
}
