[AI Generated]: feat(*): 修复连接设备页及配网页交互逻辑、蓝牙状态显示与设备名获取

This commit is contained in:
17792275749
2026-05-14 17:18:10 +08:00
parent 29680cb7a4
commit 00a1529256
14 changed files with 402 additions and 155 deletions
@@ -167,26 +167,33 @@
display: flex;
flex-wrap: nowrap;
align-items: center;
>view {
width: 12rpx;
height: 12rpx;
border-radius: 50%;
margin-right: 14rpx;
background-color: #2AC79F;
}
>text {
color: #2AC79F;
height: 28rpx;
font-size: 28rpx;
font-weight: 500;
line-height: 28rpx;
}
}
}
}
.status-dot {
width: 12rpx;
height: 12rpx;
border-radius: 50%;
margin-right: 14rpx;
flex-shrink: 0;
&--green { background-color: #2AC79F; }
&--red { background-color: #F24439; }
&--gray { background-color: #999999; }
}
.status-text {
height: 28rpx;
font-size: 28rpx;
font-weight: 500;
line-height: 28rpx;
&--green { color: #2AC79F; }
&--red { color: #F24439; }
&--gray { color: #999999; }
}
/* idle / searching 两态:居中文案 */
.wifiIdle,
.wifiSearching {
@@ -246,7 +253,7 @@
width: 100%;
flex: 1;
min-height: 0;
padding: 0 12rpx;
padding-left: 12rpx;
box-sizing: border-box;
}
@@ -259,15 +266,20 @@
align-items: center;
margin-bottom: 32rpx;
/* 左侧灰底圆形图标占位 */
/* 左侧 wifi 图标容器 */
>view:nth-of-type(1) {
width: 80rpx;
height: 80rpx;
overflow: hidden;
margin-right: 24rpx;
border-radius: 50%;
background-color: #F7F7F7;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
image {
width: 100%;
height: 100%;
}
}
/* WiFi 名称 */
@@ -284,17 +296,11 @@
>view:nth-of-type(2) {
width: 10rpx;
height: 18rpx;
background-color: #B6B6B6;
flex-shrink: 0;
}
}
/* 列表项:已连接态 */
.wifiItem-connected {
/* 左侧图标不加底色圆,保留图标自身颜色 */
>view:nth-of-type(1) {
background-color: transparent;
}
>text {
color: #1385FA;
@@ -364,6 +370,7 @@
>view {
width: 566rpx;
height: 499rpx;
margin-bottom: 40rpx;
border-radius: 16rpx;
background-color: #F5F8FF;
}
@@ -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,
@@ -5,9 +5,9 @@
<view class="setWifi-form">
<view>
<view>
<image src="/images/configureDevice/wifiPwd.png"/>
<image src="/images/connectedWifi/wifiPwd.png"/>
</view>
<view>{{selectedWifi.name}}</view>
<view>{{selectedWifi.ssid}}</view>
</view>
<view>
<view>密码</view>
@@ -30,7 +30,7 @@
</block>
</view>
<view bind:tap="inputTypeChange">
<image src="/images/configureDevice/{{type ? 'open' : 'close'}}.png"/>
<image src="/images/connectedWifi/{{type ? 'open' : 'close'}}.png"/>
</view>
</view>
</view>
@@ -48,8 +48,18 @@
<view>
<view>{{currentDevice.name}}</view>
<view>
<view></view>
<text>已连接</text>
<block wx:if="{{bleStatus === 'connected'}}">
<view class="status-dot status-dot--green"></view>
<text class="status-text status-text--green">已连接</text>
</block>
<block wx:elif="{{bleStatus === 'failed'}}">
<view class="status-dot status-dot--red"></view>
<text class="status-text status-text--red">连接断开</text>
</block>
<block wx:else>
<view class="status-dot status-dot--gray"></view>
<text class="status-text status-text--gray">连接中...</text>
</block>
</view>
</view>
</view>
@@ -74,11 +84,13 @@
<view>网络列表(请选择一个网络进行连接)</view>
</view>
<scroll-view class="wifiList" scroll-y>
<block wx:for="{{wifiList}}" wx:key="id">
<block wx:for="{{wifiList}}" wx:key="ssid">
<block wx:if="{{item.connected}}">
<view class="wifiItem wifiItem-connected" data-id="{{item.id}}" bind:tap="onTapWifi">
<view></view>
<text>{{item.name}}</text>
<view class="wifiItem wifiItem-connected" data-ssid="{{item.ssid}}" bind:tap="onTapWifi">
<view>
<image src="/images/connectedWifi/wifi.png" mode="aspectFit"/>
</view>
<text>{{item.ssid}}</text>
<view>
<text>已连接</text>
<view></view>
@@ -86,10 +98,12 @@
</view>
</block>
<block wx:else>
<view class="wifiItem" data-id="{{item.id}}" bind:tap="onTapWifi">
<view></view>
<text>{{item.name}}</text>
<view></view>
<view class="wifiItem" data-ssid="{{item.ssid}}" bind:tap="onTapWifi">
<view>
<image src="/images/connectedWifi/wifi.png" mode="aspectFit"/>
</view>
<text>{{item.ssid}}</text>
<view class="arrow"></view>
</view>
</block>
</block>
@@ -108,7 +122,9 @@
<view>网络列表(请选择一个网络进行连接)</view>
</view>
<view class="wifiEmpty">
<view></view>
<view>
<image src="/images/connectedWifi/noData.png" mode="aspectFit"/>
</view>
<text>未查找到可用wifi,请重试</text>
</view>
</block>