151 lines
3.9 KiB
JavaScript
151 lines
3.9 KiB
JavaScript
import store from "@/store"
|
||
import constant from "@/utils/constant.js"
|
||
import * as commonUtils from "@/utils/common.js"
|
||
import {
|
||
deptTreeSelectByRole,
|
||
getManagerGroundList,
|
||
} from "@/api/index.js"
|
||
|
||
class land {
|
||
static isFirst = true //是否第一次加载
|
||
static userId = null;
|
||
static list = [];
|
||
static selectCId = null;
|
||
static selectItem = null;
|
||
|
||
//接收创建来的数据
|
||
constructor() {
|
||
this.isFirst = true;
|
||
this.userId = null;
|
||
this.list = [];
|
||
this.selectCId = null;
|
||
this.selectItem = null;
|
||
}
|
||
|
||
// 获取列表
|
||
getList(userId) {
|
||
console.error("isFirst:",this.isFirst);
|
||
this.userId = userId;
|
||
if (this.isFirst) {
|
||
const lands = uni.getStorageSync(constant.lands);
|
||
if (lands && lands.userId == userId) {
|
||
this.list = lands.data;
|
||
}
|
||
}
|
||
if (this.isFirst) {
|
||
this.isFirst = false;
|
||
this.getNewList(userId);
|
||
}
|
||
console.error("list:",this.list);
|
||
}
|
||
|
||
// 获取最新数据
|
||
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);
|
||
// 持久化到本地缓存中
|
||
this.setStorage(1, {
|
||
userId: this.userId,
|
||
data: this.list,
|
||
});
|
||
this.setStorage(2, this.selectItem);
|
||
} else {
|
||
commonUtils.toast("无数据");
|
||
// 持久化到本地缓存中
|
||
this.setStorage(1, null);
|
||
this.setStorage(2, null);
|
||
}
|
||
} else {
|
||
commonUtils.toast("无数据");
|
||
this.setStorage(1, null);
|
||
this.setStorage(2, null);
|
||
}
|
||
}
|
||
|
||
// 组织数据
|
||
handleData(companys, lands) {
|
||
let id = null;
|
||
let selectCId = 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].isLoad = true;
|
||
companys[index].children[0].isSelect = true;
|
||
selectItem = companys[index].children[0];
|
||
}
|
||
}
|
||
|
||
this.selectCId = selectItem.companyId;
|
||
this.list = companys;
|
||
this.selectItem = selectItem;
|
||
}
|
||
|
||
// 缓存数据
|
||
setStorage(type, data) {
|
||
if (type == 1) {
|
||
uni.setStorageSync(constant.lands, data);
|
||
} else if (type == 2) {
|
||
uni.setStorageSync(constant.landItem, data);
|
||
} else {
|
||
commonUtils.toast("数据持久化异常");
|
||
}
|
||
|
||
}
|
||
}
|
||
export default land |