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

export const getUrl = async () => {
  return await get('/tBuisSupportDoc/getUrl')
}

export const getDataForZx = async (params) => {
  return await get('/tBusiAssociation/getDataForZx', params)
}

export const getAttachmentList = async (data) => {
  return await post('/tBuisSupportDoc/getList', data)
}

export const addguanlian = async (data) => {
  return await post('/tBuisSupportDoc/guanlian/', data)
}

export const deleteAttachment = async (data) => {
  return await post('/tBuisSupportDoc/delete', data)
}

export const getAttachmentListNew = async (data) => {
  return await post('/tBusiSupportMaterial/getData', data)
}

const baseUrl = process.env.VUE_APP_BASE_API
export function downLoadAttachment(str, filename) {
  var url = baseUrl + str
  axios({
    method: 'post',
    url: url,
    data: filename,
    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)
}
