Files
bodyWeight/components/configureDevice_3/configureDevice_3.js
T
17792275749andClaude Opus 4.6 81f9301c64 [AI Generated]: refactor(*): 重构蓝牙生命周期管理,修复最小化后重连与进度跳步问题
- app.js: 提取 registerBusListeners 方法,新增自动重连机制(3次上限)
- configureDevice: bus 订阅迁移至 onShow,deviceConnect 按步骤区分处理逻辑
- configureDevice_2: setUserInfo 兼容 WIFISUCCESS 状态,WXML 覆盖六态
- configureDevice_3: 新增 reInitWifi 方法,最小化后重置 WiFi 列表
- replaceNetwork: 订阅迁移至 onShow,修复 selectComponent 空指针
- replaceNetwork_3: 修复 domain1 配网参数引用错误
- userInfo: 订阅迁移至 onShow,setUserInfo 兼容 WIFISUCCESS
- searchDevice: 移除 onLoad 重复初始化,统一由 onShow 处理
- 所有 BLE 页面统一 onHide/onUnload 清理:resetReconnectCount + setGlobalData(null) + stop()

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-09 17:34:08 +08:00

188 lines
6.4 KiB
JavaScript

const app = getApp();
import $ from "../../utils/request";
Component({
data: {
progress: 0,
wifiList: [],
selectWifi: null,
type: true,
selectWifiPWD: ""
},
lifetimes: {
attached() {
this.initWifi();
},
detached() {
// 在组件实例被从页面节点树移除时执行
console.log('MyComponent detached!');
}
},
// 组件的方法
methods: {
initWifi() {
wx.showLoading({
title: "正在获取Wi-Fi列表...",
mask: true
});
app.globalData.ppScale.activeProtocol.dataFindSurroundDevice((res) => {
wx.hideLoading();
this.setData({
wifiList: res
})
})
},
// 最小化后重连,重置所有状态并重新获取 WiFi 列表
reInitWifi() {
this.setData({
progress: 0,
wifiList: [],
selectWifi: null,
selectWifiPWD: ""
});
this.initWifi();
},
selectDevice(e) {
this.setData({
progress: 1,
selectWifi: e.currentTarget.dataset.item,
})
},
inputTypeChange() {
this.setData({
type: !this.data.type
})
},
passwordInput(e) {
this.setData({
selectWifiPWD: e.detail.value
})
},
setWifiPWD() {
this.setData({
progress: 2
})
let version = app.globalData.ppScale.version;
if (version === 'domain1') {
app.globalData.ppScale.activeProtocol.dataConfigNetWork({
domain: app.globalData.ppScale.domain1,
ssid: this.data.selectWifi.ssid,
password: this.data.selectWifiPWD
}, (res) => {
console.log("setNetwork.js dataConfigNetWork 1", res);
this.netWorkCallBack(res);
})
}
if (version === 'domain2') {
app.globalData.ppScale.activeProtocol.dataConfigUserNetWork({
domain: app.globalData.ppScale.domain2,
ssid: this.data.selectWifi.ssid,
password: this.data.selectWifiPWD,
userName: "apiUser",
userPassword: "3acebb95eb49577e9c2a2082589b9bd6",
}, (res) => {
console.log("setNetwork.js dataConfigUserNetWork 2", res);
this.netWorkCallBack(res);
})
}
},
netWorkCallBack(res) {
if(res === 23) {
app.globalData.ppScale.wifi.ssid = this.data.selectWifi.ssid;
app.globalData.ppScale.wifi.password = this.data.selectWifiPWD;
let mac = app.globalData.ppScale.device.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 => {
if (res.success) {
app.globalData.ppScale.activeProtocol.codeSetBindingState((res) => {
console.log("setNetwork.js codeSetBindingState", res);
if(res == 0) {
this.triggerEvent('deviceEvent', {
status: true
});
} else {
app.globalData.ppScale.plugin.Blue.stop();
wx.showToast({
title: "绑定失败,请重试。",
icon: "none"
})
setTimeout(() => {
wx.navigateBack({
delta: 2
})
}, 1500)
}
})
} else {
app.globalData.ppScale.plugin.Blue.stop();
wx.showToast({
icon: "none",
title: res.message
})
setTimeout(() => {
wx.navigateBack({
delta: 2
})
}, 1500)
}
}).catch(err => {
app.globalData.ppScale.plugin.Blue.stop();
wx.showToast({
icon: "none",
title: err.message
})
setTimeout(() => {
wx.navigateBack({
delta: 2
})
}, 1500)
})
} else {
app.globalData.ppScale.plugin.Blue.stop();
wx.showToast({
icon: "none",
title: "Mac 地址与 deviceId 获取失败,请重新绑定"
})
setTimeout(() => {
wx.navigateBack({
delta: 2
})
}, 1500)
}
} else {
app.globalData.ppScale.plugin.Blue.stop();
wx.showToast({
icon: "none",
title: "配网失败,错误码:" + res
})
setTimeout(() => {
wx.navigateBack({
delta: 2
})
}, 1500)
}
}
}
});