- connectedDevice:扫描列表与 connectDeviceInfo 缓存按 deviceId 实时比对,命中后一次性自动连,仅刷状态/缓存不跳页 - connectedDevice:新增 onTapCard,整卡点击 → 已连接跳 connectedWifi,未连接走 onConnect;按钮改 catch:tap 防冒泡 - connectedWifi:移除 onConfirm 中下发主用户与 calcAge/StoredUserInfo 残留,统一跳 supplementPersonal Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
132 lines
4.2 KiB
TypeScript
132 lines
4.2 KiB
TypeScript
import { lefuService } from '../../lefu/index'
|
|
import type { LeFuWifiItem } from '../../lefu/index'
|
|
|
|
interface WifiItem {
|
|
ssid: string
|
|
connected: boolean
|
|
}
|
|
|
|
type WifiStatus = 'idle' | 'searching' | 'list' | 'empty' | 'password' | 'configuring'
|
|
type BleStatus = 'connected' | 'failed' | 'connecting'
|
|
|
|
Page({
|
|
|
|
data: {
|
|
status: 'idle' as WifiStatus,
|
|
deviceName: '智能体重秤',
|
|
bleStatus: 'connecting' as BleStatus,
|
|
wifiList: [] as WifiItem[],
|
|
hasConnected: false,
|
|
selectedSsid: '',
|
|
selectWifiPWD: '',
|
|
type: true,
|
|
},
|
|
|
|
_toBleStatus(state: string): BleStatus {
|
|
const bs = lefuService.BLUE_STATE
|
|
if (state === bs.CONNECTSUCCESS || state === bs.WIFISUCCESS) return 'connected'
|
|
if (state === bs.CONNECTFAILED) return 'failed'
|
|
return 'connecting'
|
|
},
|
|
|
|
onLoad() {
|
|
// 尽早注册 deviceInfo 回调,防止 onShow 前已触发而错过
|
|
lefuService.onDeviceInfo((info) => {
|
|
this.setData({ deviceName: info.modelNumber })
|
|
})
|
|
this.setData({
|
|
bleStatus: this._toBleStatus(lefuService.connectState),
|
|
deviceName: 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) })
|
|
}
|
|
})
|
|
},
|
|
|
|
onUnload() {
|
|
wx.hideLoading()
|
|
if (this.data.status === 'configuring') {
|
|
lefuService.disconnect()
|
|
}
|
|
},
|
|
|
|
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
|
|
}
|
|
this.setData({
|
|
status: 'list',
|
|
wifiList: items.map(item => ({ ssid: item.ssid, connected: false })),
|
|
})
|
|
})
|
|
.catch(() => {
|
|
wx.hideLoading()
|
|
this.setData({ status: 'empty' })
|
|
})
|
|
},
|
|
|
|
onRefresh() {
|
|
this.onStartSearch()
|
|
},
|
|
|
|
onTapWifi(e: WechatMiniprogram.TouchEvent) {
|
|
const ssid = e.currentTarget.dataset.ssid as string
|
|
const target = this.data.wifiList.find(item => item.ssid === ssid)
|
|
if (!target || target.connected) return
|
|
this.setData({ status: 'password', selectedSsid: ssid, selectWifiPWD: '', type: true })
|
|
},
|
|
|
|
passwordInput(e: WechatMiniprogram.Input) {
|
|
this.setData({ selectWifiPWD: e.detail.value })
|
|
},
|
|
|
|
inputTypeChange() {
|
|
this.setData({ type: !this.data.type })
|
|
},
|
|
|
|
onCancelPwd() {
|
|
this.setData({ status: 'list', selectedSsid: '', selectWifiPWD: '' })
|
|
},
|
|
|
|
onConfirmPwd() {
|
|
const { selectedSsid, selectWifiPWD } = this.data
|
|
if (!selectedSsid) return
|
|
this.setData({ status: 'configuring' })
|
|
wx.showLoading({ title: '正在配网...', mask: true })
|
|
lefuService.configWifi(selectedSsid, selectWifiPWD)
|
|
.then(() => {
|
|
wx.hideLoading()
|
|
this.setData({
|
|
status: 'list',
|
|
wifiList: this.data.wifiList.map(item => ({ ...item, connected: item.ssid === selectedSsid })),
|
|
hasConnected: true,
|
|
selectedSsid: '',
|
|
selectWifiPWD: '',
|
|
})
|
|
})
|
|
.catch((err: Error) => {
|
|
wx.hideLoading()
|
|
wx.showToast({ title: err.message || '配网失败,请重试', icon: 'none' })
|
|
this.setData({ status: 'password' })
|
|
})
|
|
},
|
|
|
|
onConfirm() {
|
|
if (!this.data.hasConnected) return
|
|
wx.navigateTo({ url: '/pages/supplementPersonal/supplementPersonal' })
|
|
},
|
|
})
|