2025-09-27 14:59:48 +08:00
|
|
|
|
// MQTT工具包 - 兼容H5、App、微信小程序
|
|
|
|
|
|
// 参考:https://blogs.seecsdn.cn/online/2025-09-27/0ecf1401e23b25be5f8c7e4377d8b5dd.html
|
|
|
|
|
|
|
|
|
|
|
|
let mqtturl;
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
|
mqtturl = "ws://122.51.194.184:8083/mqtt";
|
|
|
|
|
|
import * as mqtt from "mqtt/dist/mqtt.min.js";
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
2025-09-29 23:53:09 +08:00
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
|
|
mqtturl = "wx://122.51.194.184:8083/mqtt";
|
|
|
|
|
|
import * as mqtt from "mqtt/dist/mqtt.min.js";
|
|
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef MP-WEIXIN
|
2025-09-27 14:59:48 +08:00
|
|
|
|
mqtturl = "wx://122.51.194.184:8083/mqtt";
|
|
|
|
|
|
import * as mqtt from "mqtt/dist/mqtt.min.js";
|
|
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef MP-ALIPAY
|
|
|
|
|
|
mqtturl = "alis://122.51.194.184:8083/mqtt";
|
|
|
|
|
|
import * as mqtt from "@/utils/mqtt.min.js";
|
|
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
|
|
|
|
function messageid2() {
|
|
|
|
|
|
// 2位随机数
|
|
|
|
|
|
return Math.floor(Math.random() * (99 - 10)) + 10;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var client;
|
|
|
|
|
|
|
|
|
|
|
|
// 创建MQTT连接
|
|
|
|
|
|
const createMqtt = () => {
|
|
|
|
|
|
let options = {
|
|
|
|
|
|
keepalive: 30,
|
|
|
|
|
|
clientId: `mobile-inspection-system-${messageid2()}`, // 客户端ID
|
|
|
|
|
|
protocolId: 'MQTT',
|
|
|
|
|
|
username: "dmbroker",
|
|
|
|
|
|
password: "qwer1234",
|
|
|
|
|
|
protocolVersion: 4,
|
|
|
|
|
|
clean: true,
|
2025-09-29 23:53:09 +08:00
|
|
|
|
reconnectPeriod: 1000, // 恢复自动重连,1秒重连一次
|
|
|
|
|
|
connectTimeout: 5000, // 5s超时时间
|
|
|
|
|
|
// topic: "HDYDCJ_01_DOWN",
|
|
|
|
|
|
topic: "HDYDCJ_01_UP",
|
2025-09-27 14:59:48 +08:00
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
|
|
// #ifdef MP-ALIPAY
|
|
|
|
|
|
my: my,//注意这里的my
|
|
|
|
|
|
//#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (!client) {
|
|
|
|
|
|
console.log('🔧 开始创建MQTT连接...');
|
|
|
|
|
|
console.log('🔧 MQTT URL:', mqtturl);
|
|
|
|
|
|
console.log('🔧 连接选项:', options);
|
2025-09-29 23:53:09 +08:00
|
|
|
|
console.log('🔧 当前平台:',
|
|
|
|
|
|
// #ifdef H5
|
|
|
|
|
|
'H5'
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
|
|
'APP-PLUS'
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
// #ifdef MP-WEIXIN
|
|
|
|
|
|
'MP-WEIXIN'
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
// #ifdef MP-ALIPAY
|
|
|
|
|
|
'MP-ALIPAY'
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 显示连接loading
|
|
|
|
|
|
uni.showLoading({
|
|
|
|
|
|
title: 'MQTT连接中...',
|
|
|
|
|
|
mask: true
|
|
|
|
|
|
});
|
2025-09-27 14:59:48 +08:00
|
|
|
|
|
|
|
|
|
|
client = mqtt.connect(mqtturl, options);
|
|
|
|
|
|
initEventHandleMqtt(options.topic);
|
2025-09-29 23:53:09 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
console.log('🔧 MQTT客户端已存在,跳过创建');
|
2025-09-27 14:59:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('❌ MQTT连接创建失败:', e);
|
2025-09-29 23:53:09 +08:00
|
|
|
|
console.error('❌ 错误详情:', e.message);
|
|
|
|
|
|
console.error('❌ 错误堆栈:', e.stack);
|
|
|
|
|
|
|
|
|
|
|
|
// 连接失败时隐藏loading
|
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: 'MQTT连接失败' + mqtturl,
|
|
|
|
|
|
icon: 'error',
|
|
|
|
|
|
duration: 3000
|
|
|
|
|
|
});
|
2025-09-27 14:59:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//建立连接
|
|
|
|
|
|
const initEventHandleMqtt = (topicUrl) => {
|
|
|
|
|
|
// 当连接成功时触发
|
|
|
|
|
|
client.on("connect", function() {
|
|
|
|
|
|
uni.hideLoading();
|
|
|
|
|
|
console.log("✅ MQTT连接成功");
|
2025-09-29 23:53:09 +08:00
|
|
|
|
|
|
|
|
|
|
// 显示连接成功提示
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: 'MQTT连接成功' + mqtturl,
|
|
|
|
|
|
icon: 'success',
|
|
|
|
|
|
duration: 2000
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-27 14:59:48 +08:00
|
|
|
|
//订阅主题
|
|
|
|
|
|
client.subscribe(topicUrl, function(err) {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error("❌ MQTT订阅主题失败:", err);
|
2025-09-29 23:53:09 +08:00
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: '订阅主题失败',
|
|
|
|
|
|
icon: 'error',
|
|
|
|
|
|
duration: 3000
|
|
|
|
|
|
});
|
2025-09-27 14:59:48 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
console.log("✅ MQTT订阅主题成功:", topicUrl);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//如果mqttws订阅主题成功,那么这里就是当接收到自己订阅主题的处理逻辑
|
|
|
|
|
|
client.on("message", function(topic, message) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
console.log('📨 收到MQTT消息:');
|
|
|
|
|
|
console.log('主题:', topic);
|
|
|
|
|
|
console.log('消息内容:', message.toString());
|
|
|
|
|
|
|
|
|
|
|
|
// 获取信息
|
|
|
|
|
|
const mqttData = JSON.parse(message.toString());
|
|
|
|
|
|
console.log('📋 解析后的数据:', mqttData);
|
2025-09-29 23:53:09 +08:00
|
|
|
|
console.log('数据类型:', Array.isArray(mqttData) ? '数组' : '对象');
|
|
|
|
|
|
|
|
|
|
|
|
// 如果是数组,打印数组信息
|
|
|
|
|
|
if (Array.isArray(mqttData)) {
|
|
|
|
|
|
console.log('📋 数组长度:', mqttData.length);
|
|
|
|
|
|
mqttData.forEach((item, index) => {
|
|
|
|
|
|
console.log(`📦 数组[${index}]:`, item);
|
|
|
|
|
|
if (item.Device) {
|
|
|
|
|
|
console.log(`🔍 设备类型[${index}]: ${item.Device}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-27 14:59:48 +08:00
|
|
|
|
|
|
|
|
|
|
// 传递信息
|
|
|
|
|
|
uni.$emit("mqttData", mqttData);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('❌ 处理MQTT消息失败:', error);
|
|
|
|
|
|
console.error('原始消息:', message.toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 当断开连接后,经过重连间隔时间重新自动连接到 Broker 时触发
|
|
|
|
|
|
client.on('reconnect', function() {
|
|
|
|
|
|
console.log('🔄 MQTT重新连接中...');
|
|
|
|
|
|
uni.showLoading({
|
2025-09-29 23:53:09 +08:00
|
|
|
|
title: "重新连接中..."
|
2025-09-27 14:59:48 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 当客户端无法成功连接时或发生解析错误时触发,参数 error 为错误信息
|
|
|
|
|
|
client.on("error", function(err) {
|
|
|
|
|
|
console.error('❌ MQTT连接错误:', err);
|
2025-09-29 23:53:09 +08:00
|
|
|
|
uni.hideLoading();
|
2025-09-27 14:59:48 +08:00
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title: 'MQTT连接错误',
|
|
|
|
|
|
icon: 'error',
|
|
|
|
|
|
duration: 3000
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 在收到 Broker 发送过来的断开连接的报文时触发
|
|
|
|
|
|
client.on('disconnect', function() {
|
|
|
|
|
|
console.log('⚠️ MQTT连接断开');
|
2025-09-29 23:53:09 +08:00
|
|
|
|
uni.hideLoading();
|
2025-09-27 14:59:48 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 在断开连接以后触发
|
|
|
|
|
|
client.on("close", function() {
|
|
|
|
|
|
console.log('🔌 MQTT连接关闭');
|
2025-09-29 23:53:09 +08:00
|
|
|
|
uni.hideLoading();
|
2025-09-27 14:59:48 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 当客户端下线时触发
|
|
|
|
|
|
client.on("offline", function() {
|
|
|
|
|
|
console.log('📴 MQTT客户端离线');
|
2025-09-29 23:53:09 +08:00
|
|
|
|
uni.hideLoading();
|
2025-09-27 14:59:48 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//强制断开Mqtt
|
|
|
|
|
|
const closeMqtt = () => {
|
|
|
|
|
|
if (client) {
|
|
|
|
|
|
console.log('🔌 强制断开MQTT连接');
|
2025-09-29 23:53:09 +08:00
|
|
|
|
uni.hideLoading();
|
2025-09-27 14:59:48 +08:00
|
|
|
|
client.end();
|
|
|
|
|
|
client = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 使用pingResp心跳判断客户端和服务端是否还在连接着
|
|
|
|
|
|
const judgeBeat = () => {
|
|
|
|
|
|
if (client && client.pingResp === false) {
|
|
|
|
|
|
console.log('💔 MQTT心跳停止,准备重连');
|
|
|
|
|
|
uni.showLoading({
|
|
|
|
|
|
title: "心跳停止,等待重连..."
|
|
|
|
|
|
});
|
|
|
|
|
|
closeMqtt();
|
|
|
|
|
|
createMqtt();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取连接状态
|
|
|
|
|
|
const getConnectionStatus = () => {
|
2025-09-29 23:53:09 +08:00
|
|
|
|
if (!client) {
|
|
|
|
|
|
console.log('🔍 连接状态检查: 客户端不存在');
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const isConnected = client.connected;
|
|
|
|
|
|
console.log('🔍 连接状态检查:', {
|
|
|
|
|
|
clientExists: !!client,
|
|
|
|
|
|
connected: isConnected,
|
|
|
|
|
|
readyState: client.stream ? client.stream.readyState : 'unknown'
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return isConnected;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 手动重连函数
|
|
|
|
|
|
const manualReconnect = () => {
|
|
|
|
|
|
console.log('🔄 手动触发重连');
|
|
|
|
|
|
closeMqtt();
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
createMqtt();
|
|
|
|
|
|
}, 1000);
|
2025-09-27 14:59:48 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
createMqtt,
|
|
|
|
|
|
closeMqtt,
|
|
|
|
|
|
judgeBeat,
|
|
|
|
|
|
getConnectionStatus,
|
2025-09-29 23:53:09 +08:00
|
|
|
|
manualReconnect,
|
2025-09-27 14:59:48 +08:00
|
|
|
|
client,
|
|
|
|
|
|
}
|