From 0e4a1aa6221c2cf3bdb077a359e12b86830d8866 Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Tue, 8 Apr 2025 14:08:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86636=E7=9A=84=E5=85=BC?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 44 ++++++++++- app.json | 4 +- pages/deviceInfo/deviceInfo.js | 85 ++++++--------------- pages/deviceInfo/deviceInfo.wxml | 2 +- pages/my/my.js | 42 +++++++++- pages/searchDevice/searchDevice.js | 45 +---------- pages/setNetwork/setNetwork.js | 6 +- pages/userInfo/userInfo.js | 119 ++++++++++++++--------------- 8 files changed, 170 insertions(+), 177 deletions(-) diff --git a/app.js b/app.js index 726024e..bf5a125 100644 --- a/app.js +++ b/app.js @@ -23,7 +23,49 @@ App({ secret: "eQ50JYimMp0X7uhNLj6D3lTEk4TUUvEG7dvaqKM9t1U=" }, device: { - setting: [], // 即将要搜索周边秤的配置 + setting: [{ + "advLength": 999, + "calorieStatus": 0, + "createBy": null, + "deviceAccuracyType": 2, + "deviceCalcuteType": 3, + "deviceConnectType": 2, + "deviceFuncType": 223, + "deviceName": "CF568_G", + "devicePowerType": 3, + "deviceProtocolType": 3, + "deviceType": 1, + "deviceUnitType": "0,1,11", + "id": 65, + "imgUrl": null, + "macAddressStart": 6, + "remark": null, + "sign": "FF", + "status": 0, + "uhStatus": 1, + "updateBy": null + }, { + "advLength": 999, + "calorieStatus": 0, + "createBy": null, + "deviceAccuracyType": 2, + "deviceCalcuteType": 4, + "deviceConnectType": 2, + "deviceFuncType": 65759, + "deviceName": "CF636_G", + "devicePowerType": 3, + "deviceProtocolType": 3, + "deviceType": 1, + "deviceUnitType": "0,1,2", + "id": 216, + "imgUrl": null, + "macAddressStart": 6, + "remark": null, + "sign": "FF", + "status": 0, + "uhStatus": 1, + "updateBy": null + }], // 即将要搜索周边秤的配置 list: [], // 根据配置搜索到秤的列表 mac: null, // 选中的设备mac地址/SN码 name: "", // 设备名称 diff --git a/app.json b/app.json index fd57520..9252bf3 100644 --- a/app.json +++ b/app.json @@ -38,8 +38,8 @@ "style": "v2", "plugins": { "ppScale-plugin": { - "version": "1.2.14", - "provider": "wx0ffb48417ce6345c" + "version": "0.0.11", + "provider": "wx0826af4e4908f524" } }, "sitemapLocation": "sitemap.json", diff --git a/pages/deviceInfo/deviceInfo.js b/pages/deviceInfo/deviceInfo.js index 9cfda1f..f01fe33 100644 --- a/pages/deviceInfo/deviceInfo.js +++ b/pages/deviceInfo/deviceInfo.js @@ -5,12 +5,11 @@ Page({ data: { deviceConnect: false, - deviceInfo: null + deviceInfo: null, }, onLoad(options) { this.getDevice(); - this.initBluetooth(); app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => { console.log('deviceInfo ===》deviceConnect', res); @@ -21,17 +20,8 @@ Page({ app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => { console.log("deviceInfo ===》codeUpdateMTU", res); - $.ajax("weighingScale/list/device", {}, "GET", false).then((res) => { - if(res.result && res.result.length) { - let deviceConnect = res.result.some(item => item.macAddress === app.globalData.ppScale.device.mac); - if(deviceConnect) { - this.setData({ - deviceConnect: true - }) - } else { - app.globalData.ppScale.plugin.Blue.disconnect(); - } - } + this.setData({ + deviceConnect: true }) }); }); @@ -54,56 +44,27 @@ Page({ }); }, - async refreshToken(fn, flag) { - let bodyToken = wx.getStorageSync('bodyToken'); - let bodyTokenTime = wx.getStorageSync('bodyTokenTime'); - if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) { - let res = await app.globalData.ppScale.plugin.refreshToken({ - url: "https://uniquehealth.lefuenergy.com", - data: { - "appKey": app.globalData.ppScale.options.key, - "appSecret": app.globalData.ppScale.options.secret + getDeviceSettingList() { + let seviceList = []; + app.globalData.ppScale.device.setting.map(item => { + if(this.data.deviceInfo.type === item.deviceName) { + seviceList.push(item); + } + }) + if(seviceList && seviceList.length) { + app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList); + app.globalData.ppScale.plugin.Blue.start(seviceList[0].deviceName, false); + app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res) => { + console.log("deviceInfo ===> devicesList", res); + let fIndex = res.findIndex(item => item.deviceId === this.data.deviceInfo.deviceId); + if (fIndex >= 0) { + app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); + app.globalData.ppScale.plugin.Blue.createBLEConnection(res[fIndex]); + return; } }); - if (res.data.code == 200) { - wx.setStorageSync('bodyToken', res.data.data.token); - wx.setStorageSync('bodyTokenTime', res.data.data.expireTime); - fn(); - } - } else { - fn(); } }, - getDeviceSettingList() { - let bodyToken = wx.getStorageSync("bodyToken"); - app.globalData.ppScale.plugin.getDeviceSettingList({ - url: "https://uniquehealth.lefuenergy.com", - data: { - appKey: app.globalData.ppScale.options.key - }, - header: { - "token": bodyToken, - "Accept-Language": wx.getStorageSync("lang") || 'zh' - } - }).then((res) => { - if (res.data.code == 200) { - app.globalData.ppScale.plugin.Blue.setDeviceSetting(res.data.data); - app.globalData.ppScale.device.setting = res.data.data; - let deviceNames = res.data.data.map(item => item.deviceName); - app.globalData.ppScale.plugin.Blue.start(deviceNames, false); - app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => { - app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); - app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[0]); - }); - } else if (res.data.code == 401 || res.data.code == 4008) { - // 401 token过期 / 4008 token为空 - // 重新请求token解析数据 - this.refreshToken(() => { - this.getDeviceSettingList(); - }, true); - } - }); - }, // 获取已经绑定的设备列表 getDevice() { @@ -112,6 +73,7 @@ Page({ this.setData({ deviceInfo: res.result[0] }) + this.initBluetooth(); }) }, @@ -120,7 +82,7 @@ Page({ app.globalData.ppScale.activeProtocol.codeClearDeviceData("00", (res) => { console.log("deviceInfo === codeClearDeviceData", res); - let scaleDeviceId = app.globalData.ppScale.device.mac.replace(/:/g, ''); + let scaleDeviceId = this.data.deviceInfo.macAddress.replace(/:/g, ''); let params = { equipmentCode: scaleDeviceId, }; @@ -141,7 +103,8 @@ Page({ icon: "none", title: "删除失败,请靠近设备并点亮后再试" }) - this.initBluetooth(); + app.globalData.ppScale.plugin.Blue.stop(); + this.getDeviceSettingList(); } }, diff --git a/pages/deviceInfo/deviceInfo.wxml b/pages/deviceInfo/deviceInfo.wxml index 97c33a5..0f666ca 100644 --- a/pages/deviceInfo/deviceInfo.wxml +++ b/pages/deviceInfo/deviceInfo.wxml @@ -31,4 +31,4 @@ -删除设备 \ No newline at end of file +删除设备 diff --git a/pages/my/my.js b/pages/my/my.js index 4b3cb79..4f6667e 100644 --- a/pages/my/my.js +++ b/pages/my/my.js @@ -25,13 +25,49 @@ Page({ url: "/pages/userInfo/userInfo?initBluetooth=1&customerType=1" }) } else { - wx.showToast({ - icon: "none", - title: "请先登录" + wx.login({ + success: (res) => { + if (res.code) { + this.checkOpenIdLogin(res.code); + } else { + wx.showToast({ + icon: "none", + title: "登录失败,请稍后再试!", + }) + } + } }) } }, + // 微信一键登录 + checkOpenIdLogin(code) { + let params = { + code: code + }; + $.ajax("weighingScale/checkOpenIdLogin", params, "POST", true, "登录中...").then((res) => { + wx.setStorageSync("userInfo", res.result.user); + wx.setStorageSync("token", res.result.token); + if(res.result.token && res.result.user) { + this.setData({ + token: res.result.token, + userInfo: res.result.user + }) + this.getDevice(); + } else { + wx.showToast({ + icon: "none", + title: "请先补全个人信息" + }) + setTimeout(() => { + wx.navigateTo({ + url: "/pages/userInfo/userInfo?initBluetooth=0&customerType=1" + }) + }, 1500) + } + }) + }, + // 打开设备详情 openDeviceInfo() { if(this.data.userInfo) { diff --git a/pages/searchDevice/searchDevice.js b/pages/searchDevice/searchDevice.js index 00c08d3..731b68c 100644 --- a/pages/searchDevice/searchDevice.js +++ b/pages/searchDevice/searchDevice.js @@ -114,51 +114,8 @@ Page({ this.setData({ initText: "设备搜索中..." }); - let seviceList = [{ - "advLength": 999, - "calorieStatus": 0, - "createBy": null, - "deviceAccuracyType": 2, - "deviceCalcuteType": 3, - "deviceConnectType": 2, - "deviceFuncType": 223, - "deviceName": "CF568_G", - "devicePowerType": 3, - "deviceProtocolType": 3, - "deviceType": 1, - "deviceUnitType": "0,1,11", - "id": 65, - "imgUrl": null, - "macAddressStart": 6, - "remark": null, - "sign": "FF", - "status": 0, - "uhStatus": 1, - "updateBy": null - }, { - "advLength": 999, - "calorieStatus": 0, - "createBy": null, - "deviceAccuracyType": 2, - "deviceCalcuteType": 4, - "deviceConnectType": 2, - "deviceFuncType": 65759, - "deviceName": "CF636_G", - "devicePowerType": 3, - "deviceProtocolType": 3, - "deviceType": 1, - "deviceUnitType": "0,1,2", - "id": 216, - "imgUrl": null, - "macAddressStart": 6, - "remark": null, - "sign": "FF", - "status": 0, - "uhStatus": 1, - "updateBy": null - }]; + let seviceList = app.globalData.ppScale.device.setting; app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList); - app.globalData.ppScale.device.setting = seviceList; let deviceNames = seviceList.map(item => item.deviceName); app.globalData.ppScale.plugin.Blue.start(deviceNames, false); app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res) => { diff --git a/pages/setNetwork/setNetwork.js b/pages/setNetwork/setNetwork.js index cfead89..6ff675f 100644 --- a/pages/setNetwork/setNetwork.js +++ b/pages/setNetwork/setNetwork.js @@ -14,11 +14,13 @@ Page({ app.globalData.ppScale.wifi.password = password; let mac = app.globalData.ppScale.device.mac; - if(mac) { + let deviceId = app.globalData.ppScale.device.connection.deviceId; + if(mac && deviceId) { let scaleDeviceId = mac.replace(/:/g, ''); let params = { equipmentName: app.globalData.ppScale.device.name, scaleDeviceId: scaleDeviceId, + deviceId: deviceId }; $.ajax("weighingScale/binding/device", params, "POST").then(res => { app.globalData.ppScale.activeProtocol.codeSetBindingState((res) => { @@ -42,7 +44,7 @@ Page({ } else { wx.showToast({ icon: "none", - title: "Mac 地址获取失败,请重新绑定" + title: "Mac 地址与 deviceId 获取失败,请重新绑定" }) setTimeout(() => { wx.switchTab({ diff --git a/pages/userInfo/userInfo.js b/pages/userInfo/userInfo.js index 4bd57e1..e32ad0d 100644 --- a/pages/userInfo/userInfo.js +++ b/pages/userInfo/userInfo.js @@ -7,6 +7,7 @@ Page({ initBluetooth: "0", customerType: "1", + deviceInfo: null, id: "", realname: "", @@ -69,35 +70,55 @@ Page({ } } if(options.initBluetooth === "1") { - this.initBluetooth(); + this.getDevice(); app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => { - console.log('deviceInfo ===》deviceConnect', res); - + console.log('userInfo ===》deviceConnect', res); app.globalData.ppScale.plugin.ScaleAction.startDataProgress(); app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol(); app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => { - console.log("deviceInfo ===》codeUpdateMTU", res); + console.log("userInfo ===》codeUpdateMTU", res); - $.ajax("weighingScale/list/device", {}, "GET", false).then((res) => { - if(res.result && res.result.length) { - let deviceConnect = res.result.some(item => item.macAddress === app.globalData.ppScale.device.mac); - if(deviceConnect) { - this.setData({ - deviceConnect: true - }) - } else { - app.globalData.ppScale.plugin.Blue.disconnect(); - } - } + this.setData({ + deviceConnect: true }) }); }); } }, + // 获取已经绑定的设备列表 + getDevice() { + let params = {}; + $.ajax("weighingScale/list/device", params, "GET", false).then((res) => { + if(res.result && res.result.length) { + this.setData({ + deviceInfo: res.result[0] + }) + this.initBluetooth(); + } else { + wx.showModal({ + title: "提示", + content: "当前暂未绑定设备,需要绑定吗?", + confirmText: "去绑定", + success: (res) => { + if (res.confirm) { + wx.navigateTo({ + url: "/pages/searchDevice/searchDevice" + }) + } else if (res.cancel) { + wx.navigateBack({ + delta: 1 + }) + } + } + }) + } + }) + }, + // 初始化蓝牙 initBluetooth() { wx.openBluetoothAdapter({ @@ -115,56 +136,27 @@ Page({ }); }, - async refreshToken(fn, flag) { - let bodyToken = wx.getStorageSync('bodyToken'); - let bodyTokenTime = wx.getStorageSync('bodyTokenTime'); - if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) { - let res = await app.globalData.ppScale.plugin.refreshToken({ - url: "https://uniquehealth.lefuenergy.com", - data: { - "appKey": app.globalData.ppScale.options.key, - "appSecret": app.globalData.ppScale.options.secret + getDeviceSettingList() { + let seviceList = []; + app.globalData.ppScale.device.setting.map(item => { + if(this.data.deviceInfo.type === item.deviceName) { + seviceList.push(item); + } + }) + if(seviceList && seviceList.length) { + app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList); + app.globalData.ppScale.plugin.Blue.start(seviceList[0].deviceName, false); + app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res) => { + console.log("deviceInfo ===> devicesList", res); + let fIndex = res.findIndex(item => item.deviceId === this.data.deviceInfo.deviceId); + if (fIndex >= 0) { + app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); + app.globalData.ppScale.plugin.Blue.createBLEConnection(res[fIndex]); + return; } }); - if (res.data.code == 200) { - wx.setStorageSync('bodyToken', res.data.data.token); - wx.setStorageSync('bodyTokenTime', res.data.data.expireTime); - fn(); - } - } else { - fn(); } }, - getDeviceSettingList() { - let bodyToken = wx.getStorageSync("bodyToken"); - app.globalData.ppScale.plugin.getDeviceSettingList({ - url: "https://uniquehealth.lefuenergy.com", - data: { - appKey: app.globalData.ppScale.options.key - }, - header: { - "token": bodyToken, - "Accept-Language": wx.getStorageSync("lang") || 'zh' - } - }).then((res) => { - if (res.data.code == 200) { - app.globalData.ppScale.plugin.Blue.setDeviceSetting(res.data.data); - app.globalData.ppScale.device.setting = res.data.data; - let deviceNames = res.data.data.map(item => item.deviceName); - app.globalData.ppScale.plugin.Blue.start(deviceNames, false); - app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => { - app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); - app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[0]); - }); - } else if (res.data.code == 401 || res.data.code == 4008) { - // 401 token过期 / 4008 token为空 - // 重新请求token解析数据 - this.refreshToken(() => { - this.getDeviceSettingList(); - }, true); - } - }); - }, // 姓名输入 realnameInput(e) { @@ -247,7 +239,7 @@ Page({ icon: "none", title: "请靠近设备并点亮后再试" }) - this.initBluetooth(); + this.getDeviceSettingList(); return false; } if(!realname) { @@ -426,7 +418,8 @@ Page({ icon: "none", title: "删除失败,请靠近设备并点亮后再试" }) - this.initBluetooth(); + app.globalData.ppScale.plugin.Blue.stop(); + this.getDeviceSettingList(); } },