dbsd_kczx/src/hooks/system/useMethods.ts

110 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-06-10 10:44:44 +08:00
import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';
import { useGlobSetting } from '/@/hooks/setting';
2022-03-10 09:47:29 +08:00
2022-06-10 10:44:44 +08:00
const { createMessage, createWarningModal } = useMessage();
2022-03-10 09:47:29 +08:00
const glob = useGlobSetting();
/**
* xlsx的mime-type
*/
2022-06-10 10:44:44 +08:00
export const XLSX_MIME_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
/**
* xlsx的文件后缀
*/
2022-06-10 10:44:44 +08:00
export const XLSX_FILE_SUFFIX = '.xlsx';
2022-03-10 09:47:29 +08:00
export function useMethods() {
/**
* xls
* @param name
* @param url
*/
2022-06-10 10:44:44 +08:00
async function exportXls(name, url, params, isXlsx = false) {
2024-02-26 16:23:19 +08:00
const data = await defHttp.get({ url: url, params: params, responseType: 'blob', timeout: 9000000 }, { isTransformResponse: false });
2022-03-10 09:47:29 +08:00
if (!data) {
2022-06-10 10:44:44 +08:00
createMessage.warning('文件下载失败');
return;
2022-03-10 09:47:29 +08:00
}
2022-06-10 10:44:44 +08:00
if (!name || typeof name != 'string') {
name = '导出文件';
2022-03-10 09:47:29 +08:00
}
2022-06-10 10:44:44 +08:00
let blobOptions = { type: 'application/vnd.ms-excel' };
let fileSuffix = '.xls';
if (isXlsx === true) {
blobOptions['type'] = XLSX_MIME_TYPE;
fileSuffix = XLSX_FILE_SUFFIX;
}
2022-03-10 09:47:29 +08:00
if (typeof window.navigator.msSaveBlob !== 'undefined') {
2022-06-10 10:44:44 +08:00
window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix);
2022-03-10 09:47:29 +08:00
} else {
2022-06-10 10:44:44 +08:00
let url = window.URL.createObjectURL(new Blob([data], blobOptions));
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', name + fileSuffix);
document.body.appendChild(link);
link.click();
2022-03-10 09:47:29 +08:00
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) => {
2022-06-10 10:44:44 +08:00
try {
if (fileInfo.code === 201) {
let {
message,
result: { msg, fileUrl, fileName },
} = fileInfo;
let href = glob.uploadUrl + fileUrl;
createWarningModal({
title: message,
centered: false,
content: `<div>
2024-02-26 16:23:19 +08:00
<span>${msg}</span><br/>
<span><a href = ${href} download = ${fileName}> </a> </span>
</div>`,
2022-06-10 10:44:44 +08:00
});
//update-begin---author:wangshuai ---date:20221121 for[VUEN-2827]导入无权限,提示图标错误------------
} else if (fileInfo.code === 500 || fileInfo.code === 510) {
2022-06-10 10:44:44 +08:00
createMessage.error(fileInfo.message || `${data.file.name} 导入失败`);
//update-end---author:wangshuai ---date:20221121 for[VUEN-2827]导入无权限,提示图标错误------------
2022-06-10 10:44:44 +08:00
} else {
2023-05-27 10:45:45 +08:00
// createMessage.success(fileInfo.message || `${data.file.name} 文件上传成功`);
// createMessage.success({
// content:`<div>${fileInfo.message}</div>`,
// style: {
// marginTop: '20vh',
// },
// });
createWarningModal({
centered: false,
content: `<div>
2024-02-26 16:23:19 +08:00
<span>${fileInfo.message}</span><br/>
</div>`,
2023-05-27 10:45:45 +08:00
});
2022-03-10 09:47:29 +08:00
}
2022-06-10 10:44:44 +08:00
} catch (error) {
console.log('导入的数据异常', error);
} finally {
typeof success === 'function' ? success(fileInfo) : '';
}
2022-03-10 09:47:29 +08:00
};
2022-06-10 10:44:44 +08:00
await defHttp.uploadFile({ url }, { file: data.file }, { success: isReturn });
2022-03-10 09:47:29 +08:00
}
return {
2022-06-10 10:44:44 +08:00
handleExportXls: (name: string, url: string, params?: object) => exportXls(name, url, params),
2022-03-10 09:47:29 +08:00
handleImportXls: (data, url, success) => importXls(data, url, success),
2022-06-10 10:44:44 +08:00
handleExportXlsx: (name: string, url: string, params?: object) => exportXls(name, url, params, true),
2022-03-10 09:47:29 +08:00
};
}