xm
2024-06-14 722af26bc6fec32bb289b1df51a9016a4935610f
提交 | 用户 | 时间
722af2 1 <template>
X 2   <div style="margin-top: 16px">
3     <el-form-item label="消息实例">
4       <div style="display: flex; align-items: center; justify-content: space-between; flex-wrap: nowrap">
5         <el-select v-model="bindMessageId" @change="updateTaskMessage">
6           <el-option v-for="id in Object.keys(messageMap)" :value="id" :label="messageMap[id]" :key="id" />
7         </el-select>
8         <el-button size="mini" type="primary" icon="el-icon-plus" style="margin-left: 8px" @click="openMessageModel" />
9       </div>
10     </el-form-item>
11     <el-dialog :visible.sync="messageModelVisible" :close-on-click-modal="false" title="创建新消息" width="400px" append-to-body destroy-on-close>
12       <el-form :model="newMessageForm" size="mini" label-width="90px" @submit.native.prevent>
13         <el-form-item label="消息ID">
14           <el-input v-model="newMessageForm.id" clearable />
15         </el-form-item>
16         <el-form-item label="消息名称">
17           <el-input v-model="newMessageForm.name" clearable />
18         </el-form-item>
19       </el-form>
20       <template slot="footer">
21         <el-button size="mini" type="primary" @click="createNewMessage">确 认</el-button>
22       </template>
23     </el-dialog>
24   </div>
25 </template>
26
27 <script>
28 export default {
29   name: "ReceiveTask",
30   props: {
31     id: String,
32     type: String
33   },
34   data() {
35     return {
36       bindMessageId: "",
37       newMessageForm: {},
38       messageMap: {},
39       messageModelVisible: false
40     };
41   },
42   watch: {
43     id: {
44       immediate: true,
45       handler() {
46         this.$nextTick(() => this.getBindMessage());
47       }
48     }
49   },
50   created() {
51     this.bpmnMessageRefsMap = Object.create(null);
52     this.bpmnRootElements = window.bpmnInstances.modeler.getDefinitions().rootElements;
53     this.bpmnRootElements
54       .filter(el => el.$type === "bpmn:Message")
55       .forEach(m => {
56         this.bpmnMessageRefsMap[m.id] = m;
57         this.$set(this.messageMap, m.id, m.name);
58       });
59     this.$set(this.messageMap, "-1", "无"); // 添加一个空对象,保证可以取消原消息绑定
60   },
61   methods: {
62     getBindMessage() {
63       this.bpmnElement = window.bpmnInstances.bpmnElement;
64       this.bindMessageId = this.bpmnElement.businessObject?.messageRef?.id || "-1";
65     },
66     openMessageModel() {
67       this.messageModelVisible = true;
68       this.newMessageForm = {};
69     },
70     createNewMessage() {
71       if (this.messageMap[this.newMessageForm.id]) {
72         this.$message.error("该消息已存在,请修改id后重新保存");
73         return;
74       }
75       const newMessage = window.bpmnInstances.moddle.create("bpmn:Message", this.newMessageForm);
76       this.bpmnRootElements.push(newMessage);
77       this.$set(this.messageMap, this.newMessageForm.id, this.newMessageForm.name);
78       this.bpmnMessageRefsMap[this.newMessageForm.id] = newMessage;
79       this.messageModelVisible = false;
80     },
81     updateTaskMessage(messageId) {
82       if (messageId === "-1") {
83         window.bpmnInstances.modeling.updateProperties(this.bpmnElement, {
84           messageRef: null
85         });
86       } else {
87         window.bpmnInstances.modeling.updateProperties(this.bpmnElement, {
88           messageRef: this.bpmnMessageRefsMap[messageId]
89         });
90       }
91     }
92   },
93   beforeDestroy() {
94     this.bpmnElement = null;
95   }
96 };
97 </script>