useMethods.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { defHttp } from '/@/utils/http/axios';
  2. import { useMessage } from '/@/hooks/web/useMessage';
  3. import { useGlobSetting } from '/@/hooks/setting';
  4. const { createMessage, createWarningModal } = useMessage();
  5. const glob = useGlobSetting();
  6. /**
  7. * 导出文件xlsx的mime-type
  8. */
  9. export const XLSX_MIME_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  10. /**
  11. * 导出文件xlsx的文件后缀
  12. */
  13. export const XLSX_FILE_SUFFIX = '.xlsx';
  14. export function useMethods() {
  15. /**
  16. * 导出xls
  17. * @param name
  18. * @param url
  19. */
  20. async function exportXls(name, url, params, isXlsx = false) {
  21. const data = await defHttp.get({ url: url, params: params, responseType: 'blob' }, { isTransformResponse: false });
  22. if (!data) {
  23. createMessage.warning('文件下载失败');
  24. return;
  25. }
  26. if (!name || typeof name != 'string') {
  27. name = '导出文件';
  28. }
  29. let blobOptions = { type: 'application/vnd.ms-excel' };
  30. let fileSuffix = '.xls';
  31. if (isXlsx === true) {
  32. blobOptions['type'] = XLSX_MIME_TYPE;
  33. fileSuffix = XLSX_FILE_SUFFIX;
  34. }
  35. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  36. window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix);
  37. } else {
  38. let url = window.URL.createObjectURL(new Blob([data], blobOptions));
  39. let link = document.createElement('a');
  40. link.style.display = 'none';
  41. link.href = url;
  42. link.setAttribute('download', name + fileSuffix);
  43. document.body.appendChild(link);
  44. link.click();
  45. document.body.removeChild(link); //下载完成移除元素
  46. window.URL.revokeObjectURL(url); //释放掉blob对象
  47. }
  48. }
  49. /**
  50. * 导入xls
  51. * @param data 导入的数据
  52. * @param url
  53. * @param success 成功后的回调
  54. */
  55. async function importXls(data, url, success) {
  56. const isReturn = (fileInfo) => {
  57. try {
  58. if (fileInfo.code === 201) {
  59. let {
  60. message,
  61. result: { msg, fileUrl, fileName },
  62. } = fileInfo;
  63. let href = glob.uploadUrl + fileUrl;
  64. createWarningModal({
  65. title: message,
  66. centered: false,
  67. content: `<div>
  68. <span>${msg}</span><br/>
  69. <span>具体详情请<a href = ${href} download = ${fileName}> 点击下载 </a> </span>
  70. </div>`,
  71. });
  72. } else if (fileInfo.code === 500) {
  73. createMessage.error(fileInfo.message || `${data.file.name} 导入失败`);
  74. } else {
  75. createMessage.success(fileInfo.message || `${data.file.name} 文件上传成功`);
  76. }
  77. } catch (error) {
  78. console.log('导入的数据异常', error);
  79. } finally {
  80. typeof success === 'function' ? success(fileInfo) : '';
  81. }
  82. };
  83. await defHttp.uploadFile({ url }, { file: data.file }, { success: isReturn });
  84. }
  85. return {
  86. handleExportXls: (name: string, url: string, params?: object) => exportXls(name, url, params),
  87. handleImportXls: (data, url, success) => importXls(data, url, success),
  88. handleExportXlsx: (name: string, url: string, params?: object) => exportXls(name, url, params, true),
  89. };
  90. }