浏览代码

1、修复设备详情mqtt不订阅bug
2、本地存储名称更改

lixing 11 月之前
父节点
当前提交
511677faa7

+ 0 - 2
.eslintrc.js

@@ -30,14 +30,12 @@
     "isReactive": true,
     "isReadonly": true,
     "isRef": true,
-    "isTrue": true,
     "mapActions": true,
     "mapGetters": true,
     "mapState": true,
     "mapStores": true,
     "mapWritableState": true,
     "markRaw": true,
-    "mqttMessageCallback": true,
     "nextTick": true,
     "onActivated": true,
     "onBeforeMount": true,

+ 0 - 3
src/layout/header.vue

@@ -49,13 +49,10 @@
   import { onMounted } from 'vue'
   import { useSidebarStore } from '../store/sidebar'
   import { useRouter } from 'vue-router'
-  import imgurl from '../assets/img/img.jpg'
   import { ElMessage } from 'element-plus'
   import { fetchCurrentUserInfo } from '@/api/profile'
-  import { signOut } from '@/api/login'
   import { getUserNotifications } from '@/api'
 
-  const username: string | null = localStorage.getItem('ms_username')
   let message = ref(0)
 
   const sidebar = useSidebarStore()

+ 31 - 5
src/utils/hooks.ts

@@ -3,7 +3,6 @@ import router from '@/router'
 import { useSidebarStore } from '@/store/sidebar'
 import { fetchDeviceInfo } from '@/api/deviceAuthorize'
 import '@/utils/mqtt/dist/mqtt.min'
