// WCS系统任务异常监控服务
|
import http from '../api/http';
|
|
// WCS任务异常监控服务类
|
class WcsTaskMonitorService {
|
constructor() {
|
this.checkTaskStatusTimer = null;
|
this.apiTaskData = []; // 存储通过API获取的任务数据
|
this.apiRefreshTimer = null; // API数据刷新定时器
|
this.apiRefreshInterval = 5000; // API数据刷新间隔,5秒
|
}
|
|
// 初始化WCS任务异常监控服务
|
init(app) {
|
// 设置定时器,每5秒检查一次
|
this.checkTaskStatusTimer = setInterval(() => {
|
this.checkTaskExceptions();
|
}, 5000);
|
|
// 设置API数据刷新定时器,每5秒刷新一次任务数据
|
this.apiRefreshTimer = setInterval(() => {
|
this.refreshTaskDataFromApi();
|
}, this.apiRefreshInterval);
|
|
// 初始刷新一次任务数据
|
this.refreshTaskDataFromApi();
|
|
// 保存定时器引用,方便后续清理
|
if (app && app.config) {
|
app.config.globalProperties.$global.wcsTaskMonitorTimer = this.checkTaskStatusTimer;
|
app.config.globalProperties.$global.wcsTaskMonitorApiTimer = this.apiRefreshTimer;
|
}
|
|
// 方便手动测试,将check方法挂载到window对象
|
window.testWcsTaskMonitor = () => {
|
this.checkTaskExceptions();
|
};
|
}
|
|
// 从API刷新任务数据
|
async refreshTaskDataFromApi() {
|
try {
|
// 调用WCS专门的API获取异常任务数据
|
const apiData = await http.post('http://localhost:9291/api/Task/GetTasksWithException', {});
|
|
if (apiData && apiData.data) {
|
this.apiTaskData = apiData.data;
|
} else {
|
this.apiTaskData = [];
|
}
|
} catch (error) {
|
console.error('从API获取异常任务数据失败:', error);
|
this.apiTaskData = [];
|
}
|
}
|
|
// 检查任务异常
|
checkTaskExceptions() {
|
// 直接使用从API获取的异常任务数据
|
const exceptionTasks = this.apiTaskData || [];
|
|
// 去重,确保每个任务只处理一次
|
const uniqueExceptionTasks = [];
|
const taskNumSet = new Set();
|
|
exceptionTasks.forEach(task => {
|
if (task && task.taskNum && !taskNumSet.has(task.taskNum)) {
|
uniqueExceptionTasks.push(task);
|
taskNumSet.add(task.taskNum);
|
}
|
});
|
|
// 获取全局对象
|
const globalObj = window.$global || {};
|
|
// 处理消息删除逻辑,重置对应任务的异常标记
|
const deletedMessages = [...(globalObj.messageDeleted || [])];
|
|
if (deletedMessages.length > 0) {
|
// 遍历被删除的消息ID,重置对应任务的异常标记
|
deletedMessages.forEach(deletedId => {
|
// 直接遍历所有已处理的异常任务
|
// 因为消息可能已经被从messageList中删除
|
for (const taskNum of this.processedExceptions) {
|
// 这里不依赖messageList,直接重置所有可能的任务
|
// 确保删除后能重新发送消息
|
this.processedExceptions.delete(taskNum);
|
}
|
});
|
|
// 清空已处理的删除消息列表,避免重复处理
|
globalObj.messageDeleted = [];
|
}
|
|
// 处理异常任务
|
uniqueExceptionTasks.forEach(task => {
|
// 不管是否已经处理过,只要任务在异常列表中,就发送消息
|
// 这样确保删除后能重新发送消息
|
this.sendTaskExceptionMessage(task);
|
// 不需要添加到processedExceptions,因为我们不再使用它来阻止重发
|
});
|
|
// 清除已解决的任务异常消息
|
this.clearResolvedTaskExceptionMessages(uniqueExceptionTasks);
|
}
|
|
// 清除已解决的任务异常消息
|
clearResolvedTaskExceptionMessages(currentExceptionTasks) {
|
// 获取全局对象
|
const globalObj = window.$global || {};
|
|
// 获取全局消息列表
|
const globalMessageList = [...(globalObj.messageList || [])];
|
|
// 找出需要清除的任务异常消息
|
const taskExceptionMessages = globalMessageList.filter(msg => msg.businessType === 'wcs_task_exception');
|
|
// 构建当前异常任务集合
|
const currentExceptionTaskNums = new Set();
|
currentExceptionTasks.forEach(task => {
|
if (task && task.taskNum) {
|
currentExceptionTaskNums.add(task.taskNum);
|
}
|
});
|
|
// 遍历所有任务异常消息
|
taskExceptionMessages.forEach(msg => {
|
const taskNum = msg.taskNum;
|
// 检查任务是否不在当前异常任务列表中(说明异常已解决)
|
if (taskNum && !currentExceptionTaskNums.has(taskNum)) {
|
// 任务异常已解决,清除该消息
|
if (globalObj.messageList && Array.isArray(globalObj.messageList)) {
|
const index = globalObj.messageList.findIndex(m => m.id === msg.id);
|
if (index !== -1) {
|
globalObj.messageList.splice(index, 1);
|
}
|
}
|
}
|
});
|
}
|
|
// 发送任务异常消息
|
sendTaskExceptionMessage(task) {
|
// 创建唯一的消息ID
|
const messageId = Date.now() + Math.random().toString(36).substr(2, 9);
|
|
// 获取异常描述,确保不为空
|
const exceptionDescription = task.ExceptionMessage || '未知异常';
|
|
// 创建异常消息
|
const exceptionMessage = {
|
id: messageId,
|
title: '任务异常警告',
|
message: `任务号 ${task.taskNum} 出现异常:${exceptionDescription},请及时处理!`,
|
type: 'error',
|
businessType: 'wcs_task_exception',
|
taskNum: task.taskNum,
|
createTime: new Date().toLocaleString(),
|
relatedData: {
|
TaskNum: task.taskNum,
|
ExceptionMessage: exceptionDescription,
|
CreateDate: task.createDate
|
}
|
};
|
|
// 获取全局对象
|
const globalObj = window.$global || {};
|
|
// 检查是否已经存在相同的任务异常警告
|
const globalMessageList = globalObj.messageList || [];
|
|
const hasExistingWarning = globalMessageList.some(msg =>
|
msg.businessType === 'wcs_task_exception' && msg.taskNum === task.taskNum
|
);
|
|
if (hasExistingWarning) return;
|
|
// 发送消息到全局消息列表
|
try {
|
if (globalObj.messageList) {
|
globalObj.messageList.push(exceptionMessage);
|
}
|
} catch (error) {
|
// 出错时使用浏览器原生alert作为最终备选
|
try {
|
alert(`任务异常警告: 任务号 ${task.taskNum} 出现异常:${exceptionDescription},请及时处理!`);
|
} catch (e) {
|
// 忽略所有错误
|
}
|
}
|
}
|
|
// 手动检查任务异常(用于外部调用)
|
manualCheckTaskExceptions() {
|
this.checkTaskExceptions();
|
}
|
|
// 清理资源
|
cleanup() {
|
if (this.checkTaskStatusTimer) {
|
clearInterval(this.checkTaskStatusTimer);
|
this.checkTaskStatusTimer = null;
|
}
|
|
if (this.apiRefreshTimer) {
|
clearInterval(this.apiRefreshTimer);
|
this.apiRefreshTimer = null;
|
}
|
|
this.apiTaskData = [];
|
delete window.testWcsTaskMonitor;
|
}
|
}
|
|
// 创建单例实例
|
const wcsTaskMonitorService = new WcsTaskMonitorService();
|
|
export default wcsTaskMonitorService;
|