186 lines
4.2 KiB
JavaScript
186 lines
4.2 KiB
JavaScript
import store from "@/store"
|
|
import constant from "@/utils/constant.js"
|
|
import * as commonUtils from "@/utils/common.js"
|
|
|
|
|
|
import {
|
|
companyTreeSelect
|
|
} from "@/api/system/company.js"
|
|
import {
|
|
getLandList
|
|
} from "@/api/system/land.js"
|
|
|
|
import {
|
|
getList,
|
|
stationRefresh,
|
|
valveRefresh,
|
|
valveControl,
|
|
} from "@/api/controlInterface.js"
|
|
|
|
const cacheKey = "cacheData";
|
|
class indexUtil {
|
|
static companys = null; //公司选择的列表
|
|
static land = null; //选中的数据
|
|
static wo = null; //地块的数据列表
|
|
static cacheData = {
|
|
userId: null,
|
|
companys: null,
|
|
land: null,
|
|
wo: null,
|
|
}
|
|
|
|
//接收创建来的数据
|
|
constructor(eventHandler) {
|
|
this.eventHandler = eventHandler;
|
|
}
|
|
|
|
/**
|
|
* 获取数据的规则
|
|
* 如果是第一次打开,则先获取缓存,如果有缓存,则先使用缓存,然后异步更新数据;如果没有缓存,则只能查询数据;
|
|
*/
|
|
|
|
// 获取缓存的数据
|
|
initData(userId, callback) {
|
|
const that = this;
|
|
//缓存
|
|
const cacheData = uni.getStorageSync(cacheKey);
|
|
if (cacheData && cacheData?.userId == userId) {
|
|
that.cacheData = {
|
|
userId: userId,
|
|
companys: cacheData.companys,
|
|
land: cacheData.land,
|
|
wo: cacheData.wo,
|
|
}
|
|
that.companys = cacheData.companys;
|
|
that.land = cacheData.land;
|
|
that.wo = cacheData.wo;
|
|
} else {
|
|
that.cacheData = {
|
|
userId: userId,
|
|
companys: null,
|
|
land: null,
|
|
wo: null,
|
|
}
|
|
}
|
|
callback(that.land, that.wo);
|
|
that.getNewCompany();
|
|
}
|
|
|
|
// 获取最新地块数据
|
|
async getNewCompany() {
|
|
const obj = {
|
|
status: 0,
|
|
delFlag: "0",
|
|
companyTypes: [1, 2, 3],
|
|
}
|
|
const that = this;
|
|
companyTreeSelect(obj).then(response => {
|
|
if (response.data.length) {
|
|
that.companys = response.data;
|
|
that.setStorage(constant.companys, JSON.parse(JSON.stringify(response.data)));
|
|
} else {
|
|
commonUtils.toast("无数据");
|
|
that.companys = [];
|
|
that.selectItem = null;
|
|
uni.removeStorage(constant.companys);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 获取地块信息
|
|
getLand(company) {
|
|
const that = this;
|
|
return new Promise((resolve, reject) => {
|
|
const query = {
|
|
companyGroupId: company.rootCompanyId,
|
|
companyId: company.companyId,
|
|
status: 0,
|
|
delFlag: 0,
|
|
}
|
|
getLandList(query).then(res => {
|
|
if (res.data && res.data.length) {
|
|
let companys = that.cacheData.companys || [];
|
|
that.findObjectById(companys, company.companyId, res.data);
|
|
that.setStorage(constant.companys, companys);
|
|
}
|
|
resolve(res)
|
|
}).catch(error => {
|
|
reject(error)
|
|
})
|
|
})
|
|
}
|
|
|
|
// 获取出水口信息
|
|
getWoList(land) {
|
|
const that = this;
|
|
that.land = land;
|
|
that.setStorage(constant.land, land);
|
|
return new Promise((resolve, reject) => {
|
|
getList(land.id).then((res) => {
|
|
that.wo = res.data;
|
|
that.setStorage(constant.wo, res.data);
|
|
resolve(res)
|
|
}).catch(error => {
|
|
reject(error)
|
|
})
|
|
})
|
|
}
|
|
|
|
// 发送刷新命令
|
|
sendDeviceRefresh(item, params) {
|
|
if (["valve", "fiveValve", "butterflyValve"].includes(item.deviceTypeKey)) {
|
|
valveRefresh(params).finally(() => {})
|
|
} else if (item.deviceTypeId == 1) {
|
|
stationRefresh(params).finally(() => {})
|
|
}
|
|
}
|
|
|
|
// 发送控制命令
|
|
sendDeviceControl(params) {
|
|
if (!params) {
|
|
commonUtils.toast("操作失败!");
|
|
return;
|
|
}
|
|
valveControl(params).finally(() => {})
|
|
}
|
|
|
|
// 缓存数据
|
|
setStorage(type, data) {
|
|
switch (type) {
|
|
case constant.companys:
|
|
this.cacheData.companys = data;
|
|
break
|
|
case constant.land:
|
|
this.cacheData.land = data;
|
|
break
|
|
case constant.wo:
|
|
this.cacheData.wo = data;
|
|
break
|
|
}
|
|
uni.setStorageSync(cacheKey, this.cacheData);
|
|
}
|
|
|
|
// 查找数据源,并赋值
|
|
findObjectById(array, targetId, lands) {
|
|
const that = this;
|
|
for (let i = 0; i < array.length; i++) {
|
|
const item = array[i];
|
|
if (item.id === targetId) {
|
|
// 找到目标节点,在其 children 中插入新数据
|
|
// item.children.push(newData);
|
|
item.lands = lands;
|
|
return true; // 插入成功
|
|
}
|
|
if (item.children && item.children.length > 0) {
|
|
// 递归查找子节点
|
|
const result = that.findObjectById(item.children, targetId, lands);
|
|
if (result) {
|
|
return true; // 插入成功
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
export default indexUtil |