unis-oa/frontend/src/utils/nodeConfig.ts

324 lines
15 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 环节配置的分组成人。
*
* 右侧那一栏过去把一个环节的所有开关从上到下摊平,人工环节要滚好几屏才看完。
* 这里把"同一件事的配置"并成一格(处理人 / 表单与字段权限 / 流转与超时 / 抄送与写回……),
* 右栏只留摘要与入口,展开与弹窗读的是同一份分组定义,两处不会各说一套。
*
* 纯计算、不碰 DOM:摘要口径要能被测试钉住,免得以后又变成"只有画布知道自己在配什么"。
*/
import type { Activity } from '@/types'
import { gateDigestText, gateEnabled, gateOf } from './nodeGate'
import { formModeOf } from './formBinding'
import { permsOf } from './formRuntime'
import { mountSummary } from './formWriteback'
export type NodeGroupKey =
| 'basics'
| 'approver'
| 'form'
| 'collab'
| 'openApprove'
| 'cc'
| 'writeback'
| 'gate'
| 'quota'
| 'routes'
| 'payload'
/** 分组标题:``process.`` 下的 i18n 键。 */
export const NODE_GROUP_LABEL_KEYS: Record<NodeGroupKey, string> = {
basics: 'groupBasics',
approver: 'groupApprover',
form: 'groupForm',
collab: 'groupCollab',
openApprove: 'groupOpenApprove',
cc: 'groupCc',
writeback: 'groupWriteback',
gate: 'groupGate',
quota: 'groupQuota',
routes: 'groupRoutes',
payload: 'groupPayload'
}
/** 每种环节有哪几格;顺序就是弹窗里那一列 tab 的顺序。 */
/**
* 每种环节有哪几格;顺序就是弹窗里那一列 tab 的顺序。
*
* 「写回挂载」每种类型都有:要发回业务系统的不止审批环节——发起那一刻要建单(创建)、
* 结束那一刻要归档、脚本环节也能按结果分流,运行期那两处触发点对所有环节都成立。
*
* 「表单与字段权限」开始环节也有:发起页铺的就是流程默认那张表单,运行时把
* ``definition.begin`` 一起交给 ``_apply_form_runtime`` 与 ``_validate_form``,
* 藏起来的格子不上发起页、也不参与必填 —— 以前设计器里这一格只长在人工环节上。
*/
const GROUPS_BY_TYPE: Record<string, NodeGroupKey[]> = {
// 闸门排在写回前面:先问"这一单能不能往外走",再说"往外走的时候要发什么"。
// 开始环节也长出「外部审批」这一格:第三方替发起人按「提交」这颗钮(hold 建单、
// 表态或点头才真正流转),与人工环节的「替他按审批钮」是同一套配置的两种落点。
begin: ['basics', 'form', 'gate', 'openApprove', 'writeback'],
manual: ['basics', 'approver', 'form', 'collab', 'openApprove', 'cc', 'gate', 'writeback'],
choice: ['basics', 'routes', 'writeback'],
parallel: ['basics', 'writeback'],
merge: ['basics', 'quota', 'writeback'],
split: ['basics', 'routes', 'writeback'],
script: ['basics', 'payload', 'writeback'],
invoke: ['basics', 'payload', 'writeback'],
embed: ['basics', 'payload', 'writeback'],
delay: ['basics', 'payload', 'writeback'],
end: ['basics', 'cc', 'writeback'],
cancel: ['basics', 'cc', 'writeback']
}
/** 认不出的类型至少要有「基本信息」,别让弹窗开出一个空壳。 */
export function nodeGroups(type: string): NodeGroupKey[] {
return GROUPS_BY_TYPE[type] ?? ['basics']
}
/** 「执行内容」这一格在四种环节上叫不同名字,别让人对着四个字猜。 */
export function groupLabelKey(group: NodeGroupKey, type: string): string {
if (group !== 'payload') return NODE_GROUP_LABEL_KEYS[group]
if (type === 'script') return 'groupPayloadScript'
if (type === 'invoke') return 'groupPayloadInvoke'
if (type === 'embed') return 'groupPayloadEmbed'
if (type === 'delay') return 'groupPayloadDelay'
return NODE_GROUP_LABEL_KEYS.payload
}
/** 协作开关:与环节面板上那一排复选框同一份清单(含单列的退回 / 加签)。 */
export const COLLAB_FLAG_KEYS = [
'allowGoBack',
'allowAddTask',
'allowCc',
'allowShare',
'allowComment',
'allowTransfer',
'allowRecall',
'allowPress'
] as const
export interface GroupDigest {
/** 一行摘要;空串表示这一格没什么可说。 */
text: string
/** warn 用琥珀色:不是错,但多半不是设计者想要的现状。 */
tone?: 'default' | 'warn'
}
export interface DigestContext {
/** 表单 id → 表单名。 */
formName: (id: string) => string
/** 流程默认表单 id / 名。 */
defaultForm?: string
defaultFormName?: string
/** 子流程 id → 流程名。 */
processName?: (id: string) => string
/** 本环节的出口路由条数:分支那一格要说"现在连成了什么样"。 */
outboundCount?: number
/** 带参数的文案由调用方用 i18n 渲染。 */
fmt: (key: string, params?: Record<string, string | number>) => string
/**
* 闸门那一格读的是 ``gate.*`` 词条(同一批词条运行期的审批页也要用),
* 与这里默认的 ``process.*`` 不是一个命名空间,所以单独给一份取词。
*/
fmtGate?: (key: string, params?: Record<string, string | number>) => string
}
/** 审批方式那四档的名字,与环节面板上的下拉一致。 */
const MODE_LABEL_KEYS: Record<string, string> = {
any: 'modeAny',
all: 'modeAll',
ratio: 'modeRatio',
serial: 'modeSerial'
}
const TIMEOUT_LABEL_KEYS: Record<string, string> = {
notify: 'timeoutNotify',
autoPass: 'timeoutPass',
autoReject: 'timeoutReject'
}
/** 没写过的开关按"开"算,与运行时默认一致(见 NodeInspector 的 flag())。 */
function flagOn(config: Record<string, unknown>, key: string): boolean {
return Boolean(config[key] ?? true)
}
function hostOf(url: string): string {
return url.replace(/^[a-z]+:\/\//i, '').split('/')[0] || url
}
function approverDigest(node: Activity, fmt: DigestContext['fmt']): GroupDigest {
const config = (node.config ?? {}) as Record<string, unknown>
const modeLabel = fmt(MODE_LABEL_KEYS[String(config.mode ?? 'any')] ?? 'modeAny')
const rows = Array.isArray(config.taskValues) ? (config.taskValues as unknown[]) : []
// 免审规则只在配了的时候占位:常态是零条,没必要每格都多缀一句「免审 0 条」
const skip = Array.isArray(config.skipRules) ? (config.skipRules as unknown[]) : []
const must = Array.isArray(config.mustRules) ? (config.mustRules as unknown[]) : []
// 两套规则各报各的条数,右栏一行就能看出这一环的条件有多重
const suffix =
(skip.length ? ` · ${fmt('digestSkip', { count: skip.length })}` : '') +
(must.length ? ` · ${fmt('digestMust', { count: must.length })}` : '')
if (String(config.taskScript ?? '').trim()) return { text: `${modeLabel} · ${fmt('digestApproverScript')}${suffix}` }
if (!rows.length) return { text: fmt('digestApproverEmpty'), tone: 'warn' }
return { text: `${modeLabel} · ${fmt('digestApproverCount', { count: rows.length })}${suffix}` }
}
function formDigest(node: Activity, ctx: DigestContext): GroupDigest {
const { fmt, defaultForm, defaultFormName } = ctx
const own = formModeOf(node) === 'own'
const id = own ? node.form || '' : defaultForm || ''
const name = ctx.formName(id) || (own ? '' : defaultFormName || '')
if (!id) return { text: fmt('digestFormNone'), tone: 'warn' }
return { text: fmt(own ? 'digestOwnForm' : 'digestInheritForm', { name }) }
}
function collabDigest(node: Activity, fmt: DigestContext['fmt']): GroupDigest {
const config = (node.config ?? {}) as Record<string, unknown>
const touched = COLLAB_FLAG_KEYS.filter((key) => key in config).length
const on = COLLAB_FLAG_KEYS.filter((key) => flagOn(config, key)).length
const parts = [touched ? fmt('digestCollabOn', { on, total: COLLAB_FLAG_KEYS.length }) : fmt('digestCollabDefault')]
const minutes = Number(config.expireValue ?? 0)
if (minutes > 0) {
const action = String(config.timeoutAction ?? 'notify')
parts.push(fmt('digestTimeout', { n: minutes, action: fmt(TIMEOUT_LABEL_KEYS[action] ?? 'timeoutNotify') }))
} else {
parts.push(fmt('digestNoTimer'))
}
return { text: parts.join(' · ') }
}
function ccDigest(node: Activity, fmt: DigestContext['fmt']): GroupDigest {
const config = (node.config ?? {}) as Record<string, unknown>
const rules = Array.isArray(config.ccRules) ? (config.ccRules as { expression?: string }[]) : []
// 带条件的抄送只在部分单子上发,右栏先说清有几条不是每次都抄
const conditional = rules.filter((rule) => String(rule?.expression ?? '').trim()).length
const parts = [rules.length ? fmt('digestCc', { count: rules.length }) : fmt('digestCcNone')]
if (conditional) parts.push(fmt('digestCcCondition', { count: conditional }))
return { text: parts.join(' · ') }
}
/** 与后端 ``node_approve.LOCK_ACTIONS`` / ``LOCK_ACTIONS_DEFAULT`` 同一份清单与默认:
* 勾了锁但没显式写 actions 时按这四颗算,认不出的词不计数(后端把它们丢掉)。 */
const LOCK_ACTIONS = ['handle', 'start', 'goBack', 'recall', 'addTask', 'transfer']
const DEFAULT_LOCK_ACTIONS = ['handle', 'start', 'goBack', 'recall']
/** 这一环实际锁了哪几颗:口径抄 backend 的 ``locked_actions``,别自己解释空值。 */
export function lockedActionsOf(openApprove: Record<string, any> | null | undefined): string[] {
const actions = openApprove?.oaLock?.actions
const picked = Array.isArray(actions)
? actions.filter((item: unknown) => LOCK_ACTIONS.includes(String(item)))
: []
return picked.length ? picked : [...DEFAULT_LOCK_ACTIONS]
}
/** 外部审批那一格:开着就说对谁开、钉了几条出口;关着没话可说。 */
function openApproveDigest(node: Activity, fmt: DigestContext['fmt']): GroupDigest {
const cfg = (node.config ?? {}) as Record<string, any>
const open = cfg.openApprove
if (!open || open.enabled !== true) return { text: '' }
const apps = Array.isArray(open.apps) ? open.apps.filter(Boolean).length : 0
const routes = open.routes && typeof open.routes === 'object' ? Object.keys(open.routes).length : 0
const parts = [fmt('openApproveDigestOn')]
if (apps > 0) parts.push(fmt('openApproveDigestApps', { count: apps }))
if (routes > 0) parts.push(fmt('openApproveDigestRoutes', { count: routes }))
// 锁这一栏单独说一句:勾掉外部审批但留着锁、或反过来,右栏要看得出是哪一个在起作用
const lock = open.oaLock
if (lock && lock.enabled === true) {
parts.push(fmt('openApproveDigestLocked', { count: lockedActionsOf(open).length }))
}
return { text: parts.join(' · ') }
}
/**
* 写回那一格的摘要:几条规则、几个模板。
*
* 只数"挂了几条规则"没用——真正决定上游收不收的是模板在不在,所以两个数都给;
* 一条都没挂就直说,右栏扫一眼就知道这一环节不发东西。
*/
function writebackDigest(node: Activity, fmt: DigestContext['fmt']): GroupDigest {
const config = (node.config ?? {}) as Record<string, unknown>
const { rules, templates } = mountSummary(config.writeback as Parameters<typeof mountSummary>[0])
if (!templates) return { text: fmt('digestWritebackNone') }
const parts = [rules > 1 ? fmt('digestWritebackRules', { count: rules }) : '']
parts.push(fmt('digestWriteback', { count: templates }))
return { text: parts.filter(Boolean).join(' · ') }
}
function quotaDigest(node: Activity, fmt: DigestContext['fmt']): GroupDigest {
const config = (node.config ?? {}) as Record<string, unknown>
const count = Number(config.passCount ?? 0)
const ratio = Number(config.passRatio ?? 0)
if (count) return { text: fmt('digestQuotaCount', { n: count }) }
if (ratio) return { text: fmt('digestQuotaRatio', { n: ratio }) }
return { text: fmt('digestQuotaNone'), tone: 'warn' }
}
/**
* 闸门那一格:一句话说清「判哪个场景、管哪几条出口、过不了怎么办」。
*
* 没挂就不占一行(这是常态)。挂了的这一环必须在右栏看得出它**默认是挡住的**,
* 否则配的人和批的人对"这单为什么点不动"各信一套。
*/
function gateDigest(node: Activity, fmt: NonNullable<DigestContext['fmtGate']>): GroupDigest {
const cfg = gateOf(node.config as Record<string, unknown> | null)
if (!cfg || !gateEnabled(cfg)) return { text: fmt('digestOff') }
return { text: gateDigestText(cfg, fmt) }
}
function routesDigest(node: Activity, ctx: DigestContext): GroupDigest {
const { fmt, outboundCount = 0 } = ctx
if (node.type === 'split') {
const path = String(node.config?.dataPath ?? '').trim()
return path
? { text: `${fmt('digestSplitPath', { path })} · ${fmt('digestRoutes', { count: outboundCount })}` }
: { text: fmt('digestSplitNone'), tone: 'warn' }
}
if (!outboundCount) return { text: fmt('digestRoutesNone'), tone: 'warn' }
return { text: fmt('digestRoutes', { count: outboundCount }) }
}
function payloadDigest(node: Activity, ctx: DigestContext): GroupDigest {
const { fmt } = ctx
const config = (node.config ?? {}) as Record<string, unknown>
if (node.type === 'script') {
return String(config.script ?? '').trim()
? { text: fmt('digestScriptDone') }
: { text: fmt('digestScriptNone'), tone: 'warn' }
}
if (node.type === 'invoke') {
const url = String(config.url ?? '').trim()
return url ? { text: hostOf(url) } : { text: fmt('digestInvokeNone'), tone: 'warn' }
}
if (node.type === 'embed') {
const id = String(config.process ?? '').trim()
if (!id) return { text: fmt('digestEmbedNone'), tone: 'warn' }
return { text: ctx.processName?.(id) || id }
}
const minutes = Number(config.minutes ?? 0)
return minutes > 0 ? { text: fmt('digestDelay', { n: minutes }) } : { text: fmt('digestDelayNone'), tone: 'warn' }
}
/** 字段权限改了几项:右栏要在"表单"那一格下面再说一行。 */
export function permsDigest(node: Activity, fmt: DigestContext['fmt']): GroupDigest {
const count = Object.keys(permsOf(node.config as Record<string, unknown> | null)).length
return { text: count ? fmt('digestPerms', { count }) : fmt('digestPermsNone') }
}
/** 一格的摘要:右栏那一行和弹窗里分组标题下的小字都用它。 */
export function groupDigest(group: NodeGroupKey, node: Activity, ctx: DigestContext): GroupDigest {
if (group === 'basics') return { text: String(node.description ?? '').trim() }
if (group === 'approver') return approverDigest(node, ctx.fmt)
if (group === 'form') {
const form = formDigest(node, ctx)
const perms = permsDigest(node, ctx.fmt)
return { text: [form.text, perms.text].filter(Boolean).join(' · '), tone: form.tone }
}
if (group === 'collab') return collabDigest(node, ctx.fmt)
if (group === 'openApprove') return openApproveDigest(node, ctx.fmt)
if (group === 'cc') return ccDigest(node, ctx.fmt)
if (group === 'writeback') return writebackDigest(node, ctx.fmt)
if (group === 'gate') return gateDigest(node, ctx.fmtGate ?? ctx.fmt)
if (group === 'quota') return quotaDigest(node, ctx.fmt)
if (group === 'routes') return routesDigest(node, ctx)
return payloadDigest(node, ctx)
}