plugin.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { icon } from '@/plugins'
  2. import { DialogEnum } from '@/enums/pluginEnum'
  3. import { dialogIconSize } from '@/settings/designSetting'
  4. import { maskClosable } from '@/settings/systemSetting'
  5. import { DialogReactive } from 'naive-ui'
  6. const { InformationCircleIcon } = icon.ionicons5
  7. import { renderIcon } from '@/utils'
  8. /**
  9. * * render 对话框
  10. * @param { Object} params 配置参数
  11. * @param { Function } dialogFn 函数
  12. */
  13. export const goDialog = (
  14. params: {
  15. // 基本
  16. type?: DialogEnum
  17. // 提示
  18. message?: string
  19. // 点击遮罩是否关闭
  20. isMaskClosable?: boolean
  21. // 回调
  22. onPositiveCallback: Function
  23. onNegativeCallback?: Function
  24. // 异步
  25. promise?: boolean
  26. promiseResCallback?: Function
  27. promiseRejCallback?: Function
  28. },
  29. dialogFn?: Function
  30. ) => {
  31. const {
  32. type,
  33. message,
  34. isMaskClosable,
  35. onPositiveCallback,
  36. onNegativeCallback,
  37. promise,
  38. promiseResCallback,
  39. promiseRejCallback,
  40. } = params
  41. const typeObj = {
  42. // 自定义
  43. delete: {
  44. fn: dialogFn || window['$dialog'].warning,
  45. message: message || '是否删除此数据?',
  46. },
  47. // 原有
  48. warning: {
  49. fn: window['$dialog'].warning,
  50. message: message || '是否执行此操作?',
  51. },
  52. error: {
  53. fn: window['$dialog'].error,
  54. message: message || '是否执行此操作?',
  55. },
  56. success: {
  57. fn: window['$dialog'].success,
  58. message: message || '是否执行此操作?',
  59. },
  60. }
  61. const d: DialogReactive = (typeObj as any)[type || DialogEnum.warning]['fn']({
  62. title: '提示',
  63. icon: renderIcon(InformationCircleIcon, { size: dialogIconSize }),
  64. content: (typeObj as any)[type || DialogEnum.warning]['message'],
  65. positiveText: '确定',
  66. negativeText: '取消',
  67. // 是否通过遮罩关闭
  68. maskClosable: isMaskClosable || maskClosable,
  69. onPositiveClick: async () => {
  70. // 执行异步
  71. if (promise && onPositiveCallback) {
  72. d.loading = true
  73. try {
  74. const res = await onPositiveCallback()
  75. promiseResCallback && promiseResCallback(res)
  76. } catch (err) {
  77. promiseRejCallback && promiseRejCallback(err)
  78. }
  79. d.loading = false
  80. return
  81. }
  82. onPositiveCallback && onPositiveCallback(d)
  83. },
  84. onNegativeClick: async () => {
  85. onNegativeCallback && onNegativeCallback(d)
  86. },
  87. })
  88. }