From cec3669a0bea14c0ca98acc44aa40f1d006418b7 Mon Sep 17 00:00:00 2001 From: lxz Date: Thu, 4 Jan 2024 19:29:00 +0800 Subject: [PATCH] scoket --- pages/index/massif.vue | 63 +++++++----------------------------------- store/index.js | 12 +++++++- 2 files changed, 21 insertions(+), 54 deletions(-) diff --git a/pages/index/massif.vue b/pages/index/massif.vue index 47847a0..f115042 100644 --- a/pages/index/massif.vue +++ b/pages/index/massif.vue @@ -383,9 +383,7 @@ ]) onLoad(() => { - uni.hideTabBar() - // console.log(store.state.userInfo) if (!store.state.userInfo) { uni.redirectTo({ url: '/pages/Login/login' @@ -428,17 +426,13 @@ // 环境基准 let threshold = [] const thresholdFn = (item, min, max) => { - - if (item.num < min) { item.status = (item.num - min).toFixed(1) } if (item.num > max) { item.status = (item.num - max).toFixed(1) } - } - // 环境监测数据 const environmentList = reactive([{ icon: "FS", @@ -499,6 +493,7 @@ ]) onShow(() => { + // getlist() getlist1() getlist2() @@ -514,13 +509,14 @@ lindexist({ land_id: tdId.value }).then((res) => { - if (res.code == 1) { userInfo.value = res.data scoketData = { username: userInfo.value.master_phone, device: 'lihai_lot_walnutpi_dev_' + userInfo.value.device_id } + openScoket() + // console.log(scoketData) video_url.value = res.data.video_url threshold = res.data?.monitor?.threshold || "" if (Object.keys(res.data).length > 0) { @@ -552,29 +548,15 @@ environmentList[6].num = res.data.monitor.air_monitor_data.rainfall environmentList[7].num = res.data.monitor.air_monitor_data.light_intensity thresholdFn(environmentList[0], threshold.wind_speed_min, threshold.wind_speed_max) - - - // thresholdFn(environmentList[1], threshold.wind_speed_min, threshold.wind_speed_max) - - - // sdf - thresholdFn(environmentList[2], threshold.air_temp_min, threshold.air_temp_max) thresholdFn(environmentList[3], threshold.air_mois_min, threshold.air_mois_max) thresholdFn(environmentList[4], threshold.air_co2_content_min, threshold .air_co2_content_max) thresholdFn(environmentList[5], threshold.ambient_air_pressure_min, threshold .ambient_air_pressure_max) - - // sdf - - // thresholdFn(environmentList[6], threshold.ambient_air_pressure_min, threshold.ambient_air_pressure_max) // thresholdFn(environmentList[6], threshold.ambient_air_pressure_min, threshold.ambient_air_pressure_max) - - - } } @@ -590,12 +572,6 @@ keyword: '' }).then((res) => { if (res.code == 1) { - - - - - - tdId.value = res.data[0]?.id || '' getlist() if (res.data.length > 0) { @@ -739,12 +715,17 @@ const openScoket = () => { + console.log("openScoket") + if (store.state.onScoket) return + store.dispatch('changeScoket') + // console.log("建立scoket") + // return uni.connectSocket({ - url: 'ws://60.204.152.17:8765' + url: 'ws://iot.lihaink.cn' }); uni.onSocketOpen(function(res) { - + store.dispatch('changeScoket') uni.sendSocketMessage({ data: JSON.stringify(scoketData) }); @@ -757,30 +738,6 @@ console.log('WebSocket 已关闭!'); }); } - - setTimeout(() => { - - - uni.connectSocket({ - url: 'ws://60.204.152.17:8765' - }); - uni.onSocketOpen(function(res) { - - uni.sendSocketMessage({ - data: JSON.stringify(scoketData) - }); - - }); - uni.onSocketError(function(res) { - console.log('WebSocket连接打开失败,请检查!'); - }); - uni.onSocketClose(function(res) { - console.log('WebSocket 已关闭!'); - }); - - }, 5000) - - onPullDownRefresh(() => { getlist1() getlist2() diff --git a/store/index.js b/store/index.js index 0fbc647..ed4df1b 100644 --- a/store/index.js +++ b/store/index.js @@ -4,7 +4,8 @@ import { const store = createStore({ state: { userInfo: uni.getStorageSync("SY_USER") || null, - tabbrIndex: 0 + tabbrIndex: 0, + onScoket: false, }, @@ -15,6 +16,9 @@ const store = createStore({ }, changeTabbar(state, index) { state.tabbrIndex = index + }, + changeScoket(state, index) { + state.onScoket = true } }, actions: { @@ -30,6 +34,12 @@ const store = createStore({ }, info) { commit('changeTabbar', info) + }, + changeScoket({ + commit + }, info) { + commit('changeScoket') + } } })