rockeryutil.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * @Description: 定制封装公共方法
  3. * @Author: Rockery
  4. * @Date: 2021-04-26 15:53:59
  5. * @LastEditors: Rockery
  6. * @LastEditTime: 2021-12-29 17:56:27
  7. * @FilePath: \party_construct_web\src\utils\rockeryutil.js
  8. * @Copyright: Copyright (c) 2016~2021 Rockery(1113269755@qq.com)
  9. */
  10. import request from '@/utils/request';
  11. import { parseTime } from "@/utils/ruoyi";
  12. import { Message } from 'element-ui';
  13. /**
  14. * @description: 开启遮护罩
  15. * @param {String} paramText 遮护罩描述信息
  16. * @param {String} paramTarget 遮护罩位置,默认全局
  17. * @return {*} 遮护罩对象
  18. */
  19. export function handleOpenLoading(paramText, paramTarget) {
  20. if (!paramTarget) {
  21. return this.$loading({
  22. lock: true,
  23. text: paramText || "正在拼命加载中...",
  24. spinner: 'el-icon-loading',
  25. background: 'rgba(0, 0, 0, 0.5)'
  26. });
  27. }
  28. return this.$loading({
  29. lock: true,
  30. text: paramText || "正在拼命加载中...",
  31. spinner: 'el-icon-loading',
  32. background: 'rgba(0, 0, 0, 0.5)',
  33. target: paramTarget
  34. });
  35. }
  36. /**
  37. * @description: 关闭遮护罩
  38. * @param {*} paramLoading 遮护罩对象
  39. */
  40. export function handleCloseLoading(paramLoading) {
  41. (null !== paramLoading) && paramLoading.close();
  42. }
  43. /**
  44. * 图片加载失败事件
  45. * @param {Object} ele
  46. */
  47. export function imgViewerOnerror(ele) {
  48. const img = ele.srcElement;
  49. img.src = require('@/assets/images/empty_img.png');
  50. img.onerror = null;
  51. }
  52. /**
  53. * 常规下载PDF文件
  54. * @param {String} pdfFileUrl PDF文件地址 必传
  55. * @param {String} pdfFileName PDF文件别名 非必传
  56. */
  57. export function regularDownloadPdfFile(pdfFileUrl, pdfFileName) {
  58. if (pdfFileUrl) {
  59. request({
  60. url: pdfFileUrl,
  61. method: 'get',
  62. responseType: 'blob'
  63. }).then(res => {
  64. // 文件流
  65. const blob = new Blob([res], { type: 'application/pdf;charset=utf-8' });
  66. // 文件名称
  67. const filename = `${pdfFileName || 'PDF文件'}_${parseTime(new Date(), "{y}-{m}-{d}")}_${new Date().getTime()}.pdf`;
  68. // 执行文件下载操作
  69. if (typeof window.navigator.msSaveBlob !== "undefined") {
  70. window.navigator.msSaveBlob(blob, filename);
  71. Message({
  72. message: "下载PDF文件成功!",
  73. type: 'success',
  74. showClose: true,
  75. center: true
  76. });
  77. } else {
  78. // 创建一个指向该参数对象的URL
  79. const blobURL = window.URL.createObjectURL(blob);
  80. // 创建隐藏<a>标签进行下载
  81. const tempLink = document.createElement("a");
  82. tempLink.style.display = "none";
  83. tempLink.href = blobURL;
  84. tempLink.setAttribute("download", filename);
  85. if (typeof tempLink.download === "undefined") {
  86. tempLink.setAttribute("target", "_blank");
  87. }
  88. document.body.appendChild(tempLink);
  89. tempLink.click();
  90. window.URL.revokeObjectURL(blobURL);
  91. document.body.removeChild(tempLink);
  92. Message({
  93. message: "下载PDF文件成功!",
  94. type: 'success',
  95. showClose: true,
  96. center: true
  97. });
  98. }
  99. }).catch(() => {});
  100. } else {
  101. Message({
  102. message: "PDF文件地址不能为空!",
  103. type: 'error',
  104. showClose: true,
  105. center: true
  106. });
  107. }
  108. }