From ee6dc2b3cfe1d35f3da56808fda1d83a04a9bb2d Mon Sep 17 00:00:00 2001 From: Iruka <1017819588@qq.com> Date: Thu, 26 Sep 2024 12:42:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9C=B0=E5=9D=97=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=92=8C=E9=80=89=E6=8B=A9=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 4 +- .../custom-select-land/custom-select-land.vue | 69 +++++++++++-------- pages/index-list.vue | 8 ++- pages/index.vue | 10 ++- utils/{land.js => indexUtil.js} | 66 +++++++++++------- 5 files changed, 98 insertions(+), 59 deletions(-) rename utils/{land.js => indexUtil.js} (75%) diff --git a/App.vue b/App.vue index 8e85087..892d0a4 100644 --- a/App.vue +++ b/App.vue @@ -1,11 +1,11 @@ diff --git a/pages/index.vue b/pages/index.vue index 4c65eeb..4040023 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -382,7 +382,7 @@ - + @@ -399,10 +399,16 @@ landList: [1, 2, 3], value: false, sliderValue: 1, + landDataChange: true, } }, onLoad() { - // uni.$emit("mqtt-link"); + // uni.$emit("mqtt-link"); + uni.$off("notify-updates-land"); + uni.$on("notify-updates-land", () => { + console.error("更新数据") + this.landDataChange = true; + }); }, onShow() { // #ifdef APP-PLUS diff --git a/utils/land.js b/utils/indexUtil.js similarity index 75% rename from utils/land.js rename to utils/indexUtil.js index 4934a3d..f0682dd 100644 --- a/utils/land.js +++ b/utils/indexUtil.js @@ -6,37 +6,40 @@ import { getManagerGroundList, } from "@/api/index.js" -class land { +class indexUtil { static isFirst = true //是否第一次加载 - static userId = null; - static list = []; - static selectCId = null; - static selectItem = null; + static userId = null; //用户id + static list = []; //列表 + static showIndex = null; //默认显示的面板索引 + static selectItem = null; //选中的数据 + static isDataChange = false; //数据是否改变 //接收创建来的数据 constructor() { this.isFirst = true; this.userId = null; this.list = []; - this.selectCId = null; + this.showIndex = null; this.selectItem = null; } // 获取列表 getList(userId) { - console.error("isFirst:",this.isFirst); + uni.showLoading(); this.userId = userId; if (this.isFirst) { const lands = uni.getStorageSync(constant.lands); if (lands && lands.userId == userId) { this.list = lands.data; } + this.selectItem = uni.getStorageSync(constant.landItem) || null; + this.showIndex = this.findLandIndex(this.list, this.selectItem); } + uni.hideLoading(); if (this.isFirst) { this.isFirst = false; this.getNewList(userId); } - console.error("list:",this.list); } // 获取最新数据 @@ -56,29 +59,31 @@ class land { // 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); + this.list = null; + this.selectItem = null; } } else { commonUtils.toast("无数据"); - this.setStorage(1, null); - this.setStorage(2, null); + this.list = null; + this.selectItem = null; } + // 持久化到本地缓存中 + if (this.list) { + this.setStorage(1, { + userId: this.userId, + data: this.list, + }); + } else { + this.setStorage(1, null); + } + this.setStorage(2, this.selectItem); } // 组织数据 handleData(companys, lands) { let id = null; - let selectCId = null; let selectItem = null; const landItem = uni.getStorageSync(constant.landItem); if (landItem) { @@ -125,15 +130,27 @@ class land { 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; + this.showIndex = this.findLandIndex(this.list, this.selectItem); + } + + //寻找默认显示的行 + findLandIndex(list, item) { + let showIndex = null + if (list.length && item) { + for (var i = 0; i < list.length; i++) { + if (list[i].id == item.companyId) { + showIndex = i; + break; + } + } + } + return showIndex; } // 缓存数据 @@ -145,7 +162,6 @@ class land { } else { commonUtils.toast("数据持久化异常"); } - } } -export default land \ No newline at end of file +export default indexUtil \ No newline at end of file