plugin.ts 2.2 KB

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