useMethods.ts 6.8 KB

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