149 lines
4.0 KiB
JavaScript
149 lines
4.0 KiB
JavaScript
import { Modal } from 'ant-design-vue'
|
|
import { saveAs } from 'file-saver'
|
|
import JSZip from 'jszip'
|
|
import Vue from 'vue'
|
|
import signMd5Utils from '@/utils/encryption/signMd5Utils'
|
|
import Axios from 'axios'
|
|
import { ACCESS_TOKEN, TENANT_ID } from '@/store/mutation-types'
|
|
import { message } from 'ant-design-vue'
|
|
|
|
/**
|
|
* 弹窗填入文件名保存文件
|
|
* @param {Blob} data 数据
|
|
* @param {string} ext 扩展名,不带.
|
|
*/
|
|
export const showSaveFileModal = (data, ext) => {
|
|
let fileName = ''
|
|
const handleClick = event => {
|
|
fileName = event.target.value
|
|
}
|
|
|
|
Modal.confirm({
|
|
title: 'Please enter file name',
|
|
content: h => <a-input onChange={handleClick} />,
|
|
okText: 'Cancle',
|
|
cancelText: 'Save',
|
|
okButtonProps: { style: { backgroundColor: '#b98326', color: '#fff', borderColor: 'transparent' } },
|
|
cancelButtonProps: { style: { color: '#fff', backgroundColor: '#31aab0', borderColor: 'transparent' } },
|
|
onOk() {
|
|
console.log('Cancel')
|
|
},
|
|
onCancel() {
|
|
if (fileName) {
|
|
saveAs(data, `${fileName}.${ext}`)
|
|
} else {
|
|
throw new Error()
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 读文件
|
|
* @param {File} file
|
|
* @param { 'arrayBuffer' | 'text' | 'dataURL' | 'binaryString'} fileType
|
|
* @returns {Promise<string | ArrayBuffer}
|
|
*/
|
|
export const readFile = (file, fileType = 'text') => {
|
|
return new Promise((resolve, reject) => {
|
|
const fileReader = new FileReader()
|
|
const category = {
|
|
arrayBuffer: 'readAsArrayBuffer',
|
|
text: 'readAsText',
|
|
dataURL: 'readAsDataURL',
|
|
binaryString: 'readAsBinaryString'
|
|
}
|
|
fileReader[category[fileType]](file)
|
|
fileReader.onload = event => {
|
|
resolve(event.target.result)
|
|
}
|
|
fileReader.onerror = error => {
|
|
reject(error)
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 压缩文件
|
|
* @param {Array<File>} fileList
|
|
* @param {string} zipName
|
|
* @returns
|
|
*/
|
|
export const zipFile = async (fileList, zipName) => {
|
|
const zip = new JSZip()
|
|
const promises = []
|
|
|
|
const readFileWithName = async file => {
|
|
const data = await readFile(file, 'arrayBuffer')
|
|
return {
|
|
fileName: file.name,
|
|
data
|
|
}
|
|
}
|
|
fileList.forEach(file => {
|
|
promises.push(readFileWithName(file))
|
|
})
|
|
const result = await Promise.all(promises)
|
|
result.forEach(res => {
|
|
zip.file(res.fileName, res.data)
|
|
})
|
|
const content = await zip.generateAsync({ type: 'blob' })
|
|
return new File([content], zipName, { type: content.type })
|
|
}
|
|
|
|
/**
|
|
* 根据响应头获取文件名
|
|
* @param {String} contentDisposition
|
|
*/
|
|
export function getFileNameByHeaderContentDisposition(contentDisposition) {
|
|
const patt = new RegExp('file[Nn]ame=([^;]+\\.[^\\.;]+);*')
|
|
contentDisposition = decodeURI(contentDisposition)
|
|
const result = patt.exec(contentDisposition)
|
|
let fileName = result[1]
|
|
fileName = fileName.replace(/"/g, '')
|
|
return fileName
|
|
}
|
|
|
|
export const fetchAndDownload = async (url, data, method='post') => {
|
|
const apiBaseUrl = window._CONFIG['domianURL'] || '/jeecg-boot'
|
|
const sign = signMd5Utils.getSign(url, data)
|
|
|
|
const config = {
|
|
baseURL: apiBaseUrl,
|
|
method,
|
|
url,
|
|
data,
|
|
|
|
headers: {
|
|
'X-Sign': sign,
|
|
'X-TIMESTAMP': signMd5Utils.getTimestamp(),
|
|
'X-Access-Token': Vue.ls.get(ACCESS_TOKEN),
|
|
'tenant-id': Vue.ls.get(TENANT_ID)
|
|
}
|
|
}
|
|
if(method == 'get') {
|
|
config.params = data
|
|
}
|
|
|
|
const response = await Axios(config)
|
|
const { status, headers, data: responseData } = response
|
|
if (status == 200) {
|
|
if (typeof responseData == 'object') {
|
|
const { message: msg } = responseData
|
|
message.error(msg)
|
|
throw new Error(msg)
|
|
} else {
|
|
const disposition = headers['content-disposition']
|
|
const fileName = getFileNameByHeaderContentDisposition(disposition)
|
|
if (typeof responseData == 'string') {
|
|
const blob = new Blob([responseData], { type: headers['content-type'] })
|
|
saveAs(blob, fileName)
|
|
return fileName
|
|
}
|
|
}
|
|
} else {
|
|
message.error('This operation fails. Contact your system administrator')
|
|
throw new Error('This operation fails. Contact your system administrator')
|
|
}
|
|
}
|