batchDown.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { getBatchAttachment } from '@/model/indexRx'
  2. import { getFileList } from '@/model/upload'
  3. import i18n from '../lang/index.js'
  4. import { EventBus, dayjs } from 'base-core-lib'
  5. function downLoad (res, name) {
  6. const reader = new FileReader()
  7. reader.readAsText(res, 'utf-8')
  8. reader.onload = function () {
  9. if (reader.result.indexOf('noPermission') != -1) {
  10. EventBus.$emit('warning', this.$t('upload.message07'))
  11. } else {
  12. const a = document.createElement('a')
  13. a.download = name
  14. a.href = window.URL.createObjectURL(res)
  15. a.style.display = 'none'
  16. document.body.appendChild(a)
  17. a.click()
  18. URL.revokeObjectURL(a.href)
  19. document.body.removeChild(a)
  20. }
  21. }.bind(this)
  22. }
  23. /**
  24. * 静态下载文件类型
  25. */
  26. const BLOB_TYPE = {
  27. 'doc': 'application/msword',
  28. 'bin': 'application/octet-stream',
  29. 'exe': 'application/octet-stream',
  30. 'so': 'application/octet-stream',
  31. 'dll': 'application/octet-stream',
  32. 'pdf': 'application/pdf',
  33. 'ai': 'application/postscript',
  34. 'xls': 'application/vnd.ms-excel',
  35. 'xlsx': 'application/vnd.ms-excel',
  36. 'ppt': 'application/vnd.ms-powerpoint',
  37. 'dir': 'application/x-director',
  38. 'js': 'application/x-javascript',
  39. 'swf': 'application/x-shockwave-flash',
  40. 'xhtml': 'application/xhtml+xml',
  41. 'xht': 'application/xhtml+xml',
  42. 'zip': 'application/zip',
  43. 'mid': 'audio/midi',
  44. 'midi': 'audio/midi',
  45. 'mp3': 'audio/mpeg',
  46. 'rm': 'audio/x-pn-realaudio',
  47. 'rpm': 'audio/x-pn-realaudio-plugin',
  48. 'wav': 'audio/x-wav',
  49. 'bmp': 'image/bmp',
  50. 'gif': 'image/gif',
  51. 'jpeg': 'image/jpeg',
  52. 'jpg': 'image/jpeg',
  53. 'png': 'image/png',
  54. 'css': 'text/css',
  55. 'html': 'text/html',
  56. 'htm': 'text/html',
  57. 'txt': 'text/plain',
  58. 'xsl': 'text/xml',
  59. 'xml': 'text/xml',
  60. 'mpeg': 'video/mpeg',
  61. 'mpg': 'video/mpeg',
  62. 'avi': 'video/x-msvideo',
  63. 'movie': 'video/x-sgi-movie',
  64. }
  65. /**
  66. * 通用流文件下载
  67. * @param {*} param {res:流文件, fileName:文件名称, type:文件类型 }
  68. */
  69. export async function downloadFile ({ res, fileName, type }) {
  70. fileName = fileName + '.' + type
  71. if (!res.size) {
  72. EventBus.$emit('error', '接口未返回任何流文件,请联系业务人员~')
  73. return
  74. }
  75. if (!type) {
  76. EventBus.$emit('error', '请指定下载文件类型~')
  77. return
  78. }
  79. if (!BLOB_TYPE[type.toLowerCase()]) {
  80. EventBus.$emit('error', '未找到相应的文件类型,请更换文件类型后重试~')
  81. return
  82. }
  83. // type 为需要导出的文件类型,此处为xls表格类型
  84. const blob = new Blob([res],
  85. // {type: BLOB_TYPE[type.toLowerCase()]}
  86. )
  87. if (window.navigator.msSaveOrOpenBlob) {
  88. navigator.msSaveBlob(blob, fileName)
  89. return
  90. }
  91. // 兼容不同浏览器的URL对象
  92. const url = window.URL || window.webkitURL || window.moxURL
  93. // 创建下载链接
  94. const href = url.createObjectURL(blob)
  95. // 创建a标签并为其添加属性
  96. let downloadElement = document.createElement('a')
  97. downloadElement.href = href
  98. downloadElement.style.display = 'none'
  99. downloadElement.download = `${fileName}`
  100. document.body.appendChild(downloadElement)
  101. // 触发点击事件执行下载
  102. downloadElement.click()
  103. document.body.removeChild(downloadElement) //下载完成移除元素
  104. window.URL.revokeObjectURL(href) //释放掉blob对象
  105. }
  106. export async function zipDown ({ appendixIds, zipName = i18n.t('dataManage.downloadFileName'), baseUrl }) {
  107. if (appendixIds.split(',').length === 1) {
  108. const getFileInfo = await getFileList({ appendixIds: appendixIds }).toPromise()
  109. if (getFileInfo.length == 1) {
  110. const a = document.createElement('a')
  111. a.setAttribute('target', '_blank')
  112. a.href = `${baseUrl}appendix/api/singleDownLoadFile?appendixId=${appendixIds}`
  113. a.style.display = 'none'
  114. document.body.appendChild(a)
  115. a.click()
  116. URL.revokeObjectURL(a.href)
  117. document.body.removeChild(a)
  118. return
  119. }
  120. return
  121. }
  122. const { data } = await getBatchAttachment({ appendixIds: appendixIds, zipName: zipName }, {}, { responseType: 'blob' }).toPromise()
  123. downLoad(data, zipName + dayjs().format('YYYYMMDD') + '.zip')
  124. }
  125. export function getLocationAdress (data) {
  126. let url = ''
  127. if (process.env.VUE_APP_BASE_API) {
  128. url = process.env.VUE_APP_BASE_API + data
  129. }
  130. else {
  131. url = `${window.location.protocol}//${window.location.host}/${data}`
  132. }
  133. window.location.href = url
  134. }
  135. export function exportFileUrl (module, type, id, fileName, vendorId) {
  136. console.log(`${process.env.VUE_APP_API_SUPPLY}/purExport/${module}/${type}?id=${id}&fileName=${fileName}&vendorId=${vendorId}`, 1919)
  137. return `${process.env.VUE_APP_API_SUPPLY}/purExport/${module}/${type}?id=${id}&fileName=${fileName}&vendorId=${vendorId}`
  138. }