useAlert.ts 969 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { Dialog } from '@kankan/components/index'
  2. import { ui18n } from '@/lang'
  3. const cacheMap = new Map<any, Promise<any>>()
  4. export const useAlert = async (msg: any) => {
  5. if (cacheMap.has(msg)) {
  6. await cacheMap.get(msg)
  7. } else {
  8. const caches = Array.from(cacheMap.values())
  9. if (caches.length) {
  10. await Promise.all(caches)
  11. }
  12. if (typeof msg == 'string') {
  13. msg = {
  14. content: msg
  15. }
  16. }
  17. msg.title = msg.title || ui18n.t('sys.dialogTitle')
  18. msg.okText = msg.okText || ui18n.t('sys.enter')
  19. const promise = Dialog.alert(msg)
  20. cacheMap.set(msg, promise)
  21. await promise
  22. cacheMap.delete(msg)
  23. }
  24. }
  25. export const useConfirm = async (msg: any) => {
  26. if (typeof msg == 'string') {
  27. msg = {
  28. content: msg
  29. }
  30. }
  31. msg.title = msg.title || ui18n.t('sys.dialogTitle')
  32. msg.okText = msg.okText || ui18n.t('sys.enter')
  33. msg.noText = msg.noText || ui18n.t('sys.cancel')
  34. return Dialog.confirm(msg)
  35. }