优化了一部分代码

This commit is contained in:
17792275749
2025-04-30 15:33:24 +08:00
parent 230490b995
commit 2fc1f423bd
5 changed files with 172 additions and 18 deletions
+93 -8
View File
@@ -8,6 +8,8 @@ Page({
initBluetooth: "0",
customerType: "1",
deviceInfo: null,
seviceList: [],
userInfo: null,
id: "",
realname: "",
@@ -34,6 +36,12 @@ Page({
index: 20,
data: Array.from({ length: 100 - 50 + 1 }, (_, i) => i + 50)
},
connectedDevice: {
code: "",
color: "",
message: ""
}
},
onLoad(options) {
@@ -57,6 +65,7 @@ Page({
if (userInfo && options.initBluetooth === "1") {
let [year, month, day] = userInfo.birthday.split("-");
this.setData({
userInfo: userInfo,
realname: userInfo.realname,
["sex.index"]: this.data.sex.data.findIndex(item => {return item.id === userInfo.sex}),
birthday: {
@@ -70,10 +79,17 @@ Page({
}
}
if(options.initBluetooth === "1") {
this.getDevice();
this.initBluetooth();
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
console.log('userInfo ===》deviceConnect', res);
this.setData({
connectedDevice: {
code: "",
color: "#2AC79F",
message: "等待设备回应"
}
})
app.globalData.ppScale.plugin.ScaleAction.startDataProgress();
app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol();
@@ -82,7 +98,12 @@ Page({
console.log("userInfo ===》codeUpdateMTU", res);
this.setData({
deviceConnect: true
deviceConnect: true,
connectedDevice: {
code: "",
color: "#2AC79F",
message: "已连接"
}
})
});
});
@@ -91,14 +112,28 @@ Page({
// 获取已经绑定的设备列表
getDevice() {
this.setData({
connectedDevice: {
code: "",
color: "#808080",
message: "获取设备列表"
}
})
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();
this.getDeviceSettingList();
} else {
this.setData({
connectedDevice: {
code: "",
color: "#808080",
message: "未绑定设备"
}
})
wx.showModal({
title: "提示",
content: "当前暂未绑定设备,需要绑定吗?",
@@ -121,12 +156,26 @@ Page({
// 初始化蓝牙
initBluetooth() {
this.setData({
connectedDevice: {
code: "",
color: "#808080",
message: "初始化蓝牙"
}
})
wx.openBluetoothAdapter({
success: () => {
this.getDeviceSettingList();
this.getDevice();
},
fail: (err) => {
if (err.errCode === 10001) {
this.setData({
connectedDevice: {
code: "",
color: "#F24439",
message: "初始化蓝牙失败"
}
})
wx.showModal({
title: "提示",
content: "请打开手机蓝牙",
@@ -137,20 +186,57 @@ Page({
},
getDeviceSettingList() {
this.setData({
connectedDevice: {
code: "",
color: "#808080",
message: "获取配置信息"
}
})
let seviceList = [];
app.globalData.ppScale.device.setting.map(item => {
if(this.data.deviceInfo.type === item.deviceName) {
seviceList.push(item);
}
})
this.setData({
seviceList: seviceList,
connectedDevice: {
code: "",
color: "#2AC79F",
message: "配置获取成功"
}
})
if(seviceList && seviceList.length) {
app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList);
this.setData({
connectedDevice: {
code: "",
color: "#808080",
message: "开始搜索设备"
}
})
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);
this.setData({
connectedDevice: {
code: "",
color: "#808080",
message: "设备搜索中"
}
})
if (fIndex >= 0) {
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
this.setData({
connectedDevice: {
code: "",
color: "#2AC79F",
message: "连接中"
}
})
app.globalData.ppScale.plugin.Blue.createBLEConnection(res[fIndex]);
return;
}
@@ -239,7 +325,7 @@ Page({
icon: "none",
title: "请靠近设备并点亮后再试"
})
this.getDeviceSettingList();
app.globalData.ppScale.plugin.Blue.start(this.data.seviceList[0].deviceName, false);
return false;
}
if(!realname) {
@@ -323,7 +409,7 @@ Page({
gender: res_.result.sex, //
height: res_.result.height, //
isAthleteMode: 0,
currentWeight: res_.result.weight, //
currentWeight: res_.result.weight, //
deviceHeaderIndex: 0,
targetWeight: "",
idealWeight: "",
@@ -418,8 +504,7 @@ Page({
icon: "none",
title: "删除失败,请靠近设备并点亮后再试"
})
app.globalData.ppScale.plugin.Blue.stop();
this.getDeviceSettingList();
app.globalData.ppScale.plugin.Blue.start(this.data.seviceList[0].deviceName, false);
}
},