pc端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.2 KiB

import { getDevices, getDevicesAll, getDevicesCount } from 'apis';
import dayjs from 'dayjs';
import { ElMessage } from 'element-plus';
const user = {
namespaced: true,
state: {
devices: [], // 站点列表 设备列表简版
devicesAll: null, // 设备列表完整版
currentDeviceId: '', // 当前正在编辑的设备deviceId
count: {
total: 0, // 总设备数量
online: 0, // 在线
offline: 0, // 离线
warning: 0, // 警告
fault: 0, // 故障
},
},
getters: {
// 当前正在编辑的设备的完整信息
current({ devicesAll, currentDeviceId }) {
try {
return devicesAll.data.find(device => device.deviceId === currentDeviceId);
} catch (error) {
return null;
}
},
},
mutations: {
/**
* 设置devices数据
* @param {*} state
* @param {array} devices
*/
setDevices(state, devices) {
if (devices && devices.length) {
state.devices = devices;
user.mutations.setCurrentDeviceId(state, devices[0].deviceId);
} else {
state.devices = [];
}
},
/**
* 设置设备统计数据信息
* @param {object} state
* @param {object} count // 服务端返回的对象
* @param {number} count.total // 总设备数量
* @param {number} count.online // 在线
* @param {number} count.offline // 离线
* @param {number} count.warning // 报警
* @param {number} count.fault // 故障
*/
setDevicesCount(state, count) {
state.count = count;
},
/**
* 设置devicesAll的数据
* @param {*} state
* @param {object} devices {page, data}
*/
setDevicesAll(state, devices) {
if (devices && devices.data) {
for (let i = 0; i < devices.data.length; i++) {
const device = devices.data[i];
if (device.installTime) {
devices.data[i].installTime = dayjs(new Date(+device.installTime)).format('YYYY-MM-DD HH:mm:ss');
}
if (device.runTime) {
devices.data[i].runTime = dayjs(new Date(+device.runTime)).format('YYYY-MM-DD HH:mm:ss');
}
}
}
state.devicesAll = devices;
},
/**
* 设置正则编辑的设备deviceId
* @param {*} state
* @param {string} deviceId
*/
setCurrentDeviceId(state, deviceId) {
state.currentDeviceId = deviceId;
},
/**
* 更新某个设备的信息
* @param {*} param0
* @param {object} newData 设备更新后的信息
*/
updateDevice({ devicesAll }, newData) {
for (let i = 0; i < devicesAll.data.length; i++) {
const item = devicesAll.data[i];
if (item && item.deviceId === newData.deviceId) {
newData.installTime = dayjs(new Date(+item.installTime)).format('YYYY-MM-DD HH:mm:ss');
newData.runTime = dayjs(new Date(+item.runTime)).format('YYYY-MM-DD HH:mm:ss');
devicesAll.data[i] = newData;
break;
}
}
},
/**
* 删除设备
* @param {*} param0
* @param {string} deviceId 设备id
*/
deleteDevice({ devicesAll }, deviceId) {
if (!devicesAll || !devicesAll.data || !devicesAll.data.length) return;
devicesAll.data = devicesAll.data.filter(item => item.deviceId !== deviceId);
},
},
actions: {
// 获取设备列表(站点列表)
async getDevices({ commit }) {
try {
const data = await getDevices();
commit('setDevices', data || []);
return data;
} catch (error) {
throw new Error(error);
}
},
// 查询设备数量信息
async getDevicesCount({ commit }) {
try {
const data = await getDevicesCount();
commit('setDevicesCount', data);
} catch (error) {
ElMessage.error(error.message || '获取设备统计信息失败');
throw new Error(error);
}
},
// 获取设备列表(站点列表) 完整信息
async getDevicesAll({ commit }, params) {
try {
const data = await getDevicesAll(params);
commit('setDevicesAll', data || null);
return data;
} catch (error) {
throw new Error(error);
}
},
},
};
export default user;