useMethods.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. const key = 'updatable';
  57. message.loading({ content: '正在导出,请稍等...', key });
  58. defHttp
  59. .post({ url: url, params: params, timeout: 1000 * 1000 }, { isTransformResponse: false })
  60. .then((data) => {
  61. debugger;
  62. if (data.code == 200 && data.result) {
  63. const messageArr = data.result.split('/');
  64. const fileUrl = messageArr[messageArr.length - 1];
  65. if (fileUrl) {
  66. const baseApiUrl = glob.domainUrl;
  67. // 下载文件
  68. const a = document.createElement('a');
  69. // 定义下载名称
  70. a.download = name;
  71. // 隐藏标签
  72. a.style.display = 'none';
  73. // 设置文件路径
  74. a.href = `${baseApiUrl}/sys/common/static/${fileUrl}`;
  75. // 将创建的标签插入dom
  76. document.body.appendChild(a);
  77. // 点击标签,执行下载
  78. a.click();
  79. // 将标签从dom移除
  80. document.body.removeChild(a);
  81. message.success({ content: '导出成功!', key, duration: 1 });
  82. }
  83. } else {
  84. message.error({ content: '下载失败!', key, duration: 1 });
  85. }
  86. })
  87. .catch(() => {
  88. message.error({ content: '下载失败!', key, duration: 1 });
  89. });
  90. }
  91. async function exportXlsPost1(name, url, params, isXlsx = false) {
  92. const data = await defHttp.post({ url: url, data: params, responseType: 'blob' }, { isTransformResponse: false });
  93. debugger;
  94. if (!data) {
  95. createMessage.warning('文件下载失败');
  96. return;
  97. }
  98. if (!name || typeof name != 'string') {
  99. name = '导出文件';
  100. }
  101. const blobOptions = { type: 'application/vnd.ms-excel' };
  102. let fileSuffix = '.xls';
  103. if (isXlsx === true) {
  104. blobOptions['type'] = XLSX_MIME_TYPE;
  105. fileSuffix = XLSX_FILE_SUFFIX;
  106. }
  107. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  108. window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix);
  109. } else {
  110. const url = window.URL.createObjectURL(new Blob([data], blobOptions));
  111. const link = document.createElement('a');
  112. link.style.display = 'none';
  113. link.href = url;
  114. link.setAttribute('download', name + fileSuffix);
  115. document.body.appendChild(link);
  116. link.click();
  117. document.body.removeChild(link); //下载完成移除元素
  118. window.URL.revokeObjectURL(url); //释放掉blob对象
  119. }
  120. }
  121. /**
  122. * 导入xls
  123. * @param data 导入的数据
  124. * @param url
  125. * @param success 成功后的回调
  126. */
  127. async function importXls(data, url, success) {
  128. const isReturn = (fileInfo) => {
  129. try {
  130. if (fileInfo.code === 201) {
  131. const {
  132. message,
  133. result: { msg, fileUrl, fileName },
  134. } = fileInfo;
  135. const href = glob.uploadUrl + fileUrl;
  136. createWarningModal({
  137. title: message,
  138. centered: false,
  139. content: `<div>
  140. <span>${msg}</span><br/>
  141. <span>具体详情请<a href = ${href} download = ${fileName}> 点击下载 </a> </span>
  142. </div>`,
  143. });
  144. //update-begin---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
  145. } else if (fileInfo.code === 500 || fileInfo.code === 510) {
  146. createMessage.error(fileInfo.message || `${data.file.name} 导入失败`);
  147. //update-end---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
  148. } else {
  149. createMessage.success(fileInfo.message || `${data.file.name} 文件上传成功`);
  150. }
  151. } catch (error) {
  152. console.log('导入的数据异常', error);
  153. } finally {
  154. typeof success === 'function' ? success(fileInfo) : '';
  155. }
  156. };
  157. await defHttp.uploadFile({ url }, { file: data.file }, { success: isReturn });
  158. }
  159. return {
  160. handleExportXls: (name: string, url: string, params?: object) => exportXls(name, url, params),
  161. handleExportXlsPost: (name: string, url: string, params?: object) => exportXlsPost(name, url, params),
  162. exportXlsGetBlob: (name: string, url: string, params?: object) => exportXls(name, url, params),
  163. exportXlsPostBlob: (name: string, url: string, params?: object) => exportXlsPost1(name, url, params),
  164. handleImportXls: (data, url, success) => importXls(data, url, success),
  165. handleExportXlsx: (name: string, url: string, params?: object) => exportXls(name, url, params, true),
  166. };
  167. }