2024-09-25 18:47:15 +08:00
|
|
|
|
import store from "@/store"
|
|
|
|
|
import constant from "@/utils/constant.js"
|
|
|
|
|
import * as commonUtils from "@/utils/common.js"
|
|
|
|
|
import {
|
|
|
|
|
deptTreeSelectByRole,
|
|
|
|
|
getManagerGroundList,
|
2024-09-26 18:52:14 +08:00
|
|
|
|
getDeviceList
|
2024-09-25 18:47:15 +08:00
|
|
|
|
} from "@/api/index.js"
|
|
|
|
|
|
2024-09-26 12:42:59 +08:00
|
|
|
|
class indexUtil {
|
2024-09-25 18:47:15 +08:00
|
|
|
|
static isFirst = true //是否第一次加载
|
2024-09-26 12:42:59 +08:00
|
|
|
|
static userId = null; //用户id
|
|
|
|
|
static list = []; //列表
|
|
|
|
|
static showIndex = null; //默认显示的面板索引
|
|
|
|
|
static selectItem = null; //选中的数据
|
|
|
|
|
static isDataChange = false; //数据是否改变
|
2024-09-26 18:52:14 +08:00
|
|
|
|
static deviceData = null; //设备数据
|
2024-09-25 18:47:15 +08:00
|
|
|
|
|
|
|
|
|
//接收创建来的数据
|
2024-09-26 18:52:14 +08:00
|
|
|
|
constructor(eventHandler) {
|
2024-09-25 18:47:15 +08:00
|
|
|
|
this.isFirst = true;
|
|
|
|
|
this.userId = null;
|
|
|
|
|
this.list = [];
|
2024-09-26 12:42:59 +08:00
|
|
|
|
this.showIndex = null;
|
2024-09-25 18:47:15 +08:00
|
|
|
|
this.selectItem = null;
|
2024-09-26 18:52:14 +08:00
|
|
|
|
|
|
|
|
|
this.eventHandler = eventHandler;
|
2024-09-25 18:47:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取列表
|
|
|
|
|
getList(userId) {
|
2024-09-26 12:42:59 +08:00
|
|
|
|
uni.showLoading();
|
2024-09-25 18:47:15 +08:00
|
|
|
|
this.userId = userId;
|
2024-09-26 18:52:14 +08:00
|
|
|
|
//如果是第一次获取,则先取缓存,然后异步更新数据
|
2024-09-25 18:47:15 +08:00
|
|
|
|
if (this.isFirst) {
|
2024-09-26 18:52:14 +08:00
|
|
|
|
//地块列表缓存
|
2024-09-25 18:47:15 +08:00
|
|
|
|
const lands = uni.getStorageSync(constant.lands);
|
|
|
|
|
if (lands && lands.userId == userId) {
|
|
|
|
|
this.list = lands.data;
|
|
|
|
|
}
|
2024-09-26 18:52:14 +08:00
|
|
|
|
//选中地块缓存
|
2024-09-26 12:42:59 +08:00
|
|
|
|
this.selectItem = uni.getStorageSync(constant.landItem) || null;
|
2024-09-26 18:52:14 +08:00
|
|
|
|
//默认显示索引
|
2024-09-26 12:42:59 +08:00
|
|
|
|
this.showIndex = this.findLandIndex(this.list, this.selectItem);
|
2024-09-26 18:52:14 +08:00
|
|
|
|
//设备列表
|
|
|
|
|
if (this.selectItem) {
|
|
|
|
|
const device = uni.getStorageSync(constant.device) || null;
|
|
|
|
|
if (device && device.landId == this.selectItem.id) {
|
|
|
|
|
this.device = device;
|
|
|
|
|
} else {
|
|
|
|
|
this.device = null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.device = null;
|
|
|
|
|
}
|
2024-09-25 18:47:15 +08:00
|
|
|
|
}
|
2024-09-26 12:42:59 +08:00
|
|
|
|
uni.hideLoading();
|
2024-09-25 18:47:15 +08:00
|
|
|
|
if (this.isFirst) {
|
|
|
|
|
this.isFirst = false;
|
2024-09-26 18:52:14 +08:00
|
|
|
|
this.getNewLandList(userId);
|
2024-09-27 12:01:15 +08:00
|
|
|
|
|
2024-09-25 18:47:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 18:52:14 +08:00
|
|
|
|
// 获取最新地块数据
|
|
|
|
|
async getNewLandList(userId) {
|
2024-09-25 18:47:15 +08:00
|
|
|
|
this.userId = userId;
|
|
|
|
|
// 如果是第一次获取,则重新查询一遍数据,更新旧数据
|
|
|
|
|
let company = await deptTreeSelectByRole();
|
|
|
|
|
// console.error("company:",company);
|
|
|
|
|
if (company.data.length) {
|
|
|
|
|
let companys = [];
|
|
|
|
|
if (company.data.length == 1 && company.data[0].id == 100) {
|
|
|
|
|
companys = companys.concat(company.data[0].children)
|
|
|
|
|
} else {
|
|
|
|
|
companys = company.data;
|
|
|
|
|
}
|
|
|
|
|
let land = await getManagerGroundList(userId);
|
|
|
|
|
// console.error("land:", land);
|
|
|
|
|
if (land.data.length) {
|
2024-09-26 18:52:14 +08:00
|
|
|
|
this.handleLandData(companys, land.data);
|
2024-09-25 18:47:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
commonUtils.toast("无数据");
|
2024-09-26 12:42:59 +08:00
|
|
|
|
this.list = null;
|
|
|
|
|
this.selectItem = null;
|
2024-09-25 18:47:15 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
commonUtils.toast("无数据");
|
2024-09-26 12:42:59 +08:00
|
|
|
|
this.list = null;
|
|
|
|
|
this.selectItem = null;
|
|
|
|
|
}
|
|
|
|
|
// 持久化到本地缓存中
|
|
|
|
|
if (this.list) {
|
|
|
|
|
this.setStorage(1, {
|
|
|
|
|
userId: this.userId,
|
|
|
|
|
data: this.list,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2024-09-25 18:47:15 +08:00
|
|
|
|
this.setStorage(1, null);
|
|
|
|
|
}
|
2024-09-26 12:42:59 +08:00
|
|
|
|
this.setStorage(2, this.selectItem);
|
2024-09-27 12:01:15 +08:00
|
|
|
|
this.getNewDeviceList();
|
2024-09-25 18:47:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 18:52:14 +08:00
|
|
|
|
// 组织地块数据
|
|
|
|
|
handleLandData(companys, lands) {
|
2024-09-25 18:47:15 +08:00
|
|
|
|
let id = null;
|
|
|
|
|
let selectItem = null;
|
|
|
|
|
const landItem = uni.getStorageSync(constant.landItem);
|
|
|
|
|
if (landItem) {
|
|
|
|
|
id = landItem.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const companyObj = companys.reduce((accumulator, current) => {
|
|
|
|
|
accumulator[current.id] = current;
|
|
|
|
|
return accumulator;
|
|
|
|
|
}, {});
|
|
|
|
|
let groups = {};
|
|
|
|
|
lands.map(item => {
|
|
|
|
|
let param = {
|
|
|
|
|
companyId: item.companyId,
|
|
|
|
|
companyName: companyObj[item.companyId] ? companyObj[item.companyId].label : "",
|
|
|
|
|
id: item.id,
|
|
|
|
|
name: item.landName,
|
|
|
|
|
landIcon: item.landIcon || '',
|
|
|
|
|
valueNum: item.valveNum,
|
|
|
|
|
controlFlag: item.controlFlag, //设备是否可控(0是 1否)
|
|
|
|
|
enableFlapValve: item.enableFlapValve == 0, //启用蝶阀
|
|
|
|
|
landManualOperation: item.landManualOperation == 0, //启用批量控制
|
2024-09-26 18:52:14 +08:00
|
|
|
|
mapContent: null
|
2024-09-25 18:47:15 +08:00
|
|
|
|
}
|
|
|
|
|
// 获取地图信息
|
|
|
|
|
if (item.landMap != null) {
|
|
|
|
|
param.mapContent = JSON.parse(decodeURIComponent(item.landMap.mapContent)) //地块地图 地图标点
|
|
|
|
|
}
|
|
|
|
|
// 判断是否当前选中的数据
|
|
|
|
|
if (param.id == id) {
|
|
|
|
|
selectItem = param;
|
|
|
|
|
}
|
|
|
|
|
// 判断公司是否已经分组,如果未分组,则添加分组
|
|
|
|
|
if (!groups[item.companyId]) {
|
|
|
|
|
groups[item.companyId] = [];
|
|
|
|
|
}
|
|
|
|
|
groups[item.companyId].push(param);
|
|
|
|
|
})
|
|
|
|
|
companys.forEach(item => {
|
|
|
|
|
item.children = groups[item.id] || [];
|
|
|
|
|
});
|
|
|
|
|
let index = 0;
|
|
|
|
|
while (!selectItem) {
|
|
|
|
|
if (companys[index].children.length) {
|
|
|
|
|
selectItem = companys[index].children[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.list = companys;
|
|
|
|
|
this.selectItem = selectItem;
|
2024-09-26 12:42:59 +08:00
|
|
|
|
this.showIndex = this.findLandIndex(this.list, this.selectItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//寻找默认显示的行
|
|
|
|
|
findLandIndex(list, item) {
|
|
|
|
|
let showIndex = null
|
|
|
|
|
if (list.length && item) {
|
|
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
|
if (list[i].id == item.companyId) {
|
|
|
|
|
showIndex = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return showIndex;
|
2024-09-25 18:47:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 18:52:14 +08:00
|
|
|
|
//获取最新设备数据
|
|
|
|
|
async getNewDeviceList() {
|
|
|
|
|
// 如果是第一次获取,则重新查询一遍数据,更新旧数据
|
|
|
|
|
// let device = await getDeviceList();
|
|
|
|
|
// console.error("device:", device)
|
|
|
|
|
let query = {
|
|
|
|
|
landId: this.selectItem.id,
|
|
|
|
|
deviceCode: ""
|
|
|
|
|
}
|
|
|
|
|
getDeviceList(query).then(res => {
|
|
|
|
|
// console.error("device:", res);
|
|
|
|
|
let device = {
|
|
|
|
|
stations: [],
|
|
|
|
|
flapValves: [],
|
|
|
|
|
waterOutlets: [],
|
|
|
|
|
}
|
|
|
|
|
if (res.data) {
|
|
|
|
|
if (res.data.devices.length) {
|
|
|
|
|
device.stations = res.data.devices.filter(item => item.deviceTypeId === 1);
|
|
|
|
|
device.flapValves = res.data.devices.filter(item => item.deviceTypeId === 12);
|
|
|
|
|
}
|
|
|
|
|
if (res.data.waterOutletGroups.length) {
|
|
|
|
|
device.waterOutlets = res.data.waterOutletGroups;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
device = null;
|
|
|
|
|
}
|
2024-09-27 12:01:15 +08:00
|
|
|
|
// console.error("device:", device)
|
2024-09-26 18:52:14 +08:00
|
|
|
|
this.device = device;
|
2024-09-27 12:01:15 +08:00
|
|
|
|
this.onEventHandler(this.eventType.device, device);
|
2024-09-26 18:52:14 +08:00
|
|
|
|
}).catch(error => {
|
|
|
|
|
commonUtils.toast(error);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 18:47:15 +08:00
|
|
|
|
// 缓存数据
|
|
|
|
|
setStorage(type, data) {
|
|
|
|
|
if (type == 1) {
|
|
|
|
|
uni.setStorageSync(constant.lands, data);
|
|
|
|
|
} else if (type == 2) {
|
|
|
|
|
uni.setStorageSync(constant.landItem, data);
|
2024-09-27 12:01:15 +08:00
|
|
|
|
} else if (type == 3) {
|
|
|
|
|
uni.setStorageSync(constant.device, data);
|
2024-09-25 18:47:15 +08:00
|
|
|
|
} else {
|
|
|
|
|
commonUtils.toast("数据持久化异常");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-26 18:52:14 +08:00
|
|
|
|
|
|
|
|
|
/****************************** 消息回调 ******************************/
|
|
|
|
|
eventType = {
|
|
|
|
|
land: "land",
|
|
|
|
|
device: "device",
|
|
|
|
|
}
|
2024-09-27 12:01:15 +08:00
|
|
|
|
onEventHandler(type, data) {
|
|
|
|
|
this.eventHandler(type, data);
|
2024-09-26 18:52:14 +08:00
|
|
|
|
}
|
2024-09-25 18:47:15 +08:00
|
|
|
|
}
|
2024-09-26 12:42:59 +08:00
|
|
|
|
export default indexUtil
|