jsy-app/App.vue
2025-01-07 10:02:41 +08:00

186 lines
4.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script>
import indexUtil from "@/utils/indexUtil/index.js"
import {
deviceControl,
dcMsgType,
dcEventType
} from "@/utils/indexUtil/dc.js"
import mqttUtil from "@/utils/mqttUtil.js"
const mqttUrl = import.meta.env.VITE_BASE_MQTT;
export default {
data() {
return {
ijs: new indexUtil(), //全局地块列表管理
dc: new deviceControl(2, this.dcEventHandler), // 设备控制类
mqtt: new mqttUtil(mqttUrl, this.mqttStateHandler, this.mqttMsgHandler), //Mqtt
isFirseLink: true, //Mqtt第一次链接
mqttFun: {},
landFun: {},
}
},
globalData: {},
onLaunch: function() {
console.log('App Launch')
// #ifdef APP-PLUS
plus.screen.lockOrientation("portrait-primary")
// #endif
},
onShow: function() {
console.log('App Show')
const that = this;
uni.$off("notify-update-land");
uni.$on("notify-update-land", (data) => {
// console.error("index监听地块更新", data)
// uni.$emit("index-showLoading", true);
uni.showLoading();
that.getWoList(data, null);
});
},
onHide: function() {
console.log('App Hide')
},
methods: {
// 添加一个消息回调
on(type, key, callBack) {
switch (type) {
case "land": //地块消息回调
this.landFun[key] = callBack;
break;
case "mqtt": //mqtt消息回调
this.mqttFun[key] = callBack;
break;
}
},
// 删除一个消息回调
off(type, key) {
switch (type) {
case "land": //地块消息回调
delete this.landFun[key]
break;
case "mqtt": //mqtt消息回调
delete this.mqttFun[key]
break;
}
},
// 给某个回调函数发送数据
sendMsgEvent(type, data) {
let func = {};
switch (type) {
case "land": //地块消息回调
func = this.landFun;
break;
case "mqtt": //mqtt消息回调
func = this.mqttFun;
break;
}
for (let _key in func) {
if (typeof func[_key] === 'function') {
func[_key](data);
} else {
delete func[_key]
}
}
},
getWoList(land, wo) {
const that = this;
if (land && wo) {
that.dc.setData(land, wo);
that.mqttSubscribes();
that.sendMsgEvent("land", wo);
}
that.ijs.getWoList(land).then(res => {
that.dc.setData(land, res.data);
that.mqttSubscribes();
that.sendMsgEvent("land", res.data);
uni.hideLoading();
}).catch(() => {
uni.hideLoading();
});
},
dcEventHandler(type, params, item) {
switch (type) {
case dcEventType.interface: // 界面操作
uni.$emit("open-angle-slider", params)
break;
case dcEventType.refresh: // 命令刷新设备
this.ijs.sendDeviceRefresh(item, params).then(res => {}).catch(error => {
this.dc.clearRefreshCountdown(params.deviceCode);
});
break;
case dcEventType.refreshApi: // 接口刷新阀门
break;
case dcEventType.send: // 命令控制设备
this.ijs.sendDeviceControl(params);
break;
default:
// 消息通知
if (params.success == dcMsgType.ok) {
this.$modal.msgSuccess(params.message);
} else if (params.success == dcMsgType.err) {
this.$modal.msgError(params.message);
} else {
this.$modal.msgWarning(params.message);
}
break;
}
},
//Mqtt连接
mqttLink(userId) {
this.mqtt.link(userId);
},
//Mqtt订阅
mqttSubscribes(topics) {
if (topics) {
this.mqtt.subscribes(topics);
} else {
const topic = this.dc.topic;
if (topic.newV.length) {
this.mqtt.subscribes(topic.newV);
}
if (topic.oldV.length) {
this.mqtt.subscribes(topic.oldV);
}
}
},
//Mqtt取消订阅
mqttUnsubscribes(topics) {
this.mqtt.unsubscribes(topics);
},
//Mqtt断开连接
mqttEnd() {
this.mqtt.over();
},
//Mqtt状态回调
mqttStateHandler(e) {
this.sendMsgEvent("mqtt", {
type: "state",
connected: e,
});
if (this.isFirseLink) {
this.mqttSubscribes();
}
},
//Mqtt消息回调
mqttMsgHandler(topic, data) {
console.error("mqttMsgHandler:", {
topic: topic,
data: JSON.parse(data),
});
this.dc.handleMessage(topic, JSON.parse(data));
this.sendMsgEvent("mqtt", {
type: "msg",
topic: topic,
data: JSON.parse(data),
});
},
}
}
</script>
<style lang="scss">
@import '@/uni_modules/uview-plus/index.scss';
@import '@/static/scss/index.scss';
@import '@/static/alifont/iconfont.css';
@import '@/static/scss/app.css';
</style>