useMessage.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. interface ConfirmOptions {
  26. info: ModalFunc;
  27. success: ModalFunc;
  28. error: ModalFunc;
  29. warn: ModalFunc;
  30. warning: ModalFunc;
  31. }
  32. function getIcon(iconType: string) {
  33. try {
  34. if (iconType === 'warning') {
  35. return h(InfoCircleFilled,{"class":"modal-icon-warning"})
  36. } else if (iconType === 'success') {
  37. return h(CheckCircleFilled,{"class": "modal-icon-success"});
  38. } else if (iconType === 'info') {
  39. return h(InfoCircleFilled,{"class": "modal-icon-info"});
  40. } else {
  41. return h(CloseCircleFilled,{"class":"modal-icon-error"});
  42. }
  43. } catch (e) {
  44. console.log(e);
  45. }
  46. }
  47. function renderContent({ content }: Pick<ModalOptionsEx, 'content'>) {
  48. try {
  49. if (isString(content)) {
  50. return h('div', h('div', content as string));
  51. } else {
  52. return content;
  53. }
  54. } catch (e) {
  55. console.log(e);
  56. return content;
  57. }
  58. }
  59. /**
  60. * @description: Create confirmation box
  61. */
  62. function createConfirm(options: ModalOptionsEx): ReturnType<ModalFunc> {
  63. const iconType = options.iconType || 'warning';
  64. Reflect.deleteProperty(options, 'iconType');
  65. const opt: ModalFuncProps = {
  66. centered: true,
  67. icon: getIcon(iconType),
  68. ...options,
  69. content: renderContent(options),
  70. };
  71. return Modal.confirm(opt);
  72. }
  73. const getBaseOptions = () => {
  74. const { t } = useI18n();
  75. return {
  76. okText: t('common.okText'),
  77. centered: true,
  78. };
  79. };
  80. function createModalOptions(options: ModalOptionsPartial, icon: string): ModalOptionsPartial {
  81. return {
  82. ...getBaseOptions(),
  83. ...options,
  84. content: renderContent(options),
  85. icon: getIcon(icon),
  86. };
  87. }
  88. function createSuccessModal(options: ModalOptionsPartial) {
  89. return Modal.success(createModalOptions(options, 'success'));
  90. }
  91. function createErrorModal(options: ModalOptionsPartial) {
  92. return Modal.error(createModalOptions(options, 'close'));
  93. }
  94. function createInfoModal(options: ModalOptionsPartial) {
  95. return Modal.info(createModalOptions(options, 'info'));
  96. }
  97. function createWarningModal(options: ModalOptionsPartial) {
  98. return Modal.warning(createModalOptions(options, 'warning'));
  99. }
  100. notification.config({
  101. placement: 'topRight',
  102. duration: 3,
  103. });
  104. /**
  105. * @description: message
  106. */
  107. export function useMessage() {
  108. return {
  109. createMessage: Message,
  110. notification: notification as NotifyApi,
  111. createConfirm: createConfirm,
  112. createSuccessModal,
  113. createErrorModal,
  114. createInfoModal,
  115. createWarningModal,
  116. };
  117. }