From f18274a5f37541bd20710e7ea82352403ea039b1 Mon Sep 17 00:00:00 2001 From: likaikai Date: Thu, 5 Jun 2025 16:33:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=8B=E5=8A=BF=E5=9B=BE=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/base64.js | 48 ++++ src/utils/mqttClient.js | 18 +- src/utils/time.js | 29 +++ src/views/equipment/common/DeviceDialog.vue | 2 + .../equipment/common/DualChannelChart.vue | 230 ++++++------------ src/views/equipment/common/RealtimeChart.vue | 134 +++++----- .../equipment/common/TemperatureChart.vue | 41 ++-- src/views/equipment/equipmentDetails.vue | 173 ++++++------- src/views/equipment/equipmentManage.vue | 14 +- 9 files changed, 358 insertions(+), 331 deletions(-) create mode 100644 src/utils/base64.js create mode 100644 src/utils/time.js diff --git a/src/utils/base64.js b/src/utils/base64.js new file mode 100644 index 0000000..b5f4e05 --- /dev/null +++ b/src/utils/base64.js @@ -0,0 +1,48 @@ +/** + * Base64 解码 + * @param {string} base64Str - base64编码的字符串 + * @param {boolean} toArray - 是否转换为数组 + * @returns {string|Array} 解码后的字符串或数组 + */ +export function base64Decode(base64Str, toArray = false) { + try { + // Check if input is valid + if (!base64Str || typeof base64Str !== 'string') { + return toArray ? [] : '' + } + + // Remove any whitespace and validate base64 string + const cleanStr = base64Str.replace(/\s/g, '') + if (!cleanStr.match(/^[A-Za-z0-9+/]*={0,2}$/)) { + return toArray ? [] : '' + } + + const decodedStr = decodeURIComponent(escape(window.atob(cleanStr))) + if (toArray) { + try { + return JSON.parse(decodedStr) + } catch (e) { + return [] + } + } + return decodedStr + } catch (e) { + console.error('Base64解码失败:', e) + return toArray ? [] : '' + } +} + +/** + * Base64 编码 + * @param {string|Array} data - 需要编码的字符串或数组 + * @returns {string} base64编码后的字符串 + */ +export function base64Encode(data) { + try { + const str = typeof data === 'string' ? data : JSON.stringify(data) + return window.btoa(unescape(encodeURIComponent(str))) + } catch (e) { + console.error('Base64编码失败:', e) + return '' + } +} \ No newline at end of file diff --git a/src/utils/mqttClient.js b/src/utils/mqttClient.js index e80ea31..ac0ac09 100644 --- a/src/utils/mqttClient.js +++ b/src/utils/mqttClient.js @@ -16,7 +16,7 @@ export const useMqtt = () => { }) client.value.on('connect', () => { - console.log('MQTT 连接成功') + // console.log('MQTT 连接成功') connected.value = true // 连接成功后,订阅所有待订阅的主题 @@ -30,12 +30,13 @@ export const useMqtt = () => { }) client.value.on('message', (topic, message) => { - // console.log('收到消息:', topic, JSON.parse(message.toString())) messageData.value[topic] = message.toString() - // 执行对应主题的回调函数 + // 执行该主题的所有回调函数 if (messageCallbacks.value[topic]) { - messageCallbacks.value[topic](JSON.parse(message.toString())) + messageCallbacks.value[topic].forEach(callback => { + callback(JSON.parse(message.toString())) + }) } }) } @@ -43,11 +44,16 @@ export const useMqtt = () => { const subscribe = (topic, callback) => { // 存储回调函数 if (callback) { - messageCallbacks.value[topic] = callback + if (!messageCallbacks.value[topic]) { + messageCallbacks.value[topic] = [] + } + messageCallbacks.value[topic].push(callback) } if (!connected.value) { - pendingSubscriptions.value.push(topic) + if (!pendingSubscriptions.value.includes(topic)) { + pendingSubscriptions.value.push(topic) + } return } diff --git a/src/utils/time.js b/src/utils/time.js new file mode 100644 index 0000000..49d464d --- /dev/null +++ b/src/utils/time.js @@ -0,0 +1,29 @@ +/** + * 将时间戳数组转换为时间数组 + * @param {string|Array} timestampArray - 时间戳数组或字符串 + * @param {string} format - 时间格式,默认 'HH:mm:ss' + * @returns {Array} 格式化后的时间数组 + */ +export function timestampArrayToTime(timestampArray, format = 'HH:mm:ss') { + // 如果是字符串,先转换为数组 + const timestamps = typeof timestampArray === 'string' + ? JSON.parse(timestampArray) + : timestampArray + + if (!Array.isArray(timestamps)) { + console.error('输入必须是数组或可解析为数组的字符串') + return [] + } + + return timestamps.map(timestamp => { + const date = new Date(timestamp * 1000) // 转换为毫秒 + + const formatObj = { + HH: String(date.getHours()).padStart(2, '0'), + mm: String(date.getMinutes()).padStart(2, '0'), + ss: String(date.getSeconds()).padStart(2, '0') + } + + return format.replace(/(HH|mm|ss)/g, match => formatObj[match]) + }) +} \ No newline at end of file diff --git a/src/views/equipment/common/DeviceDialog.vue b/src/views/equipment/common/DeviceDialog.vue index 49dd936..549b236 100644 --- a/src/views/equipment/common/DeviceDialog.vue +++ b/src/views/equipment/common/DeviceDialog.vue @@ -184,6 +184,7 @@ watch( () => props.formData, (newVal) => { if (props.title === '编辑' && Object.keys(newVal).length > 0) { + console.log(newVal,'newVal') Object.keys(form).forEach(key => { if (key === 'userIdList') { form[key] = Array.isArray(newVal[key]) ? [...newVal[key]] : [] @@ -201,6 +202,7 @@ defineExpose({ visible }) onMounted(() => { + console.log('999') getDeviceTypeList() }) diff --git a/src/views/equipment/common/DualChannelChart.vue b/src/views/equipment/common/DualChannelChart.vue index 05ef2c2..5674319 100644 --- a/src/views/equipment/common/DualChannelChart.vue +++ b/src/views/equipment/common/DualChannelChart.vue @@ -1,207 +1,137 @@ - - \ No newline at end of file + \ No newline at end of file diff --git a/src/views/equipment/common/RealtimeChart.vue b/src/views/equipment/common/RealtimeChart.vue index aab4760..ce82127 100644 --- a/src/views/equipment/common/RealtimeChart.vue +++ b/src/views/equipment/common/RealtimeChart.vue @@ -4,14 +4,14 @@
趋势图
近 30min 数据
- + - 冷直径 + {{ currentOption.label }} @@ -25,43 +25,64 @@