export.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { request } from './index';
  2. import { ElMessage } from 'element-plus';
  3. // 导出并自动下载
  4. export const EXPORT = async (url, params) => {
  5. const res = await request({
  6. url,
  7. method: 'GET',
  8. params,
  9. responseType: 'blob',
  10. timeout: 800000,
  11. options: {
  12. showLoading: true,
  13. },
  14. });
  15. if (res.data.type === 'application/octet-stream') {
  16. const contentType = res.headers['content-type'];
  17. const filename = contentType.substring(
  18. contentType.lastIndexOf('name=') + 5,
  19. contentType.length,
  20. );
  21. const downloadurl = window.URL.createObjectURL(new Blob([res.data]));
  22. const link = document.createElement('a');
  23. link.href = downloadurl;
  24. link.setAttribute('download', decodeURIComponent(filename));
  25. document.body.appendChild(link);
  26. link.click();
  27. } else {
  28. const reader = new FileReader();
  29. reader.onload = () => {
  30. const msg = JSON.parse(reader.result).msg;
  31. ElMessage.error(msg || '操作失败');
  32. };
  33. reader.readAsText(res.data);
  34. }
  35. };