useMethods.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. async function exportXlsPost1(name, url, params, isXlsx = false) {
  100. const data = await defHttp.get({ url: url, params: params, responseType: 'blob', timeout: 1000 * 1000 }, { isTransformResponse: false });
  101. if (!data) {
  102. createMessage.warning('文件下载失败');
  103. return;
  104. }
  105. if (!name || typeof name != 'string') {
  106. name = '导出文件';
  107. }
  108. const blobOptions = { type: 'application/vnd.ms-excel' };
  109. let fileSuffix = '.xls';
  110. if (isXlsx === true) {
  111. blobOptions['type'] = XLSX_MIME_TYPE;
  112. fileSuffix = XLSX_FILE_SUFFIX;
  113. }
  114. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  115. window.navigator.msSaveBlob(new Blob([data], blobOptions), name + fileSuffix);
  116. } else {
  117. const url = window.URL.createObjectURL(new Blob([data], blobOptions));
  118. const link = document.createElement('a');
  119. link.style.display = 'none';
  120. link.href = url;
  121. link.setAttribute('download', name + fileSuffix);
  122. document.body.appendChild(link);
  123. link.click();
  124. document.body.removeChild(link); //下载完成移除元素
  125. window.URL.revokeObjectURL(url); //释放掉blob对象
  126. }
  127. }
  128. /**
  129. * 导入xls
  130. * @param data 导入的数据
  131. * @param url
  132. * @param success 成功后的回调
  133. */
  134. async function importXls(data, url, success) {
  135. const isReturn = (fileInfo) => {
  136. try {
  137. if (fileInfo.code === 201) {
  138. const {
  139. message,
  140. result: { msg, fileUrl, fileName },
  141. } = fileInfo;
  142. const href = glob.uploadUrl + fileUrl;
  143. createWarningModal({
  144. title: message,
  145. centered: false,
  146. content: `<div>
  147. <span>${msg}</span><br/>
  148. <span>具体详情请<a href = ${href} download = ${fileName}> 点击下载 </a> </span>
  149. </div>`,
  150. });
  151. //update-begin---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
  152. } else if (fileInfo.code === 500 || fileInfo.code === 510) {
  153. createMessage.error(fileInfo.message || `${data.file.name} 导入失败`);
  154. //update-end---author:wangshuai ---date:20221121 for:[VUEN-2827]导入无权限,提示图标错误------------
  155. } else {
  156. createMessage.success(fileInfo.message || `${data.file.name} 文件上传成功`);
  157. }
  158. } catch (error) {
  159. console.log('导入的数据异常', error);
  160. } finally {
  161. typeof success === 'function' ? success(fileInfo) : '';
  162. }
  163. };
  164. await defHttp.uploadFile({ url }, { file: data.file }, { success: isReturn });
  165. }
  166. return {
  167. handleExportXls: (name: string, url: string, params?: object) => exportXls(name, url, params),
  168. handleExportXlsPost: (name: string, url: string, params?: object) => exportXlsPost(name, url, params),
  169. exportXlsPost0: (name: string, url: string, params?: object) => exportXlsPost1(name, url, params),
  170. handleImportXls: (data, url, success) => importXls(data, url, success),
  171. handleExportXlsx: (name: string, url: string, params?: object) => exportXls(name, url, params, true),
  172. };
  173. }