useMethods.ts 7.6 KB

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