gbflowengine 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +270 -2
- package/dist/gbflowengine.css +1 -0
- package/dist/gbflowengine.js +19682 -0
- package/dist/gbflowengine.js.map +1 -0
- package/dist/gbflowengine.umd.cjs +5 -0
- package/dist/gbflowengine.umd.cjs.map +1 -0
- package/package.json +83 -4
- package/src/center/DefinitionInspect.vue +291 -0
- package/src/center/FlowCenter.vue +33 -0
- package/src/center/index.js +29 -0
- package/src/center/storage.js +748 -0
- package/src/core/flow/condition.js +160 -0
- package/src/core/flow/customNode.js +385 -0
- package/src/core/flow/defaults.js +124 -0
- package/src/core/flow/flowConstant.js +265 -0
- package/src/core/flow/flowNodeUtils.js +30 -0
- package/src/core/flow/index.js +47 -0
- package/src/core/flow/node.js +44 -0
- package/src/core/flow/nodeFactory.js +58 -0
- package/src/core/flow/prediction.js +325 -0
- package/src/core/flow/runtime/auto.js +83 -0
- package/src/core/flow/runtime/decide.js +329 -0
- package/src/core/flow/runtime/event.js +28 -0
- package/src/core/flow/runtime/execute.js +357 -0
- package/src/core/flow/runtime/helpers.js +52 -0
- package/src/core/flow/runtime/query.js +146 -0
- package/src/core/flow/runtime/reject.js +137 -0
- package/src/core/flow/runtime/token.js +269 -0
- package/src/core/flow/runtime.js +129 -0
- package/src/core/flow/schema.js +864 -0
- package/src/definition/FlowDefinitionDesigner.vue +338 -0
- package/src/definition/index.js +9 -0
- package/src/index.js +39 -0
- package/src/storage.js +200 -0
- package/src/testing/FlowTestPanel.vue +2502 -0
- package/src/testing/UserConfigDrawer.vue +376 -0
- package/src/testing/diagram/FlowProgressDiagram.vue +318 -0
- package/src/testing/diagramModel.js +517 -0
- package/src/testing/index.js +23 -0
- package/src/testing/mock.js +246 -0
- package/src/testing/oaExecutors.js +51 -0
- package/src/testing/oaSeed.js +467 -0
- package/src/testing/sample.js +412 -0
- package/src/testing/seed.js +122 -0
- package/src/utils/format.js +27 -0
- package/src/visual/FlowCanvas.vue +441 -0
- package/src/visual/NodeProgressDetail.vue +43 -0
- package/src/visual/NodeStatusBadge.vue +17 -0
- package/src/visual/NodeWrap.vue +47 -0
- package/src/visual/ScWorkflow.vue +50 -0
- package/src/visual/SelectDialog.vue +460 -0
- package/src/visual/conditionHelpers.js +117 -0
- package/src/visual/index.js +6 -0
- package/src/visual/nodes/AddNode.vue +223 -0
- package/src/visual/nodes/ApproverNode.vue +348 -0
- package/src/visual/nodes/BranchNode.vue +442 -0
- package/src/visual/nodes/CustomNode.vue +339 -0
- package/src/visual/nodes/EndNode.vue +22 -0
- package/src/visual/nodes/PassNode.vue +101 -0
- package/src/visual/nodes/PromoterNode.vue +135 -0
- package/src/visual/nodes/RejectNode.vue +101 -0
- package/src/visual/nodes/SendNode.vue +152 -0
- package/src/visual/styles.scss +879 -0
- package/src/visual/useScNodeStatus.js +18 -0
- package/src/workbench/OaWorkbench.vue +1341 -0
- package/src/workbench/components/ApprovalWorkspace.vue +2090 -0
- package/src/workbench/components/CategoryIcon.vue +74 -0
- package/src/workbench/components/NotificationIcon.vue +73 -0
- package/src/workbench/components/ProcessSteps.vue +557 -0
- package/src/workbench/components/ResubmitDialog.vue +227 -0
- package/src/workbench/components/SidebarNav.vue +138 -0
- package/src/workbench/components/StartFlowDialog.vue +1058 -0
- package/src/workbench/components/UserAvatar.vue +68 -0
- package/src/workbench/custom-tasks.js +130 -0
- package/src/workbench/model.js +1080 -0
- package/src/workbench/pages/DesignPage.vue +798 -0
- package/src/workbench/pages/HomePage.vue +1050 -0
- package/src/workbench/pages/MonitorPage.vue +1029 -0
- package/src/workbench/pages/NotificationsPage.vue +554 -0
- package/src/workbench/pages/RequestsPage.vue +644 -0
- package/src/workbench/pages/TaskCenterPage.vue +1155 -0
- package/src/workbench/pages/TemplatesPage.vue +680 -0
- package/src/workbench/persistence.js +89 -0
- package/src/workbench/shared.js +18 -0
- package/src/workbench/styles.scss +555 -0
- package/src/workbench/useWorkbench.js +955 -0
- package/types/center.d.ts +5 -0
- package/types/core.d.ts +479 -0
- package/types/definition.d.ts +4 -0
- package/types/index.d.ts +34 -0
- package/types/testing.d.ts +20 -0
- package/types/visual.d.ts +20 -0
|
@@ -0,0 +1,864 @@
|
|
|
1
|
+
import { FlowNodeType, MAX_NODE_COUNT } from './flowConstant.js'
|
|
2
|
+
import {
|
|
3
|
+
createNodeDefaults,
|
|
4
|
+
isBranchNode,
|
|
5
|
+
isTerminalNode,
|
|
6
|
+
getNodeTypeLabel
|
|
7
|
+
} from './defaults.js'
|
|
8
|
+
import { generateNodeId } from './nodeFactory.js'
|
|
9
|
+
import {
|
|
10
|
+
normalizeCustomNodeConfig,
|
|
11
|
+
validateCustomNodeConfig
|
|
12
|
+
} from './customNode.js'
|
|
13
|
+
import { ensureEndNode } from './node.js'
|
|
14
|
+
import {
|
|
15
|
+
enrichFormVariables,
|
|
16
|
+
getOperatorsForDataType,
|
|
17
|
+
normalizeForm,
|
|
18
|
+
validateForm
|
|
19
|
+
} from 'gbformdesign/core'
|
|
20
|
+
|
|
21
|
+
export const SCHEMA_VERSION = '1.0.0'
|
|
22
|
+
export const SCHEMA_KIND = 'gbflow-definition'
|
|
23
|
+
export const WORKFLOW_KIND = 'gbflow-workflow'
|
|
24
|
+
export const FORM_KIND = 'gbflow-form'
|
|
25
|
+
|
|
26
|
+
const NODE_TYPES = new Set(Object.values(FlowNodeType))
|
|
27
|
+
const OPERATOR_ALIASES = {
|
|
28
|
+
'>': '>',
|
|
29
|
+
'<': '<',
|
|
30
|
+
'>=': '>=',
|
|
31
|
+
'<=': '<=',
|
|
32
|
+
'≠': '!='
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isPlainObject(value) {
|
|
36
|
+
return !!value && typeof value === 'object' && !Array.isArray(value)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function clone(value) {
|
|
40
|
+
if (value === undefined) return undefined
|
|
41
|
+
return JSON.parse(JSON.stringify(value))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function decodeOperator(operator) {
|
|
45
|
+
if (typeof operator !== 'string') return operator
|
|
46
|
+
return OPERATOR_ALIASES[operator] || operator
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function assignMissing(target, defaults) {
|
|
50
|
+
Object.keys(defaults).forEach((key) => {
|
|
51
|
+
if (target[key] === undefined) target[key] = defaults[key]
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function normalizeCondition(condition) {
|
|
56
|
+
const source = isPlainObject(condition) ? condition : {}
|
|
57
|
+
return {
|
|
58
|
+
variable: source.variable || '',
|
|
59
|
+
variableName: source.variableName || '',
|
|
60
|
+
operator: decodeOperator(source.operator) || '==',
|
|
61
|
+
value: source.value === undefined ? '' : source.value,
|
|
62
|
+
logic: source.logic === 'OR' ? 'OR' : 'AND',
|
|
63
|
+
...(source.fieldType ? { fieldType: source.fieldType } : {})
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function normalizeUserList(node) {
|
|
68
|
+
if (Array.isArray(node.userList)) {
|
|
69
|
+
node.userList = node.userList.filter((id) => id !== null && id !== undefined && id !== '')
|
|
70
|
+
} else {
|
|
71
|
+
node.userList = []
|
|
72
|
+
}
|
|
73
|
+
if (!isPlainObject(node.userNickNames)) node.userNickNames = {}
|
|
74
|
+
if (!isPlainObject(node.deptNameMap)) node.deptNameMap = {}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizeRoleList(node) {
|
|
78
|
+
if (Array.isArray(node.roleList)) {
|
|
79
|
+
node.roleList = node.roleList.filter((id) => id !== null && id !== undefined && id !== '')
|
|
80
|
+
} else {
|
|
81
|
+
node.roleList = []
|
|
82
|
+
}
|
|
83
|
+
if (!isPlainObject(node.roleNameMap)) node.roleNameMap = {}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Normalizes one node and its descendants in place. Repairs are descriptions
|
|
88
|
+
* of structural fixes applied while importing untrusted JSON.
|
|
89
|
+
*/
|
|
90
|
+
function normalizeNode(node, ctx) {
|
|
91
|
+
if (!isPlainObject(node)) return null
|
|
92
|
+
if (ctx.seen.has(node)) {
|
|
93
|
+
ctx.repairs.push('检测到循环引用,已断开重复节点')
|
|
94
|
+
return null
|
|
95
|
+
}
|
|
96
|
+
ctx.seen.add(node)
|
|
97
|
+
|
|
98
|
+
if (!node.type || !NODE_TYPES.has(node.type)) {
|
|
99
|
+
ctx.repairs.push(`未知节点类型「${node.type || '空'}」,已按审批节点处理`)
|
|
100
|
+
node.type = FlowNodeType.APPROVE
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (node.nodeId === undefined || node.nodeId === null || node.nodeId === '') {
|
|
104
|
+
node.nodeId = generateNodeId()
|
|
105
|
+
ctx.repairs.push(`节点「${node.nodeName || node.type}」缺少 nodeId,已自动生成`)
|
|
106
|
+
}
|
|
107
|
+
let idKey = String(node.nodeId)
|
|
108
|
+
if (ctx.ids.has(idKey)) {
|
|
109
|
+
const nextId = generateNodeId()
|
|
110
|
+
ctx.repairs.push(`节点 ID「${idKey}」重复,已重新分配为 ${nextId}`)
|
|
111
|
+
node.nodeId = nextId
|
|
112
|
+
idKey = String(nextId)
|
|
113
|
+
}
|
|
114
|
+
ctx.ids.add(idKey)
|
|
115
|
+
|
|
116
|
+
const defaults = createNodeDefaults(node.type)
|
|
117
|
+
assignMissing(node, clone(defaults))
|
|
118
|
+
|
|
119
|
+
if (!node.nodeName) node.nodeName = getNodeTypeLabel(node.type)
|
|
120
|
+
|
|
121
|
+
if (node.type === FlowNodeType.APPROVE) {
|
|
122
|
+
normalizeUserList(node)
|
|
123
|
+
normalizeRoleList(node)
|
|
124
|
+
if (!node.signMode) node.signMode = 'and'
|
|
125
|
+
if (!node.approverType) node.approverType = 'user'
|
|
126
|
+
if (!node.superiorLevel || node.superiorLevel < 1) node.superiorLevel = 1
|
|
127
|
+
if (node.allowTransfer === undefined) node.allowTransfer = true
|
|
128
|
+
if (!node.rejectStrategy) node.rejectStrategy = 'terminate'
|
|
129
|
+
if (node.rejectTargetNodeId === undefined) node.rejectTargetNodeId = null
|
|
130
|
+
const retryLimit = Number(node.rejectRetryLimit)
|
|
131
|
+
node.rejectRetryLimit =
|
|
132
|
+
Number.isFinite(retryLimit) && retryLimit >= 0 ? Math.floor(retryLimit) : 0
|
|
133
|
+
node.termAuto = node.termAuto === true
|
|
134
|
+
const term = Number(node.term)
|
|
135
|
+
node.term = Number.isFinite(term) && term > 0 ? term : 0
|
|
136
|
+
node.termMode = node.termMode === 1 ? 1 : 0
|
|
137
|
+
if (!node.termAuto) node.term = 0
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (node.type === FlowNodeType.CC) {
|
|
141
|
+
normalizeUserList(node)
|
|
142
|
+
if (node.allowInitiatorAddUsers === undefined) node.allowInitiatorAddUsers = true
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (node.type === FlowNodeType.CUSTOM) {
|
|
146
|
+
node.custom = normalizeCustomNodeConfig(node.custom)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (node.type === FlowNodeType.CONDITION) {
|
|
150
|
+
if (!Array.isArray(node.conditions)) node.conditions = []
|
|
151
|
+
node.conditions = node.conditions.map(normalizeCondition)
|
|
152
|
+
node.isDefaultCondition = !!node.isDefaultCondition
|
|
153
|
+
if (node.isDefaultCondition && node.conditions.length) {
|
|
154
|
+
node.conditions = []
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (node.type === FlowNodeType.INCLUSIVE_BRANCH) {
|
|
159
|
+
if (!Array.isArray(node.conditions)) node.conditions = []
|
|
160
|
+
node.conditions = node.conditions.map(normalizeCondition)
|
|
161
|
+
node.isDefaultBranch = !!node.isDefaultBranch
|
|
162
|
+
if (node.isDefaultBranch && node.conditions.length) node.conditions = []
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (isBranchNode(node)) {
|
|
166
|
+
if (!Array.isArray(node.branchList)) node.branchList = []
|
|
167
|
+
const expectedType =
|
|
168
|
+
node.type === FlowNodeType.BRANCH
|
|
169
|
+
? FlowNodeType.CONDITION
|
|
170
|
+
: node.type === FlowNodeType.PARALLEL
|
|
171
|
+
? FlowNodeType.PARALLEL_BRANCH
|
|
172
|
+
: FlowNodeType.INCLUSIVE_BRANCH
|
|
173
|
+
|
|
174
|
+
node.branchList = node.branchList.map((branch) => {
|
|
175
|
+
const normalized = isPlainObject(branch) ? branch : {}
|
|
176
|
+
if (normalized.type !== expectedType) {
|
|
177
|
+
ctx.repairs.push(`分支「${normalized.nodeName || '未命名'}」类型已修正为 ${expectedType}`)
|
|
178
|
+
normalized.type = expectedType
|
|
179
|
+
}
|
|
180
|
+
return normalizeNode(normalized, ctx)
|
|
181
|
+
}).filter(Boolean)
|
|
182
|
+
|
|
183
|
+
node.branchList.forEach((branch, index) => {
|
|
184
|
+
if (branch.type === FlowNodeType.PARALLEL_BRANCH || branch.type === FlowNodeType.INCLUSIVE_BRANCH) {
|
|
185
|
+
branch.branchIndex = index
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
if (node.type === FlowNodeType.BRANCH && node.branchList.length) {
|
|
190
|
+
const defaultIndexes = node.branchList
|
|
191
|
+
.map((branch, index) => (branch.isDefaultCondition ? index : -1))
|
|
192
|
+
.filter((index) => index >= 0)
|
|
193
|
+
if (defaultIndexes.length > 1) {
|
|
194
|
+
defaultIndexes.slice(1).forEach((index) => {
|
|
195
|
+
node.branchList[index].isDefaultCondition = false
|
|
196
|
+
})
|
|
197
|
+
ctx.repairs.push('条件分支存在多个「其他条件」,仅保留最后一个')
|
|
198
|
+
}
|
|
199
|
+
const defaultIndex = node.branchList.findIndex((branch) => branch.isDefaultCondition)
|
|
200
|
+
if (defaultIndex === -1) {
|
|
201
|
+
node.branchList.push({
|
|
202
|
+
nodeId: generateNodeId(),
|
|
203
|
+
nodeName: '其他条件',
|
|
204
|
+
type: FlowNodeType.CONDITION,
|
|
205
|
+
conditions: [],
|
|
206
|
+
isDefaultCondition: true,
|
|
207
|
+
childNode: null
|
|
208
|
+
})
|
|
209
|
+
ctx.repairs.push('条件分支缺少「其他条件」,已自动补充为最后一条')
|
|
210
|
+
} else if (defaultIndex !== node.branchList.length - 1) {
|
|
211
|
+
const [defaultBranch] = node.branchList.splice(defaultIndex, 1)
|
|
212
|
+
node.branchList.push(defaultBranch)
|
|
213
|
+
ctx.repairs.push('「其他条件」已移动到条件分支的最后一位')
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (node.childNode !== null && node.childNode !== undefined) {
|
|
219
|
+
node.childNode = normalizeNode(node.childNode, ctx)
|
|
220
|
+
} else {
|
|
221
|
+
node.childNode = null
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return node
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function normalizeWorkflow(workflow, options = {}) {
|
|
228
|
+
if (!isPlainObject(workflow)) {
|
|
229
|
+
throw new Error('流程结构必须是对象')
|
|
230
|
+
}
|
|
231
|
+
const source = clone(workflow)
|
|
232
|
+
const repairs = []
|
|
233
|
+
const normalized = normalizeNode(source, { ids: new Set(), seen: new WeakSet(), repairs })
|
|
234
|
+
if (!normalized) throw new Error('流程结构为空')
|
|
235
|
+
if (normalized.type !== FlowNodeType.START) {
|
|
236
|
+
throw new Error('流程根节点必须是开始节点(start)')
|
|
237
|
+
}
|
|
238
|
+
if (options.ensureEnd !== false) {
|
|
239
|
+
ensureEndNode(normalized)
|
|
240
|
+
}
|
|
241
|
+
return { workflow: normalized, repairs }
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function createDefinition({ workflow, form, meta } = {}) {
|
|
245
|
+
const now = new Date().toISOString()
|
|
246
|
+
const normalizedMeta = {
|
|
247
|
+
id: meta?.id || `flow_${Date.now().toString(36)}`,
|
|
248
|
+
name: meta?.name || '未命名流程',
|
|
249
|
+
description: meta?.description || '',
|
|
250
|
+
tags: Array.isArray(meta?.tags) ? meta.tags : [],
|
|
251
|
+
createdAt: meta?.createdAt || now,
|
|
252
|
+
updatedAt: now,
|
|
253
|
+
...meta
|
|
254
|
+
}
|
|
255
|
+
const normalizedWorkflow = workflow
|
|
256
|
+
? normalizeWorkflow(workflow).workflow
|
|
257
|
+
: normalizeWorkflow({
|
|
258
|
+
nodeId: generateNodeId(),
|
|
259
|
+
nodeName: '发起人',
|
|
260
|
+
type: FlowNodeType.START,
|
|
261
|
+
childNode: null
|
|
262
|
+
}).workflow
|
|
263
|
+
const formResult = normalizeForm(form)
|
|
264
|
+
return {
|
|
265
|
+
schemaVersion: SCHEMA_VERSION,
|
|
266
|
+
kind: SCHEMA_KIND,
|
|
267
|
+
meta: normalizedMeta,
|
|
268
|
+
workflow: normalizedWorkflow,
|
|
269
|
+
form: { components: formResult.components, options: formResult.options }
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function extractMeta(input) {
|
|
274
|
+
const basic = input.basicInfo || input.meta || {}
|
|
275
|
+
return {
|
|
276
|
+
id: basic.id || input.id,
|
|
277
|
+
name: basic.name || basic.flowName || basic.processName || input.flowName || input.name,
|
|
278
|
+
description: basic.description || input.description,
|
|
279
|
+
groupId: basic.groupId ?? input.groupId,
|
|
280
|
+
tags: basic.tags || input.tags
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function stripMetaUndefined(meta) {
|
|
285
|
+
return Object.keys(meta).reduce((result, key) => {
|
|
286
|
+
if (meta[key] !== undefined && meta[key] !== null) result[key] = meta[key]
|
|
287
|
+
return result
|
|
288
|
+
}, {})
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function parseJSONIfNeeded(input) {
|
|
292
|
+
if (typeof input !== 'string') return input
|
|
293
|
+
const text = input.replace(/^\uFEFF/, '').trim()
|
|
294
|
+
if (!text) throw new Error('导入内容为空')
|
|
295
|
+
try {
|
|
296
|
+
return JSON.parse(text)
|
|
297
|
+
} catch (error) {
|
|
298
|
+
throw new Error(`JSON 解析失败:${error.message}`)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Imports from a GBflow document, a raw node tree, a raw component array,
|
|
304
|
+
* or the original RuoYi { flowStructure, formStructure } record.
|
|
305
|
+
*/
|
|
306
|
+
export function importDefinition(input) {
|
|
307
|
+
const raw = parseJSONIfNeeded(input)
|
|
308
|
+
if (!isPlainObject(raw) && !Array.isArray(raw)) {
|
|
309
|
+
throw new Error('无法识别的导入内容')
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (Array.isArray(raw)) {
|
|
313
|
+
const form = normalizeForm(raw)
|
|
314
|
+
const definition = createDefinition({ form: form.components, meta: { name: '导入表单' } })
|
|
315
|
+
return {
|
|
316
|
+
document: definition,
|
|
317
|
+
repairs: form.repairs,
|
|
318
|
+
validation: validateDefinition(definition, { strict: false })
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (raw.nodeConfig) {
|
|
323
|
+
const workflowSource =
|
|
324
|
+
typeof raw.nodeConfig === 'string' ? parseJSONIfNeeded(raw.nodeConfig) : raw.nodeConfig
|
|
325
|
+
const formSource = raw.formConfig
|
|
326
|
+
? typeof raw.formConfig === 'string'
|
|
327
|
+
? parseJSONIfNeeded(raw.formConfig)
|
|
328
|
+
: raw.formConfig
|
|
329
|
+
: raw.formStructure
|
|
330
|
+
const form = normalizeForm(formSource)
|
|
331
|
+
const workflow = normalizeWorkflow(workflowSource)
|
|
332
|
+
const definition = {
|
|
333
|
+
schemaVersion: SCHEMA_VERSION,
|
|
334
|
+
kind: SCHEMA_KIND,
|
|
335
|
+
meta: stripMetaUndefined(extractMeta(raw)),
|
|
336
|
+
workflow: workflow.workflow,
|
|
337
|
+
form: { components: form.components, options: form.options }
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
document: normalizeDefinitionObject(definition),
|
|
341
|
+
repairs: [...workflow.repairs, ...form.repairs],
|
|
342
|
+
validation: validateDefinition(normalizeDefinitionObject(definition), { strict: false })
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const kind = raw.kind
|
|
347
|
+
const workflowSource =
|
|
348
|
+
raw.workflow ||
|
|
349
|
+
raw.flowStructure ||
|
|
350
|
+
raw.flow ||
|
|
351
|
+
(raw.type && (raw.childNode !== undefined || raw.branchList !== undefined) ? raw : null)
|
|
352
|
+
const formSource =
|
|
353
|
+
raw.form?.components !== undefined
|
|
354
|
+
? raw.form
|
|
355
|
+
: raw.formStructure || raw.formConfig || raw.components || raw.form
|
|
356
|
+
|
|
357
|
+
if (!workflowSource && !formSource) {
|
|
358
|
+
throw new Error('导入内容中未找到流程(workflow)或表单(form)')
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const workflow = workflowSource ? normalizeWorkflow(workflowSource) : null
|
|
362
|
+
const form = formSource ? normalizeForm(formSource) : null
|
|
363
|
+
const defaultWorkflow = workflow
|
|
364
|
+
? workflow.workflow
|
|
365
|
+
: normalizeWorkflow({
|
|
366
|
+
nodeId: generateNodeId(),
|
|
367
|
+
nodeName: '发起人',
|
|
368
|
+
type: FlowNodeType.START,
|
|
369
|
+
childNode: null
|
|
370
|
+
}).workflow
|
|
371
|
+
|
|
372
|
+
const document = {
|
|
373
|
+
schemaVersion: raw.schemaVersion || SCHEMA_VERSION,
|
|
374
|
+
kind: kind === WORKFLOW_KIND || kind === FORM_KIND ? SCHEMA_KIND : kind || SCHEMA_KIND,
|
|
375
|
+
meta: stripMetaUndefined(extractMeta(raw)),
|
|
376
|
+
workflow: defaultWorkflow,
|
|
377
|
+
form: { components: form ? form.components : [], options: form?.options || {} }
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const normalized = normalizeDefinitionObject(document)
|
|
381
|
+
return {
|
|
382
|
+
document: normalized,
|
|
383
|
+
repairs: [...(workflow?.repairs || []), ...(form?.repairs || [])],
|
|
384
|
+
validation: validateDefinition(normalized, { strict: false })
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function normalizeDefinitionObject(input) {
|
|
389
|
+
const workflowResult = normalizeWorkflow(input.workflow)
|
|
390
|
+
const formResult = normalizeForm(input.form)
|
|
391
|
+
return {
|
|
392
|
+
schemaVersion: SCHEMA_VERSION,
|
|
393
|
+
kind: SCHEMA_KIND,
|
|
394
|
+
meta: {
|
|
395
|
+
id: input.meta?.id || `flow_${Date.now().toString(36)}`,
|
|
396
|
+
name: input.meta?.name || '未命名流程',
|
|
397
|
+
description: input.meta?.description || '',
|
|
398
|
+
tags: input.meta?.tags || [],
|
|
399
|
+
createdAt: input.meta?.createdAt || new Date().toISOString(),
|
|
400
|
+
updatedAt: new Date().toISOString(),
|
|
401
|
+
...stripMetaUndefined(input.meta || {})
|
|
402
|
+
},
|
|
403
|
+
workflow: workflowResult.workflow,
|
|
404
|
+
form: { components: formResult.components, options: formResult.options }
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export function normalizeDefinition(input, options = {}) {
|
|
409
|
+
const raw = parseJSONIfNeeded(input)
|
|
410
|
+
let document
|
|
411
|
+
if (raw && raw.kind === SCHEMA_KIND) {
|
|
412
|
+
document = normalizeDefinitionObject(raw)
|
|
413
|
+
} else {
|
|
414
|
+
document = importDefinition(raw).document
|
|
415
|
+
}
|
|
416
|
+
if (options.ensureEnd === false) {
|
|
417
|
+
// keep as imported
|
|
418
|
+
}
|
|
419
|
+
return document
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export function exportDefinition(input, options = {}) {
|
|
423
|
+
const document =
|
|
424
|
+
input && input.kind === SCHEMA_KIND && input.workflow
|
|
425
|
+
? normalizeDefinitionObject(input)
|
|
426
|
+
: createDefinition({
|
|
427
|
+
workflow: input?.workflow,
|
|
428
|
+
form: input?.form,
|
|
429
|
+
meta: input?.meta
|
|
430
|
+
})
|
|
431
|
+
if (options.updateTimestamp !== false) {
|
|
432
|
+
document.meta.updatedAt = new Date().toISOString()
|
|
433
|
+
}
|
|
434
|
+
return options.raw ? document : JSON.stringify(document, null, options.space ?? 2)
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export function exportWorkflow(workflow, meta = {}, options = {}) {
|
|
438
|
+
const workflowResult = normalizeWorkflow(workflow)
|
|
439
|
+
const payload = {
|
|
440
|
+
schemaVersion: SCHEMA_VERSION,
|
|
441
|
+
kind: WORKFLOW_KIND,
|
|
442
|
+
meta: stripMetaUndefined(extractMeta({ ...meta, basicInfo: meta })),
|
|
443
|
+
workflow: workflowResult.workflow
|
|
444
|
+
}
|
|
445
|
+
return options.raw ? payload : JSON.stringify(payload, null, options.space ?? 2)
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// ==================== Validation ====================
|
|
449
|
+
|
|
450
|
+
function validateConditionVariable(condition, node, result, label, index, formVariables) {
|
|
451
|
+
if (!condition.variable || !formVariables?.size) return
|
|
452
|
+
const variable = formVariables.get(condition.variable)
|
|
453
|
+
if (!variable) {
|
|
454
|
+
result.warnings.push({
|
|
455
|
+
code: 'CONDITION_VARIABLE_NOT_FOUND',
|
|
456
|
+
message: `${label}「${node.nodeName}」第 ${index + 1} 条条件引用了不存在的表单变量「${condition.variable}」`,
|
|
457
|
+
nodeId: node.nodeId
|
|
458
|
+
})
|
|
459
|
+
return
|
|
460
|
+
}
|
|
461
|
+
const allowedOperators = getOperatorsForDataType(variable.dataType).map(
|
|
462
|
+
(operator) => operator.value
|
|
463
|
+
)
|
|
464
|
+
if (
|
|
465
|
+
condition.operator &&
|
|
466
|
+
allowedOperators.length &&
|
|
467
|
+
!allowedOperators.includes(condition.operator)
|
|
468
|
+
) {
|
|
469
|
+
result.warnings.push({
|
|
470
|
+
code: 'CONDITION_OPERATOR_INVALID',
|
|
471
|
+
message: `${label}「${node.nodeName}」第 ${index + 1} 条条件的运算符 ${condition.operator} 不适用于字段「${variable.label || condition.variable}」`,
|
|
472
|
+
nodeId: node.nodeId
|
|
473
|
+
})
|
|
474
|
+
}
|
|
475
|
+
const needsValue = !['is_empty', 'is_not_empty'].includes(condition.operator)
|
|
476
|
+
const options = typeof variable.getOptions === 'function' ? variable.getOptions() : []
|
|
477
|
+
if (!needsValue || !options.length) return
|
|
478
|
+
const values = Array.isArray(condition.value)
|
|
479
|
+
? condition.value
|
|
480
|
+
: [condition.value]
|
|
481
|
+
const allowedValues = options.map((option) =>
|
|
482
|
+
option && typeof option === 'object' ? option.value : option
|
|
483
|
+
)
|
|
484
|
+
const invalid = values.filter(
|
|
485
|
+
(value) =>
|
|
486
|
+
value !== '' &&
|
|
487
|
+
value !== undefined &&
|
|
488
|
+
value !== null &&
|
|
489
|
+
!allowedValues.some((allowed) => String(allowed) === String(value))
|
|
490
|
+
)
|
|
491
|
+
if (invalid.length) {
|
|
492
|
+
result.warnings.push({
|
|
493
|
+
code: 'CONDITION_VALUE_NOT_IN_OPTIONS',
|
|
494
|
+
message: `${label}「${node.nodeName}」第 ${index + 1} 条条件的值「${invalid.join('、')}」不在字段「${variable.label || condition.variable}」的选项中`,
|
|
495
|
+
nodeId: node.nodeId
|
|
496
|
+
})
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function validateConditionList(conditions, node, result, label, formVariables) {
|
|
501
|
+
if (!Array.isArray(conditions) || conditions.length === 0) {
|
|
502
|
+
result.warnings.push({
|
|
503
|
+
code: 'EMPTY_CONDITIONS',
|
|
504
|
+
message: `${label}「${node.nodeName}」尚未配置条件`,
|
|
505
|
+
nodeId: node.nodeId
|
|
506
|
+
})
|
|
507
|
+
return
|
|
508
|
+
}
|
|
509
|
+
conditions.forEach((condition, index) => {
|
|
510
|
+
validateConditionVariable(condition, node, result, label, index, formVariables)
|
|
511
|
+
if (!condition.variable) {
|
|
512
|
+
result.warnings.push({
|
|
513
|
+
code: 'CONDITION_NO_VARIABLE',
|
|
514
|
+
message: `${label}「${node.nodeName}」第 ${index + 1} 条条件未选择变量`,
|
|
515
|
+
nodeId: node.nodeId
|
|
516
|
+
})
|
|
517
|
+
}
|
|
518
|
+
if (!condition.operator) {
|
|
519
|
+
result.warnings.push({
|
|
520
|
+
code: 'CONDITION_NO_OPERATOR',
|
|
521
|
+
message: `${label}「${node.nodeName}」第 ${index + 1} 条条件未选择操作符`,
|
|
522
|
+
nodeId: node.nodeId
|
|
523
|
+
})
|
|
524
|
+
}
|
|
525
|
+
const needsValue = !['is_empty', 'is_not_empty'].includes(condition.operator)
|
|
526
|
+
const emptyValue =
|
|
527
|
+
condition.value === undefined ||
|
|
528
|
+
condition.value === null ||
|
|
529
|
+
(typeof condition.value === 'string' && condition.value.trim() === '')
|
|
530
|
+
if (needsValue && emptyValue) {
|
|
531
|
+
result.warnings.push({
|
|
532
|
+
code: 'CONDITION_NO_VALUE',
|
|
533
|
+
message: `${label}「${node.nodeName}」第 ${index + 1} 条条件未填写比较值`,
|
|
534
|
+
nodeId: node.nodeId
|
|
535
|
+
})
|
|
536
|
+
}
|
|
537
|
+
})
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function validateChain(node, hasContinuation, result, ctx) {
|
|
541
|
+
if (!node) {
|
|
542
|
+
if (!hasContinuation) {
|
|
543
|
+
result.errors.push({
|
|
544
|
+
code: 'DEAD_END',
|
|
545
|
+
message: '存在没有后续节点也没有结束节点的路径',
|
|
546
|
+
nodeId: ctx.lastNodeId
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
return
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
ctx.count += 1
|
|
553
|
+
ctx.lastNodeId = node.nodeId
|
|
554
|
+
|
|
555
|
+
if (ctx.visited.has(node.nodeId)) return
|
|
556
|
+
ctx.visited.add(node.nodeId)
|
|
557
|
+
|
|
558
|
+
if (!NODE_TYPES.has(node.type)) {
|
|
559
|
+
result.errors.push({
|
|
560
|
+
code: 'UNKNOWN_TYPE',
|
|
561
|
+
message: `节点「${node.nodeName}」类型未知:${node.type}`,
|
|
562
|
+
nodeId: node.nodeId
|
|
563
|
+
})
|
|
564
|
+
return
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (
|
|
568
|
+
(node.type === FlowNodeType.PASS || node.type === FlowNodeType.REJECT) &&
|
|
569
|
+
ctx.parentType !== FlowNodeType.CONDITION &&
|
|
570
|
+
ctx.parentType !== FlowNodeType.INCLUSIVE_BRANCH
|
|
571
|
+
) {
|
|
572
|
+
result.errors.push({
|
|
573
|
+
code: 'TERMINAL_PLACEMENT',
|
|
574
|
+
message: `「${getNodeTypeLabel(node.type)}」节点「${node.nodeName}」只能放在条件分支内部`,
|
|
575
|
+
nodeId: node.nodeId
|
|
576
|
+
})
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
if (isTerminalNode(node)) {
|
|
580
|
+
if (node.childNode) {
|
|
581
|
+
result.warnings.push({
|
|
582
|
+
code: 'TERMINAL_HAS_CHILD',
|
|
583
|
+
message: `终结节点「${node.nodeName}」之后的节点不会被执行`,
|
|
584
|
+
nodeId: node.nodeId
|
|
585
|
+
})
|
|
586
|
+
}
|
|
587
|
+
return
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
if (isBranchNode(node)) {
|
|
591
|
+
if (!Array.isArray(node.branchList) || node.branchList.length === 0) {
|
|
592
|
+
result.errors.push({
|
|
593
|
+
code: 'NO_BRANCHES',
|
|
594
|
+
message: `分支节点「${node.nodeName}」没有配置分支`,
|
|
595
|
+
nodeId: node.nodeId
|
|
596
|
+
})
|
|
597
|
+
} else if (node.type === FlowNodeType.PARALLEL && node.branchList.length < 2) {
|
|
598
|
+
result.errors.push({
|
|
599
|
+
code: 'PARALLEL_TOO_FEW_BRANCHES',
|
|
600
|
+
message: `并行分支「${node.nodeName}」至少需要两个分支`,
|
|
601
|
+
nodeId: node.nodeId
|
|
602
|
+
})
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (node.type === FlowNodeType.BRANCH && node.branchList?.length) {
|
|
606
|
+
const defaults = node.branchList.filter((branch) => branch.isDefaultCondition)
|
|
607
|
+
if (defaults.length === 0) {
|
|
608
|
+
result.errors.push({
|
|
609
|
+
code: 'NO_DEFAULT_BRANCH',
|
|
610
|
+
message: `条件分支「${node.nodeName}」缺少「其他条件」兜底分支`,
|
|
611
|
+
nodeId: node.nodeId
|
|
612
|
+
})
|
|
613
|
+
}
|
|
614
|
+
const last = node.branchList[node.branchList.length - 1]
|
|
615
|
+
if (defaults.length && !last.isDefaultCondition) {
|
|
616
|
+
result.errors.push({
|
|
617
|
+
code: 'DEFAULT_BRANCH_NOT_LAST',
|
|
618
|
+
message: `条件分支「${node.nodeName}」的「其他条件」必须位于最后`,
|
|
619
|
+
nodeId: node.nodeId
|
|
620
|
+
})
|
|
621
|
+
}
|
|
622
|
+
node.branchList.forEach((branch) => {
|
|
623
|
+
if (!branch.isDefaultCondition) {
|
|
624
|
+
validateConditionList(branch.conditions, branch, result, '条件', ctx.formVariables)
|
|
625
|
+
}
|
|
626
|
+
})
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
if (node.type === FlowNodeType.INCLUSIVE && node.branchList?.length) {
|
|
630
|
+
const defaults = node.branchList.filter((branch) => branch.isDefaultBranch)
|
|
631
|
+
if (defaults.length > 1) {
|
|
632
|
+
result.errors.push({
|
|
633
|
+
code: 'MULTIPLE_DEFAULT_INCLUSIVE',
|
|
634
|
+
message: `包容分支「${node.nodeName}」只能有一个默认分支`,
|
|
635
|
+
nodeId: node.nodeId
|
|
636
|
+
})
|
|
637
|
+
}
|
|
638
|
+
node.branchList.forEach((branch) => {
|
|
639
|
+
if (!branch.isDefaultBranch) {
|
|
640
|
+
validateConditionList(branch.conditions, branch, result, '包容条件', ctx.formVariables)
|
|
641
|
+
}
|
|
642
|
+
})
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
node.branchList?.forEach((branch) => {
|
|
646
|
+
validateChain(branch.childNode, !!node.childNode, result, {
|
|
647
|
+
...ctx,
|
|
648
|
+
visited: ctx.visited,
|
|
649
|
+
parentType: branch.type
|
|
650
|
+
})
|
|
651
|
+
})
|
|
652
|
+
|
|
653
|
+
validateChain(node.childNode, hasContinuation, result, {
|
|
654
|
+
...ctx,
|
|
655
|
+
parentType: node.type
|
|
656
|
+
})
|
|
657
|
+
return
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
if (node.type === FlowNodeType.APPROVE) {
|
|
661
|
+
const approverType = node.approverType || 'user'
|
|
662
|
+
if (!['user', 'role', 'superior_leader'].includes(approverType)) {
|
|
663
|
+
result.errors.push({
|
|
664
|
+
code: 'BAD_APPROVER_TYPE',
|
|
665
|
+
message: `审批节点「${node.nodeName}」的审批人类型无效:${approverType}`,
|
|
666
|
+
nodeId: node.nodeId
|
|
667
|
+
})
|
|
668
|
+
}
|
|
669
|
+
if (approverType === 'user' && (!node.userList || node.userList.length === 0)) {
|
|
670
|
+
result.warnings.push({
|
|
671
|
+
code: 'APPROVER_EMPTY',
|
|
672
|
+
message: `审批节点「${node.nodeName}」尚未选择审批人`,
|
|
673
|
+
nodeId: node.nodeId
|
|
674
|
+
})
|
|
675
|
+
}
|
|
676
|
+
if (approverType === 'role' && (!node.roleList || node.roleList.length === 0)) {
|
|
677
|
+
result.warnings.push({
|
|
678
|
+
code: 'ROLE_EMPTY',
|
|
679
|
+
message: `审批节点「${node.nodeName}」尚未选择审批角色`,
|
|
680
|
+
nodeId: node.nodeId
|
|
681
|
+
})
|
|
682
|
+
}
|
|
683
|
+
if (node.rejectStrategy === 'to_specified') {
|
|
684
|
+
if (node.rejectTargetNodeId === null || node.rejectTargetNodeId === undefined) {
|
|
685
|
+
result.warnings.push({
|
|
686
|
+
code: 'REJECT_TARGET_EMPTY',
|
|
687
|
+
message: `审批节点「${node.nodeName}」选择了「退回指定节点」但未指定目标`,
|
|
688
|
+
nodeId: node.nodeId
|
|
689
|
+
})
|
|
690
|
+
} else if (!ctx.index.has(String(node.rejectTargetNodeId))) {
|
|
691
|
+
result.errors.push({
|
|
692
|
+
code: 'REJECT_TARGET_NOT_FOUND',
|
|
693
|
+
message: `审批节点「${node.nodeName}」的驳回目标节点不存在`,
|
|
694
|
+
nodeId: node.nodeId
|
|
695
|
+
})
|
|
696
|
+
} else {
|
|
697
|
+
const target = ctx.index.get(String(node.rejectTargetNodeId))
|
|
698
|
+
if (target.type !== FlowNodeType.APPROVE) {
|
|
699
|
+
result.errors.push({
|
|
700
|
+
code: 'REJECT_TARGET_INVALID_TYPE',
|
|
701
|
+
message: `审批节点「${node.nodeName}」的驳回目标「${target.nodeName}」不是审批节点`,
|
|
702
|
+
nodeId: node.nodeId
|
|
703
|
+
})
|
|
704
|
+
} else if (
|
|
705
|
+
(ctx.order.get(String(target.nodeId)) ?? -1) >=
|
|
706
|
+
(ctx.order.get(String(node.nodeId)) ?? -1)
|
|
707
|
+
) {
|
|
708
|
+
result.warnings.push({
|
|
709
|
+
code: 'REJECT_TARGET_DOWNSTREAM',
|
|
710
|
+
message: `审批节点「${node.nodeName}」的驳回目标「${target.nodeName}」位于其下游,可能导致跳步`,
|
|
711
|
+
nodeId: node.nodeId
|
|
712
|
+
})
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
if (node.termAuto === true) {
|
|
717
|
+
const term = Number(node.term)
|
|
718
|
+
if (!Number.isFinite(term) || term <= 0) {
|
|
719
|
+
result.warnings.push({
|
|
720
|
+
code: 'TERM_DISABLED',
|
|
721
|
+
message: `审批节点「${node.nodeName}」开启了超时自动审批,但期限为 0,配置不会生效`,
|
|
722
|
+
nodeId: node.nodeId
|
|
723
|
+
})
|
|
724
|
+
}
|
|
725
|
+
if (![0, 1].includes(Number(node.termMode))) {
|
|
726
|
+
result.warnings.push({
|
|
727
|
+
code: 'TERM_MODE_INVALID',
|
|
728
|
+
message: `审批节点「${node.nodeName}」的超时处理方式无效:${node.termMode}`,
|
|
729
|
+
nodeId: node.nodeId
|
|
730
|
+
})
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
if (node.type === FlowNodeType.CC && (!node.userList || node.userList.length === 0)) {
|
|
736
|
+
result.warnings.push({
|
|
737
|
+
code: 'CC_EMPTY',
|
|
738
|
+
message: `抄送节点「${node.nodeName}」尚未选择抄送人`,
|
|
739
|
+
nodeId: node.nodeId
|
|
740
|
+
})
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
if (node.type === FlowNodeType.CUSTOM) {
|
|
744
|
+
const report = validateCustomNodeConfig(node.custom)
|
|
745
|
+
report.errors.forEach((item) => {
|
|
746
|
+
result.errors.push({
|
|
747
|
+
...item,
|
|
748
|
+
message: `自定义节点「${node.nodeName}」:${item.message}`,
|
|
749
|
+
nodeId: node.nodeId
|
|
750
|
+
})
|
|
751
|
+
})
|
|
752
|
+
report.warnings.forEach((item) => {
|
|
753
|
+
result.warnings.push({ ...item, nodeId: node.nodeId })
|
|
754
|
+
})
|
|
755
|
+
if (
|
|
756
|
+
report.config.resultVariable &&
|
|
757
|
+
ctx.formVariables?.has(report.config.resultVariable)
|
|
758
|
+
) {
|
|
759
|
+
result.warnings.push({
|
|
760
|
+
code: 'CUSTOM_RESULT_OVERRIDES_FORM_VARIABLE',
|
|
761
|
+
message: `自定义节点「${node.nodeName}」的结果变量「${report.config.resultVariable}」会覆盖同名表单字段`,
|
|
762
|
+
nodeId: node.nodeId
|
|
763
|
+
})
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
if (node.type === FlowNodeType.CONDITION) {
|
|
768
|
+
if (!node.isDefaultCondition) {
|
|
769
|
+
validateConditionList(node.conditions, node, result, '条件', ctx.formVariables)
|
|
770
|
+
}
|
|
771
|
+
if (!node.childNode && !hasContinuation) {
|
|
772
|
+
result.errors.push({
|
|
773
|
+
code: 'CONDITION_DEAD_END',
|
|
774
|
+
message: `条件「${node.nodeName}」没有后续节点,分支之后也没有节点`,
|
|
775
|
+
nodeId: node.nodeId
|
|
776
|
+
})
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
validateChain(node.childNode, hasContinuation, result, { ...ctx, parentType: node.type })
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
export function validateWorkflow(workflow, options = {}) {
|
|
784
|
+
const result = { valid: true, errors: [], warnings: [] }
|
|
785
|
+
if (!isPlainObject(workflow)) {
|
|
786
|
+
result.errors.push({ code: 'EMPTY_WORKFLOW', message: '流程结构不能为空' })
|
|
787
|
+
result.valid = false
|
|
788
|
+
return result
|
|
789
|
+
}
|
|
790
|
+
if (workflow.type !== FlowNodeType.START) {
|
|
791
|
+
result.errors.push({
|
|
792
|
+
code: 'ROOT_NOT_START',
|
|
793
|
+
message: '流程必须以开始节点开始'
|
|
794
|
+
})
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
const index = new Map()
|
|
798
|
+
const order = new Map()
|
|
799
|
+
let count = 0
|
|
800
|
+
const indexNode = (node) => {
|
|
801
|
+
if (!node || index.has(String(node.nodeId))) return
|
|
802
|
+
index.set(String(node.nodeId), node)
|
|
803
|
+
order.set(String(node.nodeId), count)
|
|
804
|
+
count += 1
|
|
805
|
+
if (node.childNode) indexNode(node.childNode)
|
|
806
|
+
node.branchList?.forEach(indexNode)
|
|
807
|
+
}
|
|
808
|
+
indexNode(workflow)
|
|
809
|
+
|
|
810
|
+
if (count > MAX_NODE_COUNT) {
|
|
811
|
+
result.errors.push({
|
|
812
|
+
code: 'TOO_MANY_NODES',
|
|
813
|
+
message: `节点数量 ${count} 超过上限 ${MAX_NODE_COUNT}`
|
|
814
|
+
})
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
const formVariables =
|
|
818
|
+
options.formVariables instanceof Map
|
|
819
|
+
? options.formVariables
|
|
820
|
+
: new Map(
|
|
821
|
+
(options.formVariables || []).map((variable) => [
|
|
822
|
+
variable.variableName,
|
|
823
|
+
variable
|
|
824
|
+
])
|
|
825
|
+
)
|
|
826
|
+
const ctx = {
|
|
827
|
+
visited: new Set(),
|
|
828
|
+
count: 0,
|
|
829
|
+
lastNodeId: workflow.nodeId,
|
|
830
|
+
index,
|
|
831
|
+
order,
|
|
832
|
+
formVariables
|
|
833
|
+
}
|
|
834
|
+
validateChain(workflow, false, result, ctx)
|
|
835
|
+
|
|
836
|
+
if (options.strict) {
|
|
837
|
+
result.errors.push(...result.warnings)
|
|
838
|
+
result.warnings = []
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
result.valid = result.errors.length === 0
|
|
842
|
+
result.nodeCount = count
|
|
843
|
+
return result
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
export function validateDefinition(definition, options = {}) {
|
|
847
|
+
const components = definition?.form?.components || []
|
|
848
|
+
const formVariables = enrichFormVariables(components)
|
|
849
|
+
const workflowValidation = validateWorkflow(definition?.workflow, {
|
|
850
|
+
...options,
|
|
851
|
+
formVariables
|
|
852
|
+
})
|
|
853
|
+
const formErrors = validateForm(definition?.form)
|
|
854
|
+
const errors = [...workflowValidation.errors]
|
|
855
|
+
const warnings = [...workflowValidation.warnings]
|
|
856
|
+
formErrors.errors.forEach((error) => errors.push(error))
|
|
857
|
+
formErrors.warnings.forEach((warning) => warnings.push(warning))
|
|
858
|
+
return {
|
|
859
|
+
...workflowValidation,
|
|
860
|
+
errors,
|
|
861
|
+
warnings,
|
|
862
|
+
valid: errors.length === 0
|
|
863
|
+
}
|
|
864
|
+
}
|