|
@@ -3,7 +3,6 @@ import router from '@/router'
|
|
import { useSidebarStore } from '@/store/sidebar'
|
|
import { useSidebarStore } from '@/store/sidebar'
|
|
import { fetchDeviceInfo } from '@/api/deviceAuthorize'
|
|
import { fetchDeviceInfo } from '@/api/deviceAuthorize'
|
|
import '@/utils/mqtt/dist/mqtt.min'
|
|
import '@/utils/mqtt/dist/mqtt.min'
|
|
-import { useUserInfo } from '@/store/user'
|
|
|
|
interface IModal {
|
|
interface IModal {
|
|
// 是否显示,隐藏对话框
|
|
// 是否显示,隐藏对话框
|
|
open: boolean
|
|
open: boolean
|
|
@@ -101,15 +100,42 @@ declare const window: any
|
|
export var _mqttMessageCallback: 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
|
|
_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 = () => {
|
|
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)
|
|
console.log(userInfo)
|
|
const { userId, securityCode } = userInfo
|
|
const { userId, securityCode } = userInfo
|
|
const { mqtt } = window as any
|
|
const { mqtt } = window as any
|
|
@@ -162,13 +188,13 @@ export const useMqtt = () => {
|
|
|
|
|
|
window.onSubscribe = (topic: string) => {
|
|
window.onSubscribe = (topic: string) => {
|
|
window.client?.subscribe(topic, function () {
|
|
window.client?.subscribe(topic, function () {
|
|
- // console.log('订阅成功' + topic)
|
|
|
|
|
|
+ console.log('订阅成功' + topic)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
window.onUnsubscribe = (topic: string) => {
|
|
window.onUnsubscribe = (topic: string) => {
|
|
window.client?.unsubscribe(topic, function () {
|
|
window.client?.unsubscribe(topic, function () {
|
|
- // console.log('取消订阅成功' + topic)
|
|
|
|
|
|
+ console.log('取消订阅成功' + topic)
|
|
_mqttMessageCallback = null
|
|
_mqttMessageCallback = null
|
|
})
|
|
})
|
|
}
|
|
}
|