liulijun
2026-03-05 98a69d51981ee6a49136024c8b005f134d3313cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
 
//此js文件是用来自定义扩展业务代码,可以扩展一些自定义页面或者重新配置生成的代码
import gridHeader from './extend/relocationTask.vue'
let extension = {
    components: {
      //查询界面扩展组件
      gridHeader: gridHeader,
      gridBody: '',
      gridFooter: '',
      //新建、编辑弹出框扩展组件
      modelHeader: '',
      modelBody: '',
      modelFooter: ''
    },
    tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
    buttons: { view: [], box: [], detail: [] }, //扩展的按钮
    methods: {
       //下面这些方法可以保留也可以删除
      onInit() {  
        let TaskHandCancelBtn = this.buttons.find(x => x.value == 'TaskHandCancel');
      if (TaskHandCancelBtn) {
        TaskHandCancelBtn.onClick = function () {
          let rows = this.$refs.table.getSelected();
          if (rows.length == 0) return this.$error("请选择数据!");
          if (rows.length > 1) return this.$error("请选择一条数据!");
          var param = rows[0].taskNum;
          this.http
            .post("api/Task/TaskCancel?taskNum="+param, "数据处理中...")
            .then((x) => {
              if (x.status) {
                this.$Message.success('任务取消成功.');
                this.refresh();
              } else {
                return this.$error(x.message);
              }
            });
        }
      }
      let TaskHandCompletedBtn = this.buttons.find(x => x.value == 'TaskHandCompleted');
      if (TaskHandCompletedBtn) {
        TaskHandCompletedBtn.onClick = function () {
          this.$confirm("是否确认完成任务","手动任务完成警告",{
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
              center: true,
              }).then(() => {
                let rows = this.$refs.table.getSelected();
                if (rows.length == 0) return this.$error("请选择数据!");
                if (rows.length > 1) return this.$error("请选择一条数据!");
                var param = rows[0].taskNum;
                this.http
                .post("api/Task/TaskCompleted?taskNum="+param, "")
                .then((x) => {
                    if (x.status) {
                    this.$Message.success('任务手动完成');
                    this.refresh();
                  } else {
                    return this.$error(x.message);
                  }
                });
              });
        }
      }
      let ResendTaskBtn = this.buttons.find(x => x.value == 'ResendTask');
      if (ResendTaskBtn) {
        ResendTaskBtn.onClick = function () {
          this.$confirm("是否确认重新下发任务","重新下发任务警告",{
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
              center: true,
              }).then(() => {
                let rows = this.$refs.table.getSelected();
                if (rows.length == 0) return this.$error("请选择数据!");
                if (rows.length > 1) return this.$error("请选择一条数据!");
                var param = rows[0].taskNum;
                this.http
                .post("api/Task/ResendTask?taskNum="+param, "")
                .then((x) => {
                    if (x.status) {
                    this.$Message.success('任务重新下发');
                    this.refresh();
                  } else {
                    return this.$error(x.message);
                  }
                });
              });
        }
      }
      let relocationBtn = this.buttons.find(x => x.value == 'Relocation');
      if (relocationBtn) {
        relocationBtn.onClick = function () {
          this.$refs.gridHeader.open();
        }
      }
      
      // 初始化任务状态检查定时器
      this.taskStatusMap = {}; // 存储任务状态开始时间
      this.taskTimeoutMinutes = 1; // 任务超时时间,单位为分钟
      this.checkTaskStatusTimer = setInterval(() => {
        this.checkTaskStatus();
      }, 1000); // 每1秒检查一次,提高检查精度,减少延迟
      },
      
      // 检查任务状态
      checkTaskStatus() {
        // 获取当前所有任务数据
        const taskData = this.$refs.table?.rowData || this.$refs.table?.tableData || [];
        const now = new Date();
        
        // 获取全局对象和store
        const globalObj = this.$global || window.$global || {};
        const store = this.$store || window.$store;
        
        // 处理消息删除逻辑,重置对应任务的定时器
        const deletedMessages = globalObj.messageDeleted || [];
        
        if (deletedMessages.length > 0) {
          const storeMessageList = store?.state?.messageList || [];
          
          // 遍历被删除的消息ID,重置对应任务的定时器
          deletedMessages.forEach(deletedId => {
            const deletedMessage = storeMessageList.find(msg => msg.id === deletedId);
            if (deletedMessage?.businessType === 'task_timeout' && deletedMessage.taskNum && this.taskStatusMap[deletedMessage.taskNum]) {
              this.taskStatusMap[deletedMessage.taskNum] = now;
            }
          });
          
          // 清空已处理的删除消息列表
          globalObj.messageDeleted = [];
        }
        
        // 获取当前所有任务号和状态
        const currentTaskStatuses = {};
        taskData.forEach(task => {
          currentTaskStatuses[task.taskNum] = task.taskStatus;
        });
        
        // 检查任务状态
        taskData.forEach(task => {
          if (task.taskStatus === 210) {
            // 堆垛机执行中状态
            if (!this.taskStatusMap[task.taskNum]) {
              this.taskStatusMap[task.taskNum] = now;
            } else {
              // 计算持续时间(分钟)
              const duration = (now - this.taskStatusMap[task.taskNum]) / 60000;
              if (duration >= this.taskTimeoutMinutes) {
                this.sendTaskWarningMessage(task, duration);
                delete this.taskStatusMap[task.taskNum];
              }
            }
          } else {
            // 任务状态已改变,清除记录
            delete this.taskStatusMap[task.taskNum];
          }
        });
        
        // 清除已解决的任务超时消息
        this.clearResolvedTaskMessages(currentTaskStatuses);
      },
      
      // 清除已解决的任务超时消息
      clearResolvedTaskMessages(currentTaskStatuses) {
        // 获取全局对象和store
        const globalObj = this.$global || window.$global || {};
        const store = this.$store || window.$store;
        
        // 获取所有消息列表
        const globalMessageList = globalObj.messageList || [];
        const storeMessageList = store?.state?.messageList || [];
        
        // 合并所有消息,找出需要清除的任务超时消息
        const allMessages = [...globalMessageList, ...storeMessageList];
        const taskTimeoutMessages = allMessages.filter(msg => msg.businessType === 'task_timeout');
        
        // 遍历所有任务超时消息
        taskTimeoutMessages.forEach(msg => {
          const taskNum = msg.taskNum;
          // 检查该任务是否仍然处于堆垛机执行中状态
          if (currentTaskStatuses[taskNum] && currentTaskStatuses[taskNum] !== 210) {
            // 任务已不再处于堆垛机执行中状态,清除该消息
            this.handleDeleteTaskMessage(msg);
          }
        });
      },
      
      // 处理删除单个任务消息
      handleDeleteTaskMessage(message) {
        // 获取全局对象和store
        const globalObj = this.$global || window.$global || {};
        const store = this.$store || window.$store;
        
        // 从全局消息列表中删除该消息
        if (globalObj.messageList) {
          const index = globalObj.messageList.findIndex(msg => msg.id === message.id);
          if (index !== -1) {
            globalObj.messageList.splice(index, 1);
          }
        }
        
        // 从store中删除该消息
        if (store) {
          store.commit('removeMessage', message.id);
        }
      },
      
      // 发送任务警告消息
      sendTaskWarningMessage(task, duration) {
        // 创建警告消息
        const warningMessage = {
          id: Date.now(),
          title: '任务异常警告',
          message: `任务号 ${task.taskNum} 已在堆垛机执行中状态超过${Math.round(duration)}分钟,请及时处理!`,
          type: 'warning',
          businessType: 'task_timeout',
          taskNum: task.taskNum,
          createTime: new Date().toLocaleString()
        };
        
        // 获取全局对象和store,检查是否存在相同的警告消息
        const globalObj = this.$global || window.$global || {};
        const store = this.$store || window.$store;
        const globalMessageList = globalObj.messageList || [];
        const storeMessageList = store?.state?.messageList || [];
        
        // 检查是否已经存在相同的任务超时警告
        const hasExistingWarning = [...globalMessageList, ...storeMessageList].some(msg => 
          msg.businessType === 'task_timeout' && msg.taskNum === task.taskNum
        );
        
        if (hasExistingWarning) return;
        
        // 发送消息到消息列表
        try {
          // 添加消息到store
          const $store = this.$store || window.$store;
          if ($store) {
            $store.commit('addMessage', warningMessage);
          }
          
          // 添加消息到全局消息列表
          if (globalObj.messageList) {
            globalObj.messageList.push(warningMessage);
          }
          
          // 显示警告对话框,优先使用$alert
          // const $global = this.$global || window.$global;
          // const alertOptions = {
          //   confirmButtonText: '确定',
          //   type: warningMessage.type,
          //   closeOnClickModal: false,
          //   closeOnPressEscape: false,
          //   showCancelButton: false
          // };
          
          // if (this.$alert || window.$alert) {
          //   const $alert = this.$alert || window.$alert;
          //   $alert(warningMessage.message, warningMessage.title, alertOptions);
          // } else if (this.$confirm || window.$confirm) {
          //   const $confirm = this.$confirm || window.$confirm;
          //   $confirm(warningMessage.message, warningMessage.title, alertOptions);
          // } else {
          //   // 使用浏览器原生alert作为备选
          //   alert(`${warningMessage.title}: ${warningMessage.message}`);
          // }
        } catch (error) {
          // 出错时使用浏览器原生alert作为最终备选
          try {
            alert(`任务异常警告: 任务号 ${task.taskNum} 已在堆垛机执行中状态超过${Math.round(duration)}分钟,请及时处理!`);
          } catch (e) {
            // 忽略所有错误
          }
        }
      },
      onInited() {
        //框架初始化配置后
        //如果要配置明细表,在此方法操作
        //this.detailOptions.columns.forEach(column=>{ });
      },
      searchBefore(param) {
        //界面查询前,可以给param.wheres添加查询参数
        //返回false,则不会执行查询
        return true;
      },
      searchAfter(result) {
        // 查询后,result返回的查询数据,可以在显示到表格前处理表格的值
        return true;
      },
      addBefore(formData) {
        //新建保存前formData为对象,包括明细表,可以给给表单设置值,自己输出看formData的值
        return true;
      },
      updateBefore(formData) {
        //编辑保存前formData为对象,包括明细表、删除行的Id
        return true;
      },
      rowClick({ row, column, event }) {
        //查询界面点击行事件
        this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
      },
      modelOpenAfter(row) {
        //点击编辑、新建按钮弹出框后,可以在此处写逻辑,如,从后台获取数据
        //(1)判断是编辑还是新建操作: this.currentAction=='Add';
        //(2)给弹出框设置默认值
        //(3)this.editFormFields.字段='xxx';
        //如果需要给下拉框设置默认值,请遍历this.editFormOptions找到字段配置对应data属性的key值
        //看不懂就把输出看:console.log(this.editFormOptions)
      }
    }
  };
  export default extension;