useMethods.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { defHttp } from '/@/utils/http/axios';
  2. import { useMessage } from '/@/hooks/web/useMessage';
  3. import { useGlobSetting } from '/@/hooks/setting';
  4. import { message } from 'ant-design-vue';
  5. const { createMessage, createWarningModal } = useMessage();
  6. const glob = useGlobSetting();
  7. /**
  8. * 导出文件xlsx的mime-type
  9. */
  10. export const XLSX_MIME_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  11. /**
  12. * 导出文件xlsx的文件后缀
  13. */
  14. export const XLSX_FILE_SUFFIX = '.xlsx';
  15. export function useMethods() {
  16. /**
  17. * 导出xls
  18. * @param name
  19. * @param url
  20. */
  21. async function exportXls(name, url, params, isXlsx = false) {
  22. const data = await defHttp.get({ url: url, params: params, responseType: 'blob', timeout: 1000 * 1000 }, { isTransformResponse: false });
  23. if (!data) {
  24. createMessage.warning('文件下载失败');
  25. return;
  26. }
  27. if (!name || typeof name != 'string') {
  28. name = '导出文件';
  29. }
  30. const blobOptions = { type: 'application/vnd.ms-excel' };
  31. let fileSuffix = '.xls';
  32. if (isXlsx === true) {
  33. blobOptions['type'] = XLSX_MIME_TYPE;
  34. fileSuffix = XLSX_FILE_SUFFIX;
  35. }
  36. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  37. window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix);
  38. } else {
  39. const url = window.URL.createObjectURL(new Blob([data], blobOptions));
  40. const link = document.createElement('a');
  41. link.style.display = 'none';
  42. link.href = url;
  43. link.setAttribute('download', name + fileSuffix);
  44. document.body.appendChild(link);
  45. link.click();
  46. document.body.removeChild(link); //下载完成移除元素
  47. window.URL.revokeObjectURL(url); //释放掉blob对象
  48. }
  49. }
  50. /**
  51. * 导出xls post
  52. * @param name
  53. * @param url
  54. */
  55. async function exportXlsPost(name, url, params, isXlsx = false) {
  56. // message.loading({ content: 'Loading...', key });
  57. // message
  58. // .loading('正在导出,请稍等..', 2.5)
  59. // .then(
  60. // () => message.success('Loading finished', 2.5),
  61. // // eslint-disable-next-line @typescript-eslint/no-empty-function
  62. // () => {}
  63. // )
  64. // .then(() => message.info('Loading finished is finished', 2.5));
  65. const key = 'updatable';
  66. message.loading({ content: '正在导出,请稍等...', key });
  67. defHttp
  68. .post({ url: url, params: params, timeout: 1000 * 1000 }, { isTransformResponse: false })
  69. .then((data) => {
  70. if (data.code == 200 && data.result) {
  71. const messageArr = data.result.split('/');
  72. const fileUrl = messageArr[messageArr.length - 1];
  73. if (fileUrl) {
  74. const baseApiUrl = glob.domainUrl;
  75. // 下载文件
  76. const a = document.createElement('a');
  77. // 定义下载名称
  78. a.download = name;
  79. // 隐藏标签
  80. a.style.display = 'none';
  81. // 设置文件路径
  82. a.href = `${baseApiUrl}/sys/common/static/${fileUrl}`;
  83. // 将创建的标签插入dom
  84. document.body.appendChild(a);
  85. // 点击标签,执行下载
  86. a.click();
  87. // 将标签从dom移除
  88. document.body.removeChild(a);
  89. message.success({ content: '导出成功!', key, duration: 1 });
  90. }
  91. } else {
  92. message.error({ content: '下载失败!', key, duration: 1 });
  93. }
  94. })
  95. .catch(() => {
  96. message.error({ content: '下载失败!', key, duration: 1 });
  97. });
  98. }
  99. /**
  100. * 导入xls
  101. * @param data 导入的数据
  102. * @param url
  103. * @param success 成功后的回调
  104. */
  105. async function importXls(data, url, success) {
  106. const isReturn = (fileInfo) => {
  107. try {
  108. if (fileInfo.code === 201) {
  109. const {
  110. message,
  111. result: { msg, fileUrl, fileName },
  112. } = fileInfo;
  113. const href = glob.uploadUrl + fileUrl;
  114. createWarningModal({
  115. title: message,
  116. centered: false,
  117. content: `<div>
  118. <span>${msg}</span><br/>
  119. <span>具体详情请<a href = ${href} download = ${fileName}> 点击下载 </a> </span>
  120. </div>`,
  121. });
  122. //update-begin---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
  123. } else if (fileInfo.code === 500 || fileInfo.code === 510) {
  124. createMessage.error(fileInfo.message || `${data.file.name} 导入失败`);
  125. //update-end---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
  126. } else {
  127. createMessage.success(fileInfo.message || `${data.file.name} 文件上传成功`);
  128. }
  129. } catch (error) {
  130. console.log('导入的数据异常', error);
  131. } finally {
  132. typeof success === 'function' ? success(fileInfo) : '';
  133. }
  134. };
  135. await defHttp.uploadFile({ url }, { file: data.file }, { success: isReturn });
  136. }
  137. return {
  138. handleExportXls: (name: string, url: string, params?: object) => exportXls(name, url, params),
  139. handleExportXlsPost: (name: string, url: string, params?: object) => exportXlsPost(name, url, params),
  140. handleImportXls: (data, url, success) => importXls(data, url, success),
  141. handleExportXlsx: (name: string, url: string, params?: object) => exportXls(name, url, params, true),
  142. };
  143. }