import { getBatchAttachment } from '@/model/indexRx' import { getFileList } from '@/model/upload' import i18n from '../lang/index.js' import { EventBus, dayjs } from 'base-core-lib' function downLoad (res, name) { const reader = new FileReader() reader.readAsText(res, 'utf-8') reader.onload = function () { if (reader.result.indexOf('noPermission') != -1) { EventBus.$emit('warning', this.$t('upload.message07')) } else { const a = document.createElement('a') a.download = name a.href = window.URL.createObjectURL(res) a.style.display = 'none' document.body.appendChild(a) a.click() URL.revokeObjectURL(a.href) document.body.removeChild(a) } }.bind(this) } /** * 静态下载文件类型 */ const BLOB_TYPE = { 'doc': 'application/msword', 'bin': 'application/octet-stream', 'exe': 'application/octet-stream', 'so': 'application/octet-stream', 'dll': 'application/octet-stream', 'pdf': 'application/pdf', 'ai': 'application/postscript', 'xls': 'application/vnd.ms-excel', 'xlsx': 'application/vnd.ms-excel', 'ppt': 'application/vnd.ms-powerpoint', 'dir': 'application/x-director', 'js': 'application/x-javascript', 'swf': 'application/x-shockwave-flash', 'xhtml': 'application/xhtml+xml', 'xht': 'application/xhtml+xml', 'zip': 'application/zip', 'mid': 'audio/midi', 'midi': 'audio/midi', 'mp3': 'audio/mpeg', 'rm': 'audio/x-pn-realaudio', 'rpm': 'audio/x-pn-realaudio-plugin', 'wav': 'audio/x-wav', 'bmp': 'image/bmp', 'gif': 'image/gif', 'jpeg': 'image/jpeg', 'jpg': 'image/jpeg', 'png': 'image/png', 'css': 'text/css', 'html': 'text/html', 'htm': 'text/html', 'txt': 'text/plain', 'xsl': 'text/xml', 'xml': 'text/xml', 'mpeg': 'video/mpeg', 'mpg': 'video/mpeg', 'avi': 'video/x-msvideo', 'movie': 'video/x-sgi-movie', } /** * 通用流文件下载 * @param {*} param {res:流文件, fileName:文件名称, type:文件类型 } */ export async function downloadFile ({ res, fileName, type }) { fileName = fileName + '.' + type if (!res.size) { EventBus.$emit('error', '接口未返回任何流文件,请联系业务人员~') return } if (!type) { EventBus.$emit('error', '请指定下载文件类型~') return } if (!BLOB_TYPE[type.toLowerCase()]) { EventBus.$emit('error', '未找到相应的文件类型,请更换文件类型后重试~') return } // type 为需要导出的文件类型,此处为xls表格类型 const blob = new Blob([res], // {type: BLOB_TYPE[type.toLowerCase()]} ) if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, fileName) return } // 兼容不同浏览器的URL对象 const url = window.URL || window.webkitURL || window.moxURL // 创建下载链接 const href = url.createObjectURL(blob) // 创建a标签并为其添加属性 let downloadElement = document.createElement('a') downloadElement.href = href downloadElement.style.display = 'none' downloadElement.download = `${fileName}` document.body.appendChild(downloadElement) // 触发点击事件执行下载 downloadElement.click() document.body.removeChild(downloadElement) //下载完成移除元素 window.URL.revokeObjectURL(href) //释放掉blob对象 } export async function zipDown ({ appendixIds, zipName = i18n.t('dataManage.downloadFileName'), baseUrl }) { if (appendixIds.split(',').length === 1) { const getFileInfo = await getFileList({ appendixIds: appendixIds }).toPromise() if (getFileInfo.length == 1) { const a = document.createElement('a') a.setAttribute('target', '_blank') a.href = `${baseUrl}appendix/api/singleDownLoadFile?appendixId=${appendixIds}` a.style.display = 'none' document.body.appendChild(a) a.click() URL.revokeObjectURL(a.href) document.body.removeChild(a) return } return } const { data } = await getBatchAttachment({ appendixIds: appendixIds, zipName: zipName }, {}, { responseType: 'blob' }).toPromise() downLoad(data, zipName + dayjs().format('YYYYMMDD') + '.zip') } export function getLocationAdress (data) { let url = '' if (process.env.VUE_APP_BASE_API) { url = process.env.VUE_APP_BASE_API + data } else { url = `${window.location.protocol}//${window.location.host}/${data}` } window.location.href = url } export function exportFileUrl (module, type, id, fileName, vendorId) { console.log(`${process.env.VUE_APP_API_SUPPLY}/purExport/${module}/${type}?id=${id}&fileName=${fileName}&vendorId=${vendorId}`, 1919) return `${process.env.VUE_APP_API_SUPPLY}/purExport/${module}/${type}?id=${id}&fileName=${fileName}&vendorId=${vendorId}` }