From 925977acefc8c2519a950a4690aa9baae163bbc3 Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Thu, 16 Apr 2026 18:27:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=89=88=E6=9C=AC=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E6=98=AF=E5=90=A6=E5=8D=87=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configureDevice_4/configureDevice_4.js | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/components/configureDevice_4/configureDevice_4.js b/components/configureDevice_4/configureDevice_4.js index 117f824..a1ba63f 100644 --- a/components/configureDevice_4/configureDevice_4.js +++ b/components/configureDevice_4/configureDevice_4.js @@ -1,4 +1,5 @@ const app = getApp(); +import $ from "../../utils/request"; Component({ data: {}, @@ -10,16 +11,50 @@ Component({ // 开始 OTA 升级 startOtaUpgrade() { - const activeProtocol = app.globalData.ppScale.activeProtocol; + const ppScale = app.globalData.ppScale; + const activeProtocol = ppScale.activeProtocol; if (!activeProtocol) { wx.showToast({ title: '设备未连接,请重新连接', icon: 'none' }); return; } - // status: false = 成功,true = 失败 - activeProtocol.codeOtaUpdate((status) => { - console.log(status); + ppScale.plugin.bus.subscribe("deviceInfo", (res) => { + console.log("===》deviceInfo", res); + + // 解析固件版本号:格式 "006.005.004.305" → [mcu, ble, wifi, res] + const [mcuVer, bleVer, wifiVer, resVer] = (res.firmwareRevision || "").split("."); + + $.ajax("weighingScale/v2/select/user", { + type: res.modelNumber + }, "GET", false, "").then(serverRes => { + if (serverRes.result) { + /** + * 判断服务端版本是否大于设备端版本(需要升级) + * @param {string} deviceVer - 设备端单段版本,如 "006" + * @param {string} serverVer - 服务端完整版本,如 "0.0.6" + * @returns {boolean} true 表示服务端版本更新,需要升级 + */ + const isServerNewer = (deviceVer, serverVer) => { + // 设备端单段数字化(去前导零),如 "006" → 6 + const deviceNum = parseInt(deviceVer, 10); + // 服务端版本各段累乘1000合并为整数,如 "0.0.6" → 6,"1.0.2" → 1002 + const serverNum = (serverVer || "").split(".").map(v => parseInt(v, 10)).reduce((acc, v) => acc * 1000 + v, 0); + return serverNum > deviceNum; + }; + + const needUpdate = isServerNewer(mcuVer, serverRes.result.mcuVersion) || isServerNewer(bleVer, serverRes.result.bleVersion) || isServerNewer(wifiVer, serverRes.result.wifiVersion) || isServerNewer(resVer, serverRes.result.resVersion); + + if (needUpdate) { + // status: false = 成功,true = 失败 + activeProtocol.codeOtaUpdate((status) => { + console.log("OTA 升级结果:", status); + }); + } + } + }).catch(() => { + wx.showToast({ title: '获取最新版本失败', icon: 'none' }); + }); }); } }