Flow ServerStarter
流程控制容器:choice

条件分支

type choice

在设计器中打开 ↗

if / else if / else

使用说明

要点:if / else if / else。when 数组每一项是 {lang, script, nodes},otherwise 是 {nodes}。
容器节点:子链写在指定属性里,生成器与设计器会按容器结构展开。详见 Flow JSON 协议。

属性

字段类型必填默认值说明
when
分支
对象数组 必填 — 每项 {lang, script, nodes}
otherwise
默认分支
JSON 对象 可选 — {nodes: [...]}

示例流程

可直接保存为 demo-choice.json 并部署;非入口节点默认套了一个 REST 入口。

Flow JSON
{
  "id": "demo-choice",
  "desc": "条件分支 示例",
  "group": "示例/流程控制",
  "schemaVersion": 1,
  "nodes": [
    {
      "id": "n1",
      "type": "rest",
      "props": {
        "method": "POST",
        "path": "/demo/choice",
        "desc": "示例入口"
      }
    },
    {
      "type": "choice",
      "props": {
        "when": [
          {
            "lang": "simple",
            "script": "${header.level} == 2",
            "nodes": [
              {
                "type": "setBody",
                "props": {
                  "lang": "constant",
                  "script": "VIP 客户"
                },
                "id": "n2"
              }
            ]
          },
          {
            "lang": "simple",
            "script": "${header.level} == 1",
            "nodes": [
              {
                "type": "setBody",
                "props": {
                  "lang": "constant",
                  "script": "白银客户"
                },
                "id": "n3"
              }
            ]
          }
        ],
        "otherwise": {
          "nodes": [
            {
              "type": "setBody",
              "props": {
                "lang": "constant",
                "script": "普通客户"
              },
              "id": "n4"
            }
          ]
        }
      },
      "id": "n1"
    }
  ]
}
部署这段流程
curl -X POST 'http://localhost:8086/flow' \
  -H 'Content-Type: application/json' \
  -H 'X-Flow-Token: change-me' \
  --data-binary @demo-choice.json

调用方式

调用示例
curl -X POST 'http://localhost:8086/demo/choice' \
  -H 'Content-Type: application/json' \
  -d '{}'

示例按 Header level 分流;可在调用时加 -H "level: 2"。

相关节点