ProcessViewer.vue 19 KB

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