|
|
|
import { getDevices, getDevicesAll } from 'apis/index';
|
|
|
|
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
|
|
|
const user = {
|
|
|
|
namespaced: true,
|
|
|
|
|
|
|
|
state: {
|
|
|
|
devices: [], // 站点列表 设备列表简版
|
|
|
|
devicesAll: null, // 设备列表完整版
|
|
|
|
currentDeviceId: '', // 当前正在编辑的设备deviceId
|
|
|
|
},
|
|
|
|
|
|
|
|
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) {
|
|
|
|
state.devices = devices;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置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 getDevicesAll({ commit }, params) {
|
|
|
|
try {
|
|
|
|
const data = await getDevicesAll(params);
|
|
|
|
commit('setDevicesAll', data || null);
|
|
|
|
return data;
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default user;
|