useMessage.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { useSetting } from '/@/hooks/core/useSetting';
  5. import { ArgsProps, ConfigProps } from 'ant-design-vue/lib/notification';
  6. export interface NotifyApi {
  7. info(config: ArgsProps): void;
  8. success(config: ArgsProps): void;
  9. error(config: ArgsProps): void;
  10. warn(config: ArgsProps): void;
  11. warning(config: ArgsProps): void;
  12. open(args: ArgsProps): void;
  13. close(key: String): void;
  14. config(options: ConfigProps): void;
  15. destroy(): void;
  16. }
  17. export declare type NotificationPlacement = 'topLeft' | 'topRight' | 'bottomLeft' | 'bottomRight';
  18. export declare type IconType = 'success' | 'info' | 'error' | 'warning';
  19. export interface ModalOptionsEx extends Omit<ModalFuncProps, 'iconType'> {
  20. iconType: 'warning' | 'success' | 'error' | 'info';
  21. }
  22. export type ModalOptionsPartial = Partial<ModalOptionsEx> & Pick<ModalOptionsEx, 'content'>;
  23. interface ConfirmOptions {
  24. info: ModalFunc;
  25. success: ModalFunc;
  26. error: ModalFunc;
  27. warn: ModalFunc;
  28. warning: ModalFunc;
  29. }
  30. const { projectSetting } = useSetting();
  31. function getIcon(iconType: string) {
  32. if (iconType === 'warning') {
  33. return <InfoCircleFilled class="modal-icon-warning" />;
  34. } else if (iconType === 'success') {
  35. return <CheckCircleFilled class="modal-icon-success" />;
  36. } else if (iconType === 'info') {
  37. return <InfoCircleFilled class="modal-icon-info" />;
  38. } else {
  39. return <CloseCircleFilled class="modal-icon-error" />;
  40. }
  41. }
  42. function renderContent({ content }: Pick<ModalOptionsEx, 'content'>) {
  43. return <div innerHTML={`<div>${content as string}</div>`}></div>;
  44. }
  45. /**
  46. * @description: Create confirmation box
  47. */
  48. function createConfirm(options: ModalOptionsEx): ConfirmOptions {
  49. const iconType = options.iconType || 'warning';
  50. Reflect.deleteProperty(options, 'iconType');
  51. const opt: ModalFuncProps = {
  52. centered: true,
  53. icon: getIcon(iconType),
  54. ...projectSetting.messageSetting,
  55. ...options,
  56. };
  57. return Modal.confirm(opt) as any;
  58. }
  59. const baseOptions = {
  60. okText: '确定',
  61. centered: true,
  62. };
  63. function createModalOptions(options: ModalOptionsPartial, icon: string): ModalOptionsPartial {
  64. return {
  65. ...baseOptions,
  66. ...options,
  67. content: renderContent(options),
  68. icon: getIcon(icon),
  69. };
  70. }
  71. function createSuccessModal(options: ModalOptionsPartial) {
  72. return Modal.success(createModalOptions(options, 'success'));
  73. }
  74. function createErrorModal(options: ModalOptionsPartial) {
  75. return Modal.error(createModalOptions(options, 'close'));
  76. }
  77. function createInfoModal(options: ModalOptionsPartial) {
  78. return Modal.info(createModalOptions(options, 'info'));
  79. }
  80. function createWarningModal(options: ModalOptionsPartial) {
  81. return Modal.warning(createModalOptions(options, 'warning'));
  82. }
  83. notification.config({
  84. placement: 'topRight',
  85. duration: 3,
  86. });
  87. /**
  88. * @description: message
  89. */
  90. export function useMessage() {
  91. return {
  92. createMessage: Message,
  93. notification: notification as NotifyApi,
  94. createConfirm: createConfirm,
  95. createSuccessModal,
  96. createErrorModal,
  97. createInfoModal,
  98. createWarningModal,
  99. };
  100. }