324 lines
12 KiB
JavaScript
324 lines
12 KiB
JavaScript
const app = getApp();
|
||
import $ from "../../utils/request";
|
||
|
||
Page({
|
||
data: {
|
||
progress: 0,
|
||
|
||
BLUE_STATE: {},
|
||
connectState: "",
|
||
|
||
device: null,
|
||
wifiInfo: {
|
||
ssid: "",
|
||
password: ""
|
||
}
|
||
},
|
||
|
||
// 存储回调函数的引用,以便在 onUnload 时取消监听
|
||
_connectStateWatcher: null,
|
||
|
||
// 标志位:防止页面级 bus 重复订阅(SDK bus 不支持 unsubscribe)
|
||
_pageBusSubscribed: false,
|
||
|
||
// 注册页面级 bus 订阅(通过标志位防重)
|
||
_subscribePageBus() {
|
||
if (this._pageBusSubscribed) return;
|
||
this._pageBusSubscribed = true;
|
||
|
||
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
|
||
console.log("replaceNetwork ===》devicesModel", res);
|
||
app.globalData.ppScale.device.mac = res.deviceMac;
|
||
});
|
||
|
||
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
||
console.log('replaceNetwork ===》deviceConnect', res);
|
||
app.globalData.ppScale.plugin.ScaleAction.startDataProgress(true);
|
||
app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol();
|
||
app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => {
|
||
console.log("replaceNetwork ===》codeUpdateMTU", res);
|
||
app.globalData.ppScale.device.name = this.data.device.name;
|
||
app.globalData.ppScale.device.connection = this.data.device;
|
||
const nameArr = this.data.device.name.split("-");
|
||
app.globalData.ppScale.version = nameArr.length === 5 ? "domain2" : "domain1";
|
||
wx.hideLoading();
|
||
this._startKeepAlive();
|
||
if (this.data.progress === 0) {
|
||
const comp = this.selectComponent('#replaceNetwork1Component');
|
||
if (comp) comp.getWiFiList();
|
||
}
|
||
});
|
||
});
|
||
},
|
||
|
||
// 重置订阅标志,下次 onShow 时重新订阅
|
||
_unsubscribePageBus() {
|
||
this._pageBusSubscribed = false;
|
||
},
|
||
|
||
onLoad(options) {
|
||
// 一次性数据初始化
|
||
this.checkBluetoothPermissionAndInit();
|
||
|
||
console.log(`[replaceNetwork] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
|
||
this.setData({
|
||
BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
|
||
connectState: app.globalData.ppScale.device.connectState
|
||
})
|
||
this._connectStateWatcher = (newValue, oldValue) => {
|
||
console.log(`[replaceNetwork] connectState 变化: ${oldValue} -> ${newValue}`);
|
||
this.setData({
|
||
connectState: newValue || ''
|
||
});
|
||
};
|
||
app.watch('ppScale.device.connectState', this._connectStateWatcher);
|
||
},
|
||
|
||
onShow() {
|
||
console.log("replaceNetwork onShow");
|
||
// 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
|
||
setTimeout(() => {
|
||
// 重新注册 app 级别的全局 bus 订阅
|
||
app.registerBusListeners();
|
||
|
||
// 页面级 bus 订阅(标志位防重)
|
||
this._subscribePageBus();
|
||
|
||
// 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
|
||
this.checkBluetoothPermissionAndInit();
|
||
}, 300);
|
||
},
|
||
|
||
checkBluetoothPermissionAndInit() {
|
||
wx.showLoading({
|
||
title: "正在检查蓝牙权限...",
|
||
mask: true
|
||
});
|
||
wx.getSetting({
|
||
success: (res) => {
|
||
if (res.authSetting['scope.bluetooth']) {
|
||
this.openBluetoothAdapter();
|
||
} else {
|
||
wx.authorize({
|
||
scope: 'scope.bluetooth',
|
||
success: () => {
|
||
this.openBluetoothAdapter();
|
||
},
|
||
fail: (err) => {
|
||
wx.hideLoading();
|
||
wx.showModal({
|
||
title: '提示',
|
||
content: '您拒绝了蓝牙权限,将无法连接蓝牙秤。是否前往设置开启?',
|
||
confirmText: '去开启',
|
||
cancelText: '不开启',
|
||
success: (modalRes) => {
|
||
if (modalRes.confirm) {
|
||
wx.openSetting({
|
||
success: (settingRes) => {
|
||
console.log('openSetting success', settingRes);
|
||
},
|
||
fail: (settingErr) => {
|
||
wx.navigateBack({
|
||
delta: 1
|
||
});
|
||
}
|
||
});
|
||
} else {
|
||
wx.navigateBack({
|
||
delta: 1
|
||
});
|
||
}
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
wx.hideLoading();
|
||
wx.showToast({
|
||
title: '获取权限设置失败',
|
||
icon: 'none'
|
||
});
|
||
setTimeout(() => {
|
||
wx.navigateBack({
|
||
delta: 1
|
||
});
|
||
}, 1500)
|
||
}
|
||
});
|
||
},
|
||
|
||
openBluetoothAdapter() {
|
||
wx.showLoading({
|
||
title: "正在初始化蓝牙...",
|
||
mask: true
|
||
});
|
||
wx.openBluetoothAdapter({
|
||
success: () => {
|
||
this.getDeviceSettingList();
|
||
},
|
||
fail: (err) => {
|
||
wx.hideLoading();
|
||
if (err.errCode === 10001) {
|
||
wx.showModal({
|
||
title: "提示",
|
||
content: "请确保手机蓝牙已开启。",
|
||
showCancel: false,
|
||
success: () => {
|
||
wx.navigateBack({
|
||
delta: 1
|
||
});
|
||
}
|
||
});
|
||
} else if (err.errMsg && err.errMsg.includes('permission denied')) {
|
||
wx.showModal({
|
||
title: '提示',
|
||
content: '蓝牙权限仍被拒绝,无法使用蓝牙功能。',
|
||
showCancel: false,
|
||
success: () => {
|
||
wx.navigateBack({
|
||
delta: 1
|
||
});
|
||
}
|
||
});
|
||
} else {
|
||
wx.showToast({
|
||
title: `蓝牙适配器打开失败: ${err.errMsg || err.errCode}`,
|
||
icon: 'none',
|
||
success: () => {
|
||
wx.navigateBack({
|
||
delta: 1
|
||
});
|
||
}
|
||
});
|
||
}
|
||
},
|
||
});
|
||
},
|
||
|
||
// 获取自己已经配对上的设备
|
||
getDeviceSettingList() {
|
||
let params = {};
|
||
$.ajax("weighingScale/list/device", params, "GET", true, "获取设备列表").then((res) => {
|
||
wx.showLoading({
|
||
title: "正在寻找设备...",
|
||
mask: true
|
||
});
|
||
|
||
app.globalData.ppScale.device.list = [];
|
||
let seviceList = app.globalData.ppScale.device.setting.find((item) => {
|
||
return item.deviceName === res.result[0].type
|
||
});
|
||
app.globalData.ppScale.plugin.Blue.setDeviceSetting([seviceList]);
|
||
app.globalData.ppScale.plugin.Blue.start(res.result[0].type, false);
|
||
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => {
|
||
console.log("searchDevice ===> devicesList", res_);
|
||
let fIndex = res_.findIndex(item => item.deviceId === res.result[0].deviceId);
|
||
if (fIndex >= 0) {
|
||
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
|
||
wx.showLoading({ title: "正在连接设备...", mask: true });
|
||
this.setData({ device: res_[fIndex] });
|
||
app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[fIndex]);
|
||
}
|
||
});
|
||
})
|
||
},
|
||
|
||
// 手动重连设备
|
||
connectedDevice() {
|
||
app.resetReconnectCount();
|
||
let device = this.data.device;
|
||
if(device) {
|
||
wx.showLoading({
|
||
title: "正在重新连接设备...",
|
||
mask: true
|
||
});
|
||
app.globalData.ppScale.plugin.Blue.disconnect((disres) => {
|
||
if (disres.errCode == 0) {
|
||
app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
|
||
} else {
|
||
app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
|
||
}
|
||
});
|
||
}
|
||
},
|
||
|
||
// 选择了某个wifi
|
||
wifiSSIDChange(e) {
|
||
let status = e.detail.status;
|
||
if(status) {
|
||
this.setData({
|
||
['wifiInfo.ssid']: e.detail.data.ssid,
|
||
progress: this.data.progress + 1
|
||
})
|
||
}
|
||
},
|
||
|
||
// 设置wifi密码
|
||
wifiPWDChange(e) {
|
||
let status = e.detail.status;
|
||
if(status) {
|
||
this.setData({
|
||
['wifiInfo.password']: e.detail.data,
|
||
progress: this.data.progress + 1
|
||
})
|
||
}
|
||
},
|
||
|
||
wifiSetChange(e) {
|
||
let status = e.detail.status;
|
||
if(status) {
|
||
this.setData({
|
||
progress: this.data.progress + 1
|
||
})
|
||
} else {
|
||
this.setData({
|
||
progress: 0
|
||
})
|
||
}
|
||
},
|
||
|
||
// 启动保活定时器,每10秒发送一次保活指令
|
||
_keepAliveTimer: null,
|
||
_startKeepAlive() {
|
||
this._stopKeepAlive();
|
||
this._keepAliveTimer = setInterval(() => {
|
||
if (app.globalData.ppScale.activeProtocol) {
|
||
app.globalData.ppScale.activeProtocol.sendKeepAliveCode();
|
||
}
|
||
}, 15000);
|
||
},
|
||
|
||
// 清除保活定时器
|
||
_stopKeepAlive() {
|
||
if (this._keepAliveTimer) {
|
||
clearInterval(this._keepAliveTimer);
|
||
this._keepAliveTimer = null;
|
||
}
|
||
},
|
||
|
||
onHide() {
|
||
console.log("replaceNetwork onHide");
|
||
this._stopKeepAlive();
|
||
this._unsubscribePageBus();
|
||
app.unregisterBusListeners();
|
||
app.resetReconnectCount();
|
||
app.setGlobalData('ppScale.device.connectState', null);
|
||
app.globalData.ppScale.plugin.Blue.stop();
|
||
},
|
||
|
||
// 卸载事件监听
|
||
onUnload() {
|
||
console.log("replaceNetwork onUnload");
|
||
this._stopKeepAlive();
|
||
this._unsubscribePageBus();
|
||
app.unregisterBusListeners();
|
||
app.resetReconnectCount();
|
||
app.setGlobalData('ppScale.device.connectState', null);
|
||
app.globalData.ppScale.plugin.Blue.stop();
|
||
if (this._connectStateWatcher) {
|
||
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);
|
||
}
|
||
}
|
||
}) |