index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <template>
  2. <div v-loading="loading" class="bpmnDialogContainers">
  3. <el-header style="border-bottom: 1px solid rgb(218 218 218); height: auto">
  4. <div class="header-div">
  5. <div>
  6. <el-tooltip effect="dark" content="自适应屏幕" placement="bottom">
  7. <el-button size="small" icon="Rank" @click="fitViewport" />
  8. </el-tooltip>
  9. <el-tooltip effect="dark" content="放大" placement="bottom">
  10. <el-button size="small" icon="ZoomIn" @click="zoomViewport(true)" />
  11. </el-tooltip>
  12. <el-tooltip effect="dark" content="缩小" placement="bottom">
  13. <el-button size="small" icon="ZoomOut" @click="zoomViewport(false)" />
  14. </el-tooltip>
  15. </div>
  16. <div>
  17. <div class="tips-label">
  18. <div class="un-complete">未完成</div>
  19. <div class="in-progress">进行中</div>
  20. <div class="complete">已完成</div>
  21. </div>
  22. </div>
  23. </div>
  24. </el-header>
  25. <div class="flow-containers">
  26. <el-container class="bpmn-el-container" style="align-items: stretch">
  27. <el-main style="padding: 0">
  28. <div ref="canvas" class="canvas" />
  29. </el-main>
  30. </el-container>
  31. </div>
  32. </div>
  33. </template>
  34. <script lang="ts" setup>
  35. import BpmnViewer from 'bpmn-js/lib/Viewer';
  36. import MoveCanvasModule from 'diagram-js/lib/navigation/movecanvas';
  37. import ZoomScrollModule from 'diagram-js/lib/navigation/zoomscroll';
  38. import { ModuleDeclaration } from 'didi';
  39. import type { Canvas, ModdleElement } from 'bpmn';
  40. import EventBus from 'diagram-js/lib/core/EventBus';
  41. import Overlays from 'diagram-js/lib/features/overlays/Overlays';
  42. import processApi from '@/api/workflow/processInstance/index';
  43. const canvas = ref<HTMLElement>();
  44. const modeler = ref<BpmnViewer>();
  45. const taskList = ref([]);
  46. const zoom = ref(1);
  47. const xml = ref('');
  48. const loading = ref(false);
  49. const bpmnVisible = ref(true);
  50. const historyList = ref([]);
  51. const init = (businessKey) => {
  52. loading.value = true;
  53. bpmnVisible.value = true;
  54. nextTick(async () => {
  55. if (modeler.value) modeler.value.destroy();
  56. modeler.value = new BpmnViewer({
  57. container: canvas.value,
  58. additionalModules: [
  59. {
  60. //禁止滚轮滚动
  61. zoomScroll: ['value', '']
  62. },
  63. ZoomScrollModule,
  64. MoveCanvasModule
  65. ] as ModuleDeclaration[]
  66. });
  67. const resp = await processApi.getHistoryList(businessKey);
  68. xml.value = resp.data.xml;
  69. taskList.value = resp.data.taskList;
  70. historyList.value = resp.data.historyList;
  71. await createDiagram(xml.value);
  72. loading.value = false;
  73. });
  74. };
  75. const initXml = (xmlStr: string) => {
  76. loading.value = true;
  77. bpmnVisible.value = true;
  78. nextTick(async () => {
  79. if (modeler.value) modeler.value.destroy();
  80. modeler.value = new BpmnViewer({
  81. container: canvas.value,
  82. additionalModules: [
  83. {
  84. //禁止滚轮滚动
  85. zoomScroll: ['value', '']
  86. },
  87. ZoomScrollModule,
  88. MoveCanvasModule
  89. ] as ModuleDeclaration[]
  90. });
  91. xml.value = xmlStr;
  92. await createDiagram(xml.value);
  93. loading.value = false;
  94. });
  95. };
  96. const createDiagram = async (data) => {
  97. try {
  98. await modeler.value.importXML(data);
  99. fitViewport();
  100. fillColor();
  101. loading.value = false;
  102. addEventBusListener();
  103. } catch (err) {
  104. console.log(err);
  105. }
  106. };
  107. const addEventBusListener = () => {
  108. const eventBus = modeler.value.get<EventBus>('eventBus');
  109. const overlays = modeler.value.get<Overlays>('overlays');
  110. eventBus.on<ModdleElement>('element.hover', (e) => {
  111. let data = historyList.value.find((t) => t.taskDefinitionKey === e.element.id);
  112. if (e.element.type === 'bpmn:UserTask' && data) {
  113. setTimeout(() => {
  114. genNodeDetailBox(e, overlays, data);
  115. }, 10);
  116. }
  117. });
  118. eventBus.on('element.out', (e) => {
  119. overlays.clear();
  120. });
  121. };
  122. const genNodeDetailBox = (e, overlays, data) => {
  123. overlays.add(e.element.id, {
  124. position: { top: e.element.height, left: 0 },
  125. html: `<div class="verlays">
  126. <p>审批人员: ${data.nickName || ''}<p/>
  127. <p>节点状态:${data.status || ''}</p>
  128. <p>开始时间:${data.startTime || ''}</p>
  129. <p>结束时间:${data.endTime || ''}</p>
  130. <p>审批耗时:${data.runDuration || ''}</p>
  131. <p>流程版本:v${data.version || ''}</p>
  132. </div>`
  133. });
  134. };
  135. // 让图能自适应屏幕
  136. const fitViewport = () => {
  137. zoom.value = modeler.value.get<Canvas>('canvas').zoom('fit-viewport');
  138. const bbox = document.querySelector<SVGGElement>('.flow-containers .viewport').getBBox();
  139. const currentViewBox = modeler.value.get('canvas').viewbox();
  140. const elementMid = {
  141. x: bbox.x + bbox.width / 2 - 65,
  142. y: bbox.y + bbox.height / 2
  143. };
  144. modeler.value.get<Canvas>('canvas').viewbox({
  145. x: elementMid.x - currentViewBox.width / 2,
  146. y: elementMid.y - currentViewBox.height / 2,
  147. width: currentViewBox.width,
  148. height: currentViewBox.height
  149. });
  150. zoom.value = (bbox.width / currentViewBox.width) * 1.8;
  151. };
  152. // 放大缩小
  153. const zoomViewport = (zoomIn = true) => {
  154. zoom.value = modeler.value.get<Canvas>('canvas').zoom();
  155. zoom.value += zoomIn ? 0.1 : -0.1;
  156. modeler.value.get<Canvas>('canvas').zoom(zoom.value);
  157. };
  158. //上色
  159. const fillColor = () => {
  160. const canvas = modeler.value.get<Canvas>('canvas');
  161. bpmnNodeList(modeler.value._definitions.rootElements[0].flowElements, canvas);
  162. };
  163. //递归上色
  164. const bpmnNodeList = (flowElements, canvas) => {
  165. flowElements.forEach((n) => {
  166. if (n.$type === 'bpmn:UserTask') {
  167. const completeTask = taskList.value.find((m) => m.key === n.id);
  168. if (completeTask) {
  169. canvas.addMarker(n.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  170. n.outgoing?.forEach((nn) => {
  171. const targetTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  172. if (targetTask) {
  173. canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
  174. } else if (nn.targetRef.$type === 'bpmn:ExclusiveGateway') {
  175. canvas.addMarker(nn.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  176. canvas.addMarker(nn.targetRef.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  177. nn.targetRef.outgoing.forEach((e) => {
  178. gateway(e.id, e.targetRef.$type, e.targetRef.id, canvas, completeTask.completed);
  179. });
  180. } else if (nn.targetRef.$type === 'bpmn:ParallelGateway') {
  181. canvas.addMarker(nn.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  182. canvas.addMarker(nn.targetRef.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  183. nn.targetRef.outgoing.forEach((e) => {
  184. gateway(e.id, e.targetRef.$type, e.targetRef.id, canvas, completeTask.completed);
  185. });
  186. } else if (nn.targetRef.$type === 'bpmn:InclusiveGateway') {
  187. canvas.addMarker(nn.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  188. canvas.addMarker(nn.targetRef.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  189. nn.targetRef.outgoing.forEach((e) => {
  190. gateway(e.id, e.targetRef.$type, e.targetRef.id, canvas, completeTask.completed);
  191. });
  192. }
  193. });
  194. }
  195. } else if (n.$type === 'bpmn:ExclusiveGateway') {
  196. n.outgoing.forEach((nn) => {
  197. const targetTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  198. if (targetTask) {
  199. canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
  200. }
  201. });
  202. } else if (n.$type === 'bpmn:ParallelGateway') {
  203. n.outgoing.forEach((nn) => {
  204. const targetTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  205. if (targetTask) {
  206. canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
  207. }
  208. });
  209. } else if (n.$type === 'bpmn:InclusiveGateway') {
  210. n.outgoing.forEach((nn) => {
  211. const targetTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  212. if (targetTask) {
  213. canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
  214. }
  215. });
  216. } else if (n.$type === 'bpmn:SubProcess') {
  217. const completeTask = taskList.value.find((m) => m.key === n.id);
  218. if (completeTask) {
  219. canvas.addMarker(n.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  220. }
  221. bpmnNodeList(n.flowElements, canvas);
  222. } else if (n.$type === 'bpmn:StartEvent') {
  223. canvas.addMarker(n.id, 'startEvent');
  224. if (n.outgoing) {
  225. n.outgoing.forEach((nn) => {
  226. const completeTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  227. if (completeTask) {
  228. canvas.addMarker(nn.id, 'highlight');
  229. canvas.addMarker(n.id, 'highlight');
  230. }
  231. });
  232. }
  233. } else if (n.$type === 'bpmn:EndEvent') {
  234. canvas.addMarker(n.id, 'endEvent');
  235. const completeTask = taskList.value.find((m) => m.key === n.id);
  236. if (completeTask) {
  237. canvas.addMarker(completeTask.key, 'highlight');
  238. canvas.addMarker(n.id, 'highlight');
  239. return;
  240. }
  241. }
  242. });
  243. };
  244. const gateway = (id, targetRefType, targetRefId, canvas, completed) => {
  245. if (targetRefType === 'bpmn:ExclusiveGateway') {
  246. canvas.addMarker(id, completed ? 'highlight' : 'highlight-todo');
  247. canvas.addMarker(targetRefId, completed ? 'highlight' : 'highlight-todo');
  248. }
  249. if (targetRefType === 'bpmn:ParallelGateway') {
  250. canvas.addMarker(id, completed ? 'highlight' : 'highlight-todo');
  251. canvas.addMarker(targetRefId, completed ? 'highlight' : 'highlight-todo');
  252. }
  253. if (targetRefType === 'bpmn:InclusiveGateway') {
  254. canvas.addMarker(id, completed ? 'highlight' : 'highlight-todo');
  255. canvas.addMarker(targetRefId, completed ? 'highlight' : 'highlight-todo');
  256. }
  257. };
  258. defineExpose({
  259. init,
  260. initXml
  261. });
  262. </script>
  263. <style lang="scss" scoped>
  264. .canvas {
  265. width: 100%;
  266. height: 100%;
  267. }
  268. .header-div {
  269. display: flex;
  270. padding: 10px 0;
  271. justify-content: space-between;
  272. .tips-label {
  273. display: flex;
  274. div {
  275. margin-right: 10px;
  276. padding: 5px;
  277. font-size: 12px;
  278. }
  279. .un-complete {
  280. border: 1px solid #000;
  281. }
  282. .in-progress {
  283. background-color: rgb(255, 237, 204);
  284. border: 1px dashed orange;
  285. }
  286. .complete {
  287. background-color: rgb(204, 230, 204);
  288. border: 1px solid green;
  289. }
  290. }
  291. }
  292. .view-mode {
  293. .el-header,
  294. .el-aside,
  295. .djs-palette,
  296. .bjs-powered-by {
  297. display: none;
  298. }
  299. .el-loading-mask {
  300. background-color: initial;
  301. }
  302. .el-loading-spinner {
  303. display: none;
  304. }
  305. }
  306. .bpmn-el-container {
  307. height: calc(100vh - 350px);
  308. }
  309. .flow-containers {
  310. width: 100%;
  311. height: 100%;
  312. overflow-y: auto;
  313. .canvas {
  314. width: 100%;
  315. height: 100%;
  316. }
  317. .load {
  318. margin-right: 10px;
  319. }
  320. :deep(.el-form-item__label) {
  321. font-size: 13px;
  322. }
  323. :deep(.djs-palette) {
  324. left: 0 !important;
  325. top: 0;
  326. border-top: none;
  327. }
  328. :deep(.djs-container svg) {
  329. min-height: 650px;
  330. }
  331. :deep(.startEvent.djs-shape .djs-visual > :nth-child(1)) {
  332. fill: #77df6d !important;
  333. }
  334. :deep(.endEvent.djs-shape .djs-visual > :nth-child(1)) {
  335. fill: #ee7b77 !important;
  336. }
  337. :deep(.highlight.djs-shape .djs-visual > :nth-child(1)) {
  338. fill: green !important;
  339. stroke: green !important;
  340. fill-opacity: 0.2 !important;
  341. }
  342. :deep(.highlight.djs-shape .djs-visual > :nth-child(2)) {
  343. fill: green !important;
  344. }
  345. :deep(.highlight.djs-shape .djs-visual > path) {
  346. fill: green !important;
  347. fill-opacity: 0.2 !important;
  348. stroke: green !important;
  349. }
  350. :deep(.highlight.djs-connection > .djs-visual > path) {
  351. stroke: green !important;
  352. }
  353. // 边框滚动动画
  354. @keyframes path-animation {
  355. from {
  356. stroke-dashoffset: 100%;
  357. }
  358. to {
  359. stroke-dashoffset: 0%;
  360. }
  361. }
  362. :deep(.highlight-todo.djs-connection > .djs-visual > path) {
  363. animation: path-animation 60s;
  364. animation-timing-function: linear;
  365. animation-iteration-count: infinite;
  366. stroke-dasharray: 4px !important;
  367. stroke: orange !important;
  368. fill-opacity: 0.2 !important;
  369. marker-end: url('#sequenceflow-end-_E7DFDF-_E7DFDF-803g1kf6zwzmcig1y2ulm5egr');
  370. }
  371. :deep(.highlight-todo.djs-shape .djs-visual > :nth-child(1)) {
  372. animation: path-animation 60s;
  373. animation-timing-function: linear;
  374. animation-iteration-count: infinite;
  375. stroke-dasharray: 4px !important;
  376. stroke: orange !important;
  377. fill: orange !important;
  378. fill-opacity: 0.2 !important;
  379. }
  380. }
  381. :deep(.verlays) {
  382. width: 250px;
  383. background: rgb(102, 102, 102);
  384. border-radius: 4px;
  385. border: 1px solid #ebeef5;
  386. color: #fff;
  387. padding: 15px 10px;
  388. p {
  389. line-height: 28px;
  390. margin: 0;
  391. padding: 0;
  392. }
  393. cursor: pointer;
  394. }
  395. </style>