[AI Generated]: feat(*): 修复连接设备页及配网页交互逻辑、蓝牙状态显示与设备名获取
This commit is contained in:
@@ -38,12 +38,21 @@ Page({
|
||||
*/
|
||||
status: 'idle' as WifiStatus,
|
||||
|
||||
/** 顶部固定卡片(从上一页状态中读取,当前先用占位) */
|
||||
/** 顶部固定卡片 */
|
||||
currentDevice: {
|
||||
id: '',
|
||||
name: '智能体重秤',
|
||||
} as CurrentDevice,
|
||||
|
||||
/** 蓝牙连接状态常量(从插件获取,供 wxml 做条件判断) */
|
||||
BLUE_STATE: {} as Record<string, string>,
|
||||
|
||||
/** 当前蓝牙连接状态(实时更新) */
|
||||
connectState: '',
|
||||
|
||||
/** 顶部卡片蓝牙状态:connected / failed / connecting */
|
||||
bleStatus: 'connected' as 'connected' | 'failed' | 'connecting',
|
||||
|
||||
/** WiFi 列表 */
|
||||
wifiList: [] as WifiItem[],
|
||||
|
||||
@@ -60,21 +69,49 @@ Page({
|
||||
type: true,
|
||||
},
|
||||
|
||||
_toBleStatus(state: string): 'connected' | 'failed' | 'connecting' {
|
||||
const bs = lefuService.BLUE_STATE
|
||||
if (state === bs.CONNECTSUCCESS || state === bs.WIFISUCCESS) return 'connected'
|
||||
if (state === bs.CONNECTFAILED) return 'failed'
|
||||
return 'connecting'
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
// idle 起步,通过统一入口触发搜索
|
||||
this.setData({
|
||||
bleStatus: this._toBleStatus(lefuService.connectState),
|
||||
currentDevice: { id: '', name: lefuService.deviceInfo?.modelNumber ?? '智能体重秤' },
|
||||
})
|
||||
this.onStartSearch()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
lefuService.onConnectState((state) => {
|
||||
const bs = lefuService.BLUE_STATE
|
||||
// 只处理明确的连接/断开状态,忽略蓝牙初始化过程中的中间状态
|
||||
if (state === bs.CONNECTSUCCESS || state === bs.WIFISUCCESS || state === bs.CONNECTFAILED) {
|
||||
this.setData({ bleStatus: this._toBleStatus(state) })
|
||||
}
|
||||
})
|
||||
lefuService.onDeviceInfo((info) => {
|
||||
this.setData({ currentDevice: { id: '', name: info.modelNumber } })
|
||||
})
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 页面卸载时不调用 stopScan,蓝牙连接由 home 页维持
|
||||
wx.hideLoading()
|
||||
if (this.data.status === 'configuring') {
|
||||
lefuService.disconnect()
|
||||
}
|
||||
},
|
||||
|
||||
/** 从设备获取 WiFi 列表 */
|
||||
onStartSearch() {
|
||||
this.setData({ status: 'searching', wifiList: [] })
|
||||
wx.showLoading({ title: '正在获取 WiFi 列表...', mask: true })
|
||||
|
||||
lefuService.getWifiList()
|
||||
.then((items: LeFuWifiItem[]) => {
|
||||
wx.hideLoading()
|
||||
if (!items.length) {
|
||||
this.setData({ status: 'empty' })
|
||||
return
|
||||
@@ -86,6 +123,7 @@ Page({
|
||||
this.setData({ status: 'list', wifiList: list })
|
||||
})
|
||||
.catch(() => {
|
||||
wx.hideLoading()
|
||||
this.setData({ status: 'empty' })
|
||||
})
|
||||
},
|
||||
@@ -139,7 +177,6 @@ Page({
|
||||
lefuService.configWifi(selectedWifi.ssid, selectWifiPWD, this._wifiVersion)
|
||||
.then(() => {
|
||||
wx.hideLoading()
|
||||
// 配网成功:标记该 WiFi,回列表态
|
||||
const next = this.data.wifiList.map(item => ({
|
||||
...item,
|
||||
connected: item.ssid === selectedWifi.ssid,
|
||||
|
||||
Reference in New Issue
Block a user