jsy-app/utils/indexUtil.js

167 lines
4.3 KiB
JavaScript
Raw Normal View History

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,
} 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-25 18:47:15 +08:00
//接收创建来的数据
constructor() {
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;
}
// 获取列表
getList(userId) {
2024-09-26 12:42:59 +08:00
uni.showLoading();
2024-09-25 18:47:15 +08:00
this.userId = userId;
if (this.isFirst) {
const lands = uni.getStorageSync(constant.lands);
if (lands && lands.userId == userId) {
this.list = lands.data;
}
2024-09-26 12:42:59 +08:00
this.selectItem = uni.getStorageSync(constant.landItem) || null;
this.showIndex = this.findLandIndex(this.list, this.selectItem);
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;
this.getNewList(userId);
}
}
// 获取最新数据
async getNewList(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.handleData(companys, land.data);
} 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-25 18:47:15 +08:00
}
// 组织数据
handleData(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,
isSelect: false
}
// 获取地图信息
if (item.landMap != null) {
param.mapContent = JSON.parse(decodeURIComponent(item.landMap.mapContent)) //地块地图 地图标点
}
// 判断是否当前选中的数据
if (param.id == id) {
param.isSelect = true;
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) {
companys[index].children[0].isSelect = true;
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
}
// 缓存数据
setStorage(type, data) {
if (type == 1) {
uni.setStorageSync(constant.lands, data);
} else if (type == 2) {
uni.setStorageSync(constant.landItem, data);
} else {
commonUtils.toast("数据持久化异常");
}
}
}
2024-09-26 12:42:59 +08:00
export default indexUtil