Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
106 lines
3.1 KiB
TypeScript
106 lines
3.1 KiB
TypeScript
interface WifiItem {
|
|
ssid: string
|
|
connected: boolean
|
|
}
|
|
|
|
type WifiStatus = 'idle' | 'searching' | 'list' | 'empty' | 'password' | 'configuring'
|
|
type BleStatus = 'connected' | 'failed' | 'connecting'
|
|
|
|
/** 假 WiFi 列表 */
|
|
const MOCK_WIFI: WifiItem[] = [
|
|
{ ssid: 'TP-LINK_5G_8A3C', connected: false },
|
|
{ ssid: 'Home_WiFi', connected: false },
|
|
{ ssid: 'Xiaomi_1234', connected: false },
|
|
]
|
|
|
|
/** 搜索 / 配网定时器 */
|
|
let _searchTimer: number | null = null
|
|
let _configTimer: number | null = null
|
|
|
|
Page({
|
|
data: {
|
|
status: 'idle' as WifiStatus,
|
|
deviceName: '智能体重秤',
|
|
bleStatus: 'connected' as BleStatus,
|
|
wifiList: [] as WifiItem[],
|
|
hasConnected: false,
|
|
selectedSsid: '',
|
|
selectWifiPWD: '',
|
|
type: true,
|
|
},
|
|
|
|
onLoad() {
|
|
this.onStartSearch()
|
|
},
|
|
|
|
onUnload() {
|
|
wx.hideLoading()
|
|
this._clearTimers()
|
|
},
|
|
|
|
_clearTimers() {
|
|
if (_searchTimer !== null) { clearTimeout(_searchTimer); _searchTimer = null }
|
|
if (_configTimer !== null) { clearTimeout(_configTimer); _configTimer = null }
|
|
},
|
|
|
|
/** 模拟获取 WiFi 列表 */
|
|
onStartSearch() {
|
|
if (_searchTimer !== null) { clearTimeout(_searchTimer); _searchTimer = null }
|
|
this.setData({ status: 'searching', wifiList: [] })
|
|
wx.showLoading({ title: '正在获取 WiFi 列表...', mask: true })
|
|
_searchTimer = setTimeout(() => {
|
|
wx.hideLoading()
|
|
this.setData({
|
|
status: 'list',
|
|
wifiList: MOCK_WIFI.map(item => ({ ...item })),
|
|
})
|
|
}, 1200)
|
|
},
|
|
|
|
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 })
|
|
_configTimer = setTimeout(() => {
|
|
wx.hideLoading()
|
|
this.setData({
|
|
status: 'list',
|
|
wifiList: this.data.wifiList.map(item => ({ ...item, connected: item.ssid === selectedSsid })),
|
|
hasConnected: true,
|
|
selectedSsid: '',
|
|
selectWifiPWD: '',
|
|
})
|
|
}, 1500)
|
|
},
|
|
|
|
onConfirm() {
|
|
if (!this.data.hasConnected) return
|
|
wx.navigateTo({ url: '/pages/supplementPersonal/supplementPersonal' })
|
|
},
|
|
})
|