utils.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. type DialogType = 'success' | 'error' | 'info'
  2. interface DialogBoxProps {
  3. show?: boolean
  4. type?: DialogType
  5. confirmText?: string
  6. confirmPlain?: boolean
  7. cancelText?: string
  8. iconSize?: string
  9. message?: string
  10. tip?: string
  11. showConfirm?: boolean
  12. showCancel?: boolean
  13. showTip?: boolean
  14. closeOnClickOverlay?: boolean
  15. }
  16. const DIALOG_PRESETS: Record<DialogType, Partial<DialogBoxProps>> = {
  17. info: {
  18. type: 'info',
  19. iconSize: '120rpx',
  20. confirmText: '知道了',
  21. },
  22. success: {
  23. type: 'success',
  24. iconSize: '120rpx',
  25. confirmText: '好的',
  26. },
  27. error: {
  28. type: 'error',
  29. iconSize: '120rpx',
  30. confirmText: '重试',
  31. },
  32. }
  33. /**
  34. * 创建预设对话框配置
  35. * @param type 对话框类型
  36. * @param message 消息内容
  37. * @param options 额外配置选项
  38. * @returns 对话框配置
  39. */
  40. export function createDialogConfig(
  41. type: DialogType,
  42. message: string,
  43. options: Partial<DialogBoxProps> = {},
  44. ): DialogBoxProps {
  45. const preset = DIALOG_PRESETS[type]
  46. return {
  47. show: true,
  48. message,
  49. ...preset,
  50. ...options,
  51. }
  52. }
  53. /**
  54. * 创建信息对话框配置
  55. */
  56. export function createInfoDialog(message: string, options?: Partial<DialogBoxProps>) {
  57. return createDialogConfig('info', message, options)
  58. }
  59. /**
  60. * 创建成功对话框配置
  61. */
  62. export function createSuccessDialog(message: string, options?: Partial<DialogBoxProps>) {
  63. return createDialogConfig('success', message, options)
  64. }
  65. /**
  66. * 创建错误对话框配置
  67. */
  68. export function createErrorDialog(message: string, options?: Partial<DialogBoxProps>) {
  69. return createDialogConfig('error', message, options)
  70. }
  71. /**
  72. * 对话框工具类
  73. */
  74. export class DialogUtils {
  75. /**
  76. * 显示信息对话框
  77. */
  78. static info(message: string, options?: Partial<DialogBoxProps>) {
  79. return createInfoDialog(message, options)
  80. }
  81. /**
  82. * 显示成功对话框
  83. */
  84. static success(message: string, options?: Partial<DialogBoxProps>) {
  85. return createSuccessDialog(message, options)
  86. }
  87. /**
  88. * 显示错误对话框
  89. */
  90. static error(message: string, options?: Partial<DialogBoxProps>) {
  91. return createErrorDialog(message, options)
  92. }
  93. }