瀏覽代碼

feat: 完成dialog动态封装

MTrun 3 年之前
父節點
當前提交
2120e26220
共有 3 個文件被更改,包括 81 次插入24 次删除
  1. 6 0
      src/enums/pluginEnum.ts
  2. 61 19
      src/utils/plugin.ts
  3. 14 5
      src/views/project/items/components/Card/index.vue

+ 6 - 0
src/enums/pluginEnum.ts

@@ -0,0 +1,6 @@
+export enum DialogEnum {
+  delete = 'delete',
+  warning = 'warning',
+  error = 'error',
+  success = 'success'
+}

+ 61 - 19
src/utils/plugin.ts

@@ -1,43 +1,85 @@
 import { icon } from '@/plugins'
+import { DialogEnum } from '@/enums/pluginEnum'
 import { dialogIconSize, maskClosable } from '@/settings/designSetting'
+import { DialogReactive } from 'naive-ui'
 const { InformationCircleIcon } = icon.ionicons5
 import { renderIcon } from '@/utils'
 
 /**
- * * render 弹出确认
+ * * render 对话
  * @param { Function } dialogFn dialog函数,暂时必须从页面传过来
  * @param { Object} params 配置参数
  */
- export const goDialog = (
-  dialogFn: Function,
+export const goDialog = (
   params: {
     // 基本
-    type: 'delete'
+    type: DialogEnum
     message?: string
+    // 回调
     onPositiveCallback?: Function
     onNegativeCallback?: Function
-    // 渲染函数
-    render?: boolean
-    contentFn?: Function
-    actionFn?: Function
-  }
+    // 异步
+    promise?: boolean
+    promiseResCallback?: Function
+    promiseRejCallback?: Function
+  },
+  dialogFn?: Function
 ) => {
-  const { type, message, onPositiveCallback, onNegativeCallback } = params
-  const tip = {
-    delete: '是否删除此数据'
+  const {
+    type,
+    message,
+    onPositiveCallback,
+    onNegativeCallback,
+    promise,
+    promiseResCallback,
+    promiseRejCallback
+  } = params
+
+  const typeObj = {
+    // 自定义
+    delete: {
+      fn: dialogFn || window['$dialog'].warning,
+      message: message || '是否删除此数据?'
+    },
+    // 原有
+    warning: {
+      fn: window['$dialog'].warning,
+      message: message || '是否执行此操作?'
+    },
+    error: {
+      fn: window['$dialog'].error,
+      message: message || '是否执行此操作?'
+    },
+    success: {
+      fn: window['$dialog'].success,
+      message: message || '是否执行此操作?'
+    }
   }
-  const instance = dialogFn({
+
+  const d: DialogReactive = (typeObj as any)[type]['fn']({
     title: '提示',
     icon: renderIcon(InformationCircleIcon, { size: dialogIconSize }),
-    content: message || tip[type] || '',
+    content: (typeObj as any)[type]['message'],
     positiveText: '确定',
     negativeText: '取消',
     maskClosable: maskClosable,
-    onPositiveClick: () => {
-      onPositiveCallback && onPositiveCallback(instance)
+    onPositiveClick: async () => {
+      // 执行异步
+      if (promise && onPositiveCallback) {
+        d.loading = true
+        try {
+          const res = await onPositiveCallback()
+          promiseResCallback && promiseResCallback(res)
+        } catch (err) {
+          promiseRejCallback && promiseRejCallback(err)
+        }
+        d.loading = false
+        return
+      }
+      onPositiveCallback && onPositiveCallback(d)
     },
-    onNegativeClick: () => {
-      onNegativeCallback && onNegativeCallback(instance)
+    onNegativeClick: async () => {
+      onNegativeCallback && onNegativeCallback(d)
     }
   })
-}
+}

+ 14 - 5
src/views/project/items/components/Card/index.vue

@@ -4,7 +4,11 @@
       <div class="list-content">
         <!-- 顶部按钮 -->
         <n-space class="list-content-top">
-          <AppleControlBtn :hidden="['remove']" @close="deleteHanlde" @resize="resizeHandle" />
+          <AppleControlBtn
+            :hidden="['remove']"
+            @close="deleteHanlde"
+            @resize="resizeHandle"
+          />
         </n-space>
         <!-- 中间 -->
         <div class="list-content-img">
@@ -76,6 +80,8 @@ import { renderIcon, goDialog } from '@/utils'
 import { icon } from '@/plugins'
 import { AppleControlBtn } from '@/components/AppleControlBtn'
 import { useMessage, useDialog } from 'naive-ui'
+import { DialogEnum } from '@/enums/pluginEnum'
+import { DialogReactive } from 'naive-ui'
 const {
   EllipsisHorizontalCircleSharpIcon,
   CopyIcon,
@@ -160,10 +166,13 @@ const requireUrl = (path: string, name: string) => {
 
 // 删除处理
 const deleteHanlde = () => {
-  goDialog(dialog.warning, {
-    type: 'delete',
-    onPositiveCallback: () => {
-      message.success('删除成功')
+  goDialog({
+    type: DialogEnum.delete,
+    promise: true,
+    onPositiveCallback: () =>
+      new Promise(res => setTimeout(() => res(1), 1000)),
+    promiseResCallback: (e: any) => {
+      window.$message.success('删除成功')
     }
   })
 }