| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- import { defHttp } from '/@/utils/http/axios';
- import { useMessage } from '/@/hooks/web/useMessage';
- import { useGlobSetting } from '/@/hooks/setting';
- import { message } from 'ant-design-vue';
- const { createMessage, createWarningModal } = useMessage();
- const glob = useGlobSetting();
- /**
- * 导出文件xlsx的mime-type
- */
- export const XLSX_MIME_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
- /**
- * 导出文件xlsx的文件后缀
- */
- export const XLSX_FILE_SUFFIX = '.xlsx';
- export function useMethods() {
- /**
- * 导出xls
- * @param name
- * @param url
- */
- async function exportXls(name, url, params, isXlsx = false) {
- const data = await defHttp.get({ url: url, params: params, responseType: 'blob', timeout: 1000 * 1000 }, { isTransformResponse: false });
- if (!data) {
- createMessage.warning('文件下载失败');
- return;
- }
- if (!name || typeof name != 'string') {
- name = '导出文件';
- }
- const blobOptions = { type: 'application/vnd.ms-excel' };
- let fileSuffix = '.xls';
- if (isXlsx === true) {
- blobOptions['type'] = XLSX_MIME_TYPE;
- fileSuffix = XLSX_FILE_SUFFIX;
- }
- if (typeof window.navigator.msSaveBlob !== 'undefined') {
- window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix);
- } else {
- const url = window.URL.createObjectURL(new Blob([data], blobOptions));
- const link = document.createElement('a');
- link.style.display = 'none';
- link.href = url;
- link.setAttribute('download', name + fileSuffix);
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link); //下载完成移除元素
- window.URL.revokeObjectURL(url); //释放掉blob对象
- }
- }
- /**
- * 导出xls post
- * @param name
- * @param url
- */
- async function exportXlsPost(name, url, params, isXlsx = false) {
- // message.loading({ content: 'Loading...', key });
- // message
- // .loading('正在导出,请稍等..', 2.5)
- // .then(
- // () => message.success('Loading finished', 2.5),
- // // eslint-disable-next-line @typescript-eslint/no-empty-function
- // () => {}
- // )
- // .then(() => message.info('Loading finished is finished', 2.5));
- const key = 'updatable';
- message.loading({ content: '正在导出,请稍等...', key });
- defHttp
- .post({ url: url, params: params, timeout: 1000 * 1000 }, { isTransformResponse: false })
- .then((data) => {
- if (data.code == 200 && data.result) {
- const messageArr = data.result.split('/');
- const fileUrl = messageArr[messageArr.length - 1];
- if (fileUrl) {
- const baseApiUrl = glob.domainUrl;
- // 下载文件
- const a = document.createElement('a');
- // 定义下载名称
- a.download = name;
- // 隐藏标签
- a.style.display = 'none';
- // 设置文件路径
- a.href = `${baseApiUrl}/sys/common/static/${fileUrl}`;
- // 将创建的标签插入dom
- document.body.appendChild(a);
- // 点击标签,执行下载
- a.click();
- // 将标签从dom移除
- document.body.removeChild(a);
- message.success({ content: '导出成功!', key, duration: 1 });
- }
- } else {
- message.error({ content: '下载失败!', key, duration: 1 });
- }
- })
- .catch(() => {
- message.error({ content: '下载失败!', key, duration: 1 });
- });
- }
- async function exportXlsPost1(name, url, params, isXlsx = false) {
- const data = await defHttp.get({ url: url, params: params, responseType: 'blob', timeout: 1000 * 1000 }, { isTransformResponse: false });
- if (!data) {
- createMessage.warning('文件下载失败');
- return;
- }
- if (!name || typeof name != 'string') {
- name = '导出文件';
- }
- const blobOptions = { type: 'application/vnd.ms-excel' };
- let fileSuffix = '.xls';
- if (isXlsx === true) {
- blobOptions['type'] = XLSX_MIME_TYPE;
- fileSuffix = XLSX_FILE_SUFFIX;
- }
- if (typeof window.navigator.msSaveBlob !== 'undefined') {
- window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix);
- } else {
- const url = window.URL.createObjectURL(new Blob([data], blobOptions));
- const link = document.createElement('a');
- link.style.display = 'none';
- link.href = url;
- link.setAttribute('download', name + fileSuffix);
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link); //下载完成移除元素
- window.URL.revokeObjectURL(url); //释放掉blob对象
- }
- }
- /**
- * 导入xls
- * @param data 导入的数据
- * @param url
- * @param success 成功后的回调
- */
- async function importXls(data, url, success) {
- const isReturn = (fileInfo) => {
- try {
- if (fileInfo.code === 201) {
- const {
- message,
- result: { msg, fileUrl, fileName },
- } = fileInfo;
- const href = glob.uploadUrl + fileUrl;
- createWarningModal({
- title: message,
- centered: false,
- content: `<div>
- <span>${msg}</span><br/>
- <span>具体详情请<a href = ${href} download = ${fileName}> 点击下载 </a> </span>
- </div>`,
- });
- //update-begin---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
- } else if (fileInfo.code === 500 || fileInfo.code === 510) {
- createMessage.error(fileInfo.message || `${data.file.name} 导入失败`);
- //update-end---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
- } else {
- createMessage.success(fileInfo.message || `${data.file.name} 文件上传成功`);
- }
- } catch (error) {
- console.log('导入的数据异常', error);
- } finally {
- typeof success === 'function' ? success(fileInfo) : '';
- }
- };
- await defHttp.uploadFile({ url }, { file: data.file }, { success: isReturn });
- }
- return {
- handleExportXls: (name: string, url: string, params?: object) => exportXls(name, url, params),
- handleExportXlsPost: (name: string, url: string, params?: object) => exportXlsPost(name, url, params),
- exportXlsPost0: (name: string, url: string, params?: object) => exportXlsPost1(name, url, params),
- handleImportXls: (data, url, success) => importXls(data, url, success),
- handleExportXlsx: (name: string, url: string, params?: object) => exportXls(name, url, params, true),
- };
- }
|