Newer
Older
safe_production_front / src / utils / exportUtils.ts
import { getDictByCode } from '@/api/system/dict'

/**
 * 导出文件工具
 * @param blob 文件
 * @param fileName 导出文件名
 */
export function exportFile(blob: Blob, getFileName: string) {
  let fileName = getFileName || ''
  // getDictByCode('secretUserLevel').then((response) => {
  //   if (response.code === 200) {
  //     if (Array.isArray(response.data) && response.data.length) {
  //       const level = window.sessionStorage.getItem('secretUserLevel')
  //       const index = response.data.findIndex((item: { value: string | null }) => item.value === level)
  //       const systemType = window.localStorage.getItem('systemType') as string
  //       if (index !== -1 && systemType === 'gm') {
  //         // fileName = `【${response.data[index].name}】${fileName}`
  //         fileName = `【内部】${fileName}`
  //       }
  //     }
  //   }
  const systemType = window.localStorage.getItem('systemType') as string
  if (systemType === 'gm') {
    fileName = `【内部】${fileName}`
  }
  if (window.navigator && (window.navigator as any).msSaveOrOpenBlob) {
    (navigator as any).msSaveBlob(blob, fileName)
  }
  else {
    const downloadElement = document.createElement('a')
    const href = window.URL.createObjectURL(blob) // 创建下载的链接
    downloadElement.href = href
    downloadElement.download = fileName
    document.body.appendChild(downloadElement)
    downloadElement.click() // 点击下载
    document.body.removeChild(downloadElement) // 下载完成移除元素
    window.URL.revokeObjectURL(href) // 释放blob对象
  }
  // })
}

export const base64ToBlob = (dataurl) => {
  const arr = dataurl.split(',')
  const mime = arr[0].match(/:(.*?);/)[1]
  const bstr = atob(arr[1])
  let n = bstr.length
  const u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new Blob([u8arr], { type: mime })
}