From 5b5772e259f46eea3f85943a9030879a6d9343e2 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 27 Nov 2021 12:01:19 +0800 Subject: [PATCH] =?UTF-8?q?Next:=20=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/workflows/assign.wflow.json | 18 ++++++++++---- workflow/types.go | 14 ++++++++--- workflow/workflow.go | 41 +++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/tests/workflows/assign.wflow.json b/tests/workflows/assign.wflow.json index 5d94a53f..c328dba0 100644 --- a/tests/workflows/assign.wflow.json +++ b/tests/workflows/assign.wflow.json @@ -53,15 +53,24 @@ "props": { "src": "/workflows/assign/approve.html" } }, "actions": ["驳回并重填", "驳回并关闭", "同意"], - "user": { "process": "flows.select.manager", "args": [2] } + "user": { "process": "flows.select.manager", "args": [2] }, + "next": [ + { + "when": [{ "审批通过": "{{$data.审批结果}}", "=": "通过" }], + "goto": "审批通过" + }, + { + "when": [{ "审批通过": "{{$data.审批结果}}", "=": "驳回" }], + "goto": "审批驳回" + } + ] }, { "name": "审批通过", - "when": [{ "审批通过": "{{$out.审批结果}}", "=": "通过" }], "body": { "type": "markdown", "props": { - "content": "审批通过, {{$out.项目名称}}商务负责人为: **{{$out.商务负责人名称}}**" + "content": "审批通过, {{$data.项目名称}}商务负责人为: **{{$data.商务负责人名称}}**" } }, "actions": ["关闭"], @@ -69,11 +78,10 @@ }, { "name": "审批驳回", - "when": [{ "审批通过": "{{$out.审批结果}}", "=": "驳回" }], "body": { "type": "markdown", "props": { - "content": "您的请求被驳回, 驳回原因: **{{$out.驳回原因}}**" + "content": "您的请求被驳回, 驳回原因: **{{$data.驳回原因}}**" } }, "actions": ["关闭"], diff --git a/workflow/types.go b/workflow/types.go index 4e00d38a..f30205f7 100644 --- a/workflow/types.go +++ b/workflow/types.go @@ -19,11 +19,17 @@ type WorkFlow struct { // Node 工作流节点 type Node struct { - Name string `json:"name"` - Body share.Render `json:"body,omitempty"` + Name string `json:"name"` + Body share.Render `json:"body,omitempty"` + Actions []string `json:"actions,omitempty"` + User User `json:"user,omitempty"` + Next []Next `json:"next,omitempty"` +} + +// Next 下一个节点描述 +type Next struct { Conditions []helper.Condition `json:"when,omitempty"` - Actions []string `json:"actions,omitempty"` - User User `json:"user,omitempty"` + Goto string `json:"goto,omitempty"` } // User 工作流相关用户读取条件 diff --git a/workflow/workflow.go b/workflow/workflow.go index a32c7cf4..bfc8bec3 100644 --- a/workflow/workflow.go +++ b/workflow/workflow.go @@ -240,6 +240,7 @@ func (workflow *WorkFlow) Next(uid int, id int, output map[string]interface{}) m "$input": wflow["input"], "$out": output, "$outupt": output, + "$data": output, } nextNode := workflow.nextNode(currNode, data) nextUID := nextNode.GetUID() @@ -278,25 +279,34 @@ func (node *Node) GetUID() int { // nextNode 查找下一个节点 func (workflow *WorkFlow) nextNode(currentNode string, data map[string]interface{}) *Node { - next := -1 + var curr *Node + nextIndex := -1 for i, node := range workflow.Nodes { if node.Name == currentNode { - next = i + 1 + nextIndex = i + 1 + curr = &node break } } - if next < 0 { + if nextIndex < 0 { exception.New("流程数据异常: 未找到工作流节点", 500).Ctx(currentNode).Throw() } + + if nextIndex == workflow.Len() { + exception.New("流程数据异常: 当前节点为最后一个节点", 500).Ctx(currentNode).Throw() + } + + // 未声明 Next 节点, 转到下一个节点 + if curr.Next == nil { + return &workflow.Nodes[nextIndex] + } + + // 声明 Next 节点, 按条件到指定节点 data = maps.Of(data).Dot() - for i := next; i < workflow.Len(); i++ { - node := workflow.Nodes[i] - if node.Conditions == nil || len(node.Conditions) == 0 { - return &node - } - // 替换数据中的变量 + nextNode := "" + for _, next := range curr.Next { conditions := []helper.Condition{} - for _, cond := range node.Conditions { + for _, cond := range next.Conditions { if left, ok := cond.Left.(string); ok { cond.Left = gshare.Bind(left, data) } @@ -306,11 +316,16 @@ func (workflow *WorkFlow) nextNode(currentNode string, data map[string]interface conditions = append(conditions, cond) } if helper.When(conditions) { - return &node + nextNode = next.Goto + for i := nextIndex; i < workflow.Len(); i++ { + node := workflow.Nodes[i] + if node.Name == nextNode { + return &node + } + } } } - - exception.New("流程数据异常: 未找到符合条件的工作流节点", 500).Ctx(currentNode).Throw() + exception.New("流程数据异常: 未找到符合条件的工作流节点", 500).Ctx(map[string]interface{}{"current": currentNode, "next": nextNode, "data": data}).Throw() return nil }