128 lines
3.8 KiB
JavaScript
128 lines
3.8 KiB
JavaScript
/** 与后端 oa_engine.INITIATOR_CONDITION_FIELD 对齐:不依赖 formData */
|
|
const INITIATOR_CONDITION_FIELD = '__initiator__'
|
|
const FORMULA_FIELD_RE = /\{([^{}]+)\}/g
|
|
|
|
function nodeType(node) {
|
|
return String((node && (node.type || node.nodeType)) || '').trim()
|
|
}
|
|
|
|
function addField(name, fields, seen) {
|
|
const key = String(name || '').trim()
|
|
if (!key || seen.has(key)) return
|
|
if (key === INITIATOR_CONDITION_FIELD) return
|
|
seen.add(key)
|
|
fields.push(key)
|
|
}
|
|
|
|
function addFormulaFields(text, fields, seen) {
|
|
if (!text) return
|
|
FORMULA_FIELD_RE.lastIndex = 0
|
|
let m
|
|
while ((m = FORMULA_FIELD_RE.exec(String(text)))) {
|
|
addField(m[1], fields, seen)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 收集流程树中非默认条件分支引用的表单字段(去重保序)。
|
|
* 语义对齐 backend `oa_engine._collect_condition_fields`。
|
|
*/
|
|
export function collectConditionFields(node) {
|
|
const fields = []
|
|
const seen = new Set()
|
|
|
|
function walk(n) {
|
|
if (!n || typeof n !== 'object') return
|
|
if (nodeType(n) === 'condition') {
|
|
for (const g of n.conditionGroups || []) {
|
|
if (!g || typeof g !== 'object') continue
|
|
const rules = g.rules
|
|
const when = g.when
|
|
if (g.isDefault || (!rules && !when)) {
|
|
if (g.child && typeof g.child === 'object') walk(g.child)
|
|
continue
|
|
}
|
|
if (Array.isArray(rules) && rules.length) {
|
|
for (const r of rules) {
|
|
if (r && typeof r === 'object' && r.field != null) {
|
|
addField(r.field, fields, seen)
|
|
}
|
|
}
|
|
} else if (typeof rules === 'string' && rules.trim()) {
|
|
addFormulaFields(rules, fields, seen)
|
|
} else if (when) {
|
|
addFormulaFields(when, fields, seen)
|
|
}
|
|
if (g.child && typeof g.child === 'object') walk(g.child)
|
|
}
|
|
if (n.child && typeof n.child === 'object') walk(n.child)
|
|
return
|
|
}
|
|
if (n.child && typeof n.child === 'object') walk(n.child)
|
|
for (const g of n.conditionGroups || []) {
|
|
if (g && typeof g === 'object' && g.child && typeof g.child === 'object') {
|
|
walk(g.child)
|
|
}
|
|
}
|
|
}
|
|
|
|
walk(node)
|
|
return fields
|
|
}
|
|
|
|
function sortKeysDeep(value) {
|
|
if (Array.isArray(value)) return value.map(sortKeysDeep)
|
|
if (value && typeof value === 'object') {
|
|
const out = {}
|
|
for (const k of Object.keys(value).sort()) {
|
|
out[k] = sortKeysDeep(value[k])
|
|
}
|
|
return out
|
|
}
|
|
return value
|
|
}
|
|
|
|
export function pickRelevantFormData(formData, conditionFields) {
|
|
const data = formData && typeof formData === 'object' ? formData : {}
|
|
const fields = Array.isArray(conditionFields) ? conditionFields : []
|
|
const out = {}
|
|
for (const f of fields) {
|
|
out[f] = data[f]
|
|
}
|
|
return out
|
|
}
|
|
|
|
/** 稳定序列化:用于判断预览请求是否实质变化 */
|
|
export function buildFlowPreviewRequestKey({
|
|
formData,
|
|
initiatorSelectByNode,
|
|
conditionFields
|
|
} = {}) {
|
|
const payload = {
|
|
formData: pickRelevantFormData(formData, conditionFields),
|
|
initiatorSelectByNode:
|
|
initiatorSelectByNode && typeof initiatorSelectByNode === 'object'
|
|
? initiatorSelectByNode
|
|
: {}
|
|
}
|
|
return JSON.stringify(sortKeysDeep(payload))
|
|
}
|
|
|
|
/**
|
|
* @param {string|null|undefined} prevKey 上次成功预览的 key
|
|
* @param {string} nextKey 本次请求 key
|
|
* @param {{ inFlightKey?: string|null }} [opts] 在途请求 key;有在途时仅跳过相同 key,避免回滚竞态
|
|
*/
|
|
export function shouldSkipFlowPreviewRequest(prevKey, nextKey, opts = {}) {
|
|
const inFlightKey = opts.inFlightKey
|
|
if (inFlightKey != null) {
|
|
return inFlightKey === nextKey
|
|
}
|
|
return prevKey != null && prevKey === nextKey
|
|
}
|
|
|
|
/** 已有预览结果时静默刷新,避免整区「加载中」闪烁 */
|
|
export function shouldShowFlowPreviewSpinner({ hasCompletedPreview } = {}) {
|
|
return !hasCompletedPreview
|
|
}
|