index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 { 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 = (instanceId) => {
  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(instanceId);
  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 createDiagram = async (data) => {
  76. try {
  77. await modeler.value.importXML(data);
  78. fitViewport();
  79. fillColor();
  80. loading.value = false;
  81. addEventBusListener();
  82. } catch (err) {
  83. console.log(err);
  84. }
  85. };
  86. const addEventBusListener = () => {
  87. const eventBus = modeler.value.get<EventBus>('eventBus');
  88. const overlays = modeler.value.get<Overlays>('overlays');
  89. eventBus.on<ModdleElement>('element.hover', (e) => {
  90. let data = historyList.value.find((t) => t.taskDefinitionKey === e.element.id);
  91. if (e.element.type === 'bpmn:UserTask' && data) {
  92. setTimeout(() => {
  93. genNodeDetailBox(e, overlays, data);
  94. }, 10);
  95. }
  96. });
  97. eventBus.on('element.out', (e) => {
  98. overlays.clear();
  99. });
  100. };
  101. const genNodeDetailBox = (e, overlays, data) => {
  102. overlays.add(e.element.id, {
  103. position: { top: e.element.height, left: 0 },
  104. html: `<div class="verlays">
  105. <p>审批人员: ${data.nickName || ''}<p/>
  106. <p>节点状态:${data.status || ''}</p>
  107. <p>开始时间:${data.startTime || ''}</p>
  108. <p>结束时间:${data.endTime || ''}</p>
  109. <p>审批耗时:${data.runDuration || ''}</p>
  110. </div>`
  111. });
  112. };
  113. // 让图能自适应屏幕
  114. const fitViewport = () => {
  115. zoom.value = modeler.value.get<Canvas>('canvas').zoom('fit-viewport');
  116. const bbox = document.querySelector<SVGGElement>('.flow-containers .viewport').getBBox();
  117. const currentViewBox = modeler.value.get('canvas').viewbox();
  118. const elementMid = {
  119. x: bbox.x + bbox.width / 2 - 65,
  120. y: bbox.y + bbox.height / 2
  121. };
  122. modeler.value.get<Canvas>('canvas').viewbox({
  123. x: elementMid.x - currentViewBox.width / 2,
  124. y: elementMid.y - currentViewBox.height / 2,
  125. width: currentViewBox.width,
  126. height: currentViewBox.height
  127. });
  128. zoom.value = (bbox.width / currentViewBox.width) * 1.8;
  129. };
  130. // 放大缩小
  131. const zoomViewport = (zoomIn = true) => {
  132. zoom.value = modeler.value.get<Canvas>('canvas').zoom();
  133. zoom.value += zoomIn ? 0.1 : -0.1;
  134. modeler.value.get<Canvas>('canvas').zoom(zoom.value);
  135. };
  136. //上色
  137. const fillColor = () => {
  138. const canvas = modeler.value.get<Canvas>('canvas');
  139. bpmnNodeList(modeler.value._definitions.rootElements[0].flowElements, canvas);
  140. };
  141. //递归上色
  142. const bpmnNodeList = (flowElements, canvas) => {
  143. flowElements.forEach((n) => {
  144. if (n.$type === 'bpmn:UserTask') {
  145. const completeTask = taskList.value.find((m) => m.key === n.id);
  146. if (completeTask) {
  147. canvas.addMarker(n.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  148. n.outgoing?.forEach((nn) => {
  149. const targetTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  150. if (targetTask) {
  151. canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
  152. } else if (nn.targetRef.$type === 'bpmn:ExclusiveGateway') {
  153. canvas.addMarker(nn.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  154. canvas.addMarker(nn.targetRef.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  155. nn.targetRef.outgoing.forEach((e) => {
  156. gateway(e.id, e.targetRef.$type, e.targetRef.id, canvas, completeTask.completed);
  157. });
  158. } else if (nn.targetRef.$type === 'bpmn:ParallelGateway') {
  159. canvas.addMarker(nn.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  160. canvas.addMarker(nn.targetRef.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  161. nn.targetRef.outgoing.forEach((e) => {
  162. gateway(e.id, e.targetRef.$type, e.targetRef.id, canvas, completeTask.completed);
  163. });
  164. } else if (nn.targetRef.$type === 'bpmn:InclusiveGateway') {
  165. canvas.addMarker(nn.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  166. canvas.addMarker(nn.targetRef.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  167. nn.targetRef.outgoing.forEach((e) => {
  168. gateway(e.id, e.targetRef.$type, e.targetRef.id, canvas, completeTask.completed);
  169. });
  170. }
  171. });
  172. }
  173. } else if (n.$type === 'bpmn:ExclusiveGateway') {
  174. n.outgoing.forEach((nn) => {
  175. const targetTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  176. if (targetTask) {
  177. canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
  178. }
  179. });
  180. } else if (n.$type === 'bpmn:ParallelGateway') {
  181. n.outgoing.forEach((nn) => {
  182. const targetTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  183. if (targetTask) {
  184. canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
  185. }
  186. });
  187. } else if (n.$type === 'bpmn:InclusiveGateway') {
  188. n.outgoing.forEach((nn) => {
  189. const targetTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  190. if (targetTask) {
  191. canvas.addMarker(nn.id, targetTask.completed ? 'highlight' : 'highlight-todo');
  192. }
  193. });
  194. } else if (n.$type === 'bpmn:SubProcess') {
  195. const completeTask = taskList.value.find((m) => m.key === n.id);
  196. if (completeTask) {
  197. canvas.addMarker(n.id, completeTask.completed ? 'highlight' : 'highlight-todo');
  198. }
  199. bpmnNodeList(n.flowElements, canvas);
  200. } else if (n.$type === 'bpmn:StartEvent') {
  201. canvas.addMarker(n.id, 'startEvent');
  202. if (n.outgoing) {
  203. n.outgoing.forEach((nn) => {
  204. const completeTask = taskList.value.find((m) => m.key === nn.targetRef.id);
  205. if (completeTask) {
  206. canvas.addMarker(nn.id, 'highlight');
  207. canvas.addMarker(n.id, 'highlight');
  208. }
  209. });
  210. }
  211. } else if (n.$type === 'bpmn:EndEvent') {
  212. canvas.addMarker(n.id, 'endEvent');
  213. const completeTask = taskList.value.find((m) => m.key === n.id);
  214. if (completeTask) {
  215. canvas.addMarker(completeTask.key, 'highlight');
  216. canvas.addMarker(n.id, 'highlight');
  217. return;
  218. }
  219. }
  220. });
  221. };
  222. const gateway = (id, targetRefType, targetRefId, canvas, completed) => {
  223. if (targetRefType === 'bpmn:ExclusiveGateway') {
  224. canvas.addMarker(id, completed ? 'highlight' : 'highlight-todo');
  225. canvas.addMarker(targetRefId, completed ? 'highlight' : 'highlight-todo');
  226. }
  227. if (targetRefType === 'bpmn:ParallelGateway') {
  228. canvas.addMarker(id, completed ? 'highlight' : 'highlight-todo');
  229. canvas.addMarker(targetRefId, completed ? 'highlight' : 'highlight-todo');
  230. }
  231. if (targetRefType === 'bpmn:InclusiveGateway') {
  232. canvas.addMarker(id, completed ? 'highlight' : 'highlight-todo');
  233. canvas.addMarker(targetRefId, completed ? 'highlight' : 'highlight-todo');
  234. }
  235. };
  236. defineExpose({
  237. init
  238. });
  239. </script>
  240. <style lang="scss" scoped>
  241. .canvas {
  242. width: 100%;
  243. height: 100%;
  244. }
  245. .header-div {
  246. display: flex;
  247. padding: 10px 0;
  248. justify-content: space-between;
  249. .tips-label {
  250. display: flex;
  251. div {
  252. margin-right: 10px;
  253. padding: 5px;
  254. font-size: 12px;
  255. }
  256. .un-complete {
  257. border: 1px solid #000;
  258. }
  259. .in-progress {
  260. background-color: rgb(255, 237, 204);
  261. border: 1px dashed orange;
  262. }
  263. .complete {
  264. background-color: rgb(204, 230, 204);
  265. border: 1px solid green;
  266. }
  267. }
  268. }
  269. .view-mode {
  270. .el-header,
  271. .el-aside,
  272. .djs-palette,
  273. .bjs-powered-by {
  274. display: none;
  275. }
  276. .el-loading-mask {
  277. background-color: initial;
  278. }
  279. .el-loading-spinner {
  280. display: none;
  281. }
  282. }
  283. .bpmn-el-container {
  284. height: 500px;
  285. }
  286. .flow-containers {
  287. width: 100%;
  288. height: 100%;
  289. overflow-y: auto;
  290. .canvas {
  291. width: 100%;
  292. height: 100%;
  293. }
  294. .load {
  295. margin-right: 10px;
  296. }
  297. :deep(.el-form-item__label) {
  298. font-size: 13px;
  299. }
  300. :deep(.djs-palette) {
  301. left: 0 !important;
  302. top: 0;
  303. border-top: none;
  304. }
  305. :deep(.djs-container svg) {
  306. min-height: 650px;
  307. }
  308. :deep(.startEvent.djs-shape .djs-visual > :nth-child(1)) {
  309. fill: #77df6d !important;
  310. }
  311. :deep(.endEvent.djs-shape .djs-visual > :nth-child(1)) {
  312. fill: #ee7b77 !important;
  313. }
  314. :deep(.highlight.djs-shape .djs-visual > :nth-child(1)) {
  315. fill: green !important;
  316. stroke: green !important;
  317. fill-opacity: 0.2 !important;
  318. }
  319. :deep(.highlight.djs-shape .djs-visual > :nth-child(2)) {
  320. fill: green !important;
  321. }
  322. :deep(.highlight.djs-shape .djs-visual > path) {
  323. fill: green !important;
  324. fill-opacity: 0.2 !important;
  325. stroke: green !important;
  326. }
  327. :deep(.highlight.djs-connection > .djs-visual > path) {
  328. stroke: green !important;
  329. }
  330. :deep(.highlight-todo.djs-connection > .djs-visual > path) {
  331. stroke: orange !important;
  332. stroke-dasharray: 4px !important;
  333. fill-opacity: 0.2 !important;
  334. marker-end: url(#sequenceflow-end-_E7DFDF-_E7DFDF-803g1kf6zwzmcig1y2ulm5egr);
  335. }
  336. :deep(.highlight-todo.djs-shape .djs-visual > :nth-child(1)) {
  337. fill: orange !important;
  338. stroke: orange !important;
  339. stroke-dasharray: 4px !important;
  340. fill-opacity: 0.2 !important;
  341. }
  342. }
  343. :deep(.verlays) {
  344. width: 250px;
  345. background: rgb(102, 102, 102);
  346. border-radius: 4px;
  347. border: 1px solid #ebeef5;
  348. color: #fff;
  349. padding: 15px 10px;
  350. p {
  351. line-height: 28px;
  352. margin: 0;
  353. padding: 0;
  354. }
  355. }
  356. </style>