ProcessViewer.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. } catch (e) {
  98. console.error(e);
  99. // console.error(`[Process Designer Warn]: ${e?.message || e}`);
  100. }
  101. },
  102. /* 高亮流程图 */
  103. // TODO 芋艿:如果多个 endActivity 的话,目前的逻辑可能有一定的问题。https://www.jdon.com/workflow/multi-events.html
  104. async highlightDiagram() {
  105. const activityList = this.activityList;
  106. if (activityList.length === 0) {
  107. return;
  108. }
  109. // 参考自 https://gitee.com/tony2y/RuoYi-flowable/blob/master/ruoyi-ui/src/components/Process/index.vue#L222 实现
  110. // 再次基础上,增加不同审批结果的颜色等等
  111. let canvas = this.bpmnModeler.get('canvas');
  112. let todoActivity = activityList.find(m => !m.endTime) // 找到待办的任务
  113. let endActivity = activityList[activityList.length - 1] // 获得最后一个任务
  114. // debugger
  115. console.log(this.bpmnModeler.getDefinitions().rootElements[0].flowElements);
  116. this.bpmnModeler.getDefinitions().rootElements[0].flowElements?.forEach(n => {
  117. let activity = activityList.find(m => m.key === n.id) // 找到对应的活动
  118. if (n.$type === 'bpmn:UserTask') { // 用户任务
  119. if (!activity) {
  120. return;
  121. }
  122. // 处理用户任务的高亮
  123. const task = this.taskList.find(m => m.id === activity.taskId); // 找到活动对应的 taskId
  124. if (task) {
  125. canvas.addMarker(n.id, this.getResultCss(task.result));
  126. // 如果非通过,就不走后面的线条了
  127. if (task.result !== 2) {
  128. return;
  129. }
  130. }
  131. // 处理 outgoing 出线
  132. const outgoing = this.getActivityOutgoing(activity);
  133. outgoing?.forEach(nn => {
  134. // debugger
  135. let targetActivity = activityList.find(m => m.key === nn.targetRef.id)
  136. // 如果目标活动存在,则根据该活动是否结束,进行【bpmn:SequenceFlow】连线的高亮设置
  137. if (targetActivity) {
  138. canvas.addMarker(nn.id, targetActivity.endTime ? 'highlight' : 'highlight-todo');
  139. } else if (nn.targetRef.$type === 'bpmn:ExclusiveGateway') { // TODO 芋艿:这个流程,暂时没走到过
  140. canvas.addMarker(nn.id, activity.endTime ? 'highlight' : 'highlight-todo');
  141. canvas.addMarker(nn.targetRef.id, activity.endTime ? 'highlight' : 'highlight-todo');
  142. } else if (nn.targetRef.$type === 'bpmn:EndEvent') { // TODO 芋艿:这个流程,暂时没走到过
  143. if (!todoActivity && endActivity.key === n.id) {
  144. canvas.addMarker(nn.id, 'highlight');
  145. canvas.addMarker(nn.targetRef.id, 'highlight');
  146. }
  147. if (!activity.endTime) {
  148. canvas.addMarker(nn.id, 'highlight-todo');
  149. canvas.addMarker(nn.targetRef.id, 'highlight-todo');
  150. }
  151. }
  152. });
  153. } else if (n.$type === 'bpmn:ExclusiveGateway') { // 排它网关
  154. if (!activity) {
  155. return
  156. }
  157. // 设置【bpmn:ExclusiveGateway】排它网关的高亮
  158. canvas.addMarker(n.id, this.getActivityHighlightCss(activity));
  159. // 查找需要高亮的连线
  160. let matchNN = undefined;
  161. let matchActivity = undefined;
  162. n.outgoing?.forEach(nn => {
  163. let targetActivity = activityList.find(m => m.key === nn.targetRef.id);
  164. if (!targetActivity) {
  165. return;
  166. }
  167. // 特殊判断 endEvent 类型的原因,ExclusiveGateway 可能后续连有 2 个路径:
  168. // 1. 一个是 UserTask => EndEvent
  169. // 2. 一个是 EndEvent
  170. // 在选择路径 1 时,其实 EndEvent 可能也存在,导致 1 和 2 都高亮,显然是不正确的。
  171. // 所以,在 matchActivity 为 EndEvent 时,需要进行覆盖~~
  172. if (!matchActivity || matchActivity.type === 'endEvent') {
  173. matchNN = nn;
  174. matchActivity = targetActivity;
  175. }
  176. })
  177. if (matchNN && matchActivity) {
  178. canvas.addMarker(matchNN.id, this.getActivityHighlightCss(matchActivity));
  179. }
  180. } else if (n.$type === 'bpmn:ParallelGateway') { // 并行网关
  181. if (!activity) {
  182. return
  183. }
  184. // 设置【bpmn:ParallelGateway】并行网关的高亮
  185. canvas.addMarker(n.id, this.getActivityHighlightCss(activity));
  186. n.outgoing?.forEach(nn => {
  187. // 获得连线是否有指向目标。如果有,则进行高亮
  188. const targetActivity = activityList.find(m => m.key === nn.targetRef.id)
  189. if (targetActivity) {
  190. canvas.addMarker(nn.id, this.getActivityHighlightCss(targetActivity)); // 高亮【bpmn:SequenceFlow】连线
  191. // 高亮【...】目标。其中 ... 可以是 bpm:UserTask、也可以是其它的。当然,如果是 bpm:UserTask 的话,其实不做高亮也没问题,因为上面有逻辑做了这块。
  192. canvas.addMarker(nn.targetRef.id, this.getActivityHighlightCss(targetActivity));
  193. }
  194. })
  195. } else if (n.$type === 'bpmn:StartEvent') { // 开始节点
  196. n.outgoing?.forEach(nn => { // outgoing 例如说【bpmn:SequenceFlow】连线
  197. // 获得连线是否有指向目标。如果有,则进行高亮
  198. let targetActivity = activityList.find(m => m.key === nn.targetRef.id);
  199. if (targetActivity) {
  200. canvas.addMarker(nn.id, 'highlight'); // 高亮【bpmn:SequenceFlow】连线
  201. canvas.addMarker(n.id, 'highlight'); // 高亮【bpmn:StartEvent】开始节点(自己)
  202. }
  203. });
  204. } else if (n.$type === 'bpmn:EndEvent') { // 结束节点
  205. if (!this.processInstance || this.processInstance.result === 1) {
  206. return;
  207. }
  208. canvas.addMarker(n.id, this.getResultCss(this.processInstance.result));
  209. }
  210. })
  211. },
  212. getActivityHighlightCss(activity) {
  213. return activity.endTime ? 'highlight' : 'highlight-todo';
  214. },
  215. getResultCss(result) {
  216. if (result === 1) {
  217. return 'highlight-todo';
  218. } else if (result === 2) {
  219. return 'highlight';
  220. } else if (result === 3) {
  221. return 'highlight-reject';
  222. } else if (result === 4) {
  223. return 'highlight-cancel';
  224. }
  225. return '';
  226. },
  227. getActivityOutgoing(activity) {
  228. // 如果有 outgoing,则直接使用它
  229. if (activity.outgoing && activity.outgoing.length > 0) {
  230. return activity.outgoing;
  231. }
  232. // 如果没有,则遍历获得起点为它的【bpmn:SequenceFlow】节点们。原因是:bpmn-js 的 UserTask 拿不到 outgoing
  233. const flowElements = this.bpmnModeler.getDefinitions().rootElements[0].flowElements;
  234. const outgoing = [];
  235. flowElements.forEach(item => {
  236. if (item.$type !== 'bpmn:SequenceFlow') {
  237. return;
  238. }
  239. if (item.sourceRef.id === activity.key) {
  240. outgoing.push(item);
  241. }
  242. });
  243. return outgoing;
  244. },
  245. initModelListeners() {
  246. const EventBus = this.bpmnModeler.get("eventBus");
  247. const that = this;
  248. // 注册需要的监听事件
  249. EventBus.on('element.hover', function(eventObj) {
  250. let element = eventObj ? eventObj.element : null;
  251. that.elementHover(element);
  252. });
  253. EventBus.on('element.out', function(eventObj) {
  254. let element = eventObj ? eventObj.element : null;
  255. that.elementOut(element);
  256. });
  257. },
  258. // 流程图的元素被 hover
  259. elementHover(element) {
  260. this.element = element;
  261. !this.elementOverlayIds && (this.elementOverlayIds = {});
  262. !this.overlays && (this.overlays = this.bpmnModeler.get("overlays"));
  263. // 展示信息
  264. if (!this.elementOverlayIds[element.id] && element.type !== "bpmn:Process") {
  265. let html = `<div class="element-overlays">
  266. <p>Elemet id: ${element.id}</p>
  267. <p>Elemet type: ${element.type}</p>
  268. </div>`; // 默认值
  269. if (element.type === 'bpmn:StartEvent' && this.processInstance) {
  270. html = `<p>发起人:${this.processInstance.startUser.nickname}</p>
  271. <p>部门:${this.processInstance.startUser.deptName}</p>
  272. <p>创建时间:${this.parseTime(this.processInstance.createTime)}`;
  273. } else if (element.type === 'bpmn:UserTask') {
  274. // debugger
  275. const activity = this.activityList.find(m => m.key === element.id);
  276. if (!activity) {
  277. return;
  278. }
  279. let task = this.taskList.find(m => m.id === activity.taskId); // 找到活动对应的 taskId
  280. if (!task) {
  281. return;
  282. }
  283. html = `<p>审批人:${task.assigneeUser.nickname}</p>
  284. <p>部门:${task.assigneeUser.deptName}</p>
  285. <p>结果:${this.getDictDataLabel(this.DICT_TYPE.BPM_PROCESS_INSTANCE_RESULT, task.result)}</p>
  286. <p>创建时间:${this.parseTime(task.createTime)}</p>`;
  287. if (task.endTime) {
  288. html += `<p>结束时间:${this.parseTime(task.endTime)}</p>`
  289. }
  290. if (task.comment) {
  291. html += `<p>审批建议:${task.comment}</p>`
  292. }
  293. } else if (element.type === 'bpmn:EndEvent' && this.processInstance) {
  294. html = `<p>结果:${this.getDictDataLabel(this.DICT_TYPE.BPM_PROCESS_INSTANCE_RESULT, this.processInstance.result)}</p>`;
  295. if (this.processInstance.endTime) {
  296. html += `<p>结束时间:${this.parseTime(this.processInstance.endTime)}</p>`
  297. }
  298. }
  299. this.elementOverlayIds[element.id] = this.overlays.add(element, {
  300. position: { left: 0, bottom: 0 },
  301. html: `<div class="element-overlays">${html}</div>`
  302. });
  303. }
  304. },
  305. // 流程图的元素被 out
  306. elementOut(element) {
  307. this.overlays.remove({ element });
  308. this.elementOverlayIds[element.id] = null;
  309. },
  310. }
  311. };
  312. </script>
  313. <style>
  314. /** 处理中 */
  315. .highlight-todo.djs-connection > .djs-visual > path {
  316. stroke: #1890ff !important;
  317. stroke-dasharray: 4px !important;
  318. fill-opacity: 0.2 !important;
  319. }
  320. .highlight-todo.djs-shape .djs-visual > :nth-child(1) {
  321. fill: #1890ff !important;
  322. stroke: #1890ff !important;
  323. stroke-dasharray: 4px !important;
  324. fill-opacity: 0.2 !important;
  325. }
  326. /deep/.highlight-todo.djs-connection > .djs-visual > path {
  327. stroke: #1890ff !important;
  328. stroke-dasharray: 4px !important;
  329. fill-opacity: 0.2 !important;
  330. marker-end: url(#sequenceflow-end-_E7DFDF-_E7DFDF-803g1kf6zwzmcig1y2ulm5egr);
  331. }
  332. /deep/.highlight-todo.djs-shape .djs-visual > :nth-child(1) {
  333. fill: #1890ff !important;
  334. stroke: #1890ff !important;
  335. stroke-dasharray: 4px !important;
  336. fill-opacity: 0.2 !important;
  337. }
  338. /** 通过 */
  339. .highlight.djs-shape .djs-visual > :nth-child(1) {
  340. fill: green !important;
  341. stroke: green !important;
  342. fill-opacity: 0.2 !important;
  343. }
  344. .highlight.djs-shape .djs-visual > :nth-child(2) {
  345. fill: green !important;
  346. }
  347. .highlight.djs-shape .djs-visual > path {
  348. fill: green !important;
  349. fill-opacity: 0.2 !important;
  350. stroke: green !important;
  351. }
  352. .highlight.djs-connection > .djs-visual > path {
  353. stroke: green !important;
  354. }
  355. .highlight:not(.djs-connection) .djs-visual > :nth-child(1) {
  356. fill: green !important; /* color elements as green */
  357. }
  358. /deep/.highlight.djs-shape .djs-visual > :nth-child(1) {
  359. fill: green !important;
  360. stroke: green !important;
  361. fill-opacity: 0.2 !important;
  362. }
  363. /deep/.highlight.djs-shape .djs-visual > :nth-child(2) {
  364. fill: green !important;
  365. }
  366. /deep/.highlight.djs-shape .djs-visual > path {
  367. fill: green !important;
  368. fill-opacity: 0.2 !important;
  369. stroke: green !important;
  370. }
  371. /deep/.highlight.djs-connection > .djs-visual > path {
  372. stroke: green !important;
  373. }
  374. /** 不通过 */
  375. .highlight-reject.djs-shape .djs-visual > :nth-child(1) {
  376. fill: red !important;
  377. stroke: red !important;
  378. fill-opacity: 0.2 !important;
  379. }
  380. .highlight-reject.djs-shape .djs-visual > :nth-child(2) {
  381. fill: red !important;
  382. }
  383. .highlight-reject.djs-shape .djs-visual > path {
  384. fill: red !important;
  385. fill-opacity: 0.2 !important;
  386. stroke: red !important;
  387. }
  388. .highlight-reject.djs-connection > .djs-visual > path {
  389. stroke: red !important;
  390. }
  391. .highlight-reject:not(.djs-connection) .djs-visual > :nth-child(1) {
  392. fill: red !important; /* color elements as green */
  393. }
  394. /deep/.highlight-reject.djs-shape .djs-visual > :nth-child(1) {
  395. fill: red !important;
  396. stroke: red !important;
  397. fill-opacity: 0.2 !important;
  398. }
  399. /deep/.highlight-reject.djs-shape .djs-visual > :nth-child(2) {
  400. fill: red !important;
  401. }
  402. /deep/.highlight-reject.djs-shape .djs-visual > path {
  403. fill: red !important;
  404. fill-opacity: 0.2 !important;
  405. stroke: red !important;
  406. }
  407. /deep/.highlight-reject.djs-connection > .djs-visual > path {
  408. stroke: red !important;
  409. }
  410. /** 已取消 */
  411. .highlight-cancel.djs-shape .djs-visual > :nth-child(1) {
  412. fill: grey !important;
  413. stroke: grey !important;
  414. fill-opacity: 0.2 !important;
  415. }
  416. .highlight-cancel.djs-shape .djs-visual > :nth-child(2) {
  417. fill: grey !important;
  418. }
  419. .highlight-cancel.djs-shape .djs-visual > path {
  420. fill: grey !important;
  421. fill-opacity: 0.2 !important;
  422. stroke: grey !important;
  423. }
  424. .highlight-cancel.djs-connection > .djs-visual > path {
  425. stroke: grey !important;
  426. }
  427. .highlight-cancel:not(.djs-connection) .djs-visual > :nth-child(1) {
  428. fill: grey !important; /* color elements as green */
  429. }
  430. /deep/.highlight-cancel.djs-shape .djs-visual > :nth-child(1) {
  431. fill: grey !important;
  432. stroke: grey !important;
  433. fill-opacity: 0.2 !important;
  434. }
  435. /deep/.highlight-cancel.djs-shape .djs-visual > :nth-child(2) {
  436. fill: grey !important;
  437. }
  438. /deep/.highlight-cancel.djs-shape .djs-visual > path {
  439. fill: grey !important;
  440. fill-opacity: 0.2 !important;
  441. stroke: grey !important;
  442. }
  443. /deep/.highlight-cancel.djs-connection > .djs-visual > path {
  444. stroke: grey !important;
  445. }
  446. .element-overlays {
  447. box-sizing: border-box;
  448. padding: 8px;
  449. background: rgba(0, 0, 0, 0.6);
  450. border-radius: 4px;
  451. color: #fafafa;
  452. width: 200px;
  453. }
  454. </style>