jsy-app/utils/indexUtil.js
2024-09-27 12:01:15 +08:00

231 lines
6.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import store from "@/store"
import constant from "@/utils/constant.js"
import * as commonUtils from "@/utils/common.js"
import {
deptTreeSelectByRole,
getManagerGroundList,
getDeviceList
} from "@/api/index.js"
class indexUtil {
static isFirst = true //是否第一次加载
static userId = null; //用户id
static list = []; //列表
static showIndex = null; //默认显示的面板索引
static selectItem = null; //选中的数据
static isDataChange = false; //数据是否改变
static deviceData = null; //设备数据
//接收创建来的数据
constructor(eventHandler) {
this.isFirst = true;
this.userId = null;
this.list = [];
this.showIndex = null;
this.selectItem = null;
this.eventHandler = eventHandler;
}
// 获取列表
getList(userId) {
uni.showLoading();
this.userId = userId;
//如果是第一次获取,则先取缓存,然后异步更新数据
if (this.isFirst) {
//地块列表缓存
const lands = uni.getStorageSync(constant.lands);
if (lands && lands.userId == userId) {
this.list = lands.data;
}
//选中地块缓存
this.selectItem = uni.getStorageSync(constant.landItem) || null;
//默认显示索引
this.showIndex = this.findLandIndex(this.list, this.selectItem);
//设备列表
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;
}
}
uni.hideLoading();
if (this.isFirst) {
this.isFirst = false;
this.getNewLandList(userId);
}
}
// 获取最新地块数据
async getNewLandList(userId) {
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) {
this.handleLandData(companys, land.data);
} else {
commonUtils.toast("无数据");
this.list = null;
this.selectItem = null;
}
} else {
commonUtils.toast("无数据");
this.list = null;
this.selectItem = null;
}
// 持久化到本地缓存中
if (this.list) {
this.setStorage(1, {
userId: this.userId,
data: this.list,
});
} else {
this.setStorage(1, null);
}
this.setStorage(2, this.selectItem);
this.getNewDeviceList();
}
// 组织地块数据
handleLandData(companys, lands) {
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, //启用批量控制
mapContent: null
}
// 获取地图信息
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;
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;
}
//获取最新设备数据
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;
}
// console.error("device:", device)
this.device = device;
this.onEventHandler(this.eventType.device, device);
}).catch(error => {
commonUtils.toast(error);
})
}
// 缓存数据
setStorage(type, data) {
if (type == 1) {
uni.setStorageSync(constant.lands, data);
} else if (type == 2) {
uni.setStorageSync(constant.landItem, data);
} else if (type == 3) {
uni.setStorageSync(constant.device, data);
} else {
commonUtils.toast("数据持久化异常");
}
}
/****************************** 消息回调 ******************************/
eventType = {
land: "land",
device: "device",
}
onEventHandler(type, data) {
this.eventHandler(type, data);
}
}
export default indexUtil