feedback.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {
  2. ElMessage,
  3. ElMessageBox,
  4. ElNotification,
  5. ElLoading,
  6. type ElMessageBoxOptions
  7. } from 'element-plus'
  8. import type { LoadingInstance } from 'element-plus/es/components/loading/src/loading'
  9. export class Feedback {
  10. private loadingInstance : LoadingInstance | null = null
  11. static instance : Feedback | null = null
  12. static getInstance() {
  13. return this.instance ?? (this.instance = new Feedback())
  14. }
  15. // 消息提示
  16. msg(msg : string) {
  17. ElMessage.info(msg)
  18. }
  19. // 错误消息
  20. msgError(msg : string) {
  21. ElMessage.error(msg)
  22. }
  23. // 成功消息
  24. msgSuccess(msg : string) {
  25. ElMessage.success(msg)
  26. }
  27. // 警告消息
  28. msgWarning(msg : string) {
  29. ElMessage.warning(msg)
  30. }
  31. // 弹出提示
  32. alert(msg : string) {
  33. ElMessageBox.alert(msg, '系统提示')
  34. }
  35. // 错误提示
  36. alertError(msg : string) {
  37. ElMessageBox.alert(msg, '系统提示', { type: 'error' })
  38. }
  39. // 成功提示
  40. alertSuccess(msg : string) {
  41. ElMessageBox.alert(msg, '系统提示', { type: 'success' })
  42. }
  43. // 警告提示
  44. alertWarning(msg : string) {
  45. ElMessageBox.alert(msg, '系统提示', { type: 'warning' })
  46. }
  47. // 通知提示
  48. notify(msg : string) {
  49. ElNotification.info(msg)
  50. }
  51. // 错误通知
  52. notifyError(msg : string) {
  53. ElNotification.error(msg)
  54. }
  55. // 成功通知
  56. notifySuccess(msg : string) {
  57. ElNotification.success(msg)
  58. }
  59. // 警告通知
  60. notifyWarning(msg : string) {
  61. ElNotification.warning(msg)
  62. }
  63. // 确认窗体
  64. confirm(msg : string) {
  65. return ElMessageBox.confirm(msg, '温馨提示', {
  66. confirmButtonText: '确定',
  67. cancelButtonText: '取消',
  68. type: 'warning'
  69. })
  70. }
  71. // 提交内容
  72. prompt(content : string, title : string, options ?: ElMessageBoxOptions) {
  73. return ElMessageBox.prompt(content, title, {
  74. confirmButtonText: '确定',
  75. cancelButtonText: '取消',
  76. ...options
  77. })
  78. }
  79. // 提交内容
  80. messageBox(content : string, imageUrl: string, fileName: string) {
  81. ElMessageBox.confirm(
  82. content,
  83. {
  84. title: '温馨提示',
  85. dangerouslyUseHTMLString: true, // 允许使用 HTML 字符串
  86. confirmButtonText: '下载',
  87. cancelButtonText: '取消',
  88. }
  89. )
  90. .then(async () => {
  91. const response = await fetch(imageUrl);
  92. if (!response.ok) {
  93. throw new Error('图片下载失败');
  94. }
  95. // 将图片数据转换为 Blob
  96. const blob = await response.blob();
  97. const url = window.URL.createObjectURL(blob);
  98. const link = document.createElement('a')
  99. link.style.display = 'none'
  100. link.href = url
  101. link.setAttribute('download', fileName || "img.png")
  102. document.body.appendChild(link)
  103. link.click()
  104. document.body.removeChild(link) // 下载完成移除元素
  105. window.URL.revokeObjectURL(url)
  106. ElMessage.success('图片下载成功');
  107. console.log('用户点击了下载');
  108. })
  109. .catch(() => {
  110. console.log('用户点击了取消');
  111. });
  112. }
  113. // 打开全局loading
  114. loading(msg : string) {
  115. this.loadingInstance = ElLoading.service({
  116. lock: true,
  117. text: msg
  118. })
  119. }
  120. // 关闭全局loading
  121. closeLoading() {
  122. this.loadingInstance?.close()
  123. }
  124. }
  125. const feedback = Feedback.getInstance()
  126. export default feedback