进度条加载

This commit is contained in:
fengerli
2025-08-09 09:47:50 +08:00
parent 3ab58ac29e
commit c6babaa262
3 changed files with 51 additions and 19 deletions

View File

@ -912,7 +912,18 @@
icon: 'none' icon: 'none'
}) })
} }
},
handleMqttLost() {
this.Progress = {
...this.Progress,
show: false, // 隐藏进度条
};
uni.showToast({
title: '网络异常',
icon: 'none'
});
} }
}, },
onLoad(options) { onLoad(options) {
const eventChannel = this.getOpenerEventChannel(); const eventChannel = this.getOpenerEventChannel();
@ -973,9 +984,10 @@
this.selectedImage = ''; // 清空已选图片 this.selectedImage = ''; // 清空已选图片
} }
} }
});
}); });
})
// 设置连接丢失回调
uni.$on('mqttConnectionLost', this.handleMqttLost);
if (this.apiType === 'listA') { if (this.apiType === 'listA') {
this.fetchDeviceDetail(data.data.id) this.fetchDeviceDetail(data.data.id)
} else { } else {
@ -995,6 +1007,10 @@
if (this.mqttClient) { if (this.mqttClient) {
this.mqttClient.disconnect(); this.mqttClient.disconnect();
} }
uni.$off('mqttConnectionLost', this.handleMqttLost);
if (this.mqttClient) {
this.mqttClient.disconnect();
}
}, },
} }

View File

@ -1,5 +1,5 @@
// 兼容性补丁:为 Paho MQTT 提供 uni-app 环境下的 WebSocket 实现 // 兼容性补丁:为 Paho MQTT 提供 uni-app 环境下的 WebSocket 实现
(function (root) { (function(root) {
// 如果未能找到全局对象,则无法应用补丁。 // 如果未能找到全局对象,则无法应用补丁。
if (!root) { if (!root) {
console.error("MQTT Polyfill: 未能找到全局对象 (global/window/self 均未定义)。"); console.error("MQTT Polyfill: 未能找到全局对象 (global/window/self 均未定义)。");
@ -26,8 +26,11 @@
const errText = JSON.stringify(err) || "Empty error object"; const errText = JSON.stringify(err) || "Empty error object";
console.error(`uni.connectSocket 失败: ${errText}`); console.error(`uni.connectSocket 失败: ${errText}`);
if (this.onerror) { if (this.onerror) {
const errorMessage = (err && err.errMsg) ? err.errMsg : "uni.connectSocket call failed"; const errorMessage = (err && err.errMsg) ? err.errMsg :
this.onerror({ message: errorMessage }); "uni.connectSocket call failed";
this.onerror({
message: errorMessage
});
} }
} }
}); });
@ -53,22 +56,29 @@
console.error(`WebSocket polyfill 错误: ${errText}`); console.error(`WebSocket polyfill 错误: ${errText}`);
if (this.onerror) { if (this.onerror) {
// Paho expects an object that can be stringified, not a real Error object. // Paho expects an object that can be stringified, not a real Error object.
const errorMessage = (err && err.errMsg) ? err.errMsg : "WebSocket connection failed in uni-app"; const errorMessage = (err && err.errMsg) ? err.errMsg :
this.onerror({ message: errorMessage }); "WebSocket connection failed in uni-app";
this.onerror({
message: errorMessage
});
} }
}); });
socketTask.onMessage((res) => { socketTask.onMessage((res) => {
if (this.onmessage) { if (this.onmessage) {
// Paho 期望事件对象有一个 'data' 属性 // Paho 期望事件对象有一个 'data' 属性
this.onmessage({ data: res.data }); this.onmessage({
data: res.data
});
} }
}); });
} }
send(data) { send(data) {
if (this.readyState === WebSocket.OPEN) { if (this.readyState === WebSocket.OPEN) {
this._socketTask.send({ data: data }); this._socketTask.send({
data: data
});
} else { } else {
console.error('WebSocket polyfill: send() 在非 OPEN 状态下被调用。'); console.error('WebSocket polyfill: send() 在非 OPEN 状态下被调用。');
throw new Error('WebSocket is not open'); throw new Error('WebSocket is not open');
@ -109,7 +119,7 @@ import Paho from 'paho-mqtt';
import allConfigs from '../config/index.js'; import allConfigs from '../config/index.js';
// 根据环境选择正确的配置 // 根据环境选择正确的配置
const env = 'development';//production //开发of线上 改这里就行 const env = 'production'; //production //开发of线上 改这里就行
const config = allConfigs[env]; const config = allConfigs[env];
class MqttClient { class MqttClient {
@ -145,7 +155,11 @@ class MqttClient {
this.client.onConnectionLost = (responseObject) => { this.client.onConnectionLost = (responseObject) => {
if (responseObject.errorCode !== 0) { if (responseObject.errorCode !== 0) {
console.log("MQTT连接丢失: " + responseObject.errorMessage); console.log("MQTT连接丢失: " + responseObject.errorMessage);
// 可以在此添加重连逻辑 // 发送全局事件
uni.$emit('mqttConnectionLost', {
error: responseObject.errorMessage,
timestamp: Date.now()
});
} }
}; };
@ -192,7 +206,9 @@ class MqttClient {
subscribe(topic, onMessageCallback) { subscribe(topic, onMessageCallback) {
if (this.client && this.client.isConnected()) { if (this.client && this.client.isConnected()) {
console.log(`尝试订阅主题: ${topic}`); console.log(`尝试订阅主题: ${topic}`);
this.client.subscribe(topic, { qos: 1 }); this.client.subscribe(topic, {
qos: 1
});
// 存储该主题的回调函数 // 存储该主题的回调函数
this.messageCallbacks.set(topic, onMessageCallback); this.messageCallbacks.set(topic, onMessageCallback);
} else { } else {

View File

@ -1,5 +1,5 @@
import config from '../config/index.js'; import config from '../config/index.js';
const env = 'development'; //production development //开发of线上 改这里就行 const env = 'production'; //production development //开发of线上 改这里就行
const BASE = config[env]; const BASE = config[env];
const request = (options) => { const request = (options) => {
console.log("options"+JSON.stringify(options),BASE.BASE_URL) console.log("options"+JSON.stringify(options),BASE.BASE_URL)