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