gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<script setup lang="ts">
import type { Ref } from 'vue';
 
import type { RouterSetting, SimpleFlowNode } from '../../consts';
 
import { inject, ref } from 'vue';
 
import { useVbenDrawer } from '..\..\..\..\..\..\packages\effects\common-ui\src';
import { BpmNodeTypeEnum } from '..\..\..\..\..\..\packages\constants\src';
import { IconifyIcon } from '..\..\..\..\..\..\packages\icons\src';
 
import {
  Button,
  Card,
  Col,
  Form,
  FormItem,
  Input,
  message,
  Row,
  Select,
  SelectOption,
} from 'ant-design-vue';
 
import { ConditionType } from '../../consts';
import { useNodeName, useWatchNode } from '../../helpers';
import Condition from './modules/condition.vue';
 
defineOptions({ name: 'RouterNodeConfig' });
 
const props = defineProps({
  flowNode: {
    type: Object as () => SimpleFlowNode,
    required: true,
  },
});
 
const processNodeTree = inject<Ref<SimpleFlowNode>>('processNodeTree');
 
/** 当前节点 */
const currentNode = useWatchNode(props);
/** 节点名称 */
const { nodeName, showInput, clickIcon, changeNodeName, inputRef } =
  useNodeName(BpmNodeTypeEnum.ROUTER_BRANCH_NODE);
function setInputRef(el: unknown) {
  inputRef.value = el as HTMLInputElement | null;
}
const routerGroups = ref<RouterSetting[]>([]);
const nodeOptions = ref<any[]>([]);
const conditionRef = ref<any[]>([]);
const formRef = ref();
 
/** 校验节点配置 */
async function validateConfig() {
  // 校验路由分支选择
  const routeIdValid = await formRef.value.validate().catch(() => false);
  if (!routeIdValid) {
    message.warning('请配置路由目标节点');
    return false;
  }
 
  // 校验条件规则
  let valid = true;
  for (const item of conditionRef.value) {
    if (item && !(await item.validate())) {
      valid = false;
    }
  }
  if (!valid) return false;
 
  // 获取节点显示文本,如果为空,校验不通过
  const showText = getShowText();
  if (!showText) return false;
 
  return true;
}
 
/** 保存配置 */
async function saveConfig() {
  // 校验配置
  if (!(await validateConfig())) {
    return false;
  }
  // 保存配置
  currentNode.value.name = nodeName.value!;
  currentNode.value.showText = getShowText();
  currentNode.value.routerGroups = routerGroups.value;
  drawerApi.close();
  return true;
}
 
const [Drawer, drawerApi] = useVbenDrawer({
  title: nodeName.value,
  onConfirm: saveConfig,
});
 
/** 打开路由节点配置抽屉,由父组件调用 */
function openDrawer(node: SimpleFlowNode) {
  nodeOptions.value = [];
  getRouterNode(processNodeTree?.value);
  routerGroups.value = [];
  nodeName.value = node.name;
  if (node.routerGroups) {
    routerGroups.value = node.routerGroups;
  }
  drawerApi.open();
}
 
/** 获取显示文本 */
function getShowText() {
  if (
    !routerGroups.value ||
    !Array.isArray(routerGroups.value) ||
    routerGroups.value.length <= 0
  ) {
    message.warning('请配置路由!');
    return '';
  }
  for (const route of routerGroups.value) {
    if (!route.nodeId || !route.conditionType) {
      message.warning('请完善路由配置项!');
      return '';
    }
    if (
      route.conditionType === ConditionType.EXPRESSION &&
      !route.conditionExpression
    ) {
      message.warning('请完善路由配置项!');
      return '';
    }
    if (route.conditionType === ConditionType.RULE) {
      for (const condition of route.conditionGroups.conditions) {
        for (const rule of condition.rules) {
          if (!rule.leftSide || !rule.rightSide) {
            message.warning('请完善路由配置项!');
            return '';
          }
        }
      }
    }
  }
  return `${routerGroups.value.length}条路由分支`;
}
 
/** 添加路由分支 */
function addRouterGroup() {
  routerGroups.value.push({
    nodeId: undefined,
    conditionType: ConditionType.RULE,
    conditionExpression: '',
    conditionGroups: {
      and: true,
      conditions: [
        {
          and: true,
          rules: [
            {
              opCode: '==',
              leftSide: undefined,
              rightSide: '',
            },
          ],
        },
      ],
    },
  });
}
 
/** 删除路由分支 */
function deleteRouterGroup(index: number) {
  routerGroups.value.splice(index, 1);
}
 
/** 递归获取所有节点 */
function getRouterNode(node: any) {
  // TODO 最好还需要满足以下要求
  // 并行分支、包容分支内部节点不能跳转到外部节点
  // 条件分支节点可以向上跳转到外部节点
  while (true) {
    if (!node) break;
    if (
      node.type !== BpmNodeTypeEnum.ROUTER_BRANCH_NODE &&
      node.type !== BpmNodeTypeEnum.CONDITION_NODE
    ) {
      nodeOptions.value.push({
        label: node.name,
        value: node.id,
      });
    }
    if (!node.childNode || node.type === BpmNodeTypeEnum.END_EVENT_NODE) {
      break;
    }
    if (node.conditionNodes && node.conditionNodes.length > 0) {
      node.conditionNodes.forEach((item: any) => {
        getRouterNode(item);
      });
    }
    node = node.childNode;
  }
}
 
defineExpose({ openDrawer }); // 暴露方法给父组件
</script>
<template>
  <Drawer class="w-2/5">
    <template #title>
      <div class="flex items-center">
        <Input
          :ref="setInputRef"
          v-if="showInput"
          type="text"
          class="mr-2 w-48"
          @blur="changeNodeName()"
          @press-enter="changeNodeName()"
          v-model:value="nodeName"
          :placeholder="nodeName"
        />
        <div
          v-else
          class="flex cursor-pointer items-center"
          @click="clickIcon()"
        >
          {{ nodeName }}
          <IconifyIcon class="ml-1" icon="lucide:edit-3" />
        </div>
      </div>
    </template>
 
    <Form ref="formRef" :model="{ routerGroups }">
      <Card
        :body-style="{ padding: '10px' }"
        class="mt-4"
        v-for="(item, index) in routerGroups"
        :key="index"
      >
        <template #title>
          <div class="flex h-16 w-full items-center justify-between">
            <div class="flex items-center font-normal">
              <span class="font-medium">路由{{ index + 1 }}</span>
              <FormItem
                class="mb-0 ml-4 inline-block w-48"
                :name="['routerGroups', index, 'nodeId']"
                :rules="{
                  required: true,
                  message: '路由目标节点不能为空',
                  trigger: 'change',
                }"
              >
                <Select
                  v-model:value="item.nodeId"
                  placeholder="请选择路由目标节点"
                  allow-clear
                >
                  <SelectOption
                    v-for="node in nodeOptions"
                    :key="node.value"
                    :value="node.value"
                  >
                    {{ node.label }}
                  </SelectOption>
                </Select>
              </FormItem>
            </div>
            <Button
              v-if="routerGroups.length > 1"
              shape="circle"
              class="flex items-center justify-center"
              @click="deleteRouterGroup(index)"
            >
              <template #icon>
                <IconifyIcon icon="lucide:x" />
              </template>
            </Button>
          </div>
        </template>
        <Condition
          :ref="(el) => (conditionRef[index] = el)"
          :model-value="routerGroups[index]"
          @update:model-value="(val) => (routerGroups[index] = val)"
        />
      </Card>
    </Form>
 
    <Row class="mt-4">
      <Col :span="24">
        <Button
          class="flex items-center p-0"
          type="link"
          @click="addRouterGroup"
        >
          <template #icon>
            <IconifyIcon icon="lucide:settings" />
          </template>
          新增路由分支
        </Button>
      </Col>
    </Row>
  </Drawer>
</template>