-import { useUserInfo } from '@/store/user'
 interface IModal {
   // 是否显示,隐藏对话框
   open: boolean
@@ -101,15 +100,42 @@ declare const window: any
 export var _mqttMessageCallback: any
 
 
-export const mqttMessageCallback = (callback: any) => {
+/**
+ * mqtt订阅
+ * @param topics 主题['a','b']
+ * @param callback 消息回调
+ */
+export const onSubscribe = (topics: string[], callback: any) => {
+  console.log(topics)
   _mqttMessageCallback = callback
+  if (window.client == null) {
+    useMqtt()
+  }
+  topics.forEach(topic => {
+    window.client?.subscribe(topic, () => { })
+  })
+  window.client?.on('message', (topic: string, payload: any) => {
+    if (_mqttMessageCallback != null) {
+      _mqttMessageCallback(topic, payload)
+    }
+  })
+}
+
+/**
+ * mqtt取消订阅
+ * @param topic 主题
+ */
+export const onUnsubscribe = (topic: string) => {
+  window.client?.unsubscribe(topic, () => {
+    _mqttMessageCallback = null
+  })
 }
 
 /**
  * 实时数据
  */
 export const useMqtt = () => {
-  const userInfo = JSON.parse(localStorage.getItem('user') as string)
+  const userInfo = JSON.parse(localStorage.getItem('yz-user') as string)
   console.log(userInfo)
   const { userId, securityCode } = userInfo
   const { mqtt } = window as any
@@ -162,13 +188,13 @@ export const useMqtt = () => {
 
   window.onSubscribe = (topic: string) => {
     window.client?.subscribe(topic, function () {
-      // console.log('订阅成功' + topic)
+      console.log('订阅成功' + topic)
     })
   }
 
   window.onUnsubscribe = (topic: string) => {
     window.client?.unsubscribe(topic, function () {
-      // console.log('取消订阅成功' + topic)
+      console.log('取消订阅成功' + topic)
       _mqttMessageCallback = null
     })
   }

+ 0 - 91
src/utils/tools.ts

@@ -1,94 +1,3 @@
-import '../../public/mqtt/dist/mqtt.min'
-import { MqttClient } from './mqtt/types'
-declare const window: any
-var _mqttMessageCallback: any = null
-
-/**
- * mqtt订阅
- * @param topics 主题['a','b']
- * @param callback 消息回调
- */
-export const onSubscribe = (topics: string[], callback: any) => {
-  console.log(topics)
-  _mqttMessageCallback = callback
-  if (window.client == null) {
-    useMqtt()
-  }
-  topics.forEach(topic => {
-    window.client?.subscribe(topic, () => { })
-  })
-  window.client?.on('message', (topic: string, payload: any) => {
-    if (_mqttMessageCallback != null) {
-      _mqttMessageCallback(topic, payload)
-    }
-  })
-}
-
-/**
- * mqtt取消订阅
- * @param topic 主题
- */
-export const onUnsubscribe = (topic: string) => {
-  window.client?.unsubscribe(topic, () => {
-    _mqttMessageCallback = null
-  })
-}
-
-/**
- * 实时数据
- */
-export const useMqtt = () => {
-  const userInfo = JSON.parse(localStorage.getItem('user') as string)
-  const { userId, securityCode } = userInfo
-  const { mqtt } = window as any
-  const mqttOptions = {
-    clean: true,
-    connectTimeout: 10000,
-    port: 443,
-    clientId: `2${Date.now()}`, //提取到配置文件
-    username: `${userId}`, //提取到配置文件
-    password: securityCode //提取到配置文件
-  }
-  console.log(mqttOptions)
-  if (window.client == null) {
-    console.log('初始化mqtt')
-    window.client = mqtt.connect('wss://mqtt.hldcloud.cn/mqtt', mqttOptions)
-  }
-  window.client.on('connect', () => {
-    console.log('mqtt连接成功')
-  })
-
-  window.client.on('reconnect', () => {
-    console.log('mqtt正在重连')
-  })
-
-  window.client.on('disconnect', () => {
-    console.log('mqtt断开连接disconnect')
-  })
-
-  window.client.on('close', () => {
-    console.log('mqtt断开连接close')
-  })
-
-  window.client.on('error', (e: any) => {
-    console.log('mqtt无法连接error' + e.message)
-    if (e.code == 4) {
-      //用户名密码错误
-      window.client.end()
-      window.client = null
-      alert("该账号在多个设备上登录,为避免部分功能无法使用,请退出账号重新登录")
-    }
-  })
-
-}
-
-export function isTrue(e: any) {
-  if (e === true || e === 'true') {
-    return true
-  }
-  return false
-}
-
 /**
  * 清除对象中的字符串前后空格
  * @param target 目标对象

+ 1 - 1
src/views/big-screen/single.vue

@@ -360,7 +360,7 @@
 <script setup lang="ts">
   import EZUIKit from 'ezuikit-js'
   import {getRealtime, getAlertRecordsByDevice,getDeviceMonitors} from '@/api/bigScreen';
-  import { onSubscribe, onUnsubscribe, formatGears, formatValue, parseGear} from '@/utils/tools';
+  import {formatGears, formatValue, parseGear} from '@/utils/tools';
   import router from '@/router';
   const myVideoRef = ref(null)
   let videoUrl = ref('')

+ 2 - 6
src/views/equipment-center/real-time-data/european-gourd.vue

@@ -115,10 +115,7 @@
 import { getGroupAndVariables } from '@/api'
 import { fetchDeviceInfoAll } from '@/api/deviceAuthorize'
 import { useSidebarStore } from '@/store/sidebar'
-import { mqttMessageCallback } from '@/utils/hooks'
 import { formatGears, formatValue, formatWeightLight } from '@/utils/tools'
-import { def } from '@vue/shared'
-import { buffer } from 'stream/consumers'
 import LiveMessage from './live-message.vue'
 
 /** 警告数组 */
@@ -137,7 +134,7 @@ let dataSource: any = reactive({
 })
 
 onUnmounted(() => {
-  window.onUnsubscribe(`Byte/${equipmentInfo.value.code}`)
+  onUnsubscribe(`Byte/${equipmentInfo.value.code}`)
 })
 
 onMounted(async () => {
@@ -151,8 +148,7 @@ onMounted(async () => {
 const getDeviceInfo = async () => {
   const data = await fetchDeviceInfoAll(deviceId.value)
   equipmentInfo.value = data
-  window.onSubscribe(`Byte/${equipmentInfo.value.code}`)
-  mqttMessageCallback((topic: any, payload: any) => {
+  onSubscribe([`Byte/${equipmentInfo.value.code}`],(topic: any, payload: any) => {
     parseData(payload)
   })
 }

+ 4 - 6
src/views/equipment-center/real-time-data/index.vue

@@ -106,7 +106,6 @@
 import { getGroupAndVariables } from '@/api'
 import { fetchDeviceInfoAll } from '@/api/deviceAuthorize'
 import { useSidebarStore } from '@/store/sidebar'
-import { mqttMessageCallback } from '@/utils/hooks'
 import { formatGears, formatWeightLight, formatValue } from '@/utils/tools'
 import LiveMessage from './live-message.vue'
 let deviceId: any = ref('')
@@ -114,7 +113,7 @@ let equipmentInfo: any = ref({})
 const sidebar = useSidebarStore()
 
 onUnmounted(() => {
-  window.onUnsubscribe(`Json/${equipmentInfo?.value?.code}`)
+  onUnsubscribe(`Json/${equipmentInfo?.value?.code}`)
 })
 /** 警告数组 */
 const warningList: any = reactive({ array: [] })
@@ -150,15 +149,14 @@ const dataSource = useRequest(() => getGroupAndVariables({ deviceId: deviceId.va
 const getDeviceInfo = async (id: string) => {
   const data = await fetchDeviceInfoAll(id)
   equipmentInfo.value = data
-  window.onSubscribe?.(`Json/${equipmentInfo.value.code}`)
-  mqttMessageCallback((topic: any, payload: any) => {
+ 
+  onSubscribe([`Json/${equipmentInfo.value.code}`], (topic: string, payload: any) => {
     parseData(payload)
   })
-  
 }
 
 const parseData = (payload: any) => {
-  
+
   const jsonArray = JSON.parse(payload).Items
   console.log(jsonArray)
   for (var i in jsonArray) {

+ 1 - 1
src/views/login/index.vue

@@ -191,7 +191,7 @@
     localStorage.clear()
     const data: any = await userLogin({ ...param, tenancyName: e, type: 2 })
     localStorage.setItem('yz-Token', data.accessToken)
-    localStorage.setItem('user', JSON.stringify(data))
+    localStorage.setItem('yz-user', JSON.stringify(data))
     userInfo.setUser(data)
 
     ElMessage.success('登录成功')