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" class indexUtil { static userId = null; //用户id static companys = null; //公司选择的列表 static _companys = null; //缓存的数据,便于更新数据 static land = null; //选中的数据 static wodata = null; //地块的数据列表 static isFirst = true //是否第一次加载 static showIndex = 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; } /** * 获取数据的规则 * 如果是第一次打开,则先获取缓存,如果有缓存,则先使用缓存,然后异步更新数据;如果没有缓存,则只能查询数据; */ // 获取公司列表 getCompany(userId) { const that = this; that.userId = userId; if (that.isFirst) { //缓存 const companys = uni.getStorageSync(constant.companys); if (companys && companys?.userId == userId) { that.companys = companys.data; that._companys = JSON.parse(JSON.stringify(companys.data)); } 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._companys = JSON.parse(JSON.stringify(response.data)); that.setStorage(constant.companys, that._companys); } 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) { that.findObjectById(that._companys, company.companyId, res.data); that.setStorage(constant.companys, that._companys); } resolve(res) }).catch(error => { reject(error) }) }) } // 获取出水口信息 getWoList(land) { const that = this; that.land = land; that.setStorage(constant.landItem, land); return new Promise((resolve, reject) => { getList(land.id).then((res) => { that.wodata = 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) { uni.setStorageSync(type, { userId: this.userId, data: data, }); } // 清除换内存 removeStorage(type) { uni.removeStorage(type) } // 查找数据源,并赋值 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; // 插入成功 } } } } /****************************** 消息回调 ******************************/ // eventType = { // land: "land", // wo: "wo", // } // onEventHandler(type, data) { // this.eventHandler(type, data); // } } export default indexUtil