Explorar el Código

feat: 添加流程处理脚本用于生成流程数据

添加新的脚本文件 scripts/process.ts,用于通过 ProcessHelper 构建流程节点树并输出流程数据。该脚本主要用于测试和验证流程构建功能。
zhanghaijun hace 1 año
padre
commit
7c2b3ab2ba

+ 1 - 0
package.json

@@ -13,6 +13,7 @@
     "lint:prettier": "prettier --write \"**/*.{js,cjs,ts,json,tsx,css,less,scss,vue,html,md}\"",
     "lint:stylelint": "stylelint  \"**/*.{css,scss,vue}\" --fix",
     "lint:lint-staged": "lint-staged",
+    "dev:process": "tsx scripts/process.ts",
     "prepare": "husky",
     "commit": "git-cz"
   },

+ 3 - 3
src/components/business/EasyProcess/common/process.data.ts

@@ -25,10 +25,10 @@ export const nodeConfigData: Record<EasyProcess.NodeType, EasyProcess.NodeMeta>
       nodeId: getUUID(),
       nodeName: '路由',
       nodeType: 'router',
-      nodeData: { value: '1' },
+      nodeData: {},
       conditionNodes: [
-        { nodeName: '条件', nodeType: 'condition', isLastCondition: false, nodeData: { value: '1' } },
-        { nodeName: '条件', nodeType: 'condition', isLastCondition: true, nodeData: { value: '1' } }
+        { nodeId: getUUID(), nodeName: '条件', nodeType: 'condition', isLastCondition: false, nodeData: { value: '1' } },
+        { nodeId: getUUID(), nodeName: '条件', nodeType: 'condition', isLastCondition: true, nodeData: { value: '1' } }
       ]
     })
   },

+ 17 - 4
src/components/business/EasyProcess/common/process.helper.ts

@@ -6,6 +6,10 @@ export class ProcessHelper {
   constructor(processData: EasyProcess.NodeConfig) {
     this.processData = processData
   }
+
+  public getProcessData(): EasyProcess.NodeConfig {
+    return this.processData
+  }
   /**
    * 添加子节点
    * @param nodeId 父节点ID
@@ -16,13 +20,15 @@ export class ProcessHelper {
     if (!target) return false
 
     // 根据类型处理不同结构
-    if (target.nodeType === 'router') {
+    if (target.nodeType === 'router' && node.nodeType === 'condition') {
       // 路由节点添加条件分支
       const router = target as EasyProcess.RouterNode
       if (!router.conditionNodes) router.conditionNodes = []
       router.conditionNodes.push(node as EasyProcess.ConditionNode)
     } else {
       // 普通节点、条件节点添加 childNode
+      const childNode = target.childNode
+      if (childNode) node.childNode = childNode
       target.childNode = node
     }
     return true
@@ -33,9 +39,11 @@ export class ProcessHelper {
    * @param nodeId 要删除的节点ID
    */
   public deleteNode(nodeId: string, current: EasyProcess.NodeConfig = this.processData): boolean {
+    // 禁止删除根节点
+    if (this.processData.nodeId === nodeId) return false
     // 删除子节点
     if (current.childNode?.nodeId === nodeId) {
-      current.childNode = undefined
+      current.childNode = current.childNode?.childNode ?? undefined
       return true
     }
     // 删除条件节点(router专用)
@@ -44,7 +52,11 @@ export class ProcessHelper {
       const before = router.conditionNodes?.length ?? 0
       if (router.conditionNodes) {
         router.conditionNodes = router.conditionNodes.filter((c) => c.nodeId !== nodeId)
-        if (router.conditionNodes.length < before) return true
+        if (router.conditionNodes.length < before) {
+          if (router.conditionNodes.length > 1) return true
+          // 如果只剩一个条件节点,将路由节点删除
+          return this.deleteNode(router.nodeId!, this.processData)
+        }
         for (const c of router.conditionNodes) {
           if (this.deleteNode(nodeId, c)) return true
         }
@@ -92,7 +104,8 @@ export class ProcessHelper {
    * 打印流程结构(调试用)
    */
   public printTree(node: EasyProcess.NodeConfig = this.processData, indent = 0): void {
-    console.log(`${' '.repeat(indent)}- ${node.nodeType} (${node.nodeName})`)
+    if (indent == 0) console.log('====== 流程结构 ======')
+    console.log(`${' '.repeat(indent)}- ${node.nodeType} (${node.nodeName} | ${node.nodeId})`)
     if (node.nodeType === 'router') {
       const router = node as EasyProcess.RouterNode
       router.conditionNodes?.forEach((cond) => this.printTree(cond, indent + 2))

+ 1 - 1
src/components/business/EasyProcess/common/process.type.d.ts

@@ -41,7 +41,7 @@ export declare namespace EasyProcess {
    * 所有节点共享的核心属性。
    */
   export interface BaseNode {
-    nodeId?: string // 节点ID
+    nodeId: string // 节点ID
     nodeName: string // 节点名称
     nodeType: NodeType // 节点类型
     nodeData: Record<string, unknown> // 节点业务数据(动态配置项)