2024-09-18 10:30:10 +08:00
|
|
|
|
<script>
|
2025-01-20 09:56:44 +08:00
|
|
|
|
import '@/static/font/iconfont.js'
|
2024-11-27 18:46:21 +08:00
|
|
|
|
import indexUtil from "@/utils/indexUtil/index.js"
|
|
|
|
|
import {
|
|
|
|
|
deviceControl,
|
|
|
|
|
dcMsgType,
|
|
|
|
|
dcEventType
|
|
|
|
|
} from "@/utils/indexUtil/dc.js"
|
2024-09-18 10:30:10 +08:00
|
|
|
|
import mqttUtil from "@/utils/mqttUtil.js"
|
2024-12-24 11:58:40 +08:00
|
|
|
|
const mqttUrl = import.meta.env.VITE_BASE_MQTT;
|
2024-09-18 10:30:10 +08:00
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
2024-11-27 18:46:21 +08:00
|
|
|
|
ijs: new indexUtil(), //全局地块列表管理
|
2024-12-03 17:58:07 +08:00
|
|
|
|
dc: new deviceControl(2, this.dcEventHandler), // 设备控制类
|
2024-12-24 11:58:40 +08:00
|
|
|
|
mqtt: new mqttUtil(mqttUrl, this.mqttStateHandler, this.mqttMsgHandler), //Mqtt
|
2024-12-19 18:38:26 +08:00
|
|
|
|
isFirseLink: true, //Mqtt第一次链接
|
2024-12-19 17:41:53 +08:00
|
|
|
|
mqttFun: {},
|
|
|
|
|
landFun: {},
|
2024-09-18 10:30:10 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
globalData: {},
|
|
|
|
|
onLaunch: function() {
|
|
|
|
|
console.log('App Launch')
|
2024-12-25 17:36:26 +08:00
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
|
plus.screen.lockOrientation("portrait-primary")
|
|
|
|
|
// #endif
|
2024-09-18 10:30:10 +08:00
|
|
|
|
},
|
|
|
|
|
onShow: function() {
|
|
|
|
|
console.log('App Show')
|
2024-11-29 15:09:33 +08:00
|
|
|
|
const that = this;
|
2025-01-17 11:31:15 +08:00
|
|
|
|
//监听公司点击
|
|
|
|
|
uni.$off("notify-add-land");
|
|
|
|
|
uni.$on("notify-add-land", (data) => {
|
|
|
|
|
that.getLand(data);
|
|
|
|
|
});
|
|
|
|
|
//监听地块选择
|
2024-11-27 18:46:21 +08:00
|
|
|
|
uni.$off("notify-update-land");
|
|
|
|
|
uni.$on("notify-update-land", (data) => {
|
2024-11-29 15:09:33 +08:00
|
|
|
|
// console.error("index监听地块更新:", data)
|
2024-11-27 18:46:21 +08:00
|
|
|
|
// uni.$emit("index-showLoading", true);
|
|
|
|
|
uni.showLoading();
|
2024-11-29 15:09:33 +08:00
|
|
|
|
that.getWoList(data, null);
|
2024-11-27 18:46:21 +08:00
|
|
|
|
});
|
2024-09-18 10:30:10 +08:00
|
|
|
|
},
|
|
|
|
|
onHide: function() {
|
|
|
|
|
console.log('App Hide')
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
2024-12-19 17:41:53 +08:00
|
|
|
|
// 添加一个消息回调
|
2025-01-07 10:02:41 +08:00
|
|
|
|
on(type, key, callBack) {
|
2024-12-19 17:41:53 +08:00
|
|
|
|
switch (type) {
|
|
|
|
|
case "land": //地块消息回调
|
|
|
|
|
this.landFun[key] = callBack;
|
|
|
|
|
break;
|
|
|
|
|
case "mqtt": //mqtt消息回调
|
|
|
|
|
this.mqttFun[key] = callBack;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-01-07 10:02:41 +08:00
|
|
|
|
// 删除一个消息回调
|
|
|
|
|
off(type, key) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case "land": //地块消息回调
|
|
|
|
|
delete this.landFun[key]
|
|
|
|
|
break;
|
|
|
|
|
case "mqtt": //mqtt消息回调
|
|
|
|
|
delete this.mqttFun[key]
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-12-19 17:41:53 +08:00
|
|
|
|
// 给某个回调函数发送数据
|
|
|
|
|
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]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-01-17 11:31:15 +08:00
|
|
|
|
getLand(e) {
|
|
|
|
|
this.ijs.getLand(e.node).then(data => {
|
|
|
|
|
// this.$refs.landRef.addLand(e, data);
|
|
|
|
|
this.sendMsgEvent("land", {
|
|
|
|
|
type: "add",
|
|
|
|
|
data: {
|
|
|
|
|
item: e,
|
|
|
|
|
lands: data
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-11-29 15:09:33 +08:00
|
|
|
|
getWoList(land, wo) {
|
|
|
|
|
const that = this;
|
|
|
|
|
if (land && wo) {
|
|
|
|
|
that.dc.setData(land, wo);
|
2024-12-19 18:38:26 +08:00
|
|
|
|
that.mqttSubscribes();
|
2025-01-17 11:31:15 +08:00
|
|
|
|
that.sendMsgEvent("land", {
|
|
|
|
|
type: "wo",
|
|
|
|
|
data: wo
|
|
|
|
|
});
|
2024-11-29 15:09:33 +08:00
|
|
|
|
}
|
|
|
|
|
that.ijs.getWoList(land).then(res => {
|
|
|
|
|
that.dc.setData(land, res.data);
|
2024-12-19 15:28:23 +08:00
|
|
|
|
that.mqttSubscribes();
|
2025-01-17 11:31:15 +08:00
|
|
|
|
that.sendMsgEvent("land", {
|
|
|
|
|
type: "wo",
|
|
|
|
|
data: res.data
|
|
|
|
|
});
|
2024-11-29 15:09:33 +08:00
|
|
|
|
uni.hideLoading();
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
});
|
|
|
|
|
},
|
2024-11-27 18:46:21 +08:00
|
|
|
|
dcEventHandler(type, params, item) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case dcEventType.interface: // 界面操作
|
|
|
|
|
uni.$emit("open-angle-slider", params)
|
|
|
|
|
break;
|
|
|
|
|
case dcEventType.refresh: // 命令刷新设备
|
2024-12-03 12:38:13 +08:00
|
|
|
|
this.ijs.sendDeviceRefresh(item, params).then(res => {}).catch(error => {
|
|
|
|
|
this.dc.clearRefreshCountdown(params.deviceCode);
|
|
|
|
|
});
|
2024-11-27 18:46:21 +08:00
|
|
|
|
break;
|
|
|
|
|
case dcEventType.refreshApi: // 接口刷新阀门
|
|
|
|
|
break;
|
|
|
|
|
case dcEventType.send: // 命令控制设备
|
|
|
|
|
this.ijs.sendDeviceControl(params);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// 消息通知
|
2025-01-16 17:25:51 +08:00
|
|
|
|
this.$toast(params.message)
|
2024-11-27 18:46:21 +08:00
|
|
|
|
break;
|
2024-09-26 18:52:14 +08:00
|
|
|
|
}
|
2024-09-18 10:30:10 +08:00
|
|
|
|
},
|
2024-09-27 18:48:24 +08:00
|
|
|
|
//Mqtt连接
|
|
|
|
|
mqttLink(userId) {
|
|
|
|
|
this.mqtt.link(userId);
|
2024-09-18 10:30:10 +08:00
|
|
|
|
},
|
2024-09-30 15:34:20 +08:00
|
|
|
|
//Mqtt订阅
|
2024-12-19 15:28:23 +08:00
|
|
|
|
mqttSubscribes(topics) {
|
|
|
|
|
if (topics) {
|
|
|
|
|
this.mqtt.subscribes(topics);
|
2024-11-27 18:46:21 +08:00
|
|
|
|
} else {
|
|
|
|
|
const topic = this.dc.topic;
|
|
|
|
|
if (topic.newV.length) {
|
2024-11-29 15:09:33 +08:00
|
|
|
|
this.mqtt.subscribes(topic.newV);
|
2024-11-27 18:46:21 +08:00
|
|
|
|
}
|
|
|
|
|
if (topic.oldV.length) {
|
2024-11-29 15:09:33 +08:00
|
|
|
|
this.mqtt.subscribes(topic.oldV);
|
2024-11-27 18:46:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-30 10:23:30 +08:00
|
|
|
|
},
|
2024-09-30 15:34:20 +08:00
|
|
|
|
//Mqtt取消订阅
|
2024-12-19 15:28:23 +08:00
|
|
|
|
mqttUnsubscribes(topics) {
|
|
|
|
|
this.mqtt.unsubscribes(topics);
|
2024-09-18 10:30:10 +08:00
|
|
|
|
},
|
2024-09-30 15:34:20 +08:00
|
|
|
|
//Mqtt断开连接
|
|
|
|
|
mqttEnd() {
|
|
|
|
|
this.mqtt.over();
|
|
|
|
|
},
|
2024-12-19 17:41:53 +08:00
|
|
|
|
//Mqtt状态回调
|
|
|
|
|
mqttStateHandler(e) {
|
|
|
|
|
this.sendMsgEvent("mqtt", {
|
|
|
|
|
type: "state",
|
|
|
|
|
connected: e,
|
|
|
|
|
});
|
2024-12-19 18:38:26 +08:00
|
|
|
|
if (this.isFirseLink) {
|
|
|
|
|
this.mqttSubscribes();
|
|
|
|
|
}
|
2024-12-19 17:41:53 +08:00
|
|
|
|
},
|
2024-09-27 18:48:24 +08:00
|
|
|
|
//Mqtt消息回调
|
|
|
|
|
mqttMsgHandler(topic, data) {
|
2025-01-02 20:28:11 +08:00
|
|
|
|
console.error("mqttMsgHandler:", {
|
|
|
|
|
topic: topic,
|
|
|
|
|
data: JSON.parse(data),
|
|
|
|
|
});
|
2024-12-19 17:41:53 +08:00
|
|
|
|
this.dc.handleMessage(topic, JSON.parse(data));
|
|
|
|
|
this.sendMsgEvent("mqtt", {
|
|
|
|
|
type: "msg",
|
2024-09-27 18:48:24 +08:00
|
|
|
|
topic: topic,
|
2024-11-27 18:46:21 +08:00
|
|
|
|
data: JSON.parse(data),
|
2024-09-27 18:48:24 +08:00
|
|
|
|
});
|
2024-09-18 10:30:10 +08:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</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>
|