gbflowengine 0.0.0-stage → 0.1.0
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 +271 -3
- package/dist/gbflowengine.css +1 -0
- package/dist/gbflowengine.js +18363 -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 +84 -6
- 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,357 @@
|
|
|
1
|
+
// [文件] gbflowengine·runtime·execute —— 执行内核 —— 节点推进与审批/抄送/自定义节点进入
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以对象字面量导出,由 runtime.js 末尾 Object.assign 装配到 FlowRuntime.prototype(同一 this,调用形状不变)。
|
|
4
|
+
|
|
5
|
+
import { FlowNodeType, SignMode } from '../flowConstant.js'
|
|
6
|
+
import { selectBranchNode, selectInclusiveBranches } from '../condition.js'
|
|
7
|
+
import { CUSTOM_RESULT_LIMIT, isValidCustomResultVariable, normalizeCustomNodeConfig, validateCustomNodeConfig } from '../customNode.js'
|
|
8
|
+
import { clone, normalizeApprover, sameId, RUNNING, REJECTED, CANCELLED } from './helpers.js'
|
|
9
|
+
export const executeMethods = {
|
|
10
|
+
|
|
11
|
+
_run() {
|
|
12
|
+
this._releaseReadyJoins()
|
|
13
|
+
let guard = 0
|
|
14
|
+
while (this.state.status === RUNNING && guard < 10000) {
|
|
15
|
+
guard += 1
|
|
16
|
+
const runnable = this.state.tokens.find(
|
|
17
|
+
(token) => !token.blockedTaskId && !token.blockedJoinGroup
|
|
18
|
+
)
|
|
19
|
+
if (!runnable) break
|
|
20
|
+
this._step(runnable)
|
|
21
|
+
}
|
|
22
|
+
if (guard >= 10000) {
|
|
23
|
+
this._warn('EXECUTION_GUARD', '流程执行步数超过保护上限,可能存在环')
|
|
24
|
+
}
|
|
25
|
+
this._checkFinished()
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
_step(token) {
|
|
29
|
+
if (this.state.status !== RUNNING) return
|
|
30
|
+
if (token.blockedTaskId || token.blockedJoinGroup) return
|
|
31
|
+
if (token.nodeId === null || token.nodeId === undefined) {
|
|
32
|
+
this._finishToken(token)
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
const node = this.getNode(token.nodeId)
|
|
36
|
+
if (!node) {
|
|
37
|
+
this._warn('NODE_NOT_FOUND', `找不到节点:${token.nodeId}`)
|
|
38
|
+
this._finishToken(token)
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const join = token.joins[token.joins.length - 1]
|
|
43
|
+
if (join && join.targetId !== null && String(join.targetId) === String(node.nodeId)) {
|
|
44
|
+
this._arriveAtJoin(token)
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
this._emit('onNodeEnter', { runtime: this, node: clone(node), token: clone(token) })
|
|
49
|
+
this.state.history.push(this._event('enter', { nodeId: node.nodeId, type: node.type }))
|
|
50
|
+
|
|
51
|
+
switch (node.type) {
|
|
52
|
+
case FlowNodeType.START:
|
|
53
|
+
this._advanceFrom(node, token)
|
|
54
|
+
return
|
|
55
|
+
case FlowNodeType.APPROVE:
|
|
56
|
+
this._enterApprove(node, token)
|
|
57
|
+
return
|
|
58
|
+
case FlowNodeType.CC:
|
|
59
|
+
this._enterCc(node, token)
|
|
60
|
+
return
|
|
61
|
+
case FlowNodeType.CUSTOM:
|
|
62
|
+
this._enterCustom(node, token)
|
|
63
|
+
return
|
|
64
|
+
case FlowNodeType.BRANCH: {
|
|
65
|
+
const branch = selectBranchNode(node, this.state.variables)
|
|
66
|
+
if (!branch) {
|
|
67
|
+
this._warn('NO_MATCHING_BRANCH', `条件分支「${node.nodeName}」没有可用分支`)
|
|
68
|
+
this._finishToken(token)
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
this.state.history.push(
|
|
72
|
+
this._event('branch', { nodeId: node.nodeId, branchId: branch.nodeId })
|
|
73
|
+
)
|
|
74
|
+
const continuation = node.childNode ? node.childNode.nodeId : null
|
|
75
|
+
if (branch.childNode) {
|
|
76
|
+
if (continuation !== null) token.continuations.push(continuation)
|
|
77
|
+
this._placeAt(token, branch.childNode.nodeId)
|
|
78
|
+
} else {
|
|
79
|
+
this._advanceTo(token, continuation)
|
|
80
|
+
}
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
case FlowNodeType.PARALLEL:
|
|
84
|
+
this._split(node, token, node.branchList || [])
|
|
85
|
+
return
|
|
86
|
+
case FlowNodeType.INCLUSIVE: {
|
|
87
|
+
const selected = selectInclusiveBranches(node, this.state.variables)
|
|
88
|
+
if (!selected.length) {
|
|
89
|
+
this._warn('NO_MATCHING_BRANCH', `包容分支「${node.nodeName}」没有命中的分支`)
|
|
90
|
+
this._advanceFrom(node, token)
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
this.state.history.push(
|
|
94
|
+
this._event('branch', {
|
|
95
|
+
nodeId: node.nodeId,
|
|
96
|
+
branchIds: selected.map((branch) => branch.nodeId)
|
|
97
|
+
})
|
|
98
|
+
)
|
|
99
|
+
this._split(node, token, selected)
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
case FlowNodeType.PASS:
|
|
103
|
+
this.state.history.push(this._event('pass', { nodeId: node.nodeId }))
|
|
104
|
+
this._finishToken(token)
|
|
105
|
+
return
|
|
106
|
+
case FlowNodeType.REJECT:
|
|
107
|
+
this.state.history.push(this._event('rejectNode', { nodeId: node.nodeId }))
|
|
108
|
+
this._finishFlow(REJECTED)
|
|
109
|
+
return
|
|
110
|
+
case FlowNodeType.END:
|
|
111
|
+
this.state.history.push(this._event('end', { nodeId: node.nodeId }))
|
|
112
|
+
this._finishToken(token)
|
|
113
|
+
return
|
|
114
|
+
case FlowNodeType.CONDITION:
|
|
115
|
+
case FlowNodeType.PARALLEL_BRANCH:
|
|
116
|
+
case FlowNodeType.INCLUSIVE_BRANCH:
|
|
117
|
+
this._advanceFrom(node, token)
|
|
118
|
+
return
|
|
119
|
+
default:
|
|
120
|
+
this._advanceFrom(node, token)
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
_enterApprove(node, token) {
|
|
125
|
+
const approvers = this._resolveApprovers(node)
|
|
126
|
+
if (approvers.length === 0) {
|
|
127
|
+
this._warn(
|
|
128
|
+
'APPROVERS_EMPTY',
|
|
129
|
+
`审批节点「${node.nodeName}」未解析到审批人,已自动通过`
|
|
130
|
+
)
|
|
131
|
+
this.state.history.push(
|
|
132
|
+
this._event('autoApprove', { nodeId: node.nodeId, reason: 'no-approver' })
|
|
133
|
+
)
|
|
134
|
+
this._advanceFrom(node, token)
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this.seq.task += 1
|
|
139
|
+
const termHours = Number(node.term)
|
|
140
|
+
const hasDeadline = node.termAuto === true && Number.isFinite(termHours) && termHours > 0
|
|
141
|
+
const task = {
|
|
142
|
+
id: `task_${this.seq.task}`,
|
|
143
|
+
nodeId: node.nodeId,
|
|
144
|
+
nodeName: node.nodeName,
|
|
145
|
+
tokenId: token.id,
|
|
146
|
+
kind: 'approve',
|
|
147
|
+
signMode: node.signMode === SignMode.OR ? SignMode.OR : SignMode.AND,
|
|
148
|
+
approvers,
|
|
149
|
+
approvedBy: [],
|
|
150
|
+
records: [],
|
|
151
|
+
status: 'pending',
|
|
152
|
+
cycle: this.state.cycle || 1,
|
|
153
|
+
createdAt: new Date().toISOString(),
|
|
154
|
+
...(hasDeadline
|
|
155
|
+
? {
|
|
156
|
+
dueAt: new Date(Date.now() + termHours * 3600 * 1000).toISOString(),
|
|
157
|
+
termMode: node.termMode === 1 ? 1 : 0
|
|
158
|
+
}
|
|
159
|
+
: {})
|
|
160
|
+
}
|
|
161
|
+
token.blockedTaskId = task.id
|
|
162
|
+
this.state.tasks.push(task)
|
|
163
|
+
this.state.history.push(this._event('taskCreated', { nodeId: node.nodeId, taskId: task.id }))
|
|
164
|
+
this._emit('onTaskCreated', {
|
|
165
|
+
runtime: this,
|
|
166
|
+
task: clone(task),
|
|
167
|
+
node: clone(node),
|
|
168
|
+
state: this.getState()
|
|
169
|
+
})
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
_resolveCcTargets(node) {
|
|
173
|
+
const users = this._resolveUsers(node.userList, node.userNickNames)
|
|
174
|
+
if (node.allowInitiatorAddUsers !== false && typeof this.context.resolveExtraCcUsers === 'function') {
|
|
175
|
+
const extras = this.context.resolveExtraCcUsers(node, clone(this.state.variables), this.context)
|
|
176
|
+
if (Array.isArray(extras)) {
|
|
177
|
+
extras.forEach((user, index) => {
|
|
178
|
+
const extra = normalizeApprover(user, index + 1)
|
|
179
|
+
if (!users.some((item) => sameId(item.id, extra.id))) users.push(extra)
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return users
|
|
184
|
+
},
|
|
185
|
+
|
|
186
|
+
_enterCc(node, token) {
|
|
187
|
+
const users = this._resolveCcTargets(node)
|
|
188
|
+
this.seq.cc += 1
|
|
189
|
+
const record = {
|
|
190
|
+
id: `cc_${this.seq.cc}`,
|
|
191
|
+
nodeId: node.nodeId,
|
|
192
|
+
nodeName: node.nodeName,
|
|
193
|
+
users,
|
|
194
|
+
cycle: this.state.cycle || 1,
|
|
195
|
+
createdAt: new Date().toISOString()
|
|
196
|
+
}
|
|
197
|
+
this.state.ccRecords.push(record)
|
|
198
|
+
this.state.history.push(this._event('cc', { nodeId: node.nodeId, users }))
|
|
199
|
+
this._emit('onCc', { runtime: this, cc: clone(record), node: clone(node) })
|
|
200
|
+
this._advanceFrom(node, token)
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
_enterCustom(node, token) {
|
|
204
|
+
const config = normalizeCustomNodeConfig(node.custom)
|
|
205
|
+
const report = validateCustomNodeConfig(config)
|
|
206
|
+
if (report.errors.length) {
|
|
207
|
+
this._warn(
|
|
208
|
+
'CUSTOM_CONFIG_INVALID',
|
|
209
|
+
`自定义节点「${node.nodeName}」配置无效:${report.errors[0].message}`
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
this.seq.task += 1
|
|
213
|
+
const task = {
|
|
214
|
+
id: `task_${this.seq.task}`,
|
|
215
|
+
nodeId: node.nodeId,
|
|
216
|
+
nodeName: node.nodeName,
|
|
217
|
+
tokenId: token.id,
|
|
218
|
+
kind: 'custom',
|
|
219
|
+
signMode: null,
|
|
220
|
+
approvers: [],
|
|
221
|
+
approvedBy: [],
|
|
222
|
+
records: [],
|
|
223
|
+
status: 'pending',
|
|
224
|
+
attempts: 0,
|
|
225
|
+
cycle: this.state.cycle || 1,
|
|
226
|
+
createdAt: new Date().toISOString(),
|
|
227
|
+
custom: config
|
|
228
|
+
}
|
|
229
|
+
token.blockedTaskId = task.id
|
|
230
|
+
this.state.tasks.push(task)
|
|
231
|
+
this.state.history.push(
|
|
232
|
+
this._event('taskCreated', { nodeId: node.nodeId, taskId: task.id, kind: 'custom' })
|
|
233
|
+
)
|
|
234
|
+
this._emit('onTaskCreated', {
|
|
235
|
+
runtime: this,
|
|
236
|
+
task: clone(task),
|
|
237
|
+
node: clone(node),
|
|
238
|
+
state: this.getState()
|
|
239
|
+
})
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
completeCustomTask(taskId, payload = {}) {
|
|
243
|
+
const task = this.state.tasks.find((item) => item.id === taskId)
|
|
244
|
+
if (!task) throw new Error(`任务不存在:${taskId}`)
|
|
245
|
+
if (task.kind !== 'custom') throw new Error(`任务「${task.nodeName}」不是自定义节点任务`)
|
|
246
|
+
if (task.status !== 'pending') throw new Error(`任务已处理:${taskId}`)
|
|
247
|
+
if (this.state.status !== RUNNING) throw new Error('流程当前不在运行中')
|
|
248
|
+
const node = this.getNode(task.nodeId)
|
|
249
|
+
if (!node) throw new Error(`自定义节点不存在:${task.nodeId}`)
|
|
250
|
+
const token = this.state.tokens.find((item) => item.id === task.tokenId)
|
|
251
|
+
if (!token) throw new Error('自定义节点对应的流程 token 不存在')
|
|
252
|
+
const config = normalizeCustomNodeConfig(task.custom)
|
|
253
|
+
const success = payload.success !== false
|
|
254
|
+
const durationMs = Number(payload.durationMs)
|
|
255
|
+
const at = new Date().toISOString()
|
|
256
|
+
const executorLabel = payload.executorLabel || '系统执行器'
|
|
257
|
+
|
|
258
|
+
if (success) {
|
|
259
|
+
let result
|
|
260
|
+
try {
|
|
261
|
+
const serialized = JSON.stringify(payload.result ?? null)
|
|
262
|
+
if (serialized && serialized.length > CUSTOM_RESULT_LIMIT) {
|
|
263
|
+
throw new Error(
|
|
264
|
+
`执行结果超过 ${Math.round(CUSTOM_RESULT_LIMIT / 1024)}KB 限制`
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
result = serialized === undefined ? null : JSON.parse(serialized)
|
|
268
|
+
} catch (error) {
|
|
269
|
+
throw new Error(`自定义节点执行结果无法序列化:${error.message}`)
|
|
270
|
+
}
|
|
271
|
+
if (payload.variables && typeof payload.variables === 'object') {
|
|
272
|
+
this.state.variables = { ...this.state.variables, ...clone(payload.variables) }
|
|
273
|
+
}
|
|
274
|
+
if (config.resultVariable && isValidCustomResultVariable(config.resultVariable)) {
|
|
275
|
+
this.state.variables[config.resultVariable] = result
|
|
276
|
+
}
|
|
277
|
+
task.status = 'completed'
|
|
278
|
+
task.attempts = (task.attempts || 0) + 1
|
|
279
|
+
task.finishedAt = at
|
|
280
|
+
task.result = result
|
|
281
|
+
task.records = [
|
|
282
|
+
...(task.records || []),
|
|
283
|
+
{
|
|
284
|
+
action: 'execute',
|
|
285
|
+
approver: { id: 'system', name: executorLabel },
|
|
286
|
+
comment: config.resultVariable
|
|
287
|
+
? `执行成功,结果已写入 ${config.resultVariable}`
|
|
288
|
+
: '执行成功',
|
|
289
|
+
at,
|
|
290
|
+
system: true,
|
|
291
|
+
...(Number.isFinite(durationMs) ? { durationMs } : {})
|
|
292
|
+
}
|
|
293
|
+
]
|
|
294
|
+
this.state.history.push(
|
|
295
|
+
this._event('customCompleted', {
|
|
296
|
+
nodeId: task.nodeId,
|
|
297
|
+
taskId: task.id,
|
|
298
|
+
resultVariable: config.resultVariable || '',
|
|
299
|
+
durationMs: Number.isFinite(durationMs) ? durationMs : null
|
|
300
|
+
})
|
|
301
|
+
)
|
|
302
|
+
token.blockedTaskId = null
|
|
303
|
+
this._emit('onCustomTaskCompleted', {
|
|
304
|
+
runtime: this,
|
|
305
|
+
task: clone(task),
|
|
306
|
+
result: clone(result),
|
|
307
|
+
state: this.getState()
|
|
308
|
+
})
|
|
309
|
+
this._advanceFrom(node, token)
|
|
310
|
+
this._run()
|
|
311
|
+
return this.getState()
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const errorMessage = String(payload.error?.message || payload.error || '执行失败').slice(0, 1000)
|
|
315
|
+
task.status = 'failed'
|
|
316
|
+
task.attempts = (task.attempts || 0) + 1
|
|
317
|
+
task.finishedAt = at
|
|
318
|
+
task.lastError = errorMessage
|
|
319
|
+
task.records = [
|
|
320
|
+
...(task.records || []),
|
|
321
|
+
{
|
|
322
|
+
action: 'executeFailed',
|
|
323
|
+
approver: { id: 'system', name: executorLabel },
|
|
324
|
+
comment: `执行失败:${errorMessage}`,
|
|
325
|
+
at,
|
|
326
|
+
system: true,
|
|
327
|
+
...(Number.isFinite(durationMs) ? { durationMs } : {})
|
|
328
|
+
}
|
|
329
|
+
]
|
|
330
|
+
this.state.history.push(
|
|
331
|
+
this._event('customFailed', {
|
|
332
|
+
nodeId: task.nodeId,
|
|
333
|
+
taskId: task.id,
|
|
334
|
+
error: errorMessage,
|
|
335
|
+
policy: config.onError
|
|
336
|
+
})
|
|
337
|
+
)
|
|
338
|
+
this._warn(
|
|
339
|
+
'CUSTOM_EXECUTION_FAILED',
|
|
340
|
+
`自定义节点「${node.nodeName}」执行失败:${errorMessage}`
|
|
341
|
+
)
|
|
342
|
+
this._emit('onCustomTaskFailed', {
|
|
343
|
+
runtime: this,
|
|
344
|
+
task: clone(task),
|
|
345
|
+
error: errorMessage,
|
|
346
|
+
state: this.getState()
|
|
347
|
+
})
|
|
348
|
+
if (config.onError === 'continue') {
|
|
349
|
+
token.blockedTaskId = null
|
|
350
|
+
this._advanceFrom(node, token)
|
|
351
|
+
this._run()
|
|
352
|
+
return this.getState()
|
|
353
|
+
}
|
|
354
|
+
this._finishFlow(CANCELLED)
|
|
355
|
+
return this.getState()
|
|
356
|
+
}
|
|
357
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// [文件] gbflowengine·runtime·helpers —— 状态常量与纯工具(无 this,被 runtime 各职责模块共用)
|
|
2
|
+
|
|
3
|
+
export const RUNNING = 'running'
|
|
4
|
+
export const IDLE = 'idle'
|
|
5
|
+
export const COMPLETED = 'completed'
|
|
6
|
+
export const REJECTED = 'rejected'
|
|
7
|
+
export const CANCELLED = 'cancelled'
|
|
8
|
+
export const REVOKED = 'revoked'
|
|
9
|
+
|
|
10
|
+
export function clone(value) {
|
|
11
|
+
return value === undefined ? undefined : JSON.parse(JSON.stringify(value))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function normalizeApprover(user, fallbackId) {
|
|
15
|
+
if (user === null || user === undefined) {
|
|
16
|
+
return { id: fallbackId, name: `审批人${fallbackId}` }
|
|
17
|
+
}
|
|
18
|
+
if (typeof user === 'object') {
|
|
19
|
+
const id = user.id ?? user.userId ?? user.approverId ?? user.username
|
|
20
|
+
return {
|
|
21
|
+
id,
|
|
22
|
+
name: user.realName || user.nickName || user.name || user.userName || String(id),
|
|
23
|
+
deptName: user.deptName || '',
|
|
24
|
+
deptId: user.deptId ?? null
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return { id: user, name: `审批人${user}`, deptName: '', deptId: null }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function sameId(left, right) {
|
|
31
|
+
return left !== undefined && right !== undefined && String(left) === String(right)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function maxIdSuffix(list = [], prefix) {
|
|
35
|
+
return (list || []).reduce((max, item) => {
|
|
36
|
+
const value = Number(String(item?.id || '').replace(prefix, ''))
|
|
37
|
+
return Number.isFinite(value) ? Math.max(max, value) : max
|
|
38
|
+
}, 0)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function deriveCycle(state) {
|
|
42
|
+
const returns = (state?.history || []).filter((event) => event.event === 'return').length
|
|
43
|
+
return Math.max(Number(state?.cycle) || 1, returns + 1)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function taskHasApprover(task, userId) {
|
|
47
|
+
return task.approvers.some((approver) => sameId(approver.id, userId))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function taskApprovedBy(task, userId) {
|
|
51
|
+
return (task.approvedBy || []).some((approver) => sameId(approver.id, userId))
|
|
52
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// [文件] gbflowengine·runtime·query —— 状态查询与审批人解析
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以对象字面量导出,由 runtime.js 末尾 Object.assign 装配到 FlowRuntime.prototype(同一 this,调用形状不变)。
|
|
4
|
+
|
|
5
|
+
import { ApproverType, RejectStrategy } from '../flowConstant.js'
|
|
6
|
+
import { clone, normalizeApprover, sameId, taskHasApprover, taskApprovedBy } from './helpers.js'
|
|
7
|
+
export const queryMethods = {
|
|
8
|
+
|
|
9
|
+
getNode(nodeId) {
|
|
10
|
+
return this.nodeIndex.get(String(nodeId)) || null
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
getState() {
|
|
14
|
+
return {
|
|
15
|
+
...clone(this.state),
|
|
16
|
+
currentNodeIds: this.state.tokens.map((token) => token.nodeId),
|
|
17
|
+
pendingTaskCount: this.state.tasks.filter((task) => task.status === 'pending').length
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
getPendingTasks() {
|
|
22
|
+
return clone(this.state.tasks.filter((task) => task.status === 'pending'))
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
setVariables(patch) {
|
|
26
|
+
this.state.variables = { ...this.state.variables, ...clone(patch) }
|
|
27
|
+
return this.state.variables
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
getPendingCustomTasks() {
|
|
31
|
+
return clone(
|
|
32
|
+
this.state.tasks.filter(
|
|
33
|
+
(task) => task.kind === 'custom' && task.status === 'pending'
|
|
34
|
+
)
|
|
35
|
+
)
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
getPendingTasksForUser(userId) {
|
|
39
|
+
return clone(
|
|
40
|
+
this.state.tasks.filter(
|
|
41
|
+
(task) =>
|
|
42
|
+
task.status === 'pending' &&
|
|
43
|
+
taskHasApprover(task, userId) &&
|
|
44
|
+
!taskApprovedBy(task, userId)
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
getCcRecordsForUser(userId) {
|
|
50
|
+
return clone(
|
|
51
|
+
this.state.ccRecords.filter((record) =>
|
|
52
|
+
(record.users || []).some((user) => sameId(user.id, userId))
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
getRejectInfo(nodeId) {
|
|
58
|
+
const node = this.getNode(nodeId)
|
|
59
|
+
const strategy = node?.rejectStrategy || RejectStrategy.TERMINATE
|
|
60
|
+
const retryLimit = Math.max(0, Math.floor(Number(node?.rejectRetryLimit) || 0))
|
|
61
|
+
const retriesUsed = this.state.history.filter(
|
|
62
|
+
(event) => event.event === 'return' && sameId(event.nodeId, nodeId)
|
|
63
|
+
).length
|
|
64
|
+
const unlimited = retryLimit === 0
|
|
65
|
+
return {
|
|
66
|
+
nodeId,
|
|
67
|
+
strategy,
|
|
68
|
+
retryLimit,
|
|
69
|
+
retriesUsed,
|
|
70
|
+
unlimited,
|
|
71
|
+
remaining: unlimited ? null : Math.max(0, retryLimit - retriesUsed),
|
|
72
|
+
wouldTerminate:
|
|
73
|
+
strategy === RejectStrategy.TERMINATE || (!unlimited && retriesUsed >= retryLimit)
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
resolveApprovers(node) {
|
|
78
|
+
return clone(this._resolveApprovers(node))
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
resolveCcUsers(node) {
|
|
82
|
+
return clone(this._resolveCcTargets(node))
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
_resolveApprovers(node) {
|
|
86
|
+
const resolver = this.context.resolveApprovers
|
|
87
|
+
if (typeof resolver === 'function') {
|
|
88
|
+
const custom = resolver(node, clone(this.state.variables), this.context)
|
|
89
|
+
if (Array.isArray(custom)) return custom.map((user, index) => normalizeApprover(user, index + 1))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (node.approverType === ApproverType.ROLE) {
|
|
93
|
+
if (typeof this.context.resolveRoleUsers === 'function') {
|
|
94
|
+
const users = this.context.resolveRoleUsers(clone(node.roleList || []), node)
|
|
95
|
+
if (Array.isArray(users)) return users.map((user, index) => normalizeApprover(user, index + 1))
|
|
96
|
+
}
|
|
97
|
+
return (node.roleList || []).map((roleId, index) => ({
|
|
98
|
+
id: `role_${roleId}`,
|
|
99
|
+
name: node.roleNameMap?.[roleId] || `角色${roleId}`,
|
|
100
|
+
roleId
|
|
101
|
+
}))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (node.approverType === ApproverType.SUPERIOR_LEADER) {
|
|
105
|
+
if (typeof this.context.getSuperiorLeader === 'function') {
|
|
106
|
+
const users = this.context.getSuperiorLeader(node.superiorLevel || 1, node)
|
|
107
|
+
if (Array.isArray(users)) return users.map((user, index) => normalizeApprover(user, index + 1))
|
|
108
|
+
if (users) return [normalizeApprover(users, 1)]
|
|
109
|
+
}
|
|
110
|
+
return []
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return (node.userList || []).map((userId, index) => {
|
|
114
|
+
const entry = typeof userId === 'object' ? userId : null
|
|
115
|
+
const fromContext =
|
|
116
|
+
!entry && typeof this.context.resolveUser === 'function'
|
|
117
|
+
? this.context.resolveUser(userId)
|
|
118
|
+
: null
|
|
119
|
+
return normalizeApprover(
|
|
120
|
+
entry ||
|
|
121
|
+
fromContext || {
|
|
122
|
+
id: userId,
|
|
123
|
+
realName:
|
|
124
|
+
node.userNickNames?.[userId] ||
|
|
125
|
+
node.userNickNames?.[String(userId)] ||
|
|
126
|
+
`审批人${userId}`,
|
|
127
|
+
deptName: node.deptNameMap?.[userId] || node.deptNameMap?.[String(userId)] || ''
|
|
128
|
+
},
|
|
129
|
+
index + 1
|
|
130
|
+
)
|
|
131
|
+
})
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
_resolveUsers(ids = [], nameMap = {}) {
|
|
135
|
+
return ids.map((userId) => {
|
|
136
|
+
if (userId && typeof userId === 'object') return normalizeApprover(userId, userId.id)
|
|
137
|
+
const fromContext =
|
|
138
|
+
typeof this.context.resolveUser === 'function' ? this.context.resolveUser(userId) : null
|
|
139
|
+
if (fromContext) return normalizeApprover(fromContext, userId)
|
|
140
|
+
return {
|
|
141
|
+
id: userId,
|
|
142
|
+
name: nameMap?.[userId] || nameMap?.[String(userId)] || `用户${userId}`
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// [文件] gbflowengine·runtime·reject —— 驳回与重新提交
|
|
2
|
+
|
|
3
|
+
// [设计] 方法以对象字面量导出,由 runtime.js 末尾 Object.assign 装配到 FlowRuntime.prototype(同一 this,调用形状不变)。
|
|
4
|
+
|
|
5
|
+
import { FlowNodeType, SignMode, RejectStrategy } from '../flowConstant.js'
|
|
6
|
+
import { clone, normalizeApprover, sameId, RUNNING, REJECTED } from './helpers.js'
|
|
7
|
+
export const rejectMethods = {
|
|
8
|
+
|
|
9
|
+
_handleReject(task, node, operator) {
|
|
10
|
+
const strategy = node?.rejectStrategy || RejectStrategy.TERMINATE
|
|
11
|
+
if (strategy === RejectStrategy.TERMINATE) {
|
|
12
|
+
this._finishFlow(REJECTED)
|
|
13
|
+
return
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const retryLimit = Math.max(0, Math.floor(Number(node?.rejectRetryLimit) || 0))
|
|
17
|
+
const retriesUsed = this.state.history.filter(
|
|
18
|
+
(event) => event.event === 'return' && sameId(event.nodeId, node?.nodeId)
|
|
19
|
+
).length
|
|
20
|
+
if (retryLimit > 0 && retriesUsed >= retryLimit) {
|
|
21
|
+
if (task) {
|
|
22
|
+
task.limitReached = true
|
|
23
|
+
const lastRecord = task.records?.[task.records.length - 1]
|
|
24
|
+
if (lastRecord) lastRecord.limitReached = true
|
|
25
|
+
}
|
|
26
|
+
this.state.history.push(
|
|
27
|
+
this._event('rejectLimitReached', {
|
|
28
|
+
nodeId: node?.nodeId,
|
|
29
|
+
nodeName: node?.nodeName,
|
|
30
|
+
taskId: task?.id,
|
|
31
|
+
strategy,
|
|
32
|
+
retryLimit,
|
|
33
|
+
retriesUsed,
|
|
34
|
+
approver: operator
|
|
35
|
+
})
|
|
36
|
+
)
|
|
37
|
+
this._warn(
|
|
38
|
+
'REJECT_RETRY_LIMIT',
|
|
39
|
+
`审批节点「${node?.nodeName || ''}」已达到最大退回次数(${retryLimit} 次),本次流程不通过`
|
|
40
|
+
)
|
|
41
|
+
this._finishFlow(REJECTED)
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let targetNodeId = null
|
|
46
|
+
if (strategy === RejectStrategy.TO_INITIATOR) {
|
|
47
|
+
targetNodeId = this.workflow.nodeId
|
|
48
|
+
} else if (strategy === RejectStrategy.TO_SPECIFIED_NODE) {
|
|
49
|
+
targetNodeId = node.rejectTargetNodeId
|
|
50
|
+
} else if (strategy === RejectStrategy.TO_PREVIOUS) {
|
|
51
|
+
const previous = [...this.state.history]
|
|
52
|
+
.reverse()
|
|
53
|
+
.find(
|
|
54
|
+
(event) =>
|
|
55
|
+
event.event === 'approve' &&
|
|
56
|
+
event.nodeId !== node.nodeId &&
|
|
57
|
+
this.getNode(event.nodeId)?.type === FlowNodeType.APPROVE
|
|
58
|
+
)
|
|
59
|
+
targetNodeId = previous?.nodeId ?? this.workflow.nodeId
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const targetNode = this.getNode(targetNodeId)
|
|
63
|
+
if (!targetNode) {
|
|
64
|
+
this._warn('REJECT_TARGET_NOT_FOUND', '驳回目标节点不存在,流程已终止')
|
|
65
|
+
this._finishFlow(REJECTED)
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this.state.tasks.forEach((item) => {
|
|
70
|
+
if (item.status === 'pending') item.status = 'cancelled'
|
|
71
|
+
})
|
|
72
|
+
this.state.tokens = []
|
|
73
|
+
this.joinGroups.clear()
|
|
74
|
+
this.state.status = RUNNING
|
|
75
|
+
const previousCycle = this.state.cycle || 1
|
|
76
|
+
this.state.cycle = previousCycle + 1
|
|
77
|
+
this.state.history.push(
|
|
78
|
+
this._event('return', {
|
|
79
|
+
nodeId: node?.nodeId,
|
|
80
|
+
targetNodeId: targetNode.nodeId,
|
|
81
|
+
strategy,
|
|
82
|
+
approver: operator,
|
|
83
|
+
previousCycle,
|
|
84
|
+
retryIndex: retriesUsed + 1,
|
|
85
|
+
retryLimit
|
|
86
|
+
})
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
const token = this._createToken(targetNode.nodeId)
|
|
90
|
+
this.state.tokens.push(token)
|
|
91
|
+
if (targetNode.type === FlowNodeType.START) {
|
|
92
|
+
this._createResubmitTask(targetNode, token)
|
|
93
|
+
}
|
|
94
|
+
this._emit('onReturned', {
|
|
95
|
+
runtime: this,
|
|
96
|
+
targetNode: clone(targetNode),
|
|
97
|
+
strategy,
|
|
98
|
+
state: this.getState()
|
|
99
|
+
})
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
_createResubmitTask(startNode, token) {
|
|
103
|
+
const initiator =
|
|
104
|
+
this.state.initiator ||
|
|
105
|
+
(this.context.initiator ? normalizeApprover(this.context.initiator, 'initiator') : null)
|
|
106
|
+
if (!initiator || initiator.id === undefined || initiator.id === null) {
|
|
107
|
+
this._warn('INITIATOR_MISSING', '退回发起人时缺少发起人信息,将直接重新进入审批')
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
this.seq.task += 1
|
|
111
|
+
const task = {
|
|
112
|
+
id: `task_${this.seq.task}`,
|
|
113
|
+
nodeId: startNode.nodeId,
|
|
114
|
+
nodeName: startNode.nodeName || '发起人',
|
|
115
|
+
tokenId: token.id,
|
|
116
|
+
kind: 'submit',
|
|
117
|
+
signMode: SignMode.OR,
|
|
118
|
+
approvers: [initiator],
|
|
119
|
+
approvedBy: [],
|
|
120
|
+
records: [],
|
|
121
|
+
status: 'pending',
|
|
122
|
+
cycle: this.state.cycle || 1,
|
|
123
|
+
createdAt: new Date().toISOString()
|
|
124
|
+
}
|
|
125
|
+
token.blockedTaskId = task.id
|
|
126
|
+
this.state.tasks.push(task)
|
|
127
|
+
this.state.history.push(
|
|
128
|
+
this._event('taskCreated', { nodeId: startNode.nodeId, taskId: task.id, kind: 'submit' })
|
|
129
|
+
)
|
|
130
|
+
this._emit('onTaskCreated', {
|
|
131
|
+
runtime: this,
|
|
132
|
+
task: clone(task),
|
|
133
|
+
node: clone(startNode),
|
|
134
|
+
state: this.getState()
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
}
|