index.vue 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. <template>
  2. <div class="designer-container">
  3. <!-- 流程信息栏(新增/编辑模式显示) -->
  4. <div v-if="flowInfoVisible" class="flow-info-bar">
  5. <el-tag v-if="mode === 'create'" type="primary">新增流程</el-tag>
  6. <el-tag v-else-if="mode === 'edit'" type="warning">编辑流程</el-tag>
  7. <span class="flow-info-item"><strong>编码:</strong>{{ flowCode }}</span>
  8. <span class="flow-info-item"><strong>名称:</strong>{{ flowName }}</span>
  9. <span v-if="flowCategory" class="flow-info-item"><strong>分类:</strong>{{ flowCategory }}</span>
  10. </div>
  11. <div class="node-panel">
  12. <div class="panel-title">节点类型</div>
  13. <div
  14. v-for="node in nodeTypes"
  15. :key="node.type"
  16. class="node-item"
  17. draggable="true"
  18. @dragstart="handleDragStart($event, node)"
  19. >
  20. <div class="node-icon" :style="{ backgroundColor: node.color }">
  21. <el-icon><component :is="node.icon" /></el-icon>
  22. </div>
  23. <div class="node-name">{{ node.label }}</div>
  24. </div>
  25. </div>
  26. <div class="canvas-area" @drop="handleDrop" @dragover.prevent>
  27. <div ref="lfContainer" class="lf-container" />
  28. <div class="toolbar">
  29. <el-button type="primary" size="small" @click="handleSave">保存</el-button>
  30. <el-button size="small" @click="handleClear">清空</el-button>
  31. <el-button size="small" @click="goBack">返回</el-button>
  32. </div>
  33. </div>
  34. <div class="property-panel" v-if="selectedNode">
  35. <div class="panel-title">节点属性</div>
  36. <el-form label-width="80px" size="small">
  37. <el-form-item label="节点ID">
  38. <el-input v-model="selectedNode.id" disabled />
  39. </el-form-item>
  40. <el-form-item label="节点名称">
  41. <el-input v-model="nodeName" @blur="updateNodeName" />
  42. </el-form-item>
  43. <template v-if="selectedNode.type === 'approval-node'">
  44. <el-form-item label="审批员工">
  45. <el-select
  46. v-model="nodeProps.assigneeValue"
  47. filterable
  48. clearable
  49. placeholder="请选择员工管理中的成员"
  50. style="width: 100%"
  51. >
  52. <el-option
  53. v-for="role in roleList"
  54. :key="role.id"
  55. :label="role.roleName"
  56. :value="role.roleCode"
  57. />
  58. </el-select>
  59. </el-form-item>
  60. <el-form-item label="审批方式">
  61. <el-select v-model="nodeProps.approveMode" placeholder="请选择" style="width: 100%">
  62. <el-option label="或签(一人通过即可)" value="or" />
  63. <el-option label="会签(全部通过)" value="and" />
  64. </el-select>
  65. </el-form-item>
  66. <el-form-item label="审批时限">
  67. <el-input-number
  68. v-model="nodeProps.timeoutHours"
  69. :min="1"
  70. :max="720"
  71. :precision="0"
  72. placeholder="不填表示无限制"
  73. controls-position="right"
  74. style="width: 100%"
  75. />
  76. <div class="form-tip">单位:小时,为空则不计算超时</div>
  77. </el-form-item>
  78. <el-form-item label="超时动作">
  79. <el-select v-model="nodeProps.timeoutAction" placeholder="请选择" style="width: 100%">
  80. <el-option label="提醒" value="remind" />
  81. <el-option label="自动通过" value="pass" />
  82. <el-option label="自动驳回" value="reject" />
  83. </el-select>
  84. <div class="form-tip">目前仅实现「提醒」,自动通过/驳回预留</div>
  85. </el-form-item>
  86. </template>
  87. <template v-if="selectedNode.type === 'condition-node'">
  88. <el-form-item label="条件表达式">
  89. <el-input v-model="nodeProps.condition" type="textarea" placeholder="输入条件表达式" />
  90. </el-form-item>
  91. </template>
  92. <template v-if="selectedNode.type === 'cc-node'">
  93. <el-form-item label="抄送人">
  94. <el-select
  95. v-model="nodeProps.ccUsers"
  96. filterable
  97. clearable
  98. placeholder="请选择抄送人"
  99. style="width: 100%"
  100. >
  101. <el-option
  102. v-for="role in roleList"
  103. :key="role.id"
  104. :label="role.roleName"
  105. :value="role.roleCode"
  106. />
  107. </el-select>
  108. </el-form-item>
  109. </template>
  110. </el-form>
  111. </div>
  112. <div class="property-panel empty" v-else>
  113. <div class="panel-title">节点属性</div>
  114. <el-empty description="请选择节点" />
  115. </div>
  116. <!-- 保存确认弹窗(无模式时) -->
  117. <el-dialog v-model="saveDialogVisible" title="保存流程" width="600px">
  118. <el-form ref="saveFormRef" :model="saveForm" :rules="saveFormRules" label-width="100px">
  119. <el-form-item label="流程编码" prop="code">
  120. <el-input v-model="saveForm.code" />
  121. </el-form-item>
  122. <el-form-item label="流程名称" prop="name">
  123. <el-input v-model="saveForm.name" />
  124. </el-form-item>
  125. <el-form-item label="分类">
  126. <el-input v-model="saveForm.category" />
  127. </el-form-item>
  128. <el-form-item label="描述">
  129. <el-input v-model="saveForm.description" type="textarea" />
  130. </el-form-item>
  131. </el-form>
  132. <div class="form-fields-section">
  133. <div class="form-fields-header">
  134. <span class="form-fields-title">流程表单字段</span>
  135. <el-button type="primary" size="small" :icon="Delete" @click="addFormField">添加字段</el-button>
  136. </div>
  137. <el-table :data="formFields" size="small" border style="width: 100%">
  138. <el-table-column label="字段名" min-width="120">
  139. <template #default="{ $index }">
  140. <el-input v-model="formFields[$index].name" placeholder="英文标识" size="small" />
  141. </template>
  142. </el-table-column>
  143. <el-table-column label="显示名" min-width="120">
  144. <template #default="{ $index }">
  145. <el-input v-model="formFields[$index].label" placeholder="中文显示名" size="small" />
  146. </template>
  147. </el-table-column>
  148. <el-table-column label="类型" width="120">
  149. <template #default="{ $index }">
  150. <el-select v-model="formFields[$index].type" placeholder="类型" size="small" style="width: 100%">
  151. <el-option v-for="opt in fieldTypeOptions" :key="opt.value" :label="opt.label" :value="opt.value" />
  152. </el-select>
  153. </template>
  154. </el-table-column>
  155. <el-table-column label="必填" width="70" align="center">
  156. <template #default="{ $index }">
  157. <el-checkbox v-model="formFields[$index].required" />
  158. </template>
  159. </el-table-column>
  160. <el-table-column label="多选" width="70" align="center">
  161. <template #default="{ $index }">
  162. <el-checkbox
  163. v-model="formFields[$index].multiple"
  164. :disabled="formFields[$index].type !== 'select'"
  165. />
  166. </template>
  167. </el-table-column>
  168. <el-table-column label="选项" width="90" align="center">
  169. <template #default="{ $index }">
  170. <el-button
  171. link
  172. type="primary"
  173. size="small"
  174. :disabled="formFields[$index].type !== 'select'"
  175. @click="openOptionDialog($index)"
  176. >
  177. 配置选项
  178. </el-button>
  179. </template>
  180. </el-table-column>
  181. <el-table-column label="操作" width="70" align="center">
  182. <template #default="{ $index }">
  183. <el-button link type="danger" size="small" @click="removeFormField($index)">删除</el-button>
  184. </template>
  185. </el-table-column>
  186. </el-table>
  187. <el-empty v-if="formFields.length === 0" description="暂无字段,点击上方按钮添加" :image-size="60" style="padding: 10px 0;" />
  188. </div>
  189. <template #footer>
  190. <el-button @click="saveDialogVisible = false">取消</el-button>
  191. <el-button type="primary" @click="submitSave">确认保存</el-button>
  192. </template>
  193. </el-dialog>
  194. <!-- 下拉选项配置弹窗 -->
  195. <el-dialog v-model="optionDialogVisible" title="配置下拉选项" width="500px">
  196. <div v-if="optionEditingField" class="option-edit-tip">
  197. 字段:{{ optionEditingField.label || optionEditingField.name }}
  198. <el-checkbox v-model="optionEditingField.multiple" style="margin-left: 16px;">允许多选</el-checkbox>
  199. </div>
  200. <el-table :data="optionList" size="small" border>
  201. <el-table-column label="显示文本" min-width="140">
  202. <template #default="{ $index }">
  203. <el-input v-model="optionList[$index].label" placeholder="显示文本" size="small" />
  204. </template>
  205. </el-table-column>
  206. <el-table-column label="选项值" min-width="140">
  207. <template #default="{ $index }">
  208. <el-input v-model="optionList[$index].value" placeholder="选项值" size="small" />
  209. </template>
  210. </el-table-column>
  211. <el-table-column label="操作" width="70" align="center">
  212. <template #default="{ $index }">
  213. <el-button link type="danger" size="small" @click="removeOption($index)">删除</el-button>
  214. </template>
  215. </el-table-column>
  216. </el-table>
  217. <el-button type="primary" size="small" style="margin-top: 12px;" @click="addOption">添加选项</el-button>
  218. <template #footer>
  219. <el-button @click="optionDialogVisible = false">取消</el-button>
  220. <el-button type="primary" @click="saveOptions">保存</el-button>
  221. </template>
  222. </el-dialog>
  223. </div>
  224. </template>
  225. <script setup lang="ts">
  226. import { ref, onMounted, onUnmounted, nextTick, reactive, computed } from 'vue'
  227. import { useRoute, useRouter } from 'vue-router'
  228. import LogicFlow, { RectNode, RectNodeModel, CircleNode, CircleNodeModel, PolygonNode, PolygonNodeModel } from '@logicflow/core'
  229. import { Menu, Snapshot } from '@logicflow/extension'
  230. import '@logicflow/core/dist/style/index.css'
  231. import '@logicflow/extension/lib/style/index.css'
  232. import { VideoPlay, User, Message, Operation, CircleCheck } from '@element-plus/icons-vue'
  233. import { ElMessage } from 'element-plus'
  234. import type { FormInstance, FormRules } from 'element-plus'
  235. import { listRole } from '@/api/system/role'
  236. import { addDefinition, updateDefinition, getDefinition } from '@/api/flow/definition'
  237. import type { Role } from '@/types/system'
  238. import type { FlowDefinition, FormField, FormFieldOption } from '@/types/flow'
  239. import { Delete } from '@element-plus/icons-vue'
  240. const route = useRoute()
  241. const router = useRouter()
  242. const lfContainer = ref<HTMLDivElement>()
  243. let lf: LogicFlow | null = null
  244. const mode = computed(() => (route.query.mode as string) || '')
  245. const flowId = computed(() => (route.query.id as string) || '')
  246. const flowCode = computed(() => (route.query.code as string) || '')
  247. const flowName = computed(() => (route.query.name as string) || '')
  248. const flowCategory = computed(() => (route.query.category as string) || '')
  249. const flowDescription = computed(() => (route.query.description as string) || '')
  250. const flowInfoVisible = computed(() => mode.value === 'create' || mode.value === 'edit')
  251. const selectedNode = ref<any>(null)
  252. const roleList = ref<Role[]>([])
  253. const nodeProps = reactive<Record<string, any>>({
  254. assigneeType: 'ROLE',
  255. assigneeValue: '',
  256. approveMode: 'or',
  257. timeoutHours: undefined as number | undefined,
  258. timeoutAction: 'remind',
  259. condition: '',
  260. ccUsers: ''
  261. })
  262. const nodeName = computed({
  263. get: () => selectedNode.value?.text?.value || '',
  264. set: (val: string) => {
  265. if (selectedNode.value) {
  266. selectedNode.value.text = { ...selectedNode.value.text, value: val }
  267. }
  268. }
  269. })
  270. const nodeTypes = [
  271. { type: 'start-node', label: '开始', icon: VideoPlay, color: '#67C23A' },
  272. { type: 'approval-node', label: '审批', icon: User, color: '#409EFF' },
  273. { type: 'cc-node', label: '抄送', icon: Message, color: '#E6A23C' },
  274. { type: 'condition-node', label: '条件', icon: Operation, color: '#909399' },
  275. { type: 'end-node', label: '结束', icon: CircleCheck, color: '#F56C6C' }
  276. ]
  277. function getDefaultProps(type: string) {
  278. if (type === 'approval-node') return { assigneeType: 'ROLE', assigneeValue: '', approveMode: 'or', timeoutHours: undefined, timeoutAction: 'remind' }
  279. if (type === 'condition-node') return { condition: '' }
  280. if (type === 'cc-node') return { ccUsers: '' }
  281. return {}
  282. }
  283. function saveCurrentNodeProps() {
  284. if (selectedNode.value && lf) {
  285. const type = selectedNode.value.type
  286. const defaultProps = getDefaultProps(type)
  287. // 只保存当前节点类型相关的属性,避免不同类型节点属性互相污染
  288. const props: Record<string, any> = JSON.parse(JSON.stringify(defaultProps))
  289. if (type === 'approval-node') {
  290. props.assigneeType = 'ROLE'
  291. props.assigneeValue = nodeProps.assigneeValue ?? ''
  292. props.approveMode = nodeProps.approveMode ?? 'or'
  293. props.timeoutHours = nodeProps.timeoutHours
  294. props.timeoutAction = nodeProps.timeoutAction ?? 'remind'
  295. } else if (type === 'condition-node') {
  296. props.condition = nodeProps.condition ?? ''
  297. } else if (type === 'cc-node') {
  298. props.ccUsers = nodeProps.ccUsers ?? ''
  299. }
  300. lf.setProperties(selectedNode.value.id, props)
  301. }
  302. }
  303. function loadNodeProps(data: any) {
  304. const props = data.properties || getDefaultProps(data.type)
  305. const cloned = JSON.parse(JSON.stringify(props))
  306. // 先重置为默认值,避免上一个节点的属性残留
  307. Object.assign(nodeProps, getDefaultProps(data.type))
  308. // 兼容旧格式:approver + approveType -> 新格式
  309. if (data.type === 'approval-node') {
  310. let assigneeType = 'ROLE'
  311. let assigneeValue = cloned.assigneeValue ?? ''
  312. let approveMode = cloned.approveMode ?? cloned.approveType ?? 'or'
  313. if (!cloned.assigneeValue && cloned.approver) {
  314. assigneeValue = cloned.approver
  315. }
  316. // 旧数据中的 USER/SELF/LEADER 不再支持,统一清空让用户重新选择员工角色
  317. if (cloned.assigneeType && cloned.assigneeType !== 'ROLE') {
  318. assigneeValue = ''
  319. }
  320. nodeProps.assigneeType = assigneeType
  321. nodeProps.assigneeValue = assigneeValue
  322. nodeProps.approveMode = approveMode
  323. nodeProps.timeoutHours = cloned.timeoutHours ?? undefined
  324. nodeProps.timeoutAction = cloned.timeoutAction ?? 'remind'
  325. } else if (data.type === 'condition-node') {
  326. nodeProps.condition = cloned.condition ?? ''
  327. } else if (data.type === 'cc-node') {
  328. nodeProps.ccUsers = cloned.ccUsers ?? ''
  329. }
  330. }
  331. function registerNodes() {
  332. if (!lf) return
  333. class StartNodeModel extends CircleNodeModel {
  334. setAttributes() {
  335. this.r = 30
  336. this.text.value = '开始'
  337. }
  338. getNodeStyle() {
  339. const style = super.getNodeStyle()
  340. style.fill = '#67C23A'
  341. style.stroke = '#67C23A'
  342. return style
  343. }
  344. getTextStyle() {
  345. const style = super.getTextStyle()
  346. style.color = '#fff'
  347. return style
  348. }
  349. }
  350. lf.register({ type: 'start-node', view: CircleNode, model: StartNodeModel })
  351. class ApprovalNodeModel extends RectNodeModel {
  352. setAttributes() {
  353. this.width = 160
  354. this.height = 60
  355. this.radius = 8
  356. this.text.value = '审批节点'
  357. }
  358. getNodeStyle() {
  359. const style = super.getNodeStyle()
  360. style.fill = '#fff'
  361. style.stroke = '#409EFF'
  362. style.strokeWidth = 1
  363. return style
  364. }
  365. }
  366. lf.register({ type: 'approval-node', view: RectNode, model: ApprovalNodeModel })
  367. class CcNodeModel extends RectNodeModel {
  368. setAttributes() {
  369. this.width = 160
  370. this.height = 60
  371. this.radius = 8
  372. this.text.value = '抄送节点'
  373. }
  374. getNodeStyle() {
  375. const style = super.getNodeStyle()
  376. style.fill = '#fff'
  377. style.stroke = '#E6A23C'
  378. style.strokeWidth = 1
  379. return style
  380. }
  381. }
  382. lf.register({ type: 'cc-node', view: RectNode, model: CcNodeModel })
  383. class ConditionNodeModel extends PolygonNodeModel {
  384. setAttributes() {
  385. this.points = [[60, 0], [120, 40], [60, 80], [0, 40]]
  386. this.text.value = '条件'
  387. }
  388. getNodeStyle() {
  389. const style = super.getNodeStyle()
  390. style.fill = '#fff'
  391. style.stroke = '#909399'
  392. return style
  393. }
  394. }
  395. lf.register({ type: 'condition-node', view: PolygonNode, model: ConditionNodeModel })
  396. class EndNodeModel extends CircleNodeModel {
  397. setAttributes() {
  398. this.r = 30
  399. this.text.value = '结束'
  400. }
  401. getNodeStyle() {
  402. const style = super.getNodeStyle()
  403. style.fill = '#F56C6C'
  404. style.stroke = '#F56C6C'
  405. return style
  406. }
  407. getTextStyle() {
  408. const style = super.getTextStyle()
  409. style.color = '#fff'
  410. return style
  411. }
  412. }
  413. lf.register({ type: 'end-node', view: CircleNode, model: EndNodeModel })
  414. }
  415. function initLogicFlow() {
  416. if (!lfContainer.value) return
  417. lf = new LogicFlow({
  418. container: lfContainer.value,
  419. grid: true,
  420. plugins: [Menu, Snapshot],
  421. pluginsOptions: {
  422. menu: {
  423. nodeMenu: [{
  424. text: '删除',
  425. callback(node: any) { lf?.deleteNode(node.id) }
  426. }]
  427. }
  428. } as any
  429. })
  430. registerNodes()
  431. lf.on('node:click', ({ data }: any) => {
  432. saveCurrentNodeProps()
  433. selectedNode.value = data
  434. loadNodeProps(data)
  435. })
  436. lf.on('blank:click', () => {
  437. saveCurrentNodeProps()
  438. selectedNode.value = null
  439. })
  440. // 编辑模式:加载已有流程图
  441. if (mode.value === 'edit' && flowId.value) {
  442. getDefinition(Number(flowId.value)).then((def: FlowDefinition) => {
  443. parseFormSchema(def.formSchema)
  444. if (def.flowJson) {
  445. try {
  446. const graphData = convertToFrontendFormat(JSON.parse(def.flowJson))
  447. lf?.render(graphData)
  448. } catch {
  449. lf?.render({ nodes: defaultNodes(), edges: [] })
  450. }
  451. } else {
  452. lf?.render({ nodes: defaultNodes(), edges: [] })
  453. }
  454. }).catch(() => {
  455. lf?.render({ nodes: defaultNodes(), edges: [] })
  456. })
  457. } else {
  458. lf.render({ nodes: defaultNodes(), edges: [] })
  459. }
  460. }
  461. function defaultNodes() {
  462. return [
  463. { id: 'start-1', type: 'start-node', x: 200, y: 100, text: '开始' },
  464. { id: 'end-1', type: 'end-node', x: 200, y: 500, text: '结束' }
  465. ]
  466. }
  467. // 节点类型映射:LogicFlow <-> 后端
  468. const typeToBackend: Record<string, string> = {
  469. 'start-node': 'start',
  470. 'approval-node': 'approval',
  471. 'cc-node': 'cc',
  472. 'condition-node': 'condition',
  473. 'end-node': 'end'
  474. }
  475. const typeToFrontend: Record<string, string> = {
  476. 'start': 'start-node',
  477. 'approval': 'approval-node',
  478. 'cc': 'cc-node',
  479. 'condition': 'condition-node',
  480. 'end': 'end-node'
  481. }
  482. // 将 LogicFlow 的 graphData 转换为后端格式
  483. function convertToBackendFormat(graphData: any) {
  484. const data = JSON.parse(JSON.stringify(graphData))
  485. if (data.nodes) {
  486. data.nodes = data.nodes.map((node: any) => {
  487. const n = { ...node }
  488. if (n.type && typeToBackend[n.type]) {
  489. n.type = typeToBackend[n.type]
  490. }
  491. // text -> name
  492. if (n.text !== undefined) {
  493. n.name = typeof n.text === 'object' ? n.text.value : n.text
  494. delete n.text
  495. }
  496. return n
  497. })
  498. }
  499. // edges 保持原样,properties 中的 condition 会在后端解析
  500. return data
  501. }
  502. // 将后端的 graphData 转换为 LogicFlow 格式
  503. function convertToFrontendFormat(graphData: any) {
  504. const data = JSON.parse(JSON.stringify(graphData))
  505. if (data.nodes) {
  506. data.nodes = data.nodes.map((node: any) => {
  507. const n = { ...node }
  508. if (n.type && typeToFrontend[n.type]) {
  509. n.type = typeToFrontend[n.type]
  510. }
  511. // name -> text
  512. if (n.name !== undefined) {
  513. n.text = n.name
  514. delete n.name
  515. }
  516. return n
  517. })
  518. }
  519. return data
  520. }
  521. function handleDragStart(e: DragEvent, node: any) {
  522. const serializable = { type: node.type, label: node.label }
  523. e.dataTransfer?.setData('application/json', JSON.stringify(serializable))
  524. }
  525. function handleDrop(e: DragEvent) {
  526. e.preventDefault()
  527. const dataStr = e.dataTransfer?.getData('application/json')
  528. if (!dataStr || !lf) return
  529. const nodeData = JSON.parse(dataStr)
  530. const rect = lfContainer.value!.getBoundingClientRect()
  531. lf.addNode({
  532. type: nodeData.type,
  533. x: e.clientX - rect.left,
  534. y: e.clientY - rect.top,
  535. text: nodeData.label
  536. })
  537. }
  538. function updateNodeName() {
  539. if (!lf || !selectedNode.value) return
  540. lf.setNodeText(selectedNode.value.id, nodeName.value)
  541. }
  542. // 保存流程
  543. const saveDialogVisible = ref(false)
  544. const saveFormRef = ref<FormInstance>()
  545. const saveForm = reactive({
  546. code: '',
  547. name: '',
  548. category: '',
  549. description: ''
  550. })
  551. const saveFormRules: FormRules = {
  552. code: [{ required: true, message: '请输入流程编码', trigger: 'blur' }],
  553. name: [{ required: true, message: '请输入流程名称', trigger: 'blur' }]
  554. }
  555. const formFields = ref<FormField[]>([])
  556. const fieldTypeOptions = [
  557. { label: '文本', value: 'text' },
  558. { label: '多行文本', value: 'textarea' },
  559. { label: '数字', value: 'number' },
  560. { label: '日期', value: 'date' },
  561. { label: '下拉选择', value: 'select' }
  562. ]
  563. function addFormField() {
  564. formFields.value.push({ name: '', label: '', type: 'text', required: false })
  565. }
  566. function removeFormField(index: number) {
  567. formFields.value.splice(index, 1)
  568. }
  569. function getFormSchema(): string {
  570. const validFields = formFields.value.filter(f => f.name.trim() && f.label.trim())
  571. return validFields.length > 0 ? JSON.stringify(validFields) : ''
  572. }
  573. function parseFormSchema(schema?: string) {
  574. if (!schema) return
  575. try {
  576. const parsed = JSON.parse(schema)
  577. if (Array.isArray(parsed)) {
  578. formFields.value = parsed
  579. }
  580. } catch {
  581. formFields.value = []
  582. }
  583. }
  584. // 下拉选项编辑
  585. const optionDialogVisible = ref(false)
  586. const optionEditingIndex = ref<number | null>(null)
  587. const optionEditingField = ref<FormField | null>(null)
  588. const optionList = ref<FormFieldOption[]>([])
  589. function openOptionDialog(index: number) {
  590. optionEditingIndex.value = index
  591. optionEditingField.value = formFields.value[index]
  592. const rawOptions = optionEditingField.value.options || []
  593. optionList.value = rawOptions.map((opt: any) => {
  594. if (typeof opt === 'string') {
  595. return { label: opt, value: opt }
  596. }
  597. return { label: String(opt.label ?? opt.value), value: opt.value }
  598. })
  599. optionDialogVisible.value = true
  600. }
  601. function addOption() {
  602. optionList.value.push({ label: '', value: '' })
  603. }
  604. function removeOption(index: number) {
  605. optionList.value.splice(index, 1)
  606. }
  607. function saveOptions() {
  608. if (optionEditingIndex.value === null || !optionEditingField.value) return
  609. const validOptions = optionList.value.filter(opt => String(opt.label).trim() !== '' && String(opt.value).trim() !== '')
  610. formFields.value[optionEditingIndex.value].options = validOptions
  611. formFields.value[optionEditingIndex.value].multiple = optionEditingField.value.multiple
  612. optionDialogVisible.value = false
  613. }
  614. function validateFlow(graphData: any): string | null {
  615. const nodes = graphData.nodes || []
  616. const edges = graphData.edges || []
  617. if (nodes.length === 0) return '流程图不能为空'
  618. const startNodes = nodes.filter((n: any) => n.type === 'start-node' || n.type === 'start')
  619. const endNodes = nodes.filter((n: any) => n.type === 'end-node' || n.type === 'end')
  620. if (startNodes.length === 0) return '流程图缺少开始节点'
  621. if (endNodes.length === 0) return '流程图缺少结束节点'
  622. if (startNodes.length > 1) return '流程图只能有一个开始节点'
  623. if (endNodes.length > 1) return '流程图只能有一个结束节点'
  624. // 检查审批节点是否选择了员工管理中的成员(角色账号)
  625. for (const node of nodes) {
  626. if (node.type === 'approval-node' || node.type === 'approval') {
  627. const props = node.properties || {}
  628. const assigneeValue = props.assigneeValue || props.approver
  629. if (!assigneeValue) {
  630. return `审批节点 "${node.text || node.name}" 未选择员工管理中的审批成员`
  631. }
  632. }
  633. }
  634. // 检查孤立节点
  635. const connectedNodeIds = new Set<string>()
  636. for (const edge of edges) {
  637. connectedNodeIds.add(edge.sourceNodeId)
  638. connectedNodeIds.add(edge.targetNodeId)
  639. }
  640. for (const node of nodes) {
  641. if (!connectedNodeIds.has(node.id) && node.type !== 'start-node' && node.type !== 'start' && node.type !== 'end-node' && node.type !== 'end') {
  642. return `节点 "${node.text || node.name}" 是孤立节点,请删除或连接`
  643. }
  644. }
  645. // 检查条件分支是否有默认分支
  646. const sourceMap: Record<string, any[]> = {}
  647. for (const edge of edges) {
  648. if (!sourceMap[edge.sourceNodeId]) sourceMap[edge.sourceNodeId] = []
  649. sourceMap[edge.sourceNodeId].push(edge)
  650. }
  651. for (const node of nodes) {
  652. if (node.type === 'condition-node' || node.type === 'condition') {
  653. const outEdges = sourceMap[node.id] || []
  654. if (outEdges.length > 1) {
  655. const hasDefault = outEdges.some((e: any) => !e.properties?.condition && !e.condition)
  656. if (!hasDefault) {
  657. return `条件节点 "${node.text || node.name}" 需要至少一条无条件的默认分支`
  658. }
  659. }
  660. }
  661. }
  662. return null
  663. }
  664. async function handleSave() {
  665. if (!lf) return
  666. saveCurrentNodeProps()
  667. const rawGraphData = lf.getGraphData()
  668. const error = validateFlow(rawGraphData)
  669. if (error) {
  670. ElMessage.warning(error)
  671. return
  672. }
  673. // 统一弹出保存弹窗,方便配置流程表单字段
  674. if (mode.value === 'create') {
  675. saveForm.code = flowCode.value
  676. saveForm.name = flowName.value
  677. saveForm.category = flowCategory.value
  678. saveForm.description = flowDescription.value
  679. formFields.value = []
  680. } else if (mode.value === 'edit' && flowId.value) {
  681. saveForm.code = flowCode.value
  682. saveForm.name = flowName.value
  683. saveForm.category = flowCategory.value
  684. saveForm.description = flowDescription.value
  685. // formFields 在编辑模式加载流程时已通过 parseFormSchema 填充
  686. } else {
  687. saveForm.code = ''
  688. saveForm.name = ''
  689. saveForm.category = ''
  690. saveForm.description = ''
  691. formFields.value = []
  692. }
  693. saveDialogVisible.value = true
  694. }
  695. async function submitSave() {
  696. const valid = await saveFormRef.value?.validate().catch(() => false)
  697. if (!valid) return
  698. if (!lf) return
  699. saveCurrentNodeProps()
  700. const rawGraphData = lf.getGraphData()
  701. const error = validateFlow(rawGraphData)
  702. if (error) {
  703. ElMessage.warning(error)
  704. return
  705. }
  706. const graphData = convertToBackendFormat(rawGraphData)
  707. if (mode.value === 'create') {
  708. const data: Partial<FlowDefinition> = {
  709. code: saveForm.code,
  710. name: saveForm.name,
  711. category: saveForm.category,
  712. description: saveForm.description,
  713. formSchema: getFormSchema(),
  714. flowJson: JSON.stringify(graphData),
  715. status: 0
  716. }
  717. await addDefinition(data)
  718. ElMessage.success('流程新增成功')
  719. saveDialogVisible.value = false
  720. router.push('/flow/definition')
  721. } else if (mode.value === 'edit' && flowId.value) {
  722. const data: Partial<FlowDefinition> = {
  723. id: Number(flowId.value),
  724. code: saveForm.code,
  725. name: saveForm.name,
  726. category: saveForm.category,
  727. description: saveForm.description,
  728. formSchema: getFormSchema(),
  729. flowJson: JSON.stringify(graphData)
  730. }
  731. const newId = await updateDefinition(data)
  732. if (newId !== Number(flowId.value)) {
  733. ElMessage.success('流程已创建为新版本,请重新发布')
  734. } else {
  735. ElMessage.success('流程修改成功')
  736. }
  737. saveDialogVisible.value = false
  738. router.push('/flow/definition')
  739. } else {
  740. const data: Partial<FlowDefinition> = {
  741. code: saveForm.code,
  742. name: saveForm.name,
  743. category: saveForm.category,
  744. description: saveForm.description,
  745. formSchema: getFormSchema(),
  746. flowJson: JSON.stringify(graphData),
  747. status: 0
  748. }
  749. await addDefinition(data)
  750. ElMessage.success('流程保存成功')
  751. saveDialogVisible.value = false
  752. router.push('/flow/definition')
  753. }
  754. }
  755. function handleClear() {
  756. if (!lf) return
  757. lf.clearData()
  758. selectedNode.value = null
  759. }
  760. function goBack() {
  761. router.push('/flow/definition')
  762. }
  763. async function loadRoles() {
  764. try {
  765. const res = await listRole({ pageNum: 1, pageSize: 100 })
  766. roleList.value = res.list
  767. } catch { /* ignore */ }
  768. }
  769. onMounted(() => {
  770. // 防止快速进入/离开设计器时重复创建 LogicFlow 实例
  771. if (lf) {
  772. lf.destroy()
  773. lf = null
  774. }
  775. nextTick(initLogicFlow)
  776. loadRoles()
  777. })
  778. onUnmounted(() => {
  779. lf?.destroy()
  780. lf = null
  781. selectedNode.value = null
  782. })
  783. </script>
  784. <style scoped>
  785. .designer-container {
  786. display: flex;
  787. height: calc(100vh - 100px);
  788. background: #fff;
  789. }
  790. .flow-info-bar {
  791. position: absolute;
  792. top: 60px;
  793. left: 220px;
  794. right: 0;
  795. height: 40px;
  796. background: #f0f9ff;
  797. border-bottom: 1px solid #d9ecff;
  798. display: flex;
  799. align-items: center;
  800. gap: 16px;
  801. padding: 0 16px;
  802. z-index: 100;
  803. }
  804. .flow-info-item {
  805. font-size: 13px;
  806. color: #303133;
  807. }
  808. .node-panel {
  809. width: 200px;
  810. border-right: 1px solid #dcdfe6;
  811. padding: 16px;
  812. background: #fafafa;
  813. }
  814. .panel-title {
  815. font-size: 16px;
  816. font-weight: bold;
  817. margin-bottom: 16px;
  818. color: #303133;
  819. }
  820. .node-item {
  821. display: flex;
  822. align-items: center;
  823. padding: 12px;
  824. margin-bottom: 8px;
  825. border-radius: 6px;
  826. cursor: move;
  827. background: #fff;
  828. border: 1px solid #ebeef5;
  829. transition: all 0.2s;
  830. }
  831. .node-item:hover {
  832. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  833. border-color: #c6e2ff;
  834. }
  835. .node-icon {
  836. width: 32px;
  837. height: 32px;
  838. border-radius: 50%;
  839. display: flex;
  840. align-items: center;
  841. justify-content: center;
  842. color: #fff;
  843. margin-right: 12px;
  844. }
  845. .node-name {
  846. font-size: 14px;
  847. color: #606266;
  848. }
  849. .canvas-area {
  850. flex: 1;
  851. position: relative;
  852. background: #f5f7fa;
  853. }
  854. .lf-container {
  855. width: 100%;
  856. height: 100%;
  857. }
  858. .toolbar {
  859. position: absolute;
  860. top: 16px;
  861. right: 16px;
  862. z-index: 10;
  863. }
  864. .property-panel {
  865. width: 300px;
  866. border-left: 1px solid #dcdfe6;
  867. padding: 16px;
  868. background: #fafafa;
  869. overflow-y: auto;
  870. }
  871. .property-panel.empty {
  872. display: flex;
  873. flex-direction: column;
  874. }
  875. .form-fields-section {
  876. margin-top: 16px;
  877. border-top: 1px solid #ebeef5;
  878. padding-top: 16px;
  879. }
  880. .form-fields-header {
  881. display: flex;
  882. justify-content: space-between;
  883. align-items: center;
  884. margin-bottom: 12px;
  885. }
  886. .form-fields-title {
  887. font-weight: bold;
  888. color: #303133;
  889. }
  890. </style>