ProcessViewer.vue 17 KB

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