IF / Switch · 条件分支
第 4 章 · 第 1 节
线性工作流(A → B → C)只能解决一类场景。真实业务永远充满”如果……否则……”——这一节讲 IF 和 Switch 两个最常用的分支节点。
IF 节点:二分
Section titled “IF 节点:二分”IF 节点判断一个条件,输出两条线:True(走 ✓ 口)和 False(走 ✗ 口)。
🔀 if-canvas.txt
┌─── True ──→ [Send Notification]
[Get Data] ──▶│ IF (条件)│
└── False ──→ [Log Error]
⚙ if-config.txt
Conditions: Number
Value 1: {{ $json.score }}
Operator: >
Value 2: 60
Combine: AND (多个条件时的合并方式)
支持的比较类型
Section titled “支持的比较类型”| 数据类型 | 操作符 |
|---|---|
| Number | =、≠、>、<、≥、≤ |
| String | =、≠、contains、starts with、ends with、is empty、regex match |
| Boolean | is true、is false |
| Date & Time | is before、is after、is exactly |
| Array / Object | is empty、contains、length = |
多条件:AND vs OR
Section titled “多条件:AND vs OR”可以加多个条件,用 AND 或 OR 合并。AND = 全都满足;OR = 任一满足。
🎯 multi-condition.txt
场景:只通知 VIP 用户且积分 > 100 的
Conditions: AND
- {{ $json.tier }} = "VIP"
- {{ $json.score }} > 100
或者(OR):高积分用户 或 VIP 用户都通知
Conditions: OR
- {{ $json.tier }} = "VIP"
- {{ $json.score }} > 500
Switch 节点:多分
Section titled “Switch 节点:多分”Switch 像 switch...case 语句——根据一个值匹配多个分支:
🔀 switch-canvas.txt
┌── case "pro" ──→ [Pro Workflow]
[Get User] ───▶ │ Switch │── case "free" ──→ [Free Workflow]
│ $json │── case "trial" ──→ [Trial Workflow]
│ .tier │── fallback ──→ [Default Workflow]
└────────┘
| Mode | 说明 |
|---|---|
| Rules | 每个分支独立写规则(最灵活) |
| Expression | 根据一个表达式的求值结果分发到对应输出口 |
Rules 模式(推荐):
⚙ switch-rules.txt
Mode: Rules
Output 0:
Conditions: {{ $json.tier }} = "pro"
Output 1:
Conditions: {{ $json.tier }} = "free"
Output 2:
Conditions: {{ $json.tier }} = "trial"
Fallback Output: Extra Output (没匹配上的走最后一个)
IF vs Switch · 何时用哪个
Section titled “IF vs Switch · 何时用哪个”| 场景 | 用 |
|---|---|
| 是 / 否 二分 | IF |
| 多个互斥的分支(按某字段值) | Switch |
| 多个并行条件(不一定互斥) | 多个 IF 并联,或用 Filter 节点 |
一些常见模式
Section titled “一些常见模式”模式 1 · 早返回
Section titled “模式 1 · 早返回” 📍 early-return.txt
[Webhook] → [IF (数据合法?)] ──┬── True ──→ 继续处理
└── False ──→ [Stop and Error]
直接停止,避免污染下游
模式 2 · 数据分流
Section titled “模式 2 · 数据分流” 📍 split-routing.txt
[Get Orders] → [Switch (按订单金额)]
├── < $100 → [Standard Process]
├── $100~$1000 → [Manager Approval]
└── > $1000 → [VP Approval]
模式 3 · IF + Merge(两条线合流)
Section titled “模式 3 · IF + Merge(两条线合流)” 📍 if-merge.txt
[Get User] → [IF (paid?)] ── True ──→ [Add Premium Badge] ──┐
└─ False ──→ [Add Free Badge] ─┤── Merge ── [Send Email]
┘
两条线都跑完后用 Merge 合流,再走下游。
本节要点回顾
Section titled “本节要点回顾”- IF = 二分;Switch = 多分(一对多)
- 多条件用 AND(全满足)或 OR(任一满足)
- IF 后空 items 下游会”执行 0 次”,副作用节点要小心
- 复杂分流用 Switch,避免 IF 套 IF 套 IF
下一节Merge 节点——把分开的支流合回来,这是 IF 后续最常见的搭档。