useMethods.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. const 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. const url = window.URL.createObjectURL(new Blob([data], blobOptions));
  39. const 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 post
  51. * @param name
  52. * @param url
  53. */
  54. async function exportXlsPost(name, url, params, isXlsx = false) {
  55. const data = await defHttp.post({ url: url, params: params, responseType: 'blob' }, { isTransformResponse: false });
  56. if (!data) {
  57. createMessage.warning('文件下载失败');
  58. return;
  59. }
  60. if (!name || typeof name != 'string') {
  61. name = '导出文件';
  62. }
  63. const blobOptions = { type: 'application/vnd.ms-excel' };
  64. let fileSuffix = '.xls';
  65. if (isXlsx === true) {
  66. blobOptions['type'] = XLSX_MIME_TYPE;
  67. fileSuffix = XLSX_FILE_SUFFIX;
  68. }
  69. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  70. window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix);
  71. } else {
  72. const url = window.URL.createObjectURL(new Blob([data], blobOptions));
  73. const link = document.createElement('a');
  74. link.style.display = 'none';
  75. link.href = url;
  76. link.setAttribute('download', name + fileSuffix);
  77. document.body.appendChild(link);
  78. link.click();
  79. document.body.removeChild(link); //下载完成移除元素
  80. window.URL.revokeObjectURL(url); //释放掉blob对象
  81. }
  82. }
  83. /**
  84. * 导入xls
  85. * @param data 导入的数据
  86. * @param url
  87. * @param success 成功后的回调
  88. */
  89. async function importXls(data, url, success) {
  90. const isReturn = (fileInfo) => {
  91. try {
  92. if (fileInfo.code === 201) {
  93. const {
  94. message,
  95. result: { msg, fileUrl, fileName },
  96. } = fileInfo;
  97. const href = glob.uploadUrl + fileUrl;
  98. createWarningModal({
  99. title: message,
  100. centered: false,
  101. content: `<div>
  102. <span>${msg}</span><br/>
  103. <span>具体详情请<a href = ${href} download = ${fileName}> 点击下载 </a> </span>
  104. </div>`,
  105. });
  106. //update-begin---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
  107. } else if (fileInfo.code === 500 || fileInfo.code === 510) {
  108. createMessage.error(fileInfo.message || `${data.file.name} 导入失败`);
  109. //update-end---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
  110. } else {
  111. createMessage.success(fileInfo.message || `${data.file.name} 文件上传成功`);
  112. }
  113. } catch (error) {
  114. console.log('导入的数据异常', error);
  115. } finally {
  116. typeof success === 'function' ? success(fileInfo) : '';
  117. }
  118. };
  119. await defHttp.uploadFile({ url }, { file: data.file }, { success: isReturn });
  120. }
  121. return {
  122. handleExportXls: (name: string, url: string, params?: object) => exportXls(name, url, params),
  123. handleExportXlsPost: (name: string, url: string, params?: object) => exportXlsPost(name, url, params),
  124. handleImportXls: (data, url, success) => importXls(data, url, success),
  125. handleExportXlsx: (name: string, url: string, params?: object) => exportXls(name, url, params, true),
  126. };
  127. }