diff --git a/utils/mqtt.js b/utils/mqtt.js index 09c759a..4f24355 100644 --- a/utils/mqtt.js +++ b/utils/mqtt.js @@ -59,7 +59,7 @@ export function initMqttConfig() { console.log('MQTT配置初始化成功,clientId:', mqttState.options.clientId) return true } catch (err) { - uni.showToast({ title: '设备连接异常', icon: 'none', duration: 2000 }) + uni.showToast({ title: '设备连接异常-设备初始化失败', icon: 'none', duration: 2000 }) console.error('MQTT配置初始化失败:', err) return false } @@ -72,7 +72,7 @@ export function initMqttConfig() { export function connectMqtt() { // 前置校验:是否已初始化配置 if (!mqttState.options.clientId) { - uni.showToast({ title: '设备连接异常', icon: 'none' }) + uni.showToast({ title: '设备连接失败', icon: 'none' }) console.error('MQTT连接失败:请先调用initMqttConfig初始化配置') return false } @@ -156,7 +156,7 @@ export function removeOnMessageCallback() { */ export function updateSubscribeList(list) { if (!Array.isArray(list)) { - uni.showToast({ title: '设备连接异常', icon: 'none' }) + uni.showToast({ title: '设备订阅更新异常', icon: 'none' }) console.error('订阅列表必须是数组') return false } @@ -183,7 +183,7 @@ function subscribeAllTopics() { client.subscribe(subscribeList, { qos: 0 }, (err) => { if (err) { - uni.showToast({ title: '设备连接异常', icon: 'none' }) + uni.showToast({ title: '设备订阅异常', icon: 'none' }) console.error('MQTT订阅失败:', err) } else { console.log(`MQTT成功订阅:${subscribeList.join(', ')}`) @@ -198,33 +198,35 @@ function subscribeAllTopics() { * @returns {Boolean} - 是否触发发布成功 */ export function publishMqtt(topic, message) { - const { isConnected, client } = mqttState - if (!isConnected || !client) { - uni.showToast({ title: '控制异常', icon: 'none' }) - console.error('MQTT发布失败:未连接') - return false - } - if (!topic) { - uni.showToast({ title: '控制异常', icon: 'none' }) - console.error('MQTT发布失败:主题为空') - return false - } + if (process.env.NODE_ENV === "production") { + const { isConnected, client } = mqttState + if (!isConnected || !client) { + uni.showToast({ title: '控制异常', icon: 'none' }) + console.error('MQTT发布失败:未连接') + return false + } + if (!topic) { + uni.showToast({ title: '控制异常', icon: 'none' }) + console.error('MQTT发布失败:主题为空') + return false + } - try { - const msg = typeof message === 'object' ? JSON.stringify(message) : String(message) - client.publish(topic, msg, (err) => { - if (err) { - uni.showToast({ title: '控制异常', icon: 'none' }) - console.error(`MQTT发布失败:topic=${topic},err=${err}`) - } else { - console.log(`MQTT发布成功:topic=${topic},message=${msg}`) - } - }) - return true - } catch (err) { - uni.showToast({ title: '控制异常', icon: 'none' }) - console.error('MQTT发布异常:', err) - return false + try { + const msg = typeof message === 'object' ? JSON.stringify(message) : String(message) + client.publish(topic, msg, (err) => { + if (err) { + uni.showToast({ title: '控制异常', icon: 'none' }) + console.error(`MQTT发布失败:topic=${topic},err=${err}`) + } else { + console.log(`MQTT发布成功:topic=${topic},message=${msg}`) + } + }) + return true + } catch (err) { + uni.showToast({ title: '控制异常', icon: 'none' }) + console.error('MQTT发布异常:', err) + return false + } } } @@ -245,7 +247,7 @@ export function disconnectMqtt() { console.log('MQTT连接已断开') return true } catch (err) { - uni.showToast({ title: '设备连接异常', icon: 'none' }) + uni.showToast({ title: '设备通信异常', icon: 'none' }) console.error('MQTT断开失败:', err) return false }