useMessage.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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', { innerHTML: 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. //update-begin-author:taoyan date:2023-1-10 for: 可以自定义图标
  82. let titleIcon:any = ''
  83. if(options.icon){
  84. titleIcon = options.icon;
  85. }else{
  86. titleIcon = getIcon(icon)
  87. }
  88. //update-end-author:taoyan date:2023-1-10 for: 可以自定义图标
  89. return {
  90. ...getBaseOptions(),
  91. ...options,
  92. content: renderContent(options),
  93. icon: titleIcon
  94. };
  95. }
  96. function createSuccessModal(options: ModalOptionsPartial) {
  97. return Modal.success(createModalOptions(options, 'success'));
  98. }
  99. function createErrorModal(options: ModalOptionsPartial) {
  100. return Modal.error(createModalOptions(options, 'close'));
  101. }
  102. function createInfoModal(options: ModalOptionsPartial) {
  103. return Modal.info(createModalOptions(options, 'info'));
  104. }
  105. function createWarningModal(options: ModalOptionsPartial) {
  106. return Modal.warning(createModalOptions(options, 'warning'));
  107. }
  108. interface MOE extends Omit<ModalOptionsEx, 'iconType'> {
  109. iconType?: ModalOptionsEx['iconType'];
  110. }
  111. // 提示框,无需传入iconType,默认为warning
  112. function createConfirmSync(options: MOE) {
  113. return new Promise((resolve) => {
  114. createConfirm({
  115. iconType: 'warning',
  116. ...options,
  117. onOk: () => resolve(true),
  118. onCancel: () => resolve(false),
  119. });
  120. });
  121. }
  122. notification.config({
  123. placement: 'topRight',
  124. duration: 3,
  125. });
  126. /**
  127. * @description: message
  128. */
  129. export function useMessage() {
  130. return {
  131. createMessage: Message,
  132. notification: notification as NotifyApi,
  133. createConfirm: createConfirm,
  134. createConfirmSync,
  135. createSuccessModal,
  136. createErrorModal,
  137. createInfoModal,
  138. createWarningModal,
  139. };
  140. }