diff --git a/admin/src/store/index.js b/admin/src/store/index.js index d3a747b..7544c7d 100644 --- a/admin/src/store/index.js +++ b/admin/src/store/index.js @@ -2,17 +2,43 @@ * 设备选择模块 - 用于统计页面的设备切换 * 这个模块会被持久化到 localStorage */ + +// localStorage 的 key +const STORAGE_KEY = 'device_selected_sn' + +// 从 localStorage 读取上次选中的设备 +const loadSelectedDevice = () => { + try { + const saved = localStorage.getItem(STORAGE_KEY) + return saved || '' + } catch (error) { + console.error('读取选中设备失败:', error) + return '' + } +} + +// 保存选中的设备到 localStorage +const saveSelectedDevice = (deviceSn) => { + try { + localStorage.setItem(STORAGE_KEY, deviceSn) + } catch (error) { + console.error('保存选中设备失败:', error) + } +} + const deviceModule = { namespaced: true, state: { - selectedDeviceSn: '', // 当前选中的设备 SN + selectedDeviceSn: loadSelectedDevice(), // 从 localStorage 恢复上次选中的设备 deviceList: [], // 设备列表 }, mutations: { SET_SELECTED_DEVICE(state, deviceSn) { state.selectedDeviceSn = deviceSn + // 持久化到 localStorage + saveSelectedDevice(deviceSn) }, SET_DEVICE_LIST(state, list) { @@ -20,6 +46,7 @@ const deviceModule = { // 如果没有选中的设备,且列表不为空,自动选中第一个 if (!state.selectedDeviceSn && list.length > 0) { state.selectedDeviceSn = list[0].deviceSn + saveSelectedDevice(state.selectedDeviceSn) } } }, diff --git a/admin/src/views/home/index.vue b/admin/src/views/home/index.vue index d91d083..cf12e35 100644 --- a/admin/src/views/home/index.vue +++ b/admin/src/views/home/index.vue @@ -208,7 +208,7 @@ export default { data() { return { - selectedDeviceSn: '', + selectedDeviceSn: this.$store.state.device?.selectedDeviceSn || '', // 从 store 恢复上次选中的设备 deviceList: [], todayStats: { applyCount: 0, @@ -320,17 +320,25 @@ export default { }) this.deviceList = Array.from(deviceMap.values()) - + console.log('处理后的设备列表:', this.deviceList) console.log('处理后设备数量:', this.deviceList.length) - + this.$store.dispatch('device/setDeviceList', this.deviceList) - // 如果没有选中设备且有设备列表,选中第一个 - if (!this.selectedDeviceSn && this.deviceList.length > 0) { + // 优先从 store 恢复上次选中的设备 + const savedDeviceSn = this.$store.state.device?.selectedDeviceSn + + if (savedDeviceSn && this.deviceList.some(d => d.deviceSn === savedDeviceSn)) { + // 如果 store 中保存的设备在列表中存在,恢复选中 + console.log('恢复上次选中的设备:', savedDeviceSn) + this.selectedDeviceSn = savedDeviceSn + this.loadData() + } else if (!this.selectedDeviceSn && this.deviceList.length > 0) { + // 如果没有保存的设备或保存的设备不在列表中,选中第一个 + console.log('选中第一个设备:', this.deviceList[0].deviceSn) this.selectedDeviceSn = this.deviceList[0].deviceSn this.$store.dispatch('device/setSelectedDevice', this.selectedDeviceSn) - // 立即加载数据 this.loadData() } else if (this.selectedDeviceSn) { // 如果已有选中设备,直接加载数据 @@ -740,21 +748,23 @@ export default { const deviceRes = await DeviceStatusServer.getById(this.selectedDeviceSn) console.log('设备状态接口返回:', deviceRes) if (deviceRes.code === 0 && deviceRes.data) { - accountData.is_online = deviceRes.data.isOnline || false - accountData.is_logged_in = deviceRes.data.isLoggedIn || false + accountData.is_online = deviceRes.data.isOnline ? 1 : 0 + accountData.is_logged_in = deviceRes.data.isLoggedIn ? 1 : 0 } else { - accountData.is_online = false - accountData.is_logged_in = false + accountData.is_online = 0 + accountData.is_logged_in = 0 } } catch (deviceError) { console.error('获取设备状态失败:', deviceError) - accountData.is_online = false - accountData.is_logged_in = false + accountData.is_online = 0 + accountData.is_logged_in = 0 } } - + // 确保布尔值字段正确初始化,转换为数字类型(0 或 1) accountData.is_enabled = accountData.is_enabled !== undefined ? Number(accountData.is_enabled) : 1 + accountData.is_online = accountData.is_online !== undefined ? Number(accountData.is_online) : 0 + accountData.is_logged_in = accountData.is_logged_in !== undefined ? Number(accountData.is_logged_in) : 0 accountData.auto_deliver = accountData.auto_deliver !== undefined ? Number(accountData.auto_deliver) : 0 accountData.auto_chat = accountData.auto_chat !== undefined ? Number(accountData.auto_chat) : 0 accountData.auto_active = accountData.auto_active !== undefined ? Number(accountData.auto_active) : 0 diff --git a/api/middleware/schedule/deviceWorkStatusNotifier.js b/api/middleware/schedule/deviceWorkStatusNotifier.js index 175a3da..9ac1e17 100644 --- a/api/middleware/schedule/deviceWorkStatusNotifier.js +++ b/api/middleware/schedule/deviceWorkStatusNotifier.js @@ -240,21 +240,31 @@ class DeviceWorkStatusNotifier { if (workStatus.currentActivity) { const activity = workStatus.currentActivity; const typeText = activity.type === 'command' ? '指令' : '任务'; - const statusText = activity.status === 'running' ? '执行中' : - activity.status === 'completed' ? '已完成' : + const statusText = activity.status === 'running' ? '执行中' : + activity.status === 'completed' ? '已完成' : activity.status === 'failed' ? '失败' : '未知'; - - // 构建详细描述:包含指令/任务名称和描述 - let activityDesc = activity.description || activity.name; - - // 对于指令,显示指令类型和名称的详细信息 - if (activity.type === 'command') { - const cmdType = activity.commandType || ''; - if (cmdType && cmdType !== activity.name) { - activityDesc = `${activityDesc} [${cmdType}]`; + + // 使用 description 或 name,避免重复显示 + // 如果 description 存在且不等于 name,优先使用 description + // 否则,使用 name 并可能添加 commandType + let activityDesc = ''; + + if (activity.description && activity.description !== activity.name) { + // 有独立的 description,直接使用,不再添加 commandType + activityDesc = activity.description; + } else { + // 使用 name + activityDesc = activity.name; + + // 对于指令,如果 commandType 存在且与 name 不同,添加详细信息 + if (activity.type === 'command') { + const cmdType = activity.commandType || ''; + if (cmdType && cmdType !== activity.name && !activity.name.includes(cmdType)) { + activityDesc = `${activityDesc} [${cmdType}]`; + } } } - + parts.push(`${typeText}: ${activityDesc} (${statusText}${activity.progress > 0 ? `, 进度: ${activity.progress}%` : ''})`); } else { parts.push('当前活动: 无'); diff --git a/api/middleware/schedule/scheduledJobs.js b/api/middleware/schedule/scheduledJobs.js index 19bcd6f..c2189b8 100644 --- a/api/middleware/schedule/scheduledJobs.js +++ b/api/middleware/schedule/scheduledJobs.js @@ -26,8 +26,8 @@ function checkTimeRange(timeRange) { const startTime = startHour * 60 + startMinute; const endTime = endHour * 60 + endMinute; - // 检查是否仅工作日 - if (timeRange.workdays_only === 1) { + // 检查是否仅工作日(使用宽松比较,兼容字符串和数字) + if (timeRange.workdays_only == 1) { // 使用 == 而不是 === const dayOfWeek = now.getDay(); // 0=周日, 1=周一, ..., 6=周六 if (dayOfWeek === 0 || dayOfWeek === 6) { return { allowed: false, reason: '当前是周末,不在允许的时间范围内' }; diff --git a/api/middleware/schedule/taskHandlers.js b/api/middleware/schedule/taskHandlers.js index 69eca86..40300e6 100644 --- a/api/middleware/schedule/taskHandlers.js +++ b/api/middleware/schedule/taskHandlers.js @@ -523,8 +523,8 @@ class TaskHandlers { const startTime = startHour * 60 + startMinute; const endTime = endHour * 60 + endMinute; - // 检查是否仅工作日 - if (timeRange.workdays_only === 1) { + // 检查是否仅工作日(使用宽松比较,兼容字符串和数字) + if (timeRange.workdays_only == 1) { // 使用 == 而不是 === const dayOfWeek = now.getDay(); // 0=周日, 1=周一, ..., 6=周六 if (dayOfWeek === 0 || dayOfWeek === 6) { return { allowed: false, reason: '当前是周末,不在允许的时间范围内' };