This commit is contained in:
张成
2025-12-18 18:46:29 +08:00
parent 73a0999e20
commit f233f368ed
5 changed files with 77 additions and 30 deletions

View File

@@ -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)
}
}
},

View File

@@ -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

View File

@@ -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('当前活动: 无');

View File

@@ -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: '当前是周末,不在允许的时间范围内' };

View File

@@ -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: '当前是周末,不在允许的时间范围内' };