useMessage.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import type { ModalFunc, ModalFuncProps } from 'ant-design-vue/lib/modal/Modal';
  2. import { Modal, message as Message, notification } from 'ant-design-vue';
  3. import { InfoCircleFilled, CheckCircleFilled, CloseCircleFilled } from '@ant-design/icons-vue';
  4. import { NotificationArgsProps, ConfigProps } from 'ant-design-vue/lib/notification';
  5. import { useI18n } from './useI18n';
  6. import { isString } from '/@/utils/is';
  7. import { h } from 'vue';
  8. export interface NotifyApi {
  9. info(config: NotificationArgsProps): void;
  10. success(config: NotificationArgsProps): void;
  11. error(config: NotificationArgsProps): void;
  12. warn(config: NotificationArgsProps): void;
  13. warning(config: NotificationArgsProps): void;
  14. open(args: NotificationArgsProps): void;
  15. close(key: String): void;
  16. config(options: ConfigProps): void;
  17. destroy(): void;
  18. }
  19. export declare type NotificationPlacement = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
  20. export declare type IconType = 'success' | 'info' | 'error' | 'warning';
  21. export interface ModalOptionsEx extends Omit<ModalFuncProps, 'iconType'> {
  22. iconType: 'warning' | 'success' | 'error' | 'info';
  23. }
  24. export type ModalOptionsPartial = Partial<ModalOptionsEx> & Pick<ModalOptionsEx, 'content'>;
  25. function getIcon(iconType: string) {
  26. try {
  27. if (iconType === 'warning') {
  28. return h(InfoCircleFilled, { class: 'modal-icon-warning' });
  29. } else if (iconType === 'success') {
  30. return h(CheckCircleFilled, { class: 'modal-icon-success' });
  31. } else if (iconType === 'info') {
  32. return h(InfoCircleFilled, { class: 'modal-icon-info' });
  33. } else {
  34. return h(CloseCircleFilled, { class: 'modal-icon-error' });
  35. }
  36. } catch (e) {
  37. console.log(e);
  38. }
  39. }
  40. function renderContent({ content }: Pick<ModalOptionsEx, 'content'>) {
  41. try {
  42. if (isString(content)) {
  43. return h('div', h('div', { innerHTML: content as string }));
  44. } else {
  45. return content;
  46. }
  47. } catch (e) {
  48. console.log(e);
  49. return content;
  50. }
  51. }
  52. /**
  53. * @description: Create confirmation box
  54. */
  55. function createConfirm(options: ModalOptionsEx): ReturnType<ModalFunc> {
  56. const iconType = options.iconType || 'warning';
  57. Reflect.deleteProperty(options, 'iconType');
  58. const opt: ModalFuncProps = {
  59. centered: true,
  60. icon: getIcon(iconType),
  61. ...options,
  62. content: renderContent(options),
  63. };
  64. return Modal.confirm(opt);
  65. }
  66. const getBaseOptions = () => {
  67. const { t } = useI18n();
  68. return {
  69. okText: t('common.okText'),
  70. centered: true,
  71. };
  72. };
  73. function createModalOptions(options: ModalOptionsPartial, icon: string): ModalOptionsPartial {
  74. //update-begin-author:taoyan date:2023-1-10 for: 可以自定义图标
  75. let titleIcon:any = ''
  76. if(options.icon){
  77. titleIcon = options.icon;
  78. }else{
  79. titleIcon = getIcon(icon)
  80. }
  81. //update-end-author:taoyan date:2023-1-10 for: 可以自定义图标
  82. return {
  83. ...getBaseOptions(),
  84. ...options,
  85. content: renderContent(options),
  86. icon: titleIcon
  87. };
  88. }
  89. function createSuccessModal(options: ModalOptionsPartial) {
  90. return Modal.success(createModalOptions(options, 'success'));
  91. }
  92. function createErrorModal(options: ModalOptionsPartial) {
  93. return Modal.error(createModalOptions(options, 'close'));
  94. }
  95. function createInfoModal(options: ModalOptionsPartial) {
  96. return Modal.info(createModalOptions(options, 'info'));
  97. }
  98. function createWarningModal(options: ModalOptionsPartial) {
  99. return Modal.warning(createModalOptions(options, 'warning'));
  100. }
  101. interface MOE extends Omit<ModalOptionsEx, 'iconType'> {
  102. iconType?: ModalOptionsEx['iconType'];
  103. }
  104. // 提示框,无需传入iconType,默认为warning
  105. function createConfirmSync(options: MOE) {
  106. return new Promise((resolve) => {
  107. createConfirm({
  108. iconType: 'warning',
  109. ...options,
  110. onOk: () => resolve(true),
  111. onCancel: () => resolve(false),
  112. });
  113. });
  114. }
  115. notification.config({
  116. placement: 'topRight',
  117. duration: 3,
  118. });
  119. /**
  120. * @description: message
  121. */
  122. export function useMessage() {
  123. return {
  124. createMessage: Message,
  125. notification: notification as NotifyApi,
  126. createConfirm: createConfirm,
  127. createConfirmSync,
  128. createSuccessModal,
  129. createErrorModal,
  130. createInfoModal,
  131. createWarningModal,
  132. };
  133. }