Notify.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { h } from 'vue';
  2. import { notification, Button } from 'ant-design-vue';
  3. /**
  4. * 通知类型
  5. * @type {Object}
  6. * @property {string} show - 显示
  7. * @property {string} notice - 通知
  8. * @property {string} noticeBig - 通知(大)
  9. * @property {string} info - 信息
  10. * @property {string} infoBig - 信息(大)
  11. * @property {string} warning - 警告
  12. * @property {string} error - 错误
  13. * @property {string} success - 成功
  14. * author: 刘彦鹏
  15. * info: 封装antd的notification组件
  16. */
  17. export default {
  18. /**
  19. * 显示通知
  20. * @param {options} 通知选项
  21. * @param {title} 标题
  22. * @param {message} 内容
  23. * @param {buttons} 按钮选项
  24. */
  25. show: function (options) {
  26. const key = `notify_${Date.now()}`;
  27. const dialogItself = {
  28. key,
  29. close: () => notification.close(key),
  30. };
  31. const btns = options.buttons.map(btn => {
  32. const type = btn.cssClass?.includes('btn-primary') ? 'primary' : 'default';
  33. const action = () => btn.action(dialogItself);
  34. return h(
  35. Button,
  36. {
  37. type,
  38. onClick: action,
  39. style: {
  40. marginLeft: '8px',
  41. },
  42. },
  43. {
  44. default: () => btn.label,
  45. },
  46. );
  47. });
  48. notification.open({
  49. message: options.title,
  50. description: options.message,
  51. duration: 0,
  52. key,
  53. btn: btns,
  54. onClose: dialogItself.close,
  55. });
  56. },
  57. /**
  58. * 显示通知
  59. * @param {title} 标题
  60. * @param {content} 内容
  61. * @param {autoClose} 自动延时关闭时间(毫秒)
  62. * author: 杨志杰
  63. * version: 1.0
  64. */
  65. notice: function (title, content, autoClose) {
  66. notification.warning({
  67. message: title,
  68. description: content,
  69. duration: autoClose ? autoClose / 1000 : 2,
  70. });
  71. },
  72. /**
  73. * 显示通知
  74. * @param {title} 标题
  75. * @param {content} 内容
  76. * author: 杨志杰
  77. * version: 1.0
  78. */
  79. noticeBig: function (title, content) {
  80. notification.warning({
  81. message: title,
  82. description: content,
  83. duration: 0, // 不自动关闭
  84. className: 'wide-notification',
  85. style: { width: '800px' },
  86. });
  87. },
  88. /**
  89. * 显示消息
  90. * @param {title} 标题
  91. * @param {content} 内容
  92. * @param {autoClose} 自动延时关闭时间(毫秒)
  93. * author: 杨志杰
  94. * version: 1.0
  95. */
  96. info: function (title, content, autoClose) {
  97. notification.info({
  98. message: title,
  99. description: content,
  100. duration: autoClose ? autoClose / 1000 : 2,
  101. });
  102. },
  103. /**
  104. * 显示消息
  105. * @param {title} 标题
  106. * @param {content} 内容
  107. * @param {autoClose} 自动延时关闭时间(毫秒)
  108. * author: 杨志杰
  109. * version: 1.0
  110. */
  111. infoBig: function (title, content, autoClose) {
  112. notification.info({
  113. message: title,
  114. description: content,
  115. duration: autoClose ? autoClose / 1000 : 2,
  116. className: 'wide-notification',
  117. style: { width: '800px' },
  118. });
  119. },
  120. /**
  121. * 显示成功信息
  122. * @param {title} 标题
  123. * @param {content} 内容
  124. * @param {autoClose} 自动延时关闭时间(毫秒)
  125. * author: 杨志杰
  126. * version: 1.0
  127. */
  128. success: function (title, content, autoClose) {
  129. notification.success({
  130. message: title,
  131. description: content,
  132. duration: autoClose === true ? 2 : typeof autoClose === 'number' && !isNaN(autoClose) ? autoClose / 1000 : 2,
  133. });
  134. },
  135. /**
  136. * 显示错误信息
  137. * @param {title} 标题
  138. * @param {content} 内容
  139. * @param {autoClose} 自动延时关闭时间(毫秒)
  140. * author: 杨志杰
  141. * version: 1.0
  142. */
  143. error: function (title, content, autoClose) {
  144. const duration = autoClose === true ? 2 : autoClose === -1 ? 0 : autoClose ? autoClose / 1000 : 0;
  145. notification.error({
  146. message: title,
  147. description: content,
  148. duration,
  149. });
  150. },
  151. };