jsy-app/utils/indexUtil/index.js

231 lines
5.3 KiB
JavaScript
Raw Normal View History

2024-11-27 18:46:21 +08:00
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 {
2025-01-16 15:36:52 +08:00
getLandAndDevice
2024-11-27 18:46:21 +08:00
} from "@/api/system/land.js"
import {
getList,
stationRefresh,
valveRefresh,
valveControl,
2025-01-16 15:36:52 +08:00
refreshAllValve
2024-11-27 18:46:21 +08:00
} from "@/api/controlInterface.js"
2024-11-29 15:09:33 +08:00
const cacheKey = "cacheData";
2024-11-27 18:46:21 +08:00
class indexUtil {
static companys = null; //公司选择的列表
2025-01-17 11:31:15 +08:00
static companyLands = {}; //地块的数据列表
2024-11-27 18:46:21 +08:00
static land = null; //选中的数据
2024-11-29 15:09:33 +08:00
static wo = null; //地块的数据列表
static cacheData = {
userId: null,
companys: null,
land: null,
wo: null,
}
2024-11-27 18:46:21 +08:00
//接收创建来的数据
constructor(eventHandler) {
this.eventHandler = eventHandler;
2025-01-17 11:31:15 +08:00
this.companys = null; //公司选择的列表
this.companyLands = {}; //地块的数据列表
this.land = null; //选中的数据
this.wo = null; //地块的数据列表
this.cacheData = {
userId: null,
companys: null,
land: null,
wo: null,
}
2024-11-27 18:46:21 +08:00
}
/**
* 获取数据的规则
* 如果是第一次打开则先获取缓存如果有缓存则先使用缓存然后异步更新数据如果没有缓存则只能查询数据
*/
2024-11-29 15:09:33 +08:00
// 获取缓存的数据
initData(userId, callback) {
2024-11-27 18:46:21 +08:00
const that = this;
2024-11-29 15:09:33 +08:00
//缓存
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,
2024-11-27 18:46:21 +08:00
}
}
2024-11-29 15:09:33 +08:00
callback(that.land, that.wo);
that.getNewCompany();
2024-11-27 18:46:21 +08:00
}
2024-11-29 15:09:33 +08:00
2024-11-27 18:46:21 +08:00
// 获取最新地块数据
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;
2024-11-29 15:09:33 +08:00
that.setStorage(constant.companys, JSON.parse(JSON.stringify(response.data)));
2024-11-27 18:46:21 +08:00
} else {
commonUtils.toast("无数据");
that.companys = [];
that.selectItem = null;
uni.removeStorage(constant.companys);
}
});
}
// 获取地块信息
getLand(company) {
const that = this;
2025-01-17 11:31:15 +08:00
const keyName = company.companyId;
2024-11-27 18:46:21 +08:00
return new Promise((resolve, reject) => {
2025-01-17 11:31:15 +08:00
if (that.companyLands.hasOwnProperty(keyName)) {
resolve(that.companyLands[keyName])
} else {
const query = {
companyGroupId: company.companyId,
companyId: company.rootCompanyId,
status: 0,
delFlag: 0,
2025-02-08 09:50:02 +08:00
pageNum: 1,
pageSize: 999,
2024-11-27 18:46:21 +08:00
}
2025-01-17 11:31:15 +08:00
getLandAndDevice(query).then(res => {
if (res.rows && res.rows.length) {
let companys = that.cacheData.companys || [];
that.findObjectById(companys, company.companyId, res.rows);
that.setStorage(constant.companys, companys);
that.companyLands[keyName] = res.rows;
}
resolve(res.rows || [])
}).catch(error => {
reject(error)
})
}
2024-11-27 18:46:21 +08:00
})
}
// 获取出水口信息
getWoList(land) {
const that = this;
that.land = land;
2024-11-29 15:09:33 +08:00
that.setStorage(constant.land, land);
2024-11-27 18:46:21 +08:00
return new Promise((resolve, reject) => {
getList(land.id).then((res) => {
2024-11-29 15:09:33 +08:00
that.wo = res.data;
that.setStorage(constant.wo, res.data);
2024-11-27 18:46:21 +08:00
resolve(res)
}).catch(error => {
reject(error)
})
})
}
// 发送刷新命令
sendDeviceRefresh(item, params) {
2024-12-03 12:38:13 +08:00
return new Promise((resolve, reject) => {
if (["valve", "fiveValve", "butterflyValve"].includes(item.deviceTypeKey)) {
valveRefresh(params).then(res => {
resolve(res)
}).catch(error => {
reject(error)
})
} else if (item.deviceTypeId == 1) {
stationRefresh(params).then(res => {
resolve(res)
}).catch(error => {
reject(error)
})
}
})
2024-11-27 18:46:21 +08:00
}
// 发送控制命令
sendDeviceControl(params) {
if (!params) {
commonUtils.toast("操作失败!");
return;
}
valveControl(params).finally(() => {})
}
2025-01-16 15:36:52 +08:00
// 一键刷新
static timeOut = 60 * 1000; //间隔时间
refreshAll() {
const landId = this.land?.id;
return new Promise((resolve, reject) => {
if (landId) {
refreshAllValve(landId).then(res => {
resolve(res)
}).catch(error => {
reject(error)
})
} else {
reject("请选择地块")
}
})
}
2024-11-27 18:46:21 +08:00
// 缓存数据
setStorage(type, data) {
2024-11-29 15:09:33 +08:00
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);
2024-11-27 18:46:21 +08:00
}
// 查找数据源,并赋值
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