ProcessViewer.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <div class="my-process-designer">
  3. <div class="my-process-designer__container">
  4. <div class="my-process-designer__canvas" ref="bpmn-canvas"></div>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. import BpmnViewer from "bpmn-js/lib/Viewer";
  10. import DefaultEmptyXML from "./plugins/defaultEmpty";
  11. export default {
  12. name: "MyProcessViewer",
  13. componentName: "MyProcessViewer",
  14. props: {
  15. value: { // BPMN XML 字符串
  16. type: String,
  17. },
  18. prefix: { // 使用哪个引擎
  19. type: String,
  20. default: "camunda",
  21. },
  22. activityData: { // 活动的数据。传递时,可高亮流程
  23. type: Array,
  24. default: () => [],
  25. },
  26. processInstanceData: { // 流程实例的数据。传递时,可展示流程发起人等信息
  27. type: Object,
  28. },
  29. taskData: { // 任务实例的数据。传递时,可展示 UserTask 审核相关的信息
  30. type: Array,
  31. default: () => [],
  32. }
  33. },
  34. data() {
  35. return {
  36. xml: '',
  37. activityList: [],
  38. processInstance: undefined,
  39. taskList: [],
  40. };
  41. },
  42. mounted() {
  43. this.xml = this.value;
  44. this.activityList = this.activityData;
  45. // 初始化
  46. this.initBpmnModeler();
  47. this.createNewDiagram(this.xml);
  48. this.$once("hook:beforeDestroy", () => {
  49. if (this.bpmnModeler) this.bpmnModeler.destroy();
  50. this.$emit("destroy", this.bpmnModeler);
  51. this.bpmnModeler = null;
  52. });
  53. // 初始模型的监听器
  54. this.initModelListeners();
  55. },
  56. watch: {
  57. value: function (newValue) { // 在 xmlString 发生变化时,重新创建,从而绘制流程图
  58. this.xml = newValue;
  59. this.createNewDiagram(this.xml);
  60. },
  61. activityData: function (newActivityData) {
  62. this.activityList = newActivityData;
  63. this.createNewDiagram(this.xml);
  64. },
  65. processInstanceData: function (newProcessInstanceData) {
  66. this.processInstance = newProcessInstanceData;
  67. this.createNewDiagram(this.xml);
  68. },
  69. taskData: function (newTaskListData) {
  70. this.taskList = newTaskListData;
  71. this.createNewDiagram(this.xml);
  72. }
  73. },
  74. methods: {
  75. initBpmnModeler() {
  76. if (this.bpmnModeler) return;
  77. this.bpmnModeler = new BpmnViewer({
  78. container: this.$refs["bpmn-canvas"],
  79. bpmnRenderer: {
  80. }
  81. })
  82. },
  83. /* 创建新的流程图 */
  84. async createNewDiagram(xml) {
  85. // 将字符串转换成图显示出来
  86. let newId = `Process_${new Date().getTime()}`;
  87. let newName = `业务流程_${new Date().getTime()}`;
  88. let xmlString = xml || DefaultEmptyXML(newId, newName, this.prefix);
  89. try {
  90. // console.log(this.bpmnModeler.importXML);
  91. let { warnings } = await this.bpmnModeler.importXML(xmlString);
  92. if (warnings && warnings.length) {
  93. warnings.forEach(warn => console.warn(warn));
  94. }
  95. // 高亮流程图
  96. await this.highlightDiagram();
  97. const canvas = this.bpmnModeler.get('canvas');
  98. canvas.zoom("fit-viewport", "auto");
  99. } catch (e) {
  100. console.error(e);
  101. // console.error(`[Process Designer Warn]: ${e?.message || e}`);
  102. }
  103. },
  104. /* 高亮流程图 */
  105. // TODO 芋艿:如果多个 endActivity 的话,目前的逻辑可能有一定的问题。https://www.jdon.com/workflow/multi-events.html
  106. async highlightDiagram() {
  107. const activityList = this.activityList;
  108. if (activityList.length === 0) {
  109. return;
  110. }
  111. // 参考自 https://gitee.com/tony2y/RuoYi-flowable/blob/master/ruoyi-ui/src/components/Process/index.vue#L222 实现
  112. // 再次基础上,增加不同审批结果的颜色等等
  113. let canvas = this.bpmnModeler.get('canvas');
  114. let todoActivity = activityList.find(m => !m.endTime) // 找到待办的任务
  115. let endActivity = activityList[activityList.length - 1] // 获得最后一个任务
  116. // debugger
  117. console.log(this.bpmnModeler.getDefinitions().rootElements[0].flowElements);
  118. this.bpmnModeler.getDefinitions().rootElements[0].flowElements?.forEach(n => {
  119. let activity = activityList.find(m => m.key === n.id) // 找到对应的活动
  120. if (n.$type === 'bpmn:UserTask') { // 用户任务
  121. if (!activity) {
  122. return;
  123. }
  124. // 处理用户任务的高亮
  125. const task = this.taskList.find(m => m.id === activity.taskId); // 找到活动对应的 taskId
  126. if (task) {
  127. canvas.addMarker(n.id, this.getResultCss(task.result));
  128. // 如果非通过,就不走后面的线条了
  129. if (task.result !== 2) {
  130. return;
  131. }
  132. }
  133. // 处理 outgoing 出线
  134. const outgoing = this.getActivityOutgoing(activity);
  135. outgoing?.forEach(nn => {
  136. // debugger
  137. let targetActivity = activityList.find(m => m.key === nn.targetRef.id)
  138. // 如果目标活动存在,则根据该活动是否结束,进行【bpmn:SequenceFlow】连线的高亮设置
  139. if (targetActivity) {
  140. canvas.addMarker(nn.id, targetActivity.endTime ? 'highlight' : 'highlight-todo');
  141. } else if (nn.targetRef.$type === 'bpmn:ExclusiveGateway') { // TODO 芋艿:这个流程,暂时没走到过
  142. canvas.addMarker(nn.id, activity.endTime ? 'highlight' : 'highlight-todo');
  143. canvas.addMarker(nn.targetRef.id, activity.endTime ? 'highlight' : 'highlight-todo');
  144. } else if (nn.targetRef.$type === 'bpmn:EndEvent') { // TODO 芋艿:这个流程,暂时没走到过
  145. if (!todoActivity && endActivity.key === n.id) {
  146. canvas.addMarker(nn.id, 'highlight');
  147. canvas.addMarker(nn.targetRef.id, 'highlight');
  148. }
  149. if (!activity.endTime) {
  150. canvas.addMarker(nn.id, 'highlight-todo');
  151. canvas.addMarker(nn.targetRef.id, 'highlight-todo');
  152. }
  153. }
  154. });
  155. } else if (n.$type === 'bpmn:ExclusiveGateway') { // 排它网关
  156. if (!activity) {
  157. return
  158. }
  159. // 设置【bpmn:ExclusiveGateway】排它网关的高亮
  160. canvas.addMarker(n.id, this.getActivityHighlightCss(activity));
  161. // 查找需要高亮的连线
  162. let matchNN = undefined;
  163. let matchActivity = undefined;
  164. n.outgoing?.forEach(nn => {
  165. let targetActivity = activityList.find(m => m.key === nn.targetRef.id);
  166. if (!targetActivity) {
  167. return;
  168. }
  169. // 特殊判断 endEvent 类型的原因,ExclusiveGateway 可能后续连有 2 个路径:
  170. // 1. 一个是 UserTask => EndEvent
  171. // 2. 一个是 EndEvent
  172. // 在选择路径 1 时,其实 EndEvent 可能也存在,导致 1 和 2 都高亮,显然是不正确的。
  173. // 所以,在 matchActivity 为 EndEvent 时,需要进行覆盖~~
  174. if (!matchActivity || matchActivity.type === 'endEvent') {
  175. matchNN = nn;
  176. matchActivity = targetActivity;
  177. }
  178. })
  179. if (matchNN && matchActivity) {
  180. canvas.addMarker(matchNN.id, this.getActivityHighlightCss(matchActivity));
  181. }
  182. } else if (n.$type === 'bpmn:ParallelGateway') { // 并行网关
  183. if (!activity) {
  184. return
  185. }
  186. // 设置【bpmn:ParallelGateway】并行网关的高亮
  187. canvas.addMarker(n.id, this.getActivityHighlightCss(activity));
  188. n.outgoing?.forEach(nn => {
  189. // 获得连线是否有指向目标。如果有,则进行高亮
  190. const targetActivity = activityList.find(m => m.key === nn.targetRef.id)
  191. if (targetActivity) {
  192. canvas.addMarker(nn.id, this.getActivityHighlightCss(targetActivity)); // 高亮【bpmn:SequenceFlow】连线
  193. // 高亮【...】目标。其中 ... 可以是 bpm:UserTask、也可以是其它的。当然,如果是 bpm:UserTask 的话,其实不做高亮也没问题,因为上面有逻辑做了这块。
  194. canvas.addMarker(nn.targetRef.id, this.getActivityHighlightCss(targetActivity));
  195. }
  196. })
  197. } else if (n.$type === 'bpmn:StartEvent') { // 开始节点
  198. n.outgoing?.forEach(nn => { // outgoing 例如说【bpmn:SequenceFlow】连线
  199. // 获得连线是否有指向目标。如果有,则进行高亮
  200. let targetActivity = activityList.find(m => m.key === nn.targetRef.id);
  201. if (targetActivity) {
  202. canvas.addMarker(nn.id, 'highlight'); // 高亮【bpmn:SequenceFlow】连线
  203. canvas.addMarker(n.id, 'highlight'); // 高亮【bpmn:StartEvent】开始节点(自己)
  204. }
  205. });
  206. } else if (n.$type === 'bpmn:EndEvent') { // 结束节点
  207. if (!this.processInstance || this.processInstance.result === 1) {
  208. return;
  209. }
  210. canvas.addMarker(n.id, this.getResultCss(this.processInstance.result));
  211. }
  212. })
  213. },
  214. getActivityHighlightCss(activity) {
  215. return activity.endTime ? 'highlight' : 'highlight-todo';
  216. },
  217. getResultCss(result) {
  218. if (result === 1) {
  219. return 'highlight-todo';
  220. } else if (result === 2) {
  221. return 'highlight';
  222. } else if (result === 3) {
  223. return 'highlight-reject';
  224. } else if (result === 4) {
  225. return 'highlight-cancel';
  226. }
  227. return '';
  228. },
  229. getActivityOutgoing(activity) {
  230. // 如果有 outgoing,则直接使用它
  231. if (activity.outgoing && activity.outgoing.length > 0) {
  232. return activity.outgoing;
  233. }
  234. // 如果没有,则遍历获得起点为它的【bpmn:SequenceFlow】节点们。原因是:bpmn-js 的 UserTask 拿不到 outgoing
  235. const flowElements = this.bpmnModeler.getDefinitions().rootElements[0].flowElements;
  236. const outgoing = [];
  237. flowElements.forEach(item => {
  238. if (item.$type !== 'bpmn:SequenceFlow') {
  239. return;
  240. }
  241. if (item.sourceRef.id === activity.key) {
  242. outgoing.push(item);
  243. }
  244. });
  245. return outgoing;
  246. },
  247. initModelListeners() {
  248. const EventBus = this.bpmnModeler.get("eventBus");
  249. const that = this;
  250. // 注册需要的监听事件
  251. EventBus.on('element.hover', function(eventObj) {
  252. let element = eventObj ? eventObj.element : null;
  253. that.elementHover(element);
  254. });
  255. EventBus.on('element.out', function(eventObj) {
  256. let element = eventObj ? eventObj.element : null;
  257. that.elementOut(element);
  258. });
  259. },
  260. // 流程图的元素被 hover
  261. elementHover(element) {
  262. this.element = element;
  263. !this.elementOverlayIds && (this.elementOverlayIds = {});
  264. !this.overlays && (this.overlays = this.bpmnModeler.get("overlays"));
  265. // 展示信息
  266. if (!this.elementOverlayIds[element.id] && element.type !== "bpmn:Process") {
  267. let html = `<div class="element-overlays">
  268. <p>Elemet id: ${element.id}</p>
  269. <p>Elemet type: ${element.type}</p>
  270. </div>`; // 默认值
  271. if (element.type === 'bpmn:StartEvent' && this.processInstance) {
  272. html = `<p>发起人:${this.processInstance.startUser.nickname}</p>
  273. <p>部门:${this.processInstance.startUser.deptName}</p>
  274. <p>创建时间:${this.parseTime(this.processInstance.createTime)}`;
  275. } else if (element.type === 'bpmn:UserTask') {
  276. // debugger
  277. const activity = this.activityList.find(m => m.key === element.id);
  278. if (!activity) {
  279. return;
  280. }
  281. let task = this.taskList.find(m => m.id === activity.taskId); // 找到活动对应的 taskId
  282. if (!task) {
  283. return;
  284. }
  285. html = `<p>审批人:${task.assigneeUser.nickname}</p>
  286. <p>部门:${task.assigneeUser.deptName}</p>
  287. <p>结果:${this.getDictDataLabel(this.DICT_TYPE.BPM_PROCESS_INSTANCE_RESULT, task.result)}</p>
  288. <p>创建时间:${this.parseTime(task.createTime)}</p>`;
  289. if (task.endTime) {
  290. html += `<p>结束时间:${this.parseTime(task.endTime)}</p>`
  291. }
  292. if (task.reason) {
  293. html += `<p>审批建议:${task.reason}</p>`
  294. }
  295. } else if (element.type === 'bpmn:EndEvent' && this.processInstance) {
  296. html = `<p>结果:${this.getDictDataLabel(this.DICT_TYPE.BPM_PROCESS_INSTANCE_RESULT, this.processInstance.result)}</p>`;
  297. if (this.processInstance.endTime) {
  298. html += `<p>结束时间:${this.parseTime(this.processInstance.endTime)}</p>`
  299. }
  300. }
  301. this.elementOverlayIds[element.id] = this.overlays.add(element, {
  302. position: { left: 0, bottom: 0 },
  303. html: `<div class="element-overlays">${html}</div>`
  304. });
  305. }
  306. },
  307. // 流程图的元素被 out
  308. elementOut(element) {
  309. this.overlays.remove({ element });
  310. this.elementOverlayIds[element.id] = null;
  311. },
  312. }
  313. };
  314. </script>
  315. <style>
  316. /** 处理中 */
  317. .highlight-todo.djs-connection > .djs-visual > path {
  318. stroke: #1890ff !important;
  319. stroke-dasharray: 4px !important;
  320. fill-opacity: 0.2 !important;
  321. }
  322. .highlight-todo.djs-shape .djs-visual > :nth-child(1) {
  323. fill: #1890ff !important;
  324. stroke: #1890ff !important;
  325. stroke-dasharray: 4px !important;
  326. fill-opacity: 0.2 !important;
  327. }
  328. /deep/.highlight-todo.djs-connection > .djs-visual > path {
  329. stroke: #1890ff !important;
  330. stroke-dasharray: 4px !important;
  331. fill-opacity: 0.2 !important;
  332. marker-end: url(#sequenceflow-end-_E7DFDF-_E7DFDF-803g1kf6zwzmcig1y2ulm5egr);
  333. }
  334. /deep/.highlight-todo.djs-shape .djs-visual > :nth-child(1) {
  335. fill: #1890ff !important;
  336. stroke: #1890ff !important;
  337. stroke-dasharray: 4px !important;
  338. fill-opacity: 0.2 !important;
  339. }
  340. /** 通过 */
  341. .highlight.djs-shape .djs-visual > :nth-child(1) {
  342. fill: green !important;
  343. stroke: green !important;
  344. fill-opacity: 0.2 !important;
  345. }
  346. .highlight.djs-shape .djs-visual > :nth-child(2) {
  347. fill: green !important;
  348. }
  349. .highlight.djs-shape .djs-visual > path {
  350. fill: green !important;
  351. fill-opacity: 0.2 !important;
  352. stroke: green !important;
  353. }
  354. .highlight.djs-connection > .djs-visual > path {
  355. stroke: green !important;
  356. }
  357. .highlight:not(.djs-connection) .djs-visual > :nth-child(1) {
  358. fill: green !important; /* color elements as green */
  359. }
  360. /deep/.highlight.djs-shape .djs-visual > :nth-child(1) {
  361. fill: green !important;
  362. stroke: green !important;
  363. fill-opacity: 0.2 !important;
  364. }
  365. /deep/.highlight.djs-shape .djs-visual > :nth-child(2) {
  366. fill: green !important;
  367. }
  368. /deep/.highlight.djs-shape .djs-visual > path {
  369. fill: green !important;
  370. fill-opacity: 0.2 !important;
  371. stroke: green !important;
  372. }
  373. /deep/.highlight.djs-connection > .djs-visual > path {
  374. stroke: green !important;
  375. }
  376. /** 不通过 */
  377. .highlight-reject.djs-shape .djs-visual > :nth-child(1) {
  378. fill: red !important;
  379. stroke: red !important;
  380. fill-opacity: 0.2 !important;
  381. }
  382. .highlight-reject.djs-shape .djs-visual > :nth-child(2) {
  383. fill: red !important;
  384. }
  385. .highlight-reject.djs-shape .djs-visual > path {
  386. fill: red !important;
  387. fill-opacity: 0.2 !important;
  388. stroke: red !important;
  389. }
  390. .highlight-reject.djs-connection > .djs-visual > path {
  391. stroke: red !important;
  392. }
  393. .highlight-reject:not(.djs-connection) .djs-visual > :nth-child(1) {
  394. fill: red !important; /* color elements as green */
  395. }
  396. /deep/.highlight-reject.djs-shape .djs-visual > :nth-child(1) {
  397. fill: red !important;
  398. stroke: red !important;
  399. fill-opacity: 0.2 !important;
  400. }
  401. /deep/.highlight-reject.djs-shape .djs-visual > :nth-child(2) {
  402. fill: red !important;
  403. }
  404. /deep/.highlight-reject.djs-shape .djs-visual > path {
  405. fill: red !important;
  406. fill-opacity: 0.2 !important;
  407. stroke: red !important;
  408. }
  409. /deep/.highlight-reject.djs-connection > .djs-visual > path {
  410. stroke: red !important;
  411. }
  412. /** 已取消 */
  413. .highlight-cancel.djs-shape .djs-visual > :nth-child(1) {
  414. fill: grey !important;
  415. stroke: grey !important;
  416. fill-opacity: 0.2 !important;
  417. }
  418. .highlight-cancel.djs-shape .djs-visual > :nth-child(2) {
  419. fill: grey !important;
  420. }
  421. .highlight-cancel.djs-shape .djs-visual > path {
  422. fill: grey !important;
  423. fill-opacity: 0.2 !important;
  424. stroke: grey !important;
  425. }
  426. .highlight-cancel.djs-connection > .djs-visual > path {
  427. stroke: grey !important;
  428. }
  429. .highlight-cancel:not(.djs-connection) .djs-visual > :nth-child(1) {
  430. fill: grey !important; /* color elements as green */
  431. }
  432. /deep/.highlight-cancel.djs-shape .djs-visual > :nth-child(1) {
  433. fill: grey !important;
  434. stroke: grey !important;
  435. fill-opacity: 0.2 !important;
  436. }
  437. /deep/.highlight-cancel.djs-shape .djs-visual > :nth-child(2) {
  438. fill: grey !important;
  439. }
  440. /deep/.highlight-cancel.djs-shape .djs-visual > path {
  441. fill: grey !important;
  442. fill-opacity: 0.2 !important;
  443. stroke: grey !important;
  444. }
  445. /deep/.highlight-cancel.djs-connection > .djs-visual > path {
  446. stroke: grey !important;
  447. }
  448. .element-overlays {
  449. box-sizing: border-box;
  450. padding: 8px;
  451. background: rgba(0, 0, 0, 0.6);
  452. border-radius: 4px;
  453. color: #fafafa;
  454. width: 200px;
  455. }
  456. </style>