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 { getLandAndDevice } from "@/api/system/land.js" import { getList, stationRefresh, valveRefresh, valveControl, refreshAllValve, refreshControllingValve } from "@/api/controlInterface.js" const cacheKey = "cacheData"; class indexUtil { static companys = null; //公司选择的列表 static companyLands = {}; //地块的数据列表 static land = null; //选中的数据 static wo = null; //地块的数据列表 static cacheData = { userId: null, companys: null, land: null, wo: null, } //接收创建来的数据 constructor(eventHandler) { this.eventHandler = eventHandler; this.companys = null; //公司选择的列表 this.companyLands = {}; //地块的数据列表 this.land = null; //选中的数据 this.wo = null; //地块的数据列表 this.cacheData = { userId: null, companys: null, land: null, wo: null, } } /** * 获取数据的规则 * 如果是第一次打开,则先获取缓存,如果有缓存,则先使用缓存,然后异步更新数据;如果没有缓存,则只能查询数据; */ // 获取缓存的数据 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; const keyName = company.companyId; return new Promise((resolve, reject) => { if (that.companyLands.hasOwnProperty(keyName)) { resolve(that.companyLands[keyName]) } else { const query = { companyGroupId: company.companyId, companyId: company.rootCompanyId, status: 0, delFlag: 0, pageNum: 1, pageSize: 999, } 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) }) } }) } // 获取出水口信息 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) { 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) }) } }) } // 发送控制命令 sendDeviceControl(params) { return new Promise((resolve, reject) => { if (params) { valveControl(params).then(res => { resolve(res) }).catch(error => { reject(error) }) } else { commonUtils.toast("操作失败!"); reject("操作失败!") } }) } // 一键刷新 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("请选择地块") } }) } // 刷新控制中阀门 refreshControlling() { const landId = this.land?.id; return new Promise((resolve, reject) => { if (landId) { refreshControllingValve(landId).then(res => { resolve(res) }).catch(error => { reject(error) }) } else { reject("请选择地块") } }) } // 缓存数据 